more tic tac toe

Started by
30 comments, last by alvaro 11 years, 5 months ago
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;
}

if(player_O_two==player_O)
{
player_O=rand()%9+1;
board[player_O]='O';
}

I only need a small hint.
Advertisement
I don't understand the description in English of what you are trying to do. Perhaps you can try to explain it with an example?
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.
One reasonable method consist of making a list of the empty spaces, then pick a random one from those.

Do you need to see code for that?
cool yes a little bit of code would help.
There you go:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>

enum Piece {Empty, Cross, Nought};

class Board {
Piece array[9];

public:
Board() {
for (int i=0; i<9; ++i)
array = Empty;
}

void make_move(int square, Piece piece) {
array[square] = piece;
}

int pick_random_empty_square() {
std::vector<int> empties;
empties.reserve(9);
for (int i=0; i<9; ++i) {
if (array == Empty)
empties.push_back(i);
}

int random_index = std::rand() % empties.size();

return empties[random_index];
}

friend std::ostream &operator<<(std::ostream &os, Board const &board);
};

std::ostream &operator<<(std::ostream &os, Board const &board) {
for (int row=0; row<3; ++row) {
for (int col=0; col<3; ++col)
os << "_XO"[board.array[3*row+col]] << ' ';
os << '\n';
}
return os;
}

int main() {
std::srand(std::time(0));

Board board;

for (int i=0; i<9; ++i) {
board.make_move(board.pick_random_empty_square(), i%2==0 ? Cross : Nought);
std::cout << board << '\n';
}
}
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.

I love the word Clicky

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?
Always improve, never quit.

[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.

I love the word Clicky
is there anyway I can generate a random number only once and do not have it repeat.

This topic is closed to new replies.

Advertisement