0

タイトルがすべてを物語っています。Tic Tac Toe プログラムが引き分けを検出しない理由が正確にはわかりません。主な機能、追加機能などを添付しました。何が間違っているのかわかりません。どんな助けでも大歓迎です。

#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 つのスペースの値が (初期値以外の値と) 等しいことを認識することで、勝者のチェックを簡素化できます。forまた、3 行 3 列なので、同じループ 内で行と列をチェックできます。

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 に答える