help with Tic Tac Toe

I am trying to make a Human vs Computer tic tac toe, where the computer makes random moves.

The human always gets the first move 'X' and computer is next with '0' .

The problem is that the computer keeps overwriting the old moves. How can I fix this?

1
2
3
4
5
6
7
8
	if (computer == 1)
			{

				i = rand() % 3;
				j = rand() % 3;
				board[i][j] = '0';
				computer--;
			}
closed account (48T7M4Gy)
If board[i][j] is empty then the move is accepted, otherwise have another move.
I need a little more help, I still can't figure it out.

My 2D array si this one:

1
2
3
4
char board[3][3] = 
	{{'1', '2', '3'}, 
	{'4', '5', '6'}, 
	{'7', '8', '9'}};
closed account (48T7M4Gy)
Start with a blank board and on each move put an 'X' or a 'O' in the relevant cell, provided there isn't one there already.
I need my 2D array to be like that in order to know which field you are going to make the move on.
This is the closest I got to.
My idea is:
if the board doesn't have X and 0 on it make that random move.

The problem is I got an infinit loop if the random number that gets generated is for the field that is already taken.


1
2
3
4
5
6
7
8
9
10
11
12
	if (computer == 1)
			{
			
				i = rand() % 3;
				j = rand() % 3;
				if(board[i][j] !='X' && board[i][j] != '0')
				{
				board[i][j] = '0';
				computer--;
				}
				
			}
Last edited on
You might want to look at at C++11's random number generation engine.

Aceix.
I have a complete program here on my dropbox at:
https://www.dropbox.com/sh/ysyr6wok0vntaob/AADt_LXg_6YQq-T94CRAIRcra?dl=0
but here's the piece that your looking for:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void cpu(char mt[l][c])
{
    /* initialize random seed: */
    srand (time(NULL));

    int x,cont;
    bool found = false;
    while(found == false)
    {
        x = rand()%9+1;
        cont = 1;
        for(int i = 0; i < l; i++ )
        {
            for(int j = 0; j < c; j++ )
            {
                if(cont == x)
                {
                    if(mt[i][j] != 'X' && mt[i][j] != 'O')
                    {
                        mt[i][j] = 'O';
                        found = true;
                    }
                }
                cont++;
            }
        }
    }
}
Here is what I have. Everything is working very well except for one detail.

When the last move is made by the user, the computer is initialized to 1 again, and tries to search another random moves, but another random move does not exist, so the program is frozen.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	if (computer == 1)  //computer = 1, means it's computer's turn
			{
				
			    bool found = false;

				while (found == false )
				{
				i = rand() % 3;
				j = rand() % 3;
				if(board[i][j] !='X' && board[i][j] != '0')
				{			
				board[i][j] = '0';
				found = true;
				computer--;  // computer = 0, means it's the player's turn		
				}

				}
			 }
Topic archived. No new replies allowed.