Tic-Tac-Toe Problem

I'm trying to make a Tic-Tac-Toe game and have the controls relatively done but don't know how I'm going to save the location. If anyone has any tips or even a link it would be great.

I was also wandering if there is an easier way to repeat blocks of code. Right now I'm creating the controls like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
    switch (k)
    {
           case 1:   
                 
    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
        {
            if (i == 2 && j == 0)
            cout << "X   ";
            else
            cout << "-   ";
            }
            cout << endl << endl << endl;
        }
            break;


I'm sure there must be some way but I don't know how. Again any help or a link would be great. Thanks in advance.
You can save the locations to an array

From what Im understanding of the way your doing, you have 9 of those as cases? When you set it up using an array you can get it something like

1
2
y = (choice-1)%3;
x = (choice-1)/3;


You can line that up to an array[x][y]
The array starts at [0][0].

so a choice of 9 sets the int x to 8/3 = 2 (always rounds down) and y to 8%3 = 2, which corresponds to the bottom right
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.
Last edited on
try making a vector<vector> or an array[][], it is a 2D object, just like the game. I have done a game yesterday, ok one part of it and used "string" vectors/arrays. It is not complicated to do... try it :)
p.s.
as to the block of code u might want to use a for loop that makes 9 loops (there are 9 fields in the game)
and say that if the number of loop is a even number:it is the 1st players turn, if it is not even the 2nd or the compuer's turn.

try doing the rest with functions, so ewery player's "part of the code" calls the same functions but with other parameters.
Topic archived. No new replies allowed.