📄 wuziqi.cpp
字号:
/*第2题 五子棋游戏--源代码及关键源代码注解如下:*/
#include <iostream.h>
#include <stdlib.h>
void PrintBoard(); // 建立函数,输出棋盘
void PrintInfo(); // 输出游戏提示信息
void PrintStats(int, int, int); // 输出统计出的胜负局数信息
void IfError(int&, int&); // 判断输入是否有误,输出判断结果并要求重新输入
void ChoiceOfChar(char&); // 询问玩家是否再玩一盘
void PromptTurnO(int&, int&); // 第一位玩家下棋
void PromptTurnX(int&, int&); // 第二位玩家下棋
int Winer(char);
char board[9][9]; // 定义棋盘
int main()
{
int ROW;
int COLUMN; // 定义棋盘的行列元素
int FirstPlayer; //第一位玩家赢棋局数
int SecondPlayer; //第二位玩家赢棋局数
int Draws; //平局数
FirstPlayer = 0;
SecondPlayer = 0;
Draws = 0;
char choice;
choice = 'Y'; // 初始值设为"Y",使程序可以继续循环下去
PrintInfo();
while(choice == 'Y'||'y') // 判断循环条件
{
for (ROW = 0; ROW <9; ROW++) //棋盘初始化
for (COLUMN = 0; COLUMN <9; COLUMN++)
board[ROW][COLUMN]= ' ';
int x;
int y;
int flag;
int SpotsOnBoard; // 棋盘上玩家已布棋子数
SpotsOnBoard = 0;
while ( SpotsOnBoard <=81) // 当已布棋子小于81,继续
{
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
//执"O"的玩家下棋
PromptTurnO(x, y);
IfError(x, y); // 玩家输入坐标错误,报错,提示重新输入
board[x - 1][y - 1]= 'O';
SpotsOnBoard++;
system("cls"); //屏幕初始化
PrintBoard();
flag=Winer('O');
if(flag==1)
{
flag=0;
FirstPlayer++;
goto A1;
}
if (SpotsOnBoard == 81)
{
cout << "平局!" <<endl;
Draws++;
}
PromptTurnX(x, y);
IfError(x, y);
board[x - 1][y - 1]= 'X';
SpotsOnBoard++;
system("cls");
PrintBoard();
flag=Winer('X');
if(flag==1)
{
flag=0;
SecondPlayer++;
goto A1;
}
}
A1: ChoiceOfChar(choice);
}
PrintStats(FirstPlayer, SecondPlayer, Draws);
system("PAUSE");
return 0;
}
// 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
void PrintBoard()
{
cout << endl;
for(int k=1;k<10;k++)
{
cout<<" "<<k;
}
cout<<endl;
for(int x=0;x<9;x++)
{ cout << (x+1) <<" ";
for(int y=0;y<9;y++)
{
cout<< board[x][y] << " | " ;
}
cout << endl;
cout << " ---|---|---|---|---|---|---|---|---" << endl;
}
}
// Post : Some info about the rules of the game is printed
void PrintInfo()
{
cout << "第一位玩家执 O ,第二位玩家执 X 。" << endl;
cout << "输入棋子坐标的行列数以布棋" << endl;
cout << "按回车以确定输入" << endl;
}
// Pre : Players have to decide to exit the game.
// Post : Returns the number of won games and draws.
void PrintStats(int FirstPlayer, int SecondPlayer, int Draws)
{
cout << "执 O 的玩家共赢 " << FirstPlayer << " 局" << endl;
cout << "执 X 的玩家共赢 " << SecondPlayer << " 局" << endl;
cout << "平局 " << Draws << " 局" << endl;
cout<<endl;
cout<<endl;
cout << "再 见" << endl;
}
// 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
void IfError(int& x, int& y)
{
while (x > 9 || x < 1 || y > 9 || y < 1 ||('O'== board[x - 1][y - 1]
||'X'== board[x - 1][y - 1]))
{
cout << "坐标输入错误,请重新输入" << endl;
cout << "行: ";
cin >> x;
cout << "列: ";
cin >> y;
}
}
// Pre : Entire program has been executed
// Post : User is given a chance to play again
void ChoiceOfChar(char& choice)
{
cout << endl;
cout << " 继续游戏,请输入 Y ." << endl;
cout << " 否则, 输入其他任意键退出游戏." << endl;
cin >> choice;
}
// Post : First player enters the coordinates of a spot on a game board
void PromptTurnO(int& x, int& y)
{
cout << "执 O 的玩家,请输入棋子坐标的行列数" << endl;
cout << "行: ";
cin >> x;
cout << "列: ";
cin >> y;
}
// Post : Second player enters the coordinates of a spot on a game board
void PromptTurnX(int& x, int& y)
{
cout << "执 X 的玩家,请输入棋子坐标的行列数"<< endl;
cout << "行: ";
cin >> x;
cout << "列: ";
cin >> y;
}
int Winer(char tmp)
{
int i,j;
//设定水平方向的获胜组合
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -