📄 main.cpp
字号:
//【程序名称】三子棋,三子连珠
//<!-- just kankan
#include<iostream>
#include<cstdlib>
using namespace std;
void PrintBoard();
void WelcomeInfo();
void PrintInfo();
void PrintStats(int ,int ,int);
void IfError(int&,int&);
void ChoiceOfChar(char&);
void PromptTurnO(int&,int&);
void PromptTurnX(int&,int&);
char board[4][4];
int main()
{
int ROW;
int COLUMN;
int FirstPlayer;
int SecondPlayer;
int Draws;
FirstPlayer=0;
SecondPlayer=0;
Draws=0;
char choice;
choice='Y';
WelcomeInfo();
PrintInfo();
while(choice=='Y') //当选择继续玩游戏时
{
for(ROW=0;ROW<4;ROW++)
for(COLUMN=0;COLUMN<4;COLUMN++)
board[ROW][COLUMN]=' '; //格子的初始化
int x;
int y;
int SpotsOnBoard;
SpotsOnBoard=0;
while(SpotsOnBoard<=16)
{
if(SpotsOnBoard==0)
PrintBoard(); //打印棋盘
PromptTurnO(x,y);
IfError(x,y);
board[x-1][y-1]='O';
SpotsOnBoard++;
PrintBoard();
if(board[0][0]=='O'&&board[1][1]=='O'&&board[2][2]=='O'&&board[3][3]=='O'
||board[3][0]=='O'&&board[2][1]=='O'&&board[1][2]=='O'&&board[0][3]=='O'
||board[0][0]=='O'&&board[1][0]=='O'&&board[2][0]=='O'&&board[3][0]=='O'
||board[0][1]=='O'&&board[1][1]=='O'&&board[2][1]=='O'&&board[3][1]=='O'
||board[0][2]=='O'&&board[1][2]=='O'&&board[2][2]=='O'&&board[3][2]=='O'
||board[0][3]=='O'&&board[1][3]=='O'&&board[2][3]=='O'&&board[3][3]=='O'
||board[0][0]=='O'&&board[0][1]=='O'&&board[0][2]=='O'&&board[0][3]=='O'
||board[1][0]=='O'&&board[1][1]=='O'&&board[1][2]=='O'&&board[1][3]=='O'
||board[2][0]=='O'&&board[2][1]=='O'&&board[2][2]=='O'&&board[2][3]=='O'
||board[3][0]=='O'&&board[3][1]=='O'&&board[3][2]=='O'&&board[3][3]=='O'
||SpotsOnBoard==16)
{
cout<<" O play 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++;
break;
}
else if(SpotsOnBoard==9)
{
cout<<" O player wins the game"<<endl;
FirstPlayer++;
break;
}
*/
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'&&board[3][3]=='X'
||board[3][0]=='X'&&board[2][1]=='X'&&board[1][2]=='X'&&board[0][3]=='X'
||board[0][0]=='X'&&board[1][0]=='X'&&board[2][0]=='X'&&board[3][0]=='X'
||board[0][1]=='X'&&board[1][1]=='X'&&board[2][1]=='X'&&board[3][1]=='X'
||board[0][2]=='X'&&board[1][2]=='X'&&board[2][2]=='X'&&board[3][2]=='X'
||board[0][3]=='X'&&board[1][3]=='X'&&board[2][3]=='X'&&board[3][3]=='X'
||board[0][0]=='X'&&board[0][1]=='X'&&board[0][2]=='X'&&board[0][3]=='X'
||board[1][0]=='X'&&board[1][1]=='X'&&board[1][2]=='X'&&board[1][3]=='X'
||board[2][0]=='X'&&board[2][1]=='X'&&board[2][2]=='X'&&board[2][3]=='X'
||board[3][0]=='X'&&board[3][1]=='X'&&board[3][2]=='X'&&board[3][3]=='X'
||SpotsOnBoard==16 )
{
cout<<" X player is victorious"<<endl;
SecondPlayer++;
break;
}
}
/*
else if(board[2][0]=='X'&& board[1][1]=='X'&& board[0][2]=='X')
{
cout<<" X player is victorious"<<endl;
SecondPlayer++;
break;
}
else if(board[0][0]=='X'&& board[1][0]=='X'&& board[2][0]=='X')
{
cout<<" X player is victorious"<<endl;
SecondPlayer++;
break;
}
else if(board[0][1]=='X'&& board[1][1]=='X'&& board[2][1]=='X')
{
cout<<" X player is victorious"<<endl;
SecondPlayer++;
break;
}
else if(board[0][2]=='X'&& board[1][2]=='X'&& board[2][2]=='X')
{
cout<<" X player is victorious"<<endl;
SecondPlayer++;
break;
}
else if(board[0][0]=='X'&& board[0][1]=='X'&& board[0][2]=='X')
{
cout<<" X player is victorious"<<endl;
SecondPlayer++;
break;
}
else if(board[1][0]=='X'&& board[1][1]=='X'&& board[1][2]=='X')
{
cout<<" X player is victorious"<<endl;
SecondPlayer++;
break;
}
else if(board[2][0]=='X'&& board[2][1]=='X'&& board[2][2]=='X')
{
cout<<" X player is victorious"<<endl;
SecondPlayer++;
break;
}
else if(SpotsOnBoard==9)
{
cout<<"Draw!"<<endl;
Draws++;
break;
}
} */
ChoiceOfChar(choice); //比赛完毕,如果想继续,就按大写A
}
PrintStats(FirstPlayer,SecondPlayer,Draws); //打印比赛结果
system("pause");
return 0;
}
void WelcomeInfo()
{
cout<<endl
<<endl
<<endl;
cout<<" Hello! Welcome to the game"<<endl;
}
void PrintBoard() //打印棋盘
{
cout<<endl<<endl<<endl
<<"┌─┬─┬─┬─┐"<<endl
<<"│ "<<board[0][0]<<"│ "<<board[0][1]<<"│ "<<board[0][2]<<"│"<<board[0][3]<<" │"<<endl
<<"├─┼─┼─┼─┤"<<endl
<<"│ "<<board[1][0]<<"│ "<<board[1][1]<<"│ "<<board[1][2]<<"│"<<board[1][3]<<" │"<<endl
<<"├─┼─┼─┼─┤"<<endl
<<"│ "<<board[2][0]<<"│ "<<board[2][1]<<"│ "<<board[2][2]<<"│"<<board[2][3]<<" │"<<endl
<<"├─┼─┼─┼─┤"<<endl
<<"│ "<<board[3][0]<<"│ "<<board[3][1]<<"│ "<<board[3][2]<<"│"<<board[3][3]<<" │"<<endl
<<"└─┴─┴─┴─┘"<<endl
<<endl<<endl<<endl;
}
void PrintInfo() //打印信息
{
cout<<"First player is represented by O,second by X."<<endl
<<"Enter the coordinates for Rows and Columns"<<endl
<<"Strike enter when Done."<<endl
<<endl;
}
void PrintStats(int FirstPlayer,int SecondPlayer,int Draws) //打印比赛结果
{
cout<<"The O player have won"<<FirstPlayer<<"times"<<endl
<<"The X player have won"<<SecondPlayer<<"times"<<endl
<<"There was"<<Draws<<" draws"<<endl
<<"Thanks for using my program"<<endl;
}
void IfError(int& x,int& y) //输入出错提示
{
while(x>4||x<1||x>4||y<1||('O'==board[x-1][y-1]||'X'==board[x-1][y-1]))
{
cout<<"This coordinate is not allowed,try again"<<endl
<<"row:";
cin>>x;
cout<<"column:";
cin>>y;
}
}
void ChoiceOfChar(char& choice) //选择是否继续游戏
{
cout<<endl
<<"Press CAPITAL Y if you want to play again."<<endl
<<"Otherwise,press any other letter key to exit the loop."<<endl;
cin>>choice;
}
void PromptTurnO(int& x,int& y)
{
cout<<"Turn of the first player(O),enter the coordinates"<<endl
<<"Row:";
cin>>x;
cout<<"Column:";
cin>>y;
}
void PromptTurnX(int& x,int& y)
{
cout<<"Turn of the first player(X),enter the coordinates"<<endl
<<"Row:";
cin>>x;
cout<<"Column:";
cin>>y;
}
//-->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -