C++ 如何检查平局游戏初学者C++;

C++ 如何检查平局游戏初学者C++;,c++,boolean,C++,Boolean,我正在创建一个tic tac toe程序,无法让游戏准确地检查是否有平局。当棋盘上的所有数字都填上“X”或“O”时,应宣布平局&没有赢家 用我现在的代码,每次我运行程序时,它都会声明有一个平局。我把函数放错了吗?我认为tieGame()boolean的内部有问题 using namespace std; char board[9] { //array of characters with number placeholders for chars X and O '1', '2', '

我正在创建一个tic tac toe程序,无法让游戏准确地检查是否有平局。当棋盘上的所有数字都填上“X”或“O”时,应宣布平局&没有赢家

用我现在的代码,每次我运行程序时,它都会声明有一个平局。我把函数放错了吗?我认为
tieGame()
boolean的内部有问题

using namespace std;

char board[9] { //array of characters with number placeholders for chars X and O
    '1', '2', '3', '4', '5', '6', '7', '8', '9'
};

bool checkWinner(void) {
    bool winner = false;
    if // Check for possible winning solutions for X
    ((board[0] == 'X' && board[1] == 'X' && board[2] == 'X')
    ||
    (board[3] == 'X' && board[4] == 'X' && board[5] == 'X')
    ||
    (board[6] == 'X' && board[7] == 'X' && board[8] == 'X')
    ||
    (board[0] == 'X' && board[4] == 'X' && board[8] == 'X')
    ||
    (board[2] == 'X' && board[4] == 'X' && board[6] == 'X')
    ||
    (board[0] == 'X' && board[3] == 'X' && board[6] == 'X')
    ||
    (board[1] == 'X' && board[4] == 'X' && board[7] == 'X')
    ||
    (board[2] == 'X' && board[5] == 'X' && board[8] == 'X'))
    {
        winner = 1; // Winner is true if conditions are met
        cout << "Player 1 Wins!" << endl;
    }
    else if // Check for possible winning solutions for O
    ((board[0] == 'O' && board[1] == 'O' && board[2] == 'O')
    ||
    (board[3] == 'O' && board[4] == 'O' && board[5] == 'O')
    ||
    (board[6] == 'O' && board[7] == 'O' && board[8] == 'O')
    ||
    (board[0] == 'O' && board[4] == 'O' && board[8] == 'O')
    ||
    (board[2] == 'O' && board[4] == 'O' && board[6] == 'O')
    ||
    (board[0] == 'O' && board[3] == 'O' && board[6] == 'O')
    ||
    (board[1] == 'O' && board[4] == 'O' && board[7] == 'O')
    ||
    (board[2] == 'O' && board[5] == 'O' && board[8] == 'O'))
    {
        winner = 1; // winner is True if conditions are met
        cout << "Player 2 Wins!" << endl;
    }
    return winner; // Is there a winner?

}

bool tieGame() {
    bool tiegame = false;
    if // check for tie
        ((board[0] == 'X' || 'O') && (board[1] == 'X' || 'O') && (board[2] == 'X' || 'O')
         &&
         (board[3] == 'X' || 'O') && (board[4] == 'X' || 'O') && (board[5] == 'X' || 'O')
         &&
         (board[6] == 'X' || 'O') && (board[7] == 'X' || 'O') && (board[8] == 'X' || 'O'))
    {
        tiegame = 1;
        cout << "The game is a tie!  Play again!" << endl;
    }
    else {
        tiegame = 0;
    }
    return tiegame; //  Is the game a tie?
}


void displayBoard(void) { //Displays the game board
    int index; // used to access the array
    index = 0;
    cout << endl;
    cout << board[index] << "|" << board[index+1] << "|" << board[index+2] << endl;
    cout << "-----" << endl;
    cout << board[index+3] << "|" << board[index+4] << "|" << board[index+5] << endl;
    cout << "-----" << endl;
    cout << board[index+6] << "|" << board[index+7] << "|" << board[index+8] << endl;
}

void tictactoe(void) { //Main function; displays board and inputs player moves
    int movePosition; // used to track user input and replace array indexes with the user input

    cout << "Player 1 is X, player 2 is O" << endl;
    for (int i=0; i < 5; i++) {
        if (tieGame() ) {
            cout << "Tie game!" << endl;
            return;
        }
        displayBoard(); // Display game board with updated characters
        if (checkWinner() ) //if winner is TRUE, return "Winner" and exit game.
            {
                cout << "Good Game!" << endl;
                return;
            }
        cout << "Player 1, Enter the space number where you would like to place X" << endl;
        cin >> movePosition; // Retrieve user input & call it 'movePosition'
            while ((board[movePosition - 1] == 'X' || board[movePosition - 1] == 'O')) {       //Check to make sure a user has not
                cout <<  "This space is already taken.  Please choose an open space." << endl; // attempted to enter a
                cin >> movePosition;                                                           // value that has already been entered
            }
        board[movePosition - 1] = 'X';
        displayBoard(); // Display game board with updated characters
            if (checkWinner() ) //if winner is TRUE, return "Winner" and exit game.
            {
                cout << "Good Game!" << endl;
                return;
            }
        cout << "Player 2, Enter the space number where you would like to place O" << endl;
        cin >> movePosition;

            while ((board[movePosition - 1] == 'X' || board[movePosition - 1] == 'O')) {
                cout << "This space is already taken.  Please choose an open space." << endl;
                cin >> movePosition;
            }
        board[movePosition - 1] = 'O';
    }
}



int main (int argc, char *argv[]) {
    tictactoe();
}
使用名称空间std;
字符板[9]{//字符数组,带有字符X和O的数字占位符
'1', '2', '3', '4', '5', '6', '7', '8', '9'
};
布尔支票赢家(无效){
bool-winner=false;
如果//检查X的可能获胜解决方案
((单板[0]='X'和单板[1]='X'和单板[2]='X')
||
(电路板[3]=“X”和电路板[4]=“X”和电路板[5]=“X”)
||
(电路板[6]='X'和电路板[7]='X'和电路板[8]='X')
||
(单板[0]=“X”和单板[4]=“X”和单板[8]=“X”)
||
(电路板[2]='X'和电路板[4]='X'和电路板[6]='X')
||
(单板[0]=“X”和单板[3]=“X”和单板[6]=“X”)
||
(电路板[1]=“X”和电路板[4]=“X”和电路板[7]=“X”)
||
(电路板[2]=='X'和电路板[5]=='X'和电路板[8]=='X'))
{
winner=1;//如果满足条件,则winner为true

cout你可以通过一个计数器来检查它是否是平局

int freeSpaces = 9;
每次玩家在棋盘上填满一个空位时,你都会减少这个值。 然后检查

if (freeSpaces == 0 && !winner) tieGame = true;
else tieGame = false;

以下情况是错误的:

(board[0] == 'X' || 'O')

由于C++运算符优先和评价规则,编译器将其理解为:

(board[0] == 'X') || ('O' != 0)
当然,第二部分总是正确的,因此它在每个领域都会成功,因此在整个董事会都会成功

您需要在两个比较中明确地编写它,如下所示:

(board[0] == 'X' || board[0] == 'O')
对于未来,比一系列条件更好的解决方案是循环,例如:

bool tieGame()
{
    for (int i = 0; i < 9; i++) {
        if (board[i] != 'X' && board[i] != 'O') {
            // Some field is empty, not a tie
            return false;
        }
    }
    // All fields are either 'X' or 'O'
    cout << "The game is a tie!  Play again!" << endl;
    return true;
}
bool-tieGame()
{
对于(int i=0;i<9;i++){
if(board[i]!='X'和&board[i]!='O'){
//有些领域是空的,不是平局
返回false;
}
}
//所有字段都是“X”或“O”

因为
'O'
是一个非零值(准确地说是79),所以形式
(棋盘[0]='X'| |'O')
的表达式总是计算为
true
。因此,你对tieGame的所有检查都是true。你想要的是
(棋盘[0]='X'|棋盘[0]='X'='O'))

代码墙。首先想到的是:为什么要检查每一个可能的唯一行?从来没有听说过循环?非常感谢-我决定在这个项目中使用计数器。简单有效。我感谢所有的帮助!