You could also make it even simpler by using a single dimensional char array with 9 memory spots
1 2 3 4 5
|
char tic_array [9]; // locations [0-8] store the character 'X' or 'O' or nothing
int check_array [9]; // A second array to flag which spaces are taken and with what -
// With this second array 'mirroring' the first, you can use it to base
// the computers move on etc. whilst using the main 'tic_array' to print out
//the game board with.
|
With the int array, if you give a blank space a value of 1, an 'X' a value of 3 and a 'O' a value of 5 - all prime numbers then it is easy to program the computers strategy - it will 'block' if (assuming it is 'O') the numerical total for a 'row' is 6, it will try and win if the total is 10, if none of these are the case, it can chose 'randomly' from those spots in the array flagged with '1'.
Obviously, there are many different ways of doing this - however, this depends on your needs, desires, and capabilities. My capabilities are pitiful and so i strictly followed the 'rules' of the tic-tac-toe exercise at this location:
http://www.cplusplus.com/forum/articles/12974/
If this is you too, then it's inescapable that the code for a fully functioning game against a computer opponent will be long and quite repetitive.
If, however, you are able to work with functions, well ..............
Hope this helps,
Dan
Obviously - if you are programming a two player game for use against another human - just the first 'char array' would be fine.