C++做一个简单的五子棋
目前正在学习C++属于刚刚上手,看书的那种有一个小阶段的目标就是做一个五子棋但是我不晓得从哪里下手?该看那些书?从哪看起?那些是着重要理解的?希望有经验的给点建议帮助这个...
目前正在学习C++ 属于刚刚上手,看书的那种
有一个小阶段的目标就是做一个五子棋
但是我不晓得从哪里下手?该看那些书?从哪看起?那些是着重要理解的?
希望有经验的给点建议 帮助
这个五子棋不用电脑 就只有人下 就是说一手黑棋一手白棋
能够判断获胜的条件;有图形的界面,就是Windows窗体程序; 展开
有一个小阶段的目标就是做一个五子棋
但是我不晓得从哪里下手?该看那些书?从哪看起?那些是着重要理解的?
希望有经验的给点建议 帮助
这个五子棋不用电脑 就只有人下 就是说一手黑棋一手白棋
能够判断获胜的条件;有图形的界面,就是Windows窗体程序; 展开
3个回答
展开全部
你可以去我的博客上看看,有一个类似的游戏,借鉴一下。我在帮你修改一下代码,改好了发上。
现在把代码给你,代码如下:
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <ctime>
#include <cmath>
#define N 14
#define Pi 3.1415926 //圆周率π的值
#define M 1521106
#define U 72
#define D 80
#define L 75
#define R 77
#include <stdio.h>
char mm[20]="\0",pw[20]="\0";//密码
char zi,name[50],name2[50]={"\0"},named[50]={"\0"};
int tm,v=0,slct=0;
unsigned long timed;
int map[N+1][N+1];
int i,j,x=0,y=0;
int step=0,con=0;
FILE *fp;
clock_t start,end;
using namespace std;
void store(void);
int find(int a)//反向解密阶段
{
double n,s,x,z;
do{ s=pow(n,3),s-=3*n,s+=5-a;
x=n*n-1,x*=3,z=s/x,n-=z,z*=100;
}while(fabs(z)>=Pi);
return (int)n;
}
void dec()//解密
{
FILE *p,*q;int n;
system("cls");
cout<<"\n\n\n\n"<<endl;
cout<<"\t\t*****************************************"<<endl;
cout<<"\t\t|----------------开始-------------------|"<<endl;
cout<<"\t\t*****************************************"<<endl;
cout<<"\t\t请输入要用户名称: ";
scanf("%s",&name);strcpy(named,name);system("cls");
sprintf(name,"%s.lyf",name);
cout<<"\n\n\n\n"<<endl;
cout<<"\t\t*****************************************"<<endl;
cout<<"\t\t|----------------开始-------------------|"<<endl;
cout<<"\t\t*****************************************"<<endl;
cout<<"\t\t请输入密码: ";
cin>>pw;
if((p=fopen(name,"r"))==NULL)
{
system("cls");
cout<<"\n\n\n\n\n\t\t该用户不存在!"<<endl;
getch();exit(0);
}
fscanf(p,"%d",&tm);
for(i=0;i<20;i++)
{
fscanf(p,"%d",&tm);
if(tm!=M)
mm[i]=find(tm);
else break;
}
for(i=0;i<20;i++)
{
fscanf(p,"%d",&tm);
if(tm!=M)
name2[i]=find(tm);
else break;
}
for(i=0;i<=3;)
{
if(strcmp(pw,mm)!=0)
{
if(i==2)
{
system("cls");
cout<<"\n\n\n\n\n\t\t密码错误,无法进入游戏!\n\n\t\t";
system("pause");getch();exit(0);
}
system("cls");
cout<<"\n\n\n\n"<<endl;
cout<<"\t\t*****************************************"<<endl;
cout<<"\t\t|----------------开始-------------------|"<<endl;
cout<<"\t\t*****************************************"<<endl;
cout<<"\t\t|--------------密码错误!---------------|"<<endl;
cout<<"\t\t*****************************************"<<endl;
cout<<"\t\t请第"<<++i+1<<"次输入密码: ";cin>>pw;
}
else
break;
}
system("cls");
if((q=fopen(name2,"w"))==NULL)
{
system("cls");
cout<<"\n\n\n\n\n\t\t文件打开错误!"<<endl;
getch();exit(0);
}
while(!feof(p))
{
fscanf(p,"%d",&n);
zi=find(n);fputc(zi,q);
}
fclose(p);
fclose(q);
if((q=fopen(name2,"r"))==NULL)
{
system("cls");
cout<<"\n\n\n\n\n\t\t文件打开错误!"<<endl;
getch();exit(0);
}
fscanf(q,"%d%lf%d%d",&step,&timed,&x,&y);
for(i=0;i<=N;i++)
for(j=0;j<=N;j++)
fscanf(q,"%d",&map[i][j]);
fclose(q);
{
char del[50];
sprintf(del,"del %s",name2);
system(del);
}
}
void draw(void)//绘制
{
int con=0;system("cls");
for(i=0;i<2*N+1;i++)
{
for(j=0;j<N*2+1;j++)
{
if(i==y&&j==x)
switch(map[i/2][j/2])
{
L1:case 1:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)
,FOREGROUND_INTENSITY | FOREGROUND_RED);printf("★");continue;//红色
L2:case 2:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)
,FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);printf("★");continue;//黄色
default:if(slct-1)goto L2;else goto L1;
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)
,FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
if(!(i%2) && !(j%2))
switch(map[i/2][j/2])
{
case 0:goto lab;
case 1:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)
,FOREGROUND_INTENSITY | FOREGROUND_RED);printf("●");continue;//红色
case 2:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)
,FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);printf("●");continue;//黄色
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)
,FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
lab:if(!(i%2) && !(j%2))
{
if(i==0)
{
if(j==0)
printf("┏");
else if(j==2*N)
printf("┓");
else
printf("┳");
}
else if(i==2*N)
{
if(j==0)
printf("┗");
else if(j==2*N)
printf("┛");
else
printf("┻");
}
else
{
if(j==0)
printf("┣");
else if(j==2*N)
printf("┫");
else
printf("╋");
}
}
else if(!(i%2))
printf("━");
else if((i%2)&&!(j%2))
printf("┃");
else
printf(" ");
}
cout<<"\n";
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)
,FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
end=clock();
cout<<"━━━━━━━━━━━━━━━━━━\t\t用户名:"<<named<<endl;
printf(" time ┃%6.1lfs┃ step ┃Sp:%d\n",(double)(end-start)/1000,step);
cout<<"━━━━━━━━━━━━━━━━━━\t\t(按Q退出)\t"<<endl;
printf(" turn ┃ %2s ┃position┃(%d,%d)\n",(slct-1)? "黄" :"红" ,x,y);
cout<<"━━━━━━━━━━━━━━━━━━"<<endl;
}
void initialize(void)//初始化
{
for(i=0;i<=N;i++)
for(j=0;j<=N;j++)
map[i][j]=0;
}
int check(void)//监测
{
for(i=0;i<=N;i++)
for(j=0;j<=N;j++)
{
if(map[i][j]==map[i][j+1]
&& map[i][j]==map[i][j+2]
&& map[i][j]==map[i][j+3]
&& map[i][j]==map[i][j+4]
&& j<=N-4
&& map[i][j])
{
goto lab2;
}
else if(map[i][j]==map[i+1][j]
&& map[i][j]==map[i+2][j]
&& map[i][j]==map[i+3][j]
&& map[i][j]==map[i+4][j]
&& i+4<=N
&& map[i][j])
{
goto lab2;
}
else if(map[i][j]==map[i+1][j+1]
&& map[i][j]==map[i+2][j+2]
&& map[i][j]==map[i+3][j+3]
&& map[i][j]==map[i+4][j+4]
&& i+4<=N
&& j+4<=N
&& map[i][j])
{
goto lab2;
}
else if(map[i][j]==map[i-1][j+1]
&& map[i][j]==map[i-2][j+2]
&& map[i][j]==map[i-3][j+3]
&& map[i][j]==map[i-4][j+4]
&& i-4<=N
&& j+4<=N
&& map[i][j])
{
goto lab2;
}
}
return 1;lab2:
system("cls");
if(map[i][j]-1)
{cout<<"\n\n\n\t\t\t黄子赢!\n"<<endl;exit(0);}
else
{cout<<"\n\n\n\t\t\t红子赢!\n"<<endl;exit(0);}
return 0;
}
int enc()//加密
{
FILE *p,*q;int m,n;re:
system("cls");
cout<<"\n\n\n\n"<<endl;
cout<<"\t\t*****************************************"<<endl;
cout<<"\t\t|----------------新建-------------------|"<<endl;
cout<<"\t\t*****************************************"<<endl;
cout<<"\t\t请输入用户名称: ";
cin>>name2;
strcpy(named,name2);
sprintf(name,"%s.lyf",name2);
sprintf(name2,"%s.tmp",name2);
if(q=fopen(name,"r"))
{
cout<<"\n\n\n\n\n\t\t用户名已存在,请更换用户名:"<<endl;
system("pause");
goto re;
}
system("cls");
cout<<"\n\n\n\n"<<endl;
cout<<"\t\t*****************************************"<<endl;
cout<<"\t\t|----------------新建-------------------|"<<endl;
cout<<"\t\t*****************************************"<<endl;
cout<<"\t\t请输入密码: ";
cin>>mm;
system("cls");
if((q=fopen(name2,"w"))==NULL)
{
cout<<"\n\n\n\n\n\t\t文件打开错误!"<<endl;
exit(0);
}
fprintf(q,"%d %d %d %d",step,timed,x,y);
fclose(q);
if((q=fopen(name2,"r"))==NULL)
{
cout<<"\n\n\n\n\n\t\t文件打开错误!"<<endl;
exit(0);
}
if((p=fopen(name,"w"))==NULL)
{
cout<<"\n\n\n\n\n\t\t文件打开错误!"<<endl;
exit(0);
}
srand(M*time(0));
fprintf(p,"%d ",1534980+rand()%M);
for(i=0;i<20;i++)
{
tm=mm[i];
n=-3,n+=tm*tm;
if(tm==0)
{fprintf(p,"%d ",M);break;}
else
fprintf(p,"%d ",(n+5)*tm);
}
for(i=0;i<50;i++)
{
tm=name2[i];
n=-3,n+=tm*tm;
if(tm==0)
{fprintf(p,"%d ",M);break;}
else
fprintf(p,"%d ",(n+5)*tm);
}
getchar();
system("cls");
while(!feof(q))
{
m=zi=fgetc(q);
n=-3,n+=m*m;
fprintf(p,"%d ",(n+5)*m);//加密阶段
}
fclose(q);
fclose(p);
{
char del[50];
sprintf(del,"del %s",name2);
system(del);
}
return 0;
}
void store(void)
{
FILE *p,*q;
int m,n;
system("cls");
if((q=fopen(name2,"w"))==NULL)
{
cout<<"\n\n\n\n\n\t\t文件打开错误!"<<endl;
exit(0);
}
fprintf(q,"%d %lf %d %d",step,timed,x,y);
for(i=0;i<=N;i++)
for(j=0;j<=N;j++)
fprintf(q,"%d ",map[i][j]);
fclose(q);
if((q=fopen(name2,"r"))==NULL)
{
cout<<"\n\n\n\n\n\t\t文件打开错误!"<<endl;
exit(0);
}
if((p=fopen(name,"w"))==NULL)
{
cout<<"\n\n\n\n\n\t\t文件打开错误!"<<endl;
exit(0);
}
srand(M*time(0));
fprintf(p,"%d ",1534980+rand()%M);
for(i=0;i<20;i++)
{
tm=mm[i];
n=-3,n+=tm*tm;
if(tm==0)
{fprintf(p,"%d ",M);break;}
else
fprintf(p,"%d ",(n+5)*tm);
}
for(i=0;i<50;i++)
{
tm=name2[i];
n=-3,n+=tm*tm;
if(tm==0)
{fprintf(p,"%d ",M);break;}
else
fprintf(p,"%d ",(n+5)*tm);
}
system("cls");
while(!feof(q))
{
m=zi=fgetc(q);
n=-3,n+=m*m;
fprintf(p,"%d ",(n+5)*m);//加密阶段
}
fclose(q);
fclose(p);
{
char del[50];
sprintf(del,"del %s",name2);
system(del);
cout<<"Stored!"<<endl;
exit(0);
}
}
void Start(void)
{
dec();
}
void Restart(void)
{
enc();initialize();
}
int control(void)//控制
{
switch(getch())
{
case '\15':
case ' ':++slct%=2;step++;if(!map[y/2][x/2]){map[y/2][x/2]=slct+1;con=0;}
else {++slct%=2;step--;con=1;printf("\a当前位置已经有棋子!!");}check();draw();return 2;
case 'a':
case 'A':goto l;
case 's':
case 'S':goto d;
case 'd':
case 'D':goto r;
case 'w':
case 'W':goto u;
case 'q':
case 'Q':end=clock();timed+=end-start;store();
}
switch(getch())
{
l:case L:system("cls");x-=2;x%=(N*2+2);if(x<0)x+=N*2+2;break;
d:case D:system("cls");y+=2;y%=(N*2+2);break;
r:case R:system("cls");x+=2;x%=(N*2+2);break;
u:case U:system("cls");y-=2;y%=(N*2+2);if(y<0)y+=N*2+2;break;
}
check();
draw();
return 0;
}
int main()
{
start=clock();
cout<<"按N创建新游戏,按E继续上次的游戏。"<<endl;
re1:
char c=getch();
if(c=='e'||c=='E')
Start();
else if(c=='n'||c=='N')
Restart();
else goto re1;
system("cls");
draw();
while(1)control();
return 0;
}
现在把代码给你,代码如下:
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <ctime>
#include <cmath>
#define N 14
#define Pi 3.1415926 //圆周率π的值
#define M 1521106
#define U 72
#define D 80
#define L 75
#define R 77
#include <stdio.h>
char mm[20]="\0",pw[20]="\0";//密码
char zi,name[50],name2[50]={"\0"},named[50]={"\0"};
int tm,v=0,slct=0;
unsigned long timed;
int map[N+1][N+1];
int i,j,x=0,y=0;
int step=0,con=0;
FILE *fp;
clock_t start,end;
using namespace std;
void store(void);
int find(int a)//反向解密阶段
{
double n,s,x,z;
do{ s=pow(n,3),s-=3*n,s+=5-a;
x=n*n-1,x*=3,z=s/x,n-=z,z*=100;
}while(fabs(z)>=Pi);
return (int)n;
}
void dec()//解密
{
FILE *p,*q;int n;
system("cls");
cout<<"\n\n\n\n"<<endl;
cout<<"\t\t*****************************************"<<endl;
cout<<"\t\t|----------------开始-------------------|"<<endl;
cout<<"\t\t*****************************************"<<endl;
cout<<"\t\t请输入要用户名称: ";
scanf("%s",&name);strcpy(named,name);system("cls");
sprintf(name,"%s.lyf",name);
cout<<"\n\n\n\n"<<endl;
cout<<"\t\t*****************************************"<<endl;
cout<<"\t\t|----------------开始-------------------|"<<endl;
cout<<"\t\t*****************************************"<<endl;
cout<<"\t\t请输入密码: ";
cin>>pw;
if((p=fopen(name,"r"))==NULL)
{
system("cls");
cout<<"\n\n\n\n\n\t\t该用户不存在!"<<endl;
getch();exit(0);
}
fscanf(p,"%d",&tm);
for(i=0;i<20;i++)
{
fscanf(p,"%d",&tm);
if(tm!=M)
mm[i]=find(tm);
else break;
}
for(i=0;i<20;i++)
{
fscanf(p,"%d",&tm);
if(tm!=M)
name2[i]=find(tm);
else break;
}
for(i=0;i<=3;)
{
if(strcmp(pw,mm)!=0)
{
if(i==2)
{
system("cls");
cout<<"\n\n\n\n\n\t\t密码错误,无法进入游戏!\n\n\t\t";
system("pause");getch();exit(0);
}
system("cls");
cout<<"\n\n\n\n"<<endl;
cout<<"\t\t*****************************************"<<endl;
cout<<"\t\t|----------------开始-------------------|"<<endl;
cout<<"\t\t*****************************************"<<endl;
cout<<"\t\t|--------------密码错误!---------------|"<<endl;
cout<<"\t\t*****************************************"<<endl;
cout<<"\t\t请第"<<++i+1<<"次输入密码: ";cin>>pw;
}
else
break;
}
system("cls");
if((q=fopen(name2,"w"))==NULL)
{
system("cls");
cout<<"\n\n\n\n\n\t\t文件打开错误!"<<endl;
getch();exit(0);
}
while(!feof(p))
{
fscanf(p,"%d",&n);
zi=find(n);fputc(zi,q);
}
fclose(p);
fclose(q);
if((q=fopen(name2,"r"))==NULL)
{
system("cls");
cout<<"\n\n\n\n\n\t\t文件打开错误!"<<endl;
getch();exit(0);
}
fscanf(q,"%d%lf%d%d",&step,&timed,&x,&y);
for(i=0;i<=N;i++)
for(j=0;j<=N;j++)
fscanf(q,"%d",&map[i][j]);
fclose(q);
{
char del[50];
sprintf(del,"del %s",name2);
system(del);
}
}
void draw(void)//绘制
{
int con=0;system("cls");
for(i=0;i<2*N+1;i++)
{
for(j=0;j<N*2+1;j++)
{
if(i==y&&j==x)
switch(map[i/2][j/2])
{
L1:case 1:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)
,FOREGROUND_INTENSITY | FOREGROUND_RED);printf("★");continue;//红色
L2:case 2:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)
,FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);printf("★");continue;//黄色
default:if(slct-1)goto L2;else goto L1;
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)
,FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
if(!(i%2) && !(j%2))
switch(map[i/2][j/2])
{
case 0:goto lab;
case 1:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)
,FOREGROUND_INTENSITY | FOREGROUND_RED);printf("●");continue;//红色
case 2:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)
,FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);printf("●");continue;//黄色
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)
,FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
lab:if(!(i%2) && !(j%2))
{
if(i==0)
{
if(j==0)
printf("┏");
else if(j==2*N)
printf("┓");
else
printf("┳");
}
else if(i==2*N)
{
if(j==0)
printf("┗");
else if(j==2*N)
printf("┛");
else
printf("┻");
}
else
{
if(j==0)
printf("┣");
else if(j==2*N)
printf("┫");
else
printf("╋");
}
}
else if(!(i%2))
printf("━");
else if((i%2)&&!(j%2))
printf("┃");
else
printf(" ");
}
cout<<"\n";
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)
,FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
end=clock();
cout<<"━━━━━━━━━━━━━━━━━━\t\t用户名:"<<named<<endl;
printf(" time ┃%6.1lfs┃ step ┃Sp:%d\n",(double)(end-start)/1000,step);
cout<<"━━━━━━━━━━━━━━━━━━\t\t(按Q退出)\t"<<endl;
printf(" turn ┃ %2s ┃position┃(%d,%d)\n",(slct-1)? "黄" :"红" ,x,y);
cout<<"━━━━━━━━━━━━━━━━━━"<<endl;
}
void initialize(void)//初始化
{
for(i=0;i<=N;i++)
for(j=0;j<=N;j++)
map[i][j]=0;
}
int check(void)//监测
{
for(i=0;i<=N;i++)
for(j=0;j<=N;j++)
{
if(map[i][j]==map[i][j+1]
&& map[i][j]==map[i][j+2]
&& map[i][j]==map[i][j+3]
&& map[i][j]==map[i][j+4]
&& j<=N-4
&& map[i][j])
{
goto lab2;
}
else if(map[i][j]==map[i+1][j]
&& map[i][j]==map[i+2][j]
&& map[i][j]==map[i+3][j]
&& map[i][j]==map[i+4][j]
&& i+4<=N
&& map[i][j])
{
goto lab2;
}
else if(map[i][j]==map[i+1][j+1]
&& map[i][j]==map[i+2][j+2]
&& map[i][j]==map[i+3][j+3]
&& map[i][j]==map[i+4][j+4]
&& i+4<=N
&& j+4<=N
&& map[i][j])
{
goto lab2;
}
else if(map[i][j]==map[i-1][j+1]
&& map[i][j]==map[i-2][j+2]
&& map[i][j]==map[i-3][j+3]
&& map[i][j]==map[i-4][j+4]
&& i-4<=N
&& j+4<=N
&& map[i][j])
{
goto lab2;
}
}
return 1;lab2:
system("cls");
if(map[i][j]-1)
{cout<<"\n\n\n\t\t\t黄子赢!\n"<<endl;exit(0);}
else
{cout<<"\n\n\n\t\t\t红子赢!\n"<<endl;exit(0);}
return 0;
}
int enc()//加密
{
FILE *p,*q;int m,n;re:
system("cls");
cout<<"\n\n\n\n"<<endl;
cout<<"\t\t*****************************************"<<endl;
cout<<"\t\t|----------------新建-------------------|"<<endl;
cout<<"\t\t*****************************************"<<endl;
cout<<"\t\t请输入用户名称: ";
cin>>name2;
strcpy(named,name2);
sprintf(name,"%s.lyf",name2);
sprintf(name2,"%s.tmp",name2);
if(q=fopen(name,"r"))
{
cout<<"\n\n\n\n\n\t\t用户名已存在,请更换用户名:"<<endl;
system("pause");
goto re;
}
system("cls");
cout<<"\n\n\n\n"<<endl;
cout<<"\t\t*****************************************"<<endl;
cout<<"\t\t|----------------新建-------------------|"<<endl;
cout<<"\t\t*****************************************"<<endl;
cout<<"\t\t请输入密码: ";
cin>>mm;
system("cls");
if((q=fopen(name2,"w"))==NULL)
{
cout<<"\n\n\n\n\n\t\t文件打开错误!"<<endl;
exit(0);
}
fprintf(q,"%d %d %d %d",step,timed,x,y);
fclose(q);
if((q=fopen(name2,"r"))==NULL)
{
cout<<"\n\n\n\n\n\t\t文件打开错误!"<<endl;
exit(0);
}
if((p=fopen(name,"w"))==NULL)
{
cout<<"\n\n\n\n\n\t\t文件打开错误!"<<endl;
exit(0);
}
srand(M*time(0));
fprintf(p,"%d ",1534980+rand()%M);
for(i=0;i<20;i++)
{
tm=mm[i];
n=-3,n+=tm*tm;
if(tm==0)
{fprintf(p,"%d ",M);break;}
else
fprintf(p,"%d ",(n+5)*tm);
}
for(i=0;i<50;i++)
{
tm=name2[i];
n=-3,n+=tm*tm;
if(tm==0)
{fprintf(p,"%d ",M);break;}
else
fprintf(p,"%d ",(n+5)*tm);
}
getchar();
system("cls");
while(!feof(q))
{
m=zi=fgetc(q);
n=-3,n+=m*m;
fprintf(p,"%d ",(n+5)*m);//加密阶段
}
fclose(q);
fclose(p);
{
char del[50];
sprintf(del,"del %s",name2);
system(del);
}
return 0;
}
void store(void)
{
FILE *p,*q;
int m,n;
system("cls");
if((q=fopen(name2,"w"))==NULL)
{
cout<<"\n\n\n\n\n\t\t文件打开错误!"<<endl;
exit(0);
}
fprintf(q,"%d %lf %d %d",step,timed,x,y);
for(i=0;i<=N;i++)
for(j=0;j<=N;j++)
fprintf(q,"%d ",map[i][j]);
fclose(q);
if((q=fopen(name2,"r"))==NULL)
{
cout<<"\n\n\n\n\n\t\t文件打开错误!"<<endl;
exit(0);
}
if((p=fopen(name,"w"))==NULL)
{
cout<<"\n\n\n\n\n\t\t文件打开错误!"<<endl;
exit(0);
}
srand(M*time(0));
fprintf(p,"%d ",1534980+rand()%M);
for(i=0;i<20;i++)
{
tm=mm[i];
n=-3,n+=tm*tm;
if(tm==0)
{fprintf(p,"%d ",M);break;}
else
fprintf(p,"%d ",(n+5)*tm);
}
for(i=0;i<50;i++)
{
tm=name2[i];
n=-3,n+=tm*tm;
if(tm==0)
{fprintf(p,"%d ",M);break;}
else
fprintf(p,"%d ",(n+5)*tm);
}
system("cls");
while(!feof(q))
{
m=zi=fgetc(q);
n=-3,n+=m*m;
fprintf(p,"%d ",(n+5)*m);//加密阶段
}
fclose(q);
fclose(p);
{
char del[50];
sprintf(del,"del %s",name2);
system(del);
cout<<"Stored!"<<endl;
exit(0);
}
}
void Start(void)
{
dec();
}
void Restart(void)
{
enc();initialize();
}
int control(void)//控制
{
switch(getch())
{
case '\15':
case ' ':++slct%=2;step++;if(!map[y/2][x/2]){map[y/2][x/2]=slct+1;con=0;}
else {++slct%=2;step--;con=1;printf("\a当前位置已经有棋子!!");}check();draw();return 2;
case 'a':
case 'A':goto l;
case 's':
case 'S':goto d;
case 'd':
case 'D':goto r;
case 'w':
case 'W':goto u;
case 'q':
case 'Q':end=clock();timed+=end-start;store();
}
switch(getch())
{
l:case L:system("cls");x-=2;x%=(N*2+2);if(x<0)x+=N*2+2;break;
d:case D:system("cls");y+=2;y%=(N*2+2);break;
r:case R:system("cls");x+=2;x%=(N*2+2);break;
u:case U:system("cls");y-=2;y%=(N*2+2);if(y<0)y+=N*2+2;break;
}
check();
draw();
return 0;
}
int main()
{
start=clock();
cout<<"按N创建新游戏,按E继续上次的游戏。"<<endl;
re1:
char c=getch();
if(c=='e'||c=='E')
Start();
else if(c=='n'||c=='N')
Restart();
else goto re1;
system("cls");
draw();
while(1)control();
return 0;
}
2010-04-12
展开全部
玩五子棋有两种方法,一种是人跟人对战,这个很容易
另一种就是人机对战,也就是所谓的AI,人工智能,这个麻烦点
你刚入手C++,看来只能做第一种了
虽然百度可以找到很多有关算法,但是我在这里跟你说说吧
弄一个在dos界面运行的就可以了吧。可视化的现在对你来说难了点
首先要弄一个棋盘。棋盘上面每一个格子可以用二维数组来做
然后轮流下棋,每个人用不同的符号代表棋子
我亲手做了个井字过三关。研究下。扩展一下就可以了。
给我邮箱发给你
另一种就是人机对战,也就是所谓的AI,人工智能,这个麻烦点
你刚入手C++,看来只能做第一种了
虽然百度可以找到很多有关算法,但是我在这里跟你说说吧
弄一个在dos界面运行的就可以了吧。可视化的现在对你来说难了点
首先要弄一个棋盘。棋盘上面每一个格子可以用二维数组来做
然后轮流下棋,每个人用不同的符号代表棋子
我亲手做了个井字过三关。研究下。扩展一下就可以了。
给我邮箱发给你
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如果要做五子棋的话,建议你试着从没有图形界面的做起,文本界面会稍微简单一些。先把轮流下棋、判断胜负等代码写出来,这些代码都不太难,如果你自己想不出来可以在网上找类似的代码。完成之后改成图形界面,可能会麻烦一些,不过有很多关于VC的书都可以给你帮助。这个程序的交互主要有点击按钮、点击棋盘落子、显示棋子、胜负消息。每个方面都可以参考书上或者网上同样功能的程序,这样很快就能完成。
如果你有什么其它问题,可以通过Baidu Hi找我,谢谢。
如果你有什么其它问题,可以通过Baidu Hi找我,谢谢。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询