0

标题说明了一切。我不确定井字游戏程序未检测到平局的确切原因。我已经附加了主要功能,附加功能等。我不确定我做错了什么。任何帮助是极大的赞赏。

#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;

string displayBoard(string board[9]);   // displays tic tac toe board
bool isGameOver(string board[9]);       // checks if game is over
void displayGameWelcome();              // displays welcome message

int main()
{
string board[9];          // tic tac toe, top row 0 thru 2, middle row 3 thru 5, bottom row 6 thru 8
int position = 0;         // player's position
bool gameOver = false;    // flag variable to mark end of the game
bool validMove = false;   // determines if move is valid or not

displayGameWelcome();

// initializing board with blank spaces
for (int i = 0; i < 9; i++)
{
    board[i] = " ";
}

while (!gameOver)
{
    // player #1's turn **************************
    validMove = false;


    while (!validMove)
    {
        cout << "Enter your position: ";
        cin >> position;

        if (position >= 0 && position <= 8 && board[position] == " ")
        {
            validMove = true;
            board[position] = "x";      // placing x in desired board location
        }
        else
        {
            cout << "That position is already occupied." << endl;
            cout << displayBoard(board) << endl;
        }

    }

    cout << displayBoard(board) << endl;

    if (isGameOver(board))
    {
        gameOver = true;
    }

    // player #2's turn **********************************
    validMove = false;

    while (!validMove)
    {
        cout << "Enter your position: ";
        cin >> position;

        if (position >= 0 && position <= 8 && board[position] == " ")
        {
            validMove = true;
            board[position] = "o";      // placing o in desired board position
        }
        else
        {
            cout << "That position is already occupied." << endl;
            cout << displayBoard(board) << endl;
        }

    }

    cout << displayBoard(board) << endl;

    if (isGameOver(board))
    {
        gameOver = true;
    }

}// end of validMove while loop

system("pause");
return 0;
}// end of main

// ************************** functions *************************

void displayGameWelcome()
{
    cout << "Welcome to Tic Tac Toe v3.2" << endl;\
    cout << "by Brett Singley & Nick Canarelli" << endl;
    cout << "****************************\n\n\n";
}// end of displayGameWelcome

// checks if game is over
bool isGameOver(string board[9])
{

if (board[0] == "x" && board[1] == "x" && board[2] == "x")
{
    cout << endl << "The game is over - x wins" << endl;
    return true;
}
else if (board[0] == "o" && board[1] == "o" && board[2] == "o")
{
    cout << endl << "The game is over - o wins" << endl;
    return true;
}
else if (board[3]=="x" && board[4]=="x" && board[5]=="x")
{
    cout <<endl << "The game is over - X wins" <<endl;
}
else if (board[3]=="o" && board[4]=="o" && board[5]=="o")
{
    cout <<endl << "The game is over - o wins" <<endl;
}
else if (board[0]=="x" && board[3]=="x" && board[6]=="x")
{
    cout <<endl << "The game is over - X wins" <<endl;

}
else if (board[0]=="o" && board[3]=="o" && board[6]=="o")
{
    cout <<endl << "The game is over - o wins" <<endl;
}
else if (board[6]=="x" && board[7]=="x" && board[8]=="x")
{
    cout <<endl << "The game is over - X wins" <<endl;
}
else if (board[6]=="o" && board[7]=="o" && board[8]=="o")
{
    cout <<endl << "The game is over - O wins" <<endl;
}
else if (board[1]=="x" && board[4]=="x" && board[7]=="x")
{
    cout <<endl << "The game is over - X wins" <<endl;

}
else if (board[1]=="o" && board[4]=="o" && board[7]=="o")
{
    cout <<endl << "The game is over - O wins" <<endl;
}
else if (board[2]=="x" && board[5]=="x" && board[8]=="x")
{
    cout <<endl << "The game is over - X wins" <<endl;
}
else if (board[2]=="o" && board[5]=="o" && board[8]=="o")
{
    cout <<endl << "The game is over - o wins" <<endl;
}
else if (board[0]=="x" && board[4]=="x" && board[8]=="x")
{
    cout <<endl << "The game is over - X wins" << endl; 
}
else if (board[0]=="o" && board[4]=="o" && board[8]=="o")
{
    cout <<endl << "The game is over - o wins" <<endl;
}
else if (board[2]=="x" && board[4]=="x" && board[6]=="x")
{
    cout <<endl << "The game is over - X wins" <<endl;
}
else if (board[2]=="o" && board[4]=="o" && board[6]=="o")
{
    cout <<endl << "The game is over - o wins" <<endl;
}
else
{
    cout << "WOAH!!!!! Tie Game" <<endl;
}


// more to do here (don't forget to check for draw)

return false;
}// end of isGameOver

// displays tic tac toe board in the format
// |0 1 2|
// |3 4 5|
// |6 7 8|
string displayBoard(string board[9])
{
string str = "";

for (int i = 0; i < 9; i++)
{

    if (i == 0)
    {
        str = str  + "|" + board[i];
    }
    else if (i == 2 || i == 5)
    {
        str = str + board[i] + "|\n|";
    } 
    else if (i == 8)
    {
        str = str +  board[i] + "|\n";
    } 
    else
    {
        str = str + board[i];
    }

}

return str;
}// end of displayBoard
4

1 回答 1

0

您可以通过使用循环来简化对获胜者的检查,for并意识到三个空格中的值相等(与初始值不同)。此外,由于有 3 行和 3 列,因此可以在同一for循环内检查行和列:

const char initial_value = ' ';
const unsigned int max_rows_or_columns = 3;
bool Is_Game_Over(int board[9], char& winner)
{
    for (unsigned int i = 0; i < max_rows_or_columns; ++i)
    {
        // Check the row
        if (   (board[(i * 3) + 0] == board[(i * 3) + 1])
            && (board[(i * 3) + 1) == board[(i * 3) + 2]))
        {
           // By transitive property, board[(i * 3) + 0] == board[(i * 3) + 2]
           winner = board[(i * 3) + 0];
           if (winner != initial_value)
           {
               break;
           }
        }
        else // Check the columns
        {
            if (   (board[(0 * 3) + i] == board[(1 * 3) + i])  
                && (board[(1 * 3) + i] == board[(2 * 3) + i]))
            {
                winner = board[(0 * 3) + i];
                if (winner != initial_value)
                {
                    break;
                }
            }
        }
    bool winner_found = true;
    if (i >= max_rows_or_columns)
    {
        winner_found = false;
    }
    return winner_found;
}

对角线的检查留给读者作为练习。

于 2013-04-30T20:09:50.963 回答