您的位置:首页 > 编程语言 > C语言/C++

简单C++代码实现五子棋任务

2020-03-17 07:36 447 查看

简单C++代码实现五子棋任务

首先先展示一下运行的图片

话也不多说,直接分不同代码板块来介绍程序不同功能以及是如何实现的

首先,对于一个五子棋程序,我们要思考的,在过程式的编程思想里面,如何将其功能分解为不同的函数

1.打印棋盘
由于使用的棋子每一个棋子占两个字符,所以打印时要使用两个空格

int b=0,w=0,player=1,x,y; //b和w分别作为参数标记黑棋白棋的胜利,player用来指明每次是下黑棋还是白棋,x,y分别用来作为棋盘的横纵坐标
int chess[11][11];//初始化
void board()//每一次打印棋盘的函数
{
cout << "    1 2 3 4 5 6 7 8 9 10" <<endl;
cout << "  +--------------------+" <<endl;
for(int i=1;i<=9;i++)
{
cout<<" "<<i<<"|";
input(i-1);//input函数在后文介绍
cout<<"|"<<endl;
}
cout << "10|";input(9); cout <<"|" <<endl;
cout << "  +--------------------+" <<endl;
}

考虑到字符数组本身无法同时对连续两个字符位赋值,这里采用二位数组表示下棋位置并采用一个input函数将二维数组转化为棋子

void init()
{
for(int i=0;i<11;i++)
{
for(int j=0;j<11;j++)
chess[i][j]=0;//初始化棋盘全为0
}
}
void input(const int n)
{
for(int i=n,j=0;j<10;j++)
{
switch(chess[i][j])//利用这个switch语句来将空白的棋盘中需要打印的棋子打印上去
{
case(0): cout << "  ";break;
case(1): cout << "��";break;
case(-1): cout << "��";break;
}
}
}

2.下棋部分
这一部分也是最为麻烦的部分,由于每次调用检验函数检验黑棋或白棋是否胜利会带来不必要的麻烦,所以在每一次下棋之后直接在下棋的位置往各个方向检索以判断是否胜利

void play(int x,int y)
{
if(player==1)
{chess[x-1][y-1]=1;//表示下黑棋
if(chess[x-1][y]==1&&chess[x-1][y+1]==1&&chess[x-1][y+2]==1&&chess[x-1][y+3]==1)//重复的判断代码,每一次复制粘贴即可
b=5;
else if(chess[x-1][y]==1&&chess[x-1][y-1]==1&&chess[x-1][y-2]==1&&chess[x-1][y-3]==1)
b=5;
else if(chess[x][y-1]==1&&chess[x+1][y-1]==1&&chess[x+2][y-1]==1&&chess[x+3][y-1]==1)
b=5;
else if(chess[x][y-1]==1&&chess[x-1][y-1]==1&&chess[x-2][y-1]==1&&chess[x-3][y-1]==1)
b=5;
else if(chess[x][y]==1&&chess[x+1][y+1]==1&&chess[x+2][y+2]==1&&chess[x+3][y+3]==1)
b=5;
else if(chess[x-2][y-2]==1&&chess[x-3][y-3]==1&&chess[x-4][y-4]==1&&chess[x-5][y-5]==1)
b=5;
else if(chess[x-2][y]==1&&chess[x-3][y+1]==1&&chess[x-4][y+2]==1&&chess[x-5][y+3]==1)
b=5;
else if(chess[x][y-2]==1&&chess[x+1][y-3]==1&&chess[x+2][y-4]==1&&chess[x+3][y-5]==1)
b=5;
player=2;}
else if(player==2)
{chess[x-1][y-1]=-1;//表示下白棋
if(chess[x-1][y]==1&&chess[x-1][y+1]==1&&chess[x-1][y+2]==1&&chess[x-1][y+3]==1)
w=5;
else if(chess[x-1][y]==1&&chess[x-1][y-1]==1&&chess[x-1][y-2]==1&&chess[x-1][y-3]==1)
w=5;
else if(chess[x][y-1]==1&&chess[x+1][y-1]==1&&chess[x+2][y-1]==1&&chess[x+3][y-1]==1)
w=5;
else if(chess[x][y-1]==1&&chess[x-1][y-1]==1&&chess[x-2][y-1]==1&&chess[x-3][y-1]==1)
w=5;
else if(chess[x][y]==1&&chess[x+1][y+1]==1&&chess[x+2][y+2]==1&&chess[x+3][y+3]==1)
w=5;
else if(chess[x-2][y-2]==1&&chess[x-3][y-3]==1&&chess[x-4][y-4]==1&&chess[x-5][y-5]==1)
w=5;
else if(chess[x-2][y]==1&&chess[x-3][y+1]==1&&chess[x-4][y+2]==1&&chess[x-5][y+3]==1)
w=5;
else if(chess[x][y-2]==1&&chess[x+1][y-3]==1&&chess[x+2][y-4]==1&&chess[x+3][y-5]==1)
w=5;
player=1;}
}

