I am still working on my tic tac toe game using c++.What I want to do is check an array of characters for X's or O's.If it finds an X or O then it generates another O.It checks the array until it finds an empty spot and then puts an O in that spot.
here is the code I am working on.
if(board[player_O]!='X' && board[player_O]!='O')
{
board[player_O]='O';
}
else
{
player_O_two=rand()%9+1;
}
ok I am trying to check for a empty spot on a tic tac toe board and then put an X or O in that spot.I dont want the computer to put a O in an already occupied spot.
If all you're actually trying to do is find the first empty space and replace it with an O then this would do it:
//Set all spaces to ' ' when you initialize the board
for (int i = 0; i < 8; ++i) //Where 8 is the size of the board
{
board = ' ';
}
//Then when you're wanting to replace the first empty space wth 'O'
for (int i = 0; i < 8; ++i)
{
if (board == ' ')
board = 'O';
}
The majority of Internet Explorer users don't understand the concept of a browsing application, or that there are options.
They just see the big blue 'e' and think "Internet". The thought process usually does not get much deeper than that.
Worms are the weirdest and nicest creatures, and will one day prove themselves to the world.
If all you're actually trying to do is find the first empty space and replace it with an O then this would do it:
//Set all spaces to ' ' when you initialize the board
for (int i = 0; i < 8; ++i) //Where 8 is the size of the board
{
board = ' ';
}
//Then when you're wanting to replace the first empty space wth 'O'
for (int i = 0; i < 8; ++i)
{
if (board == ' ')
board = 'O';
}
if you do not put a break in that if statement, it will change ALL empty spaces into O's, rather than the first one you find
If (board =='')
{
board = 'O';
break;
}
that way only one empty is changed
also i think you may want 9 instead of 8; arent there 9 tiles in TTT?
[quote name='littletray26' timestamp='1350595688' post='4991555']
If all you're actually trying to do is find the first empty space and replace it with an O then this would do it:
//Set all spaces to ' ' when you initialize the board
for (int i = 0; i < 8; ++i) //Where 8 is the size of the board
{
board = ' ';
}
//Then when you're wanting to replace the first empty space wth 'O'
for (int i = 0; i < 8; ++i)
{
if (board == ' ')
board = 'O';
}
if you do not put a break in that if statement, it will change ALL empty spaces into O's, rather than the first one you find
If (board =='')
{
board = 'O';
break;
}
that way only one empty is changed
also i think you may want 9 instead of 8; arent there 9 tiles in TTT?
[/quote]
You're right, I forgot to put that break in there. Thank you for correcting that
Also, 0 is counted as a space so in an 8 space array there is in fact 9 spaces.
[quote name='littletray26' timestamp='1350595688' post='4991555']
If all you're actually trying to do is find the first empty space and replace it with an O then this would do it:
//Set all spaces to ' ' when you initialize the board
for (int i = 0; i < 8; ++i) //Where 8 is the size of the board
{
board = ' ';
}
//Then when you're wanting to replace the first empty space wth 'O'
for (int i = 0; i < 8; ++i)
{
if (board == ' ')
board = 'O';
}
if you do not put a break in that if statement, it will change ALL empty spaces into O's, rather than the first one you find
If (board =='')
{
board = 'O';
break;
}
that way only one empty is changed
also i think you may want 9 instead of 8; arent there 9 tiles in TTT?
[/quote]
The majority of Internet Explorer users don't understand the concept of a browsing application, or that there are options.
They just see the big blue 'e' and think "Internet". The thought process usually does not get much deeper than that.
Worms are the weirdest and nicest creatures, and will one day prove themselves to the world.