【问题标题】:Tic Tac Toe 2D Char Array not updating in row=0, col=0--CTic Tac Toe 2D Char Array 未在 row=0 中更新,col=0--C
【发布时间】:2018-10-29 22:41:58
【问题描述】:

抱歉,如果这是一个简单的修复,但我似乎无法在这里找到错误。我正在用 C 语言制作井字游戏。我的程序中的所有内容似乎都运行良好,除了数组中的左上角位置([0][0] 或 1,1,其中 row-1 和 col- 1)。人类玩家和计算机玩家都可以在该位置输入他们的标记,但它会在玩家下一次移动时移除标记。我的代码如下:

我遇到的问题是这样的:

The current state of the game is:
_ _ _ 
_ _ _ 
_ _ _ 
Player 1 enter your selection [row, col]: 2,1
The current state of the game is:
_ _ _ 
O _ _ 
_ _ _ 
Player 2 has entered [row, col]: 3,1
The current state of the game is:
_ _ _ 
O _ _ 
X _ _ 
Player 1 enter your selection [row, col]: 1,1
The current state of the game is:
O _ _ 
O _ _ 
X _ _ 
Player 2 has entered [row, col]: 3,3
The current state of the game is:
_ _ _ 
O _ _ 
X _ X 

这里到底有什么问题?感谢您的帮助。

【问题讨论】:

  • 你用什么编译器来做这个?它应该对此代码发出警告。当我对其进行测试时,我收到报告称check_table_fullcheck_end_of_gamecheck_three_in_a_row 都可以在没有正确返回值的情况下结束。我的快速调试表明check_three_in_a_row 肯定也会通过无效的代码路径运行。仅此一项就可能导致此问题。
  • 我使用的是 gcc 版本 7.3.0 (Ubuntu 7.3.0-16ubuntu3)

标签: c


【解决方案1】:

问题在于以下功能。您的 if 语句只有一个 = 符号。这导致位置0, 0 被设置为'_' 并返回false。

_Bool check_table_full(char board[SIZE][SIZE])
{
int a, b;
for (a=0; a<SIZE; a++) {
    for (b=0; b<SIZE; b++) {
        if (board[a][b]=='_') {  // <==== Fixed the double equal sign
            return false;
        }
        else {
            return true;
        }
    }
}
}

我当然建议您随时清理格式。此外,您在上面提到您使用的是 GCC。对于这样一个简单的学习程序,您可能应该使用-Wall 作为显示所有警告的选项进行编译。这也将标记此代码的其他一些问题。

【讨论】:

  • 非常感谢您的修复并感谢您的提示!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多