同时,我们还需要一点小小的附加代码,因为你不能保证每一次棋手下棋都是在合法位置

void judge()
{
while(1)//c++类似的使用很多,用永真的表达式,然后判断跳出条件break,这里主要用来重复判断落子是否合法
{
if(x<=0||x>10||y<=0||y>10)
{
cout <<"invalid position,input again:"<<endl;
cin >>x>>y;
}
else if(chess[x-1][y-1]!=0)
{
cout <<"wrong place,input again:"<<endl;
cin >>x>>y;
}
else if(x>0&&x<=10&&y>0&&y<=10&&chess[x-1][y-1]==0)
break;
}
}

3.主函数
加下来就是main函数部分了,显而易见了

int main()
{
init();
board();
while(1)
{
cout << "Black: ";
cin>>x>>y;
judge();
play(x,y);
system("cls");//清屏功能
board();
if(b==5)
{
system("cls");cout << "Black win";break;
}
else if(w==5)
{
system("cls");cout << "White win";break;
}
cout << "White: " ;
cin >>x>>y;
judge();
play(x,y);
system("cls");
board();
if(b==5)
{
system("cls");cout << "Black win";break;
}
else if(w==5)
{
system("cls");cout << "White win";break;
}
}
return 0;
}

至此,就可以实现整个五子棋代码的功能了

附上完整的代码:

