#include <iostream>
using namespace std;
void PrintBoard(); // game board
void PrintInfo(); // some info about the rules of the game is printed
void PrintStats(int, int, int); // prints the stats of the match
void IfError(int&, int&); // When invalid input is entered, prints the error
void ChoiceOfChar(char&); // Asks the users if they want to play again
void PromptTurnO(int&, int&); // Prompts the first user for data
void PromptTurnX(int&, int&); // Second user has to input coordinates
char board[3][3]; // array definition
int main()
{
int ROW; // elements of 2 dimensional array!
int COLUMN;
int FirstPlayer; // number of winning games for first player
int SecondPlayer;
int Draws; // number of draws in a game
FirstPlayer = 0;
SecondPlayer = 0;
Draws = 0;
char choice;
choice = 'Y'; // Initial value is Y in order to initialize the main loop
PrintInfo();
while (choice == 'Y') // In case the players want to play again
{
// this is the main loop
for (ROW = 0; ROW < 3; ROW++) // these forloops are used to process the array
for (COLUMN = 0; COLUMN < 3; COLUMN++) // by row and column
board[ROW][COLUMN] = ' '; // blank characters as initial values
int x; // (x,y) coordinates entered by the users
int y;
int SpotsOnBoard; // this is the sum of spots available on the game board
SpotsOnBoard = 0;
while (SpotsOnBoard <= 9) // because there is only 9 characters available on the board
{
if (SpotsOnBoard == 0)
PrintBoard(); // This function draws initialy the blank game board
// so it is easier for the user to pick the x and y values
PromptTurnO(x, y);
IfError(x, y); // If the user's input is invalid, this function prints
// the error message and prompts for correction
board[x - 1][y - 1] = 'O'; // actually the array starts from 0, NOT from 1
SpotsOnBoard++; // (as entered by the user)
// so the program have to "fix" the input
PrintBoard(); // If 3 coordintates are O's in one of 8 possible
//winning combinations, then O wins
if (board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O')
{
cout << " O player wins the game" << endl;
FirstPlayer++;
break;
}
else if (board[2][0] == 'O' && board[1][1] == 'O' && board[0][2] == 'O')
{
cout << "O player wins the game" << endl;
FirstPlayer++;
break;
}
else if (board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O')
{
cout << "O player wins the game" << endl;
FirstPlayer++;
break;
}
else if (board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O')
{
cout << "O player wins the game" << endl;
FirstPlayer++;
break;
}
else if (board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O')
{
cout << "O player wins the game" << endl;
FirstPlayer++;
break;
}
else if (board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O')
{
cout << "O player wins the game" << endl;
FirstPlayer++;
break;
}
else if (board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O')
{
cout << "O player wins the game" << endl;
FirstPlayer++;
break;
}
else if (board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O')
{
cout << "O player wins the game" << endl;
FirstPlayer++; // counts the number of won games
break; // break statement is used to make sure that only one possibility
} // will be processed
else if (SpotsOnBoard == 9)
{
cout << "Draw!" << endl;
Draws++;
break; // Without this brake statement it was not working propertly
}
PromptTurnX(x, y);
IfError(x, y);
board[x - 1][y - 1] = 'X';
SpotsOnBoard++;
PrintBoard();
if (board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X')
{
cout << "X player is victorious" << endl;
SecondPlayer++;
}
else if (board[2][0] == 'X' && board[1][1] == 'X' && board[0][2] == 'X')
{
cout << "X player is victorious" << endl;
SecondPlayer++;
}
else if (board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X')
{
cout << "X player is victorious" << endl;
SecondPlayer++;
}
else if (board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X')
{
cout << "X player is victorious" << endl;
SecondPlayer++;
}
else if (board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X')
{
cout << "X player is victorious" << endl;
SecondPlayer++;
}
else if (board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X')
{
cout << "X player is victorious" << endl;
SecondPlayer++;
}
else if (board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X')
{
cout << "X player is victorious" << endl;
SecondPlayer++;
}
else if (board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X')
{
cout << "X player is victorious" << endl;
SecondPlayer++;
}
}
ChoiceOfChar(choice);
}
PrintStats(FirstPlayer, SecondPlayer, Draws);
system("PAUSE");
return 0;
}
void PrintBoard()
// Post : The Game board is printed to help with choosing the coordintates
// Either X or O will be printed on the board.
// Board is updated while the game progresses
{
cout << endl;
cout << " 1 2 3 " << endl;
cout << "1 " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << endl;
cout << " ---|---|---" << endl;;
cout << "2 " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << endl;
cout << " ---|---|---" << endl;
cout << "3 " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << endl;
cout << endl;
cout << endl;
}
void PrintInfo()
// Post : Some info about the rules of the game is printed
{
cout << "First player is represented by O, second by X." << endl;
cout << "Enter the coordinates for Rows and Columns" << endl;
cout << "Strike enter when Done." << endl;
cout << endl;
}
void PrintStats(int FirstPlayer, int SecondPlayer, int Draws)
// Pre : Players have to decide to exit the game.
// Post : Returns the number of won games and draws.
{
cout << "The O player have won " << FirstPlayer << " times" << endl;
cout << "The X player have won " << SecondPlayer << " times" << endl;
cout << "There was " << Draws << " draws" << endl;
cout << "Thanks for using my program" << endl;
}
void IfError(int& x, int& y)
// Pre : User entered invalid data, program doesn't kow where to put them
// Post : Error message is printed, user is prompted to enter the right input
{
while (x > 3 || x < 1 || y > 3 || y < 1 || ('O' == board[x - 1][y - 1]
|| 'X' == board[x - 1][y - 1]))
{
cout << "This coordinate is not allowed, try again" << endl;
cout << "row: ";
cin >> x;
cout << "column: ";
cin >> y;
}
}
void ChoiceOfChar(char& choice)
// Pre : Entire program has been executed
// Post : User is given a chance to play again
{
cout << endl;
cout << " Press CAPITAL Y if you want to play again." << endl;
cout << " Otherwise, press any other letter key to exit the loop." << endl;
cin >> choice;
}
void PromptTurnO(int& x, int& y)
// Post : First player enters the coordinates of a spot on a game board
{
cout << "Turn of the first player (O), enter the coordinates" << endl;
cout << "Row: ";
cin >> x;
cout << "Column: ";
cin >> y;
}
void PromptTurnX(int& x, int& y)
// Post : Second player enters the coordinates of a spot on a game board
{
cout << "Turn of the second player (X), enter the coordinates" << endl;
cout << "Row: ";
cin >> x;
cout << "Column: ";
cin >> y;
```该程序是TICTACTOE游戏,棋盘为3*3,三个棋子连起来为赢。输入参数时,若输入的数字超出边界或在已有棋子的地方再输入,则提示出错,要求重新输入。 具体要求如下: 将程序改写成类。 将棋盘扩充为4*4;如果棋牌规格玩家可以定制更好,例如,玩家可以定制4*4、15*15等规格的棋牌。 统计输赢结果 打印输出。对弈算法流程如下: 两个玩家可以下棋,第一次下棋之前首先将棋盘清空;以后每次下棋时都要判断棋盘是否已满。 下棋时需要判断当前棋盘的棋子是否已满。 下棋时指定规则,由谁先下棋,下棋时判断下棋的坐标是否正确,如果不正确提示玩家继续下棋;当下棋时的坐标正确时,保存棋子的坐标和当前已下棋子的总数。 每一次下棋都要判断当前玩家的棋子是否已经连线(横、竖、对角线),当连成线时,则当前玩家赢。 如果当前玩家棋子没有连成线,则提示另一方玩家下棋,下棋过程判断同第3,4步。 结束一局后,判断是否进行下一局。该程序是TICTACTOE游戏,棋盘为3*3,当三个棋子连起来即为赢。输入参数时,若输入的数字超出边界或在已有棋子的地方再输入,则提示出错,要求重新输入。
具体要求如下:
将程序改写成类。
将棋盘扩充为4*4;如果棋牌规格玩家可以定制更好,例如,玩家可以定制4*4、15*15等规格的棋牌。
统计输赢结果 打印输出
```