Search code examples
cfunctionmatrixgame-developmentothello

Othello bot written in C picking (0, 0) indefinitely despite being an invalid move


I am attempting to make an Othello game in the console where a user can play a bot in C. The bot works by assessing the "score" of a given position on the board as shown in the getBestMove() function. For some reason the program just outputs this indefinitely whenever you try to make a move:

'Computer's turn (Player B). Computer chooses row 0, column 0. Invalid move. Try again.'

Tried to play a game against the computer. Whenever it's the computer's turn, the program outputs this indefinitely:

Computer's turn (Player B).
Computer chooses row 0, column 0.
Invalid move. Try again.

Solution

  • Given that it is produced by this statement ...

                printf("Computer chooses row %d, column %d.\n", row + 1, col + 1);
    

    ..., this output ...

    Computer chooses row 0, column 0.

    ... shows that the call to getBestMove() returned -1, -1. Examination of that function's implementation shows that that must happen because it does not accept any move. One reason for such behavior would be that isValidMove() rejects all moves suggested to it -- and that needs to be accommodated -- but even if that function does accept some moves as valid, the implementation of getBestMove() is flawed.

    Note that getBestMove() initializes its maxScore variable to -1, and selects a given (row, column) only if it attributes a higher score to that position than the current value of maxScore. Now observe that the scores it's comparing to include many negative ones, including all of those in and around the center of the board. That function will never select any of those positions, because their scores can never exceed the initial maxScore.

    So you need at least two things:

    1. Initialize maxScore to a value that is strictly less than all the elements of the scores array.

    2. Make the main game loop able to handle the possibility that a player has no valid moves on their turn. That's not the case at the start of the game, but it does sometimes happen during ordinary gameplay.