嵌套在切换语句中的if-else语句始终输出else语句

时间:2019-07-06 03:15:09

标签: c++ switch-statement

我正处于用C ++创建井字游戏的开始阶段,为了将这些“棋子”放置在板上,我使用了switch语句。在交换机内部,有if-else语句来检查请求的空间是否被占用。但是,每当我输入一个位置时,它都会执行if语句(放置棋子),然后还执行else语句(说“再试一次”并使玩家重复其回合)。

我尝试过将'break'语句移到else之前和if中,但是它似乎会产生比所修复的错误更多的错误。我还尝试过对“ else-if”语句进行显式编码,而不仅仅是“ else”,但这也不起作用。

切换说明:

bool place_pieces(int input, bool turn)
{
    bool occupied{ false };

    switch (input) {
    case 7:
        if (board[0][0] != 'x' && board[0][0] != 'o') {
            if (turn == true)
                board[0][0] = 'x';
            else
                board[0][0] = 'o';
        }
        else if (board[0][0] == 'x' || board[0][0] == 'o') {
            occupied = true;
            cout << "Try again" << endl;
        }
        break;
    case 8:
        if (board[0][1] != 'x' && board [0][1] != 'o') {
            if (turn == true)
                board[0][1] = 'x';
            else
                board[0][1] = 'o';
        }
        else if (board[0][1] == 'x' || board[0][1] == 'o') {
            occupied = true;
            cout << "Try again" << endl;
        }
        break;
    case 9:
        if (board[0][2] != 'x' && board[0][2] != 'o') {
            if (turn == true)
                board[0][2] = 'x';
            else
                board[0][2] = 'o';
        }
        else if (board[0][2] == 'x' || board[0][2] == 'o') {
            occupied = true;
            cout << "Try again" << endl;
        }
        break;
    case 4:
        if (board[1][0] != 'x' && board[1][0] != 'o') {
            if (turn == true)
                board[1][0] = 'x';
            else
                board[1][0] = 'o';
        }
        else if (board[1][0] == 'x' || board[1][0] == 'o') {
            occupied = true;
            cout << "Try again" << endl;
        }
        break;
    case 5:
        if (board[1][1] != 'x' && board[1][1] != 'o') {
            if (turn == true)
                board[1][1] = 'x';
            else
                board[1][1] = 'o';
        }
        else if (board[1][1] == 'x' || board[1][1] == 'o') {
            occupied = true;
            cout << "Try again" << endl;
        }
        break;
    case 6:
        if (board[1][2] != 'x' && board[1][2] != 'o') {
            if (turn == true)
                board[1][2] = 'x';
            else
                board[1][2] = 'o';
        }
        else if (board[1][2] == 'x' || board[1][2] == 'o') {
            occupied = true;
            cout << "Try again" << endl;
        }
        break;
    case 1:
        if (board[2][0] != 'x' && board[2][0] != 'o') {
            if (turn == true)
                board[2][0] = 'x';
            else
                board[2][0] = 'o';
        }
        else if (board[2][0] == 'x' || board[2][0] == 'o') {
            occupied = true;
            cout << "Try again" << endl;
        }
        break;
    case 2:
        if (board[2][1] != 'x' && board[2][1] != 'o') {
            if (turn == true)
                board[2][1] = 'x';
            else
                board[2][1] = 'o';
        }
        else if (board[2][1] == 'x' || board[2][1] == 'o') {
            occupied = true;
            cout << "Try again" << endl;
        }
        break;
    case 3:
        if (board[2][2] != 'x' && board[2][2] != 'o') {
            if (turn == true)
                board[2][2] = 'x';
            else
                board[2][2] = 'o';
        }
        else if (board[2][2] == 'x' || board[2][2] == 'o') {
            occupied = true;
            cout << "Try again" << endl;
        }
        break;
    }
    return occupied;
}

游戏循环:

while (!game_over) {

        x_turn = true;
        cout << "Player 1: ";
        cin >> player_input;

        place_pieces(player_input, x_turn);
        if (place_pieces(player_input, x_turn) == true) {
            draw_board();
            cout << endl;

            continue;
        }
        else {
            draw_board();
            cout << endl;
        }

        x_turn = false;
        cout << "Player 2: ";
        cin >> player_input;

        place_pieces(player_input, x_turn);
        if (place_pieces(player_input, x_turn) == true) {
            draw_board();
            cout << endl;

            continue;
        }
        else {
            draw_board();
            cout << endl;
        }
    }

我希望它只是显示添加了棋子的新棋盘,而不要说“再试一次”或让玩家重复他们的回合。

1 个答案:

答案 0 :(得分:0)

您甚至可以在不调试的情况下查找问题。评论显示您正在做

        place_pieces(player_input, x_turn);
        if (place_pieces(player_input, x_turn) == true) {

因此,您首先放置该片段,然后在if语句中再次调用该函数。并且仅在if语句中,您正在检查返回值。

如果您使用相同的参数两次调用函数,那么在第二次调用时,该位置将始终被占用。

您的误解是您认为在if语句中仅会检查返回值。但这不是事实。该函数将再次被调用。

这是所谓的副作用。您不应在if语句或其他布尔表达式中调用函数。

解决方案很简单。声明一个布尔变量,获取结果,然后在if中检查布尔值:

        bool result = place_pieces(player_input, x_turn);
        if (result) {

希望这会有所帮助。 。