如何检查领带游戏初学者C ++

时间:2014-05-08 22:09:30

标签: c++ boolean

我正在创建一个井字游戏程序,无法让游戏准确检查是否存在平局。当董事会上的所有数字都填满了' X'或者' O' &安培;没有胜利者。

使用我现在拥有的代码,每次运行程序时都声明有一个平局。我把功能放错了吗?我认为tieGame()布尔值的内部有问题。

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();
}

3 个答案:

答案 0 :(得分:2)

以下情况是错误的:

(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;
}

更好的是,正如 Nenad 在他的回答中所写的那样,只计算剩下的空闲空间数(或使用的字段) - 这只是一个变量比较,而不是通过整个板每一次。

答案 1 :(得分:0)

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

int freeSpaces = 9;

每次玩家在船上填充空位时,您将减少。 然后检查

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

答案 2 :(得分:0)

表单(board[0] == 'X' || 'O')的表达式始终评估为true,因为'O'是非零值(准确地说是79)。因此,您对tieGame的所有检查都是正确的。你想要的是(board[0] == 'X' || board[0] == 'O')