Search code examples
carraystic-tac-toe

Smart computer tic-tac-toe move selection in C


I'm sure many of you are tired of seeing tic tac toe questions, but I'd appreciate any insight as to why this part doesn't function as intended.

The rest of the code works great, but right here, the computer doesn't make its move, instead it just re-displays the board and goes back to the user's move.

I verified that the user and computer marks are carried over to the function, but the selection process isn't working. It defaults to the for loop, continuing to the other functions from there, but it still doesn't assign the computer's mark.

I already have code elsewhere that makes sure the board isn't full before being sent over to the next move.

Thank you

void cmove(char umark, char cmark)
{
    int i=0;
    if((board[4] == NULL))
    {
        board[4] == cmark;
        DisplayBoard();
        cwin(umark , cmark);
    }
    if((board[0] != NULL) && (board[1] != NULL) && (board[2] != NULL) && (board[3] != NULL) && (board[4] == umark) && (board[5] != NULL) &&
       (board[6] != NULL) && (board[7] != NULL) && (board[8] != NULL))
       {
           board[0]= cmark;
           DisplayBoard();
           cwin(umark ,cmark);
       }
    for(i=0; i<9; i++)
    {
        if(board[i] == NULL)
        {
            board[i] == cmark;
            DisplayBoard();
            cwin(umark, cmark);
            i=9;
        }
    }
}

Solution

  • This is a comparison and not an assignment:

     board[i] == cmark;
    

    You have a similar problem in the first conditional block board[4] == cmark;

    Turn on all the warnings your compiler can muster to catch this sort of mistake.