# Tic tac toe problem

## Recommended Posts

I'm coding a tic tac toe game in 2d, everything has been going good so far, but the computer puts two pieces every time. I can't understand why the computer do this, but it's really hard to find any errors. Things have a tendancy to mess up at the very end when the game is almost finished, the computer is mean :// I suspect it is any of these functions:
[SOURCE]
int Board::computerMove() {
if (turn == computer) {
for (int i=0;i<PIECES;i++) {
if (isLegal(i)) {
boardRows[i] = computer;

if (winner() == computer) {
change_turn();
return 0;
}
boardRows[i] = ' ';
}
}

for (int i=0;i<PIECES;i++) {
if (isLegal(i)) {
boardRows[i] = player;

if (winner() == player) {
boardRows[i] = computer;
change_turn();
return 0;
}
boardRows[i] = ' ';
}
}

const int bestMoves[] = {4,0,2,6,8,1,3,5,7};

for (int i=0;i<PIECES;i++) {
int move = bestMoves[i];

if (isLegal(move)) {
boardRows[move] = computer;
change_turn();
return 0;
}
}
}

return 0;
}
[/SOURCE]
Or this function that checks who is the winner if there is any. I am using this function for the game loop.
[SOURCE]
char Board::winner() {
const int winningRows[8][3] = {0,1,2,
3,4,5,
6,7,8,
0,3,6,
1,4,7,
2,5,8,
0,4,8,
2,4,6};

for (int row=0;row<8;row++) {
if ((boardRows[winningRows[row][0]] != ' ') && (boardRows[winningRows[row][0]] == boardRows[winningRows[row][1]]) && (boardRows[winningRows[row][1]] == boardRows[winningRows[row][2]])) {
SDL_FillRect(screen, &pieces[boardRows[winningRows[row][0]]], SDL_MapRGB(screen->format, 0x55, 0x55, 0x55));
SDL_FillRect(screen, &pieces[boardRows[winningRows[row][1]]], SDL_MapRGB(screen->format, 0x55, 0x55, 0x55));
SDL_FillRect(screen, &pieces[boardRows[winningRows[row][2]]], SDL_MapRGB(screen->format, 0x55, 0x55, 0x55));

return boardRows[winningRows[row][0]];
}
}

int counter=0;

for (int i=0;i<PIECES;i++) {
if (boardRows[i] != ' ') {
counter++;
}

if (counter==8)
return ('T');
}

return ('0');
}
[/SOURCE]
What have I tried to fix this error? Well, the only thing that is worth mentioning, is that I tried to remove the two first for loops in the computermove function, that didn't work. The player variable is a char and have the value 'X'. The computer variable is a char with the value 'O'. I Appreciate any help or tips, can post rest of the source if necessary.

##### Share on other sites
By looking at the code, I think that the problem is not that Board::computerMove() puts 2 pieces each time, but that the function itself gets called 2 times in a row by your main program. If that's true, try examining the code in that area, or post it here if you can't find the problem yourself.

Btw, why don't you just treat the board as a 2D 3x3 array? It would simplify things.

##### Share on other sites
The function gets called constantly in the game loop but I use change_turn() so the computer can't put another piece, because that function sets the turn to the other player.

The board is a 3x3 char vector, here is the main function:

[SOURCE]int main(int argc, char *argv[]) {    if (init() == false) {        std::cout << "Error: " << SDL_GetError() << std::endl;               }        board   = load_image("board.png");    circle  = load_image("circel.png");    cross   = load_image("cross.png");        Board game;        bool done=false;        while (game.winner() == '0' && done==false) {        while (SDL_PollEvent(&event)) {            game.playerMove();                           if (event.type == SDL_QUIT) {                done=true;                           }        }                    apply_surface(0, 0, board, screen);                    game.show();           game.computerMove();                    if (SDL_Flip(screen) < 0) {            std::cout << "Error: " << SDL_GetError() << std::endl;                                 }              }}[/SOURCE]
[/source][/source]

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628368
• Total Posts
2982293

• 10
• 9
• 13
• 24
• 11