0

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.
  • 1
    Have you tried stepping through the code with a debugger? I suspect `isValidMove()` is returning `true` for `0,0`. – Barmar May 17 '23 at 21:34
  • 1
    As per JohnBollinger's analysis a big part of the problem is likely to be in `isValidMove`. You might have to share that one as well. Of course a [mre] would be best. – teapot418 May 17 '23 at 21:57
  • Side note: I would suggest avoiding dynamic memory allocation here (and any other time you reasonably can do). Instead, I might either have `getBestMove()` return the coordinates of the selected move via out parameters, or else declare a structure type representing a pair of coordinates, and have it return instances of that structure by value. – John Bollinger May 17 '23 at 22:04
  • 1
    `getBestMove` is already working with move positions packed into one int, it could just as well return the move packed. – teapot418 May 17 '23 at 22:19

1 Answers1

0

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.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157