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.
Given that it is produced by this statement ...
..., this output ...
... 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 thatisValidMove()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 ofgetBestMove()is flawed.Note that
getBestMove()initializes itsmaxScorevariable to -1, and selects a given (row, column) only if it attributes a higher score to that position than the current value ofmaxScore. 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 initialmaxScore.So you need at least two things:
Initialize
maxScoreto a value that is strictly less than all the elements of thescoresarray.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.