改版五子棋 version 3.0
更新日志:
- v1.0 修改了bug【源码来源:链接 | yzmyyds】
- v2.0 添加了command mode
- v3.0 添加了脚本模式(
代码长度从196行加到660行)
脚本示例
0000000000001820000
0000000000000000100
0000000000000010000
0000000000000000200
0000000001000000000
0000000000000000200
0000022200000000000
0000000000000000200
0000000000000000100
0000000000000000100
0000000000000000000
0000000000000000000
0000000000000000000
0000000000000000000
0000000000000000000
0000000000000000000
0000000000000000000
0000000000000000000
0000000000000000200
off record
gamerule set black at bw true
gameevent clean black
gameevent put black a b
gemeevent remove
gameevent become 1 to 2
win black
#include<iostream>
#include<string>
#include<windows.h>
#include<conio.h>
#include <stdio.h>
using namespace std;
int shu=1,heng=1,pie=1,na=1;//横竖撇捺
int col=1;
char a[3]={'1','0','\0'},a1[3]={'2','0','\0'},a2[3]={'3','0','\0'},a3[3]={'4','0','\0'},a4[3]={'5','0','\0'},a5[3]={'6','0','\0'},a6[3]={'7','0','\0'},a7[3]={'8','0','\0'},a8[3]={'9','0','\0'};
bool cci=0,ifd=0,wcan=0,bcan=0; int qiPan[19][19]; int qiPanTwo[19][19];
void cml()
{
if(cci==1)
{
char cc[8]={'c','o','l','o','r',' ',NULL,NULL};
switch(col)
{
case 1:
strcat(cc,a);
system(cc);
col++;
if(col>=7)
{
col=1;
}
break;
case 2:
strcat(cc,a1);
system(cc);
col++;
if(col>=7)
{
col=1;
}
break;
case 3:
strcat(cc,a2);
system(cc);
col++;
if(col>=7)
{
col=1;
}
break;
case 4:
strcat(cc,a3);
system(cc);
col++;
if(col>=7)
{
col=1;
}
break;
case 5:
strcat(cc,a4);
system(cc);
col++;
if(col>=7)
{
col=1;
}
break;
case 6:
strcat(cc,a5);
system(cc);
col++;
if(col>=7)
{
col=1;
}
break;
case 7:
strcat(cc,a6);
system(cc);
col++;
if(col>=7)
{
col=1;
}
break;
case 8:
strcat(cc,a7);
system(cc);
col++;
if(col>=7)
{
col=1;
}
break;
case 9:
strcat(cc,a8);
system(cc);
col++;
if(col>=7)
{
col=1;
}
break;
}
}}
void pr()
{
system("cls");
for(int h=0;h<19;h++)
{
for(int l=0;l<19;l++)
{
if(qiPan[h][l]==8)
{
cout<<"+";
}
else if(qiPanTwo[h][l]==0)
{
cout<<"·";
}
else if(qiPanTwo[h][l]==1)
{
cout<<"○";
}
else if(qiPanTwo[h][l]==2)
{
cout<<"●";
}
}
cout<<endl;
}
}
int main()
{
pr();
FILE * fp;
FILE * set;
fp = fopen ("record.txt", "w+");
int player=2; //定义一个玩家,当1时为白方,2时为黑方
system("title 请黑棋子");
system("color f0");
int tot=0,toa=0,tob=0;
MessageBox(NULL,"华夏旧戏:五子棋","迎",MB_ICONINFORMATION);
MessageBox(NULL,"一.汝勿在棋上置棋\n二.wasd制移,按j键子","法",MB_ICONINFORMATION);
//................................棋盘1..............................................
//1.声明一个19行19列的数组来存储棋盘 1
for(int h=0;h<19;h++) //对数组进行遍历,所有元素进行初始化(赋值),默认为0值
{cml();
for(int l=0;l<19;l++)
{cml();
qiPan[h][l]=0; //对当前元素进行初始化(赋值),默认为0值
}
}
//...............................棋盘2................................................
//声明一个19行19列的数组来存储棋盘 2
for(int h=0;h<19;h++) //对数组进行遍历,所有元素进行初始化(赋值),默认为0值
{cml();
for(int l=0;l<19;l++)
{cml();
qiPanTwo[h][l]=0; //对当前元素进行初始化(赋值),默认为0值
}
}
//...............................光标声明.............................................
int X=9; //2.声明并初始化光标的横纵坐标
int Y=9;
qiPan[9][9]=8;
//..............................显示...................................................
for(int h=0;h<19;h++)
{
for(int l=0;l<19;l++)
{
if(qiPan[h][l]==8)
{
cout<<" ";
}
else if(qiPanTwo[h][l]==0)
{
cout<<" ·";
}
else if(qiPanTwo[h][l]==1)
{
cout<<" ○";
}
else if(qiPanTwo[h][l]==2)
{
cout<<" ●";
}
}
cout<<endl;cml();
}
pr();
//..............................输入控制...............................................
while(true)
{
cml();
a:
if(_kbhit())
{
string cmd;
char xx=getch();cml(); //控制台从键盘获得一个字符
switch(xx) //控制
{
cml();
case 'c':
system("title Commend Mode");
cin>>cmd;
if(cmd=="read")
{
char cm[100];
cin>>cm;
set=fopen(cm,"r");
cout<<cm<<" ";
rewind(set);
char nowin,y[100];
do
{
if( feof(set) )
{
cout<<"end";
break ;
}
// fgets(y,100,set);
// if(strcmp(y,"not have map")!=NULL)
// {
// goto as;
// }
fgets(y,100,set);
cout<<"&"<<y<<"&"<<endl;
if(strstr(y,"not have map")) goto aaa;
for(int i=0;i<=19;i++)
{
for(int j=0;j<=19;j++)
{
nowin=fgetc(set);
cout<<nowin<<" ";
if(nowin!='8'&&(nowin=='1'||nowin=='2'||nowin=='0'))
{
qiPanTwo[i][j]=(int)nowin-48;
}
if(nowin=='8')
{
qiPan[i][j]=8;
}
}
}
aaa:
for(int i=0;i<19;i++)
{
for(int j=0;j<19;j++)
{
cout<<qiPanTwo[i][j]<<" ";
}
cout<<'\n';
}
as:
while(1)
{
if( feof(set) )
{
cout<<"end";
break ;
}
fgets(y,100,set);
cout<<"&"<<y<<"&"<<endl;
if(strstr(y,"off")) fclose(fp);
if(strstr(y,"on"))
{
FILE * fp;
fp = fopen ("record.txt", "w+");cml();system("title 等待用户操作");
break;
}
if(strstr(y,"set"))
{
if(strstr(y,"gamerule"))
{
if(strstr(y,"black"))
{
if(strstr(y,"true"))
{
bcan=1;
}
if(strstr(y,"false"))
{
bcan=0;
}
}
if(strstr(y,"white"))
{
if(strstr(y,"true"))
{
wcan=1;
}
if(strstr(y,"false"))
{
wcan=0;
}
}
}
}
if(strstr(y,"change"))
{
if(strstr(y,"black"))
{
player=2;
}
if(strstr(y,"white"))
{
player=1;
}
}
if(strstr(y,"put"))
{
if(strstr(y,"black"))
{
int x=(int)y[20]-97,yy=(int)y[22]-97;
cout<<"*"<<y[20]<<"|"<<y[22]<<"*";
cout<<x<<" "<<yy<<"|";
qiPanTwo[x][yy]=2;
pr();
}
if(strstr(y,"white"))
{
int x=(int)y[20]-97,yy=(int)y[22]-97;
cout<<"*"<<y[20]<<"|"<<y[22]<<"*";
qiPanTwo[x][yy]=1;
pr();
}
if(strstr(y,"none"))
{
int x=(int)y[20]-97,yy=(int)y[22]-97;
cout<<"*"<<y[20]<<"|"<<y[22]<<"*";
qiPanTwo[x][yy]=0;
pr();
}
}
if(strstr(y,"win"))
{
if(strstr(y,"black"))
{
goto n;cml();
}
if(strstr(y,"white"))
{
goto m;cml();
}
}
if(strstr(y,"remove"))
{
for(int i=0;i<=19;i++)
{
for(int j=0;j<=19;j++)
{
if(qiPanTwo[i][j]==2)
{
qiPanTwo[i][j]=1;
}
if(qiPanTwo[i][j]==1)
{
qiPanTwo[i][j]=2;
}
}
}
pr();
}
if(strstr(y,"become"))
{
int x=(int)y[17]-48,yy=(int)y[22]-48;//17 22
cout<<x<<" "<<yy<<endl;
for(int i=0;i<=19;i++)
{
for(int j=0;j<=19;j++)
{
if(qiPanTwo[i][j]==x)
{
qiPanTwo[i][j]=yy;
}
}
}
pr();
}
if(strstr(y,"clean"))
{
if(strstr(y,"black"))
{
for(int i=0;i<=19;i++)
{
for(int j=0;j<=19;j++)
{
if(qiPanTwo[i][j]==2)
{
qiPanTwo[i][j]=0;
}
}
}
}
if(strstr(y,"white"))
{
for(int i=0;i<=19;i++)
{
for(int j=0;j<=19;j++)
{
if(qiPanTwo[i][j]==1)
{
qiPanTwo[i][j]=0;
}
}
}
}
pr();
}
}
}while(1);
system("pause");
fclose(set);
}
if(cmd=="win_b")
{system("title 等待用户操作");
goto n;cml();
break;
}
if(cmd=="win_w")
{system("title 等待用户操作");
goto m;cml();
break;
}
if(cmd=="down_at_this_b")
{system("title 等待用户操作");
goto ac;cml();
break;
}
if(cmd=="down_at_this_w")
{system("title 等待用户操作");
goto ab;cml();
break;
}
if(cmd=="game_title")
{
cout<<'\n';
char ls[50];cml();
cin>>ls;cml();
char lls[100]={'t','i','t','l','e',' '};cml();
strcat(lls,ls);cml();
system(lls);cml();
ifd=1;
break;
}
if(cmd=="off_record")
{
fclose(fp);cml();system("title 等待用户操作");
break;
}
if(cmd=="on_record")
{
FILE * fp;
fp = fopen ("record.txt", "w+");cml();system("title 等待用户操作");
break;
}
if(cmd=="dj")
{
cci=1;cml();system("title 等待用户操作");
break;
}
system("title 等待用户操作");
break;
cml();
case 'w': //上
if(Y==0)
{
cml();
MessageBox(NULL,"汝不可越界","不讲武德!",MB_ICONEXCLAMATION);
goto a;
}
Y=Y-1; cml(); //使光标纵坐标-1
qiPan[Y][X]=8; //讲光标写入棋盘
qiPan[Y+1][X]=0; //使原本位置的值还原
break;
case 's': //下
if(Y==18)
{
cml();
MessageBox(NULL,"汝不可越界","不讲武德!",MB_ICONEXCLAMATION);
goto a;
}
Y=Y+1;cml();
qiPan[Y][X]=8;
qiPan[Y-1][X]=0;
break;
case 'a': //左
if(X==0)
{
cml();
MessageBox(NULL,"汝不可越界","不讲武德!",MB_ICONEXCLAMATION);
goto a;
}
X=X-1;cml();
qiPan[Y][X]=8;
qiPan[Y][X+1]=0;
break;
case 'd': //右
if(X==18)
{
cml();
MessageBox(NULL,"汝不可越界","不讲武德!",MB_ICONEXCLAMATION);
goto a;
}
X=X+1;cml();
qiPan[Y][X]=8;
qiPan[Y][X-1]=0;
break;
case 'j': //落子
if(player==1) //如果当前玩家为白色方时
{cml();
if(qiPanTwo[Y][X]!=0&&wcan==0)
{
cml();
MessageBox(NULL,"汝不可在棋上置棋","不讲武德!",MB_ICONEXCLAMATION);
fprintf(fp,"白棋不讲武德!\n");
player=1;
toa++;
goto a;
}
cml();
ab:
fprintf(fp,"%c %d %d \n",'w',Y,X);
qiPanTwo[Y][X]=1; //通过棋盘1中的光标位置更改棋盘2中对应元素的值为1,也就是白棋落子
player=2; //白棋落子后更换玩家
if(ifd==0)
{
system("title 请黑棋子");
}
tot++; cml();
}
else if(player==2)
{
if(qiPanTwo[Y][X]!=0&&bcan==0)
{
cml();
MessageBox(NULL,"汝不可在棋上置棋","不讲武德!",MB_ICONEXCLAMATION);
fprintf(fp,"黑棋不讲武德!\n");
player=2;
tob++;
goto a;
}
ac:
cml();
fprintf(fp,"%c %d %d \n",'b',Y,X);
qiPanTwo[Y][X]=2;
player=1;
if(ifd==0)
{
system("title 请白棋子");
}
tot++;
}
break;
}
pr();
}
//................................清屏操作................................................
//...............................打印.....................................................
shu=1,heng=1,pie=1,na=1;cml();
//.........................游戏规则..............................................
for(int i=1;i<=4;i++) //上
{cml();
if(qiPanTwo[Y+i][X]==qiPanTwo[Y][X]&&qiPanTwo[Y][X]!=0)
shu++;
else
break;
}cml();
for(int i=1;i<=4;i++) //下
{cml();
if(qiPanTwo[Y-i][X]==qiPanTwo[Y][X]&&qiPanTwo[Y][X]!=0)
shu++;
else
break;
}cml();
for(int i=1;i<=4;i++) //左
{cml();
if(qiPanTwo[Y][X-i]==qiPanTwo[Y][X]&&qiPanTwo[Y][X]!=0)
heng++;
else
break;
}
for(int i=1;i<=4;i++) //右
{cml();
if(qiPanTwo[Y][X+i]==qiPanTwo[Y][X]&&qiPanTwo[Y][X]!=0)
heng++;
else
break;
}cml();
for(int i=1;i<=4;i++) //左下
{cml();
if(qiPanTwo[Y+i][X-i]==qiPanTwo[Y][X]&&qiPanTwo[Y][X]!=0)
pie++;
else
break;
}cml();
for(int i=1;i<=4;i++) //右上
{cml();
if(qiPanTwo[Y-i][X+i]==qiPanTwo[Y][X]&&qiPanTwo[Y][X]!=0)
pie++;
else
break;
}cml();
for(int i=1;i<=4;i++) //右下
{cml();
if(qiPanTwo[Y+i][X+i]==qiPanTwo[Y][X]&&qiPanTwo[Y][X]!=0)
na++;
else
break;
}
for(int i=1;i<=4;i++) //左上
{cml();
if(qiPanTwo[Y-i][X-i]==qiPanTwo[Y][X]&&qiPanTwo[Y][X]!=0)
na++;
else
break;
}
if(shu==5||heng==5||pie==5||na==5)
{cml();
if(qiPanTwo[Y][X]==1)
{cml();
m:
cout<<"白子胜!";
MessageBox(NULL,"请查看Record.txt","终",MB_ICONEXCLAMATION);
fprintf(fp,"白子胜!");
system("pause");
break;
}
else
{
n: cml();
cout<<"黑子胜!";
MessageBox(NULL,"请查看Record.txt","终",MB_ICONEXCLAMATION);
fprintf(fp,"黑子胜!");
system("pause");
break;
}
}
cml();
}
fclose(fp);
return 0;
}
标签:case,cml,cc,system,break,Game,转载,col
From: https://www.cnblogs.com/haozexu/p/17488427.html