🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

TIC-TAC-TOE in C++ !

#209
From: Jon Watson
Date: Mon, 12 Jan 1998 00:00
57 lines
2252 bytes
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.

Thanks in advance!

Jon 

-- 
****************************
The future is in beta...
****************************
Technical Details
Message-ID: <01bd1f31$0a72dbc0$db4522cf@jwatson>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10d4ba,750a5b3b86788347,start
X-Google-Attributes: gid10d4ba,public
X-Google-Thread: 114592,750a5b3b86788347,start
X-Google-Attributes: gid114592,public
X-Google-Thread: 109fba,750a5b3b86788347,start
X-Google-Attributes: gid109fba,public
From: "Jon Watson" <jwatson@freenet.calgary.ab.ca>
Subject: TIC-TAC-TOE in C++ !
Date: 1998/01/12
Message-ID: <01bd1f31$0a72dbc0$db4522cf@jwatson>
X-Deja-AN: 315146144
Organization: Telus Advanced Communications
Newsgroups: alt.comp.lang.learn.c-c++,att.lang.c++,comp.lang.c++