【问题标题】:Theoretically big problem with my Tic tac toe game , but for me all seems good我的井字游戏理论上是个大问题,但对我来说一切似乎都很好
【发布时间】:2020-02-03 16:47:43
【问题描述】:

所以我进入了学习 c++ 的下一步,那是一个矩阵。我尝试做一个简单的井字游戏,但我的游戏无法正确检查游戏是否结束。如果您在第一轮中输入 height = 2 和 width = 2,则表示您赢了...我看不出我在哪里搞砸了,在我看来一切都很好...

   int map[3][3];
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        map[i][j] = 0;
    }
}

bool finished = false;
int player = 1;
while (!finished) {
    //attack
    cout << "player " << player << " it is your turn"<< endl;

    cout << "The map looks like this:" << endl;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cout << map[i][j] << " ";
        }
        cout << endl;
    }
    bool correctMove;
    int height, width;
    do
    {
        correctMove = true;
        cout << "Where do you want to attack?" << endl;
        cout << "height = "; cin >> height;
        cout << "width = "; cin >> width;
        if (map[height][width] != 0 || width > 2 || height > 2) {
            correctMove = false;
        }
    } while (!correctMove);
    map[height][width] = player;
    //check finish game
    bool foundSequenceLine = true;
    for (int i = 0; i < 3; i++) {
        if (map[height][i] != player) {
            foundSequenceLine = false;
        }
    }
    bool foundSequenceColumn = true;
    for (int i = 0; i < 3; i++) {
        if (map[i][width] != player) {
            foundSequenceColumn = false;
        }
    }
    bool foundSequenceDiag1 = true;
    if (height == width) {
        for (int i = 0; i < 3; i++) {
            if (map[i][i] != player) {
                foundSequenceDiag1 = false;
            }
        }
    }
    bool foundSequenceDiag2 = true;
    if (height + width == 2) {
        for (int i = 0; i < 3; i++) {
            if (map[2-i][i] != player) {
                foundSequenceDiag2 = false;
            }
        }
    }
    if (foundSequenceColumn || foundSequenceLine || foundSequenceDiag1 || foundSequenceDiag2) {
        finished = true;
        cout << "Congrats player " << player << " you won!!!";
    }

    //change turn
    if (player == 1) {
        player++;
    }
    else {
        player--;
    }
}

}

【问题讨论】:

  • 您是否尝试使用调试器逐行执行代码?实际行为与您的预期在哪一行不同?
  • @ThomasSablik 这就是我现在正在做的事情,但我说,也许可以在不使用断点的情况下发现错误。身份证
  • @Victor 那么,如果您甚至没有花几分钟尝试自己分析问题或确保发布实际编译的示例,我们为什么要帮助您?
  • @Voo 我把断点放在每一行(这很重要),直到完成检查并且没有问题..
  • @Voo 示例编译。我能够按照发布的方式运行它。

标签: c++ matrix visual-c++ tic-tac-toe


【解决方案1】:

代码做了一个假设,然后避免检查它。

您的代码假定玩家赢了,除非您能详尽地证明他们没有赢。
问题是,您随后将两个证明某步棋不是获胜棋步的测试短路了。

看看这段代码在做什么:

   bool foundSequenceDiag1 = true;
    if (height == width) {
        for (int i = 0; i < 3; i++) {
            if (map[i][i] != player) {
                foundSequenceDiag1 = false;
            }
        }
    }

首先,你说“玩家赢了”foundSequenceDiag1=true;。然后你说,“是在对角线上移动吗?”,然后才运行可以将 foundSequenceDiag1 设置为 false 的代码。

如果玩家的移动不在对角线上,则检查不会进行。

修复:

    bool foundSequenceDiag1 = (height==width);  // true if the player played on diagonal
    if (foundSequenceDiag1) {  // loop code now only executes if player played on diagonal
        for (int i = 0; i < 3; i++) {
            if (map[i][i] != player) {
                foundSequenceDiag1 = false;
            }
        }
    }

当你找到东西时,停止寻找。

如果我正在编写您的支票,我会在找到答案后使用 break 关键字停止查找。

    for (int i = 0; i < 3; i++) {
        if (map[i][i] != player) {
            foundSequenceDiag1 = false;
            break; // can't be true now, so stop checking.
        }
    }

【讨论】:

  • 非常感谢!!!我会立即检查它是否有效。关于那个休息;我还没学会它的作用。我在某处读到它停止了循环,但就是这样,没有提到任何条件或下一步。再次感谢!
  • break 只是提前终止当前循环。它只影响它所在的​​循环:如果该循环在外循环内,则外循环将继续运行。
猜你喜欢
  • 2014-06-14
  • 2011-12-01
  • 2022-01-23
  • 2021-02-27
  • 1970-01-01
相关资源
最近更新 更多