🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.learn.c-c++
Article #213

Re: TIC-TAC-TOE in C++ !

#213
From: Alf P. Steinbach
Date: Tue, 13 Jan 1998 00:00
93 lines
4154 bytes
One obvious (to those who have already done it) improvement is to
parameterize whether the search should be for X or O.

Horizontal and vertical traversal can be handled by the same code
simply be changing the board representation to something suitable.
Since you're probably not into pointers, consider representing
the board as a one-dimensional array (instead of two-dimensional).
Then successive horizontal board positions could be successive
elements in the array, while successive vertical board positions
could be each third array element.  Parameterize by the array
element distance, and voil�.

Ditto for handling the two diagonal searches by the same code.
Actually the same code as above, which then is one single func
with one single loop.

But then comes the question: what is the advantage?  And the
answer is, in a production situation, there would be no
advantage whatsoever, except for the first parameterization
(on the contrary, "clever" compact code is difficult to maintain).

But for a person learning programming, there is a sense of
achievement and there is a learning experience which can be
usefully applied in other situations.  It's the kind of thing
one simply likes to do.

Hth.,

- Alf



Jeff Elliott wrote:
> I think actually, 100 lines of code sounds pretty good for a program like
> this.  I hope you've got enough whitespace and comments throughout the code to
> make it readable and easy enough to follow.
> 
> I don't suppose you'd mind sending me a copy of the code or posting it here -
> maybe someone could offer some optimization tips based on what you've already
> got written.

> 
> Jon Watson wrote:
> >
> > Hey All;
> >
> > I am in my second semester of info systems at college and have to write the
> > age old
> > program of tic-tac-toe.
> >
> > There is a requirement that the program will determine the computers move
> > as follows:
> >
> > The computer is always 'X'
> > Scan all rows, columns and both diagonals to determine if there are 2 X's
> > in any one of them.
> > If there is, place an 'X' in the vacant spot, declare a win and ask the
> > user if they want to play again.
> >
> > If there isn't,
> > Scan all rows, columns and both diagonals to determine is there are 2 O's
> > in any one of them.
> > If there is, still place an 'X' in the vacant spot to block a potential win
> > by the user.
> > If neither of these situations apply, generate a random move.
> >
> > My question is this:
> >
> > I currentl have:
> >
> > One loop to search rows for X's
> > One loop to search columns for X's
> > One loop to search the diagonals for X's
> > One loop to search rows for O's
> > One loop to search columns for O's
> > One loop to search diagonals for O's.
> >
> > This is quite a bit of code. I thought about simply searching the board for
> > two successive squares
> > that aren't blank, but that won't work because I don't want it to flag an X
> > and an O in one row
> > as a potential win or block, so it seems it has to search for each
> > individually.
> >
> > Finally, the question! Any ideas on how to compact this code so that it
> > doesn't take up
> > 100 lines?
> >
> > I would also love to incorporate this into the function that searches
> > whether or not the human has
> > won based on it's last move, but I think I can handle that myself.
> >
Technical Details
Message-ID: <34BB00E2.365@online.no>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <01bd1f31$0a72dbc0$db4522cf@jwatson> <34BADED1.3AB196D7@ibm.net>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: 114592,750a5b3b86788347
X-Google-Attributes: gid114592,public
X-Google-Thread: 10d4ba,750a5b3b86788347
X-Google-Attributes: gid10d4ba,public
X-Google-Thread: 109fba,750a5b3b86788347
X-Google-Attributes: gid109fba,public
From: "Alf P. Steinbach" <alfps@online.no>
Subject: Re: TIC-TAC-TOE in C++ !
Date: 1998/01/13
Message-ID: <34BB00E2.365@online.no>
X-Deja-AN: 315448725
Content-Transfer-Encoding: 8bit
References: <01bd1f31$0a72dbc0$db4522cf@jwatson> <34BADED1.3AB196D7@ibm.net>
Content-Type: text/plain; charset=iso-8859-1
Organization: Telenor Online
Mime-Version: 1.0
Reply-To: alfps@online.no
Newsgroups: alt.comp.lang.learn.c-c++,att.lang.c++,comp.lang.c++