#include <iostream>
using namespace std;
int b=0,w=0,player=1,x,y; //b和w分别作为参数标记黑棋白棋的胜利,player用来指明每次是下黑棋还是白棋,x,y分别用来作为棋盘的横纵坐标
int chess[11][11];//初始化
void init()
{
for(int i=0;i<11;i++)
{
for(int j=0;j<11;j++)
chess[i][j]=0;//初始化棋盘全为0
}
}
void input(const int n)
{
for(int i=n,j=0;j<10;j++)
{
switch(chess[i][j])//利用这个switch语句来将空白的棋盘中需要打印的棋子打印上去
{
case(0): cout << "  ";break;
case(1): cout << "��";break;
case(-1): cout << "��";break;
}
}
}void board()//每一次打印棋盘的函数
{
cout << "    1 2 3 4 5 6 7 8 9 10" <<endl;
cout << "  +--------------------+" <<endl;
for(int i=1;i<=9;i++)
{
cout<<" "<<i<<"|";
input(i-1);//input函数在后文介绍
cout<<"|"<<endl;
}
cout << "10|";input(9); cout <<"|" <<endl;
cout << "  +--------------------+" <<endl;
}
void play(int x,int y)
{
if(player==1)
{chess[x-1][y-1]=1;//表示下黑棋
if(chess[x-1][y]==1&&chess[x-1][y+1]==1&&chess[x-1][y+2]==1&&chess[x-1][y+3]==1)//重复的判断代码,每一次复制粘贴即可
b=5;
else if(chess[x-1][y]==1&&chess[x-1][y-1]==1&&chess[x-1][y-2]==1&&chess[x-1][y-3]==1)
b=5;
else if(chess[x][y-1]==1&&chess[x+1][y-1]==1&&chess[x+2][y-1]==1&&chess[x+3][y-1]==1)
b=5;
else if(chess[x][y-1]==1&&chess[x-1][y-1]==1&&chess[x-2][y-1]==1&&chess[x-3][y-1]==1)
b=5;
else if(chess[x][y]==1&&chess[x+1][y+1]==1&&chess[x+2][y+2]==1&&chess[x+3][y+3]==1)
b=5;
else if(chess[x-2][y-2]==1&&chess[x-3][y-3]==1&&chess[x-4][y-4]==1&&chess[x-5][y-5]==1)
b=5;
else if(chess[x-2][y]==1&&chess[x-3][y+1]==1&&chess[x-4][y+2]==1&&chess[x-5][y+3]==1)
b=5;
else if(chess[x][y-2]==1&&chess[x+1][y-3]==1&&chess[x+2][y-4]==1&&chess[x+3][y-5]==1)
b=5;
player=2;}
else if(player==2)
{chess[x-1][y-1]=-1;//表示下白棋
if(chess[x-1][y]==1&&chess[x-1][y+1]==1&&chess[x-1][y+2]==1&&chess[x-1][y+3]==1)
w=5;
else if(chess[x-1][y]==1&&chess[x-1][y-1]==1&&chess[x-1][y-2]==1&&chess[x-1][y-3]==1)
w=5;
else if(chess[x][y-1]==1&&chess[x+1][y-1]==1&&chess[x+2][y-1]==1&&chess[x+3][y-1]==1)
w=5;
else if(chess[x][y-1]==1&&chess[x-1][y-1]==1&&chess[x-2][y-1]==1&&chess[x-3][y-1]==1)
w=5;
else if(chess[x][y]==1&&chess[x+1][y+1]==1&&chess[x+2][y+2]==1&&chess[x+3][y+3]==1)
w=5;
else if(chess[x-2][y-2]==1&&chess[x-3][y-3]==1&&chess[x-4][y-4]==1&&chess[x-5][y-5]==1)
w=5;
else if(chess[x-2][y]==1&&chess[x-3][y+1]==1&&chess[x-4][y+2]==1&&chess[x-5][y+3]==1)
w=5;
else if(chess[x][y-2]==1&&chess[x+1][y-3]==1&&chess[x+2][y-4]==1&&chess[x+3][y-5]==1)
w=5;
player=1;}
}void judge()
{
while(1)//c++类似的使用很多,用永真的表达式,然后判断跳出条件break,这里主要用来重复判断落子是否合法
{
if(x<=0||x>10||y<=0||y>10)
{
cout <<"invalid position,input again:"<<endl;
cin >>x>>y;
}
else if(chess[x-1][y-1]!=0)
{
cout <<"wrong place,input again:"<<endl;
cin >>x>>y;
}
else if(x>0&&x<=10&&y>0&&y<=10&&chess[x-1][y-1]==0)
break;
}
}int main()
{
init();
board();
while(1)
{
cout << "Black: ";
cin>>x>>y;
judge();
play(x,y);
system("cls");//清屏功能
board();
if(b==5)
{
system("cls");cout << "Black win";break;
}
else if(w==5)
{
system("cls");cout << "White win";break;
}
cout << "White: " ;
cin >>x>>y;
judge();
play(x,y);
system("cls");
board();
if(b==5)
{
system("cls");cout << "Black win";break;
}
else if(w==5)
{
system("cls");cout << "White win";break;
}
}
return 0;
}
  • 点赞 1
  • 收藏
  • 分享
  • 文章举报
A_N_Huang 发布了1 篇原创文章 · 获赞 1 · 访问量 31 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: