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