Article View: alt.comp.lang.learn.c-c++
Article #214Re: TIC-TAC-TOE in C++ !
From: Larry Coon
Date: Wed, 14 Jan 1998 00:00
Date: Wed, 14 Jan 1998 00:00
66 lines
2964 bytes
2964 bytes
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.
[snip]
> 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.
One of many possible solutions is to have your 3x3
array representing the board with 1 for an X-occupied
square, -1 for an O-occupied square, and zero for an empty
square.
Next create an eight-element array, with each element
representing one of the ways you can win: across the
top, across the middle, across the bottom, down the
left, down the middle, down the bottom, and the two
diagonals. This array is initialized to zeros.
Next, determine which way to win every square applies
to. For example, the top left square applies to (using
the order from the last paragraph) ways 1, 4 and 7. The
middle square applies to 2, 5, 7 and 8, etc.
Okay, here's the algorithm: For each of the nine squares,
for each of the ways to win to which the square applies,
add the current value (-1, 0 or 1) of that square to the
value in the eight-element array. For example, if an O
is in the top-left square, add -1 to elements 1, 4 and 7.
When you're done, each of the eight elements contains the
current status of the row, column or diagonal it represents.
The value will be in the range -3..3, with the following
meanings:
-3: O wins along this row/column/diagonal.
-2: There are two O's and an empty square.
2: There are two X's and an empty square.
3: X wins along this row/column/diagonal.
For any other value, there's either a mixture of X's and
O's or it's entirely empty. So you iterate through the
eight-element array. If you find a 3 or -3, the game's
over. If you find a 2, you win by moving to the blank
square (assuming you're X). If you find a -2 and there
are no 2's, you need to block in the empty square to
prevent O from winning.
Larry Coon
University of California
larry@assist.org
and lmcoon@home.com
Technical Details
Message-ID:
<34BD018A.C68@assist.org>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<01bd1f31$0a72dbc0$db4522cf@jwatson>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10d4ba,750a5b3b86788347 X-Google-Attributes: gid10d4ba,public X-Google-Thread: 109fba,750a5b3b86788347 X-Google-Attributes: gid109fba,public X-Google-Thread: 114592,750a5b3b86788347 X-Google-Attributes: gid114592,public From: Larry Coon <larry@assist.org> Subject: Re: TIC-TAC-TOE in C++ ! Date: 1998/01/14 Message-ID: <34BD018A.C68@assist.org> X-Deja-AN: 315927261 Content-Transfer-Encoding: 7bit References: <01bd1f31$0a72dbc0$db4522cf@jwatson> Mime-Version: 1.0 Reply-To: larry@assist.org Content-Type: text/plain; charset=us-ascii Organization: University of California Newsgroups: alt.comp.lang.learn.c-c++,att.lang.c++,comp.lang.c++