英文:
Othello bot written in C picking (0, 0) indefinitely despite being an invalid move
问题
我正尝试在控制台中制作一个黑白棋游戏,用户可以在C语言中与机器对战。这个机器对局的逻辑是通过评估棋盘上给定位置的"分数"来完成的,就像在getBestMove()函数中所示。但由于某种原因,每当尝试进行一步棋时,程序都会无限输出以下内容:
'计算机的回合(玩家B)。
计算机选择了行0,列0。
无效的移动,请重试。'
尝试与计算机对战时,每当轮到计算机时,程序都会无限输出以下内容:
计算机的回合(玩家B)。
计算机选择了行0,列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
得分: 0
给定这个语句产生的内容...
printf("计算机选择了第 %d 行,第 %d 列。\n", row + 1, col + 1);
... 这个输出...
计算机选择了第 0 行,第 0 列。
... 表明调用 getBestMove()
返回了 -1, -1。对该函数实现的检查显示,这是因为它不接受任何移动。导致这种行为的原因之一可能是 isValidMove()
拒绝了建议给它的所有移动 — 需要进行相应的调整 — 但即使该函数接受某些移动作为有效,getBestMove()
的实现也存在缺陷。
注意,getBestMove()
将其 maxScore
变量初始化为 -1,并且只选择在该位置分数高于当前 maxScore
值的给定 (row, column)。现在观察一下,它比较的分数包括许多负值,包括棋盘中心周围的所有负值。该函数永远不会选择这些位置,因为它们的分数永远无法超过初始的 maxScore
。
因此,你需要至少两件事:
-
将
maxScore
初始化为严格小于scores
数组中所有元素的值。 -
使主游戏循环能够处理玩家在他们的回合中没有有效移动的可能性。这不是游戏开始时的情况,但在普通游戏过程中有时会发生。
英文:
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:
-
Initialize
maxScore
to a value that is strictly less than all the elements of thescores
array. -
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论