1. 基于人工智能搜索算法用C++编写的五子棋程序 并附上算法流程
你说一下你的邮箱,我可以发一个给你,我去年暑假弄过一个。
已经发了你的邮箱了
2. C++做一个简单的五子棋
你可以去我的博客上看看,有一个类似的游戏,借鉴一下。我在帮你修改一下代码,改好了发上。
现在把代码给你,代码如下:
#include
#include
#include
#include
#include
#define N 14
#define Pi 3.1415926 //圆周率π的值
#define M 1521106
#define U 72
#define D 80
#define L 75
#define R 77
#include
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>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;
}
3. 课程设计用C++做一个五子棋游戏,要有设计思路,所参考文献
邮箱发过来,发到你邮箱!
4. 怎么用c++ 编个人机对战的五子棋程序,
如果你写过了象棋,那你写五子棋就套用五子棋的算法就行了。像你那样,几百个文件,说出具体过程是不可能的,
5. 如何用C++编写五子棋?
6. 高手求助!用c/c++编写五子棋程序需要哪些知识储备?!
学一下MFC就行了,五子棋没你想的那么复杂
《深入浅出MFC》候捷译
补充,认真学,没什么难的,刚开始你会不太适应,可以看一下孙鑫老师的视频。http://www.sunxin.org/video/vc.asp有在线视频。他讲的你多看两遍就明白MFC是怎么回事了,然后再看一些如《深入浅出MFC》之类的书,基本就OK了,要知道编程中最简单的是界面,难的是算法。
7. 怎么用c++编写一个五子棋的游戏?
我这里提供一个详细教程,因为你只要求人与人对战,所以最后的人工智能部分不用看了。另外你自己改改这个程序。
8. 用C++编写五子棋要用到哪些算法
我只给你判断输赢的算法,其他的你自己解决//全局变量 int curX,curY;//当前下棋的坐标 0 = 5 ||SameLineNum(1,0) >= 5 ||SameLineNum(1,1) >= 5 ||SameLineNum(-1,1) >= 5) { printf("%s 方获胜\n",collor); } }//判断当前棋子的某个方向上同色棋子有多少 int SameLineNum(int x,int y) { int tX,tY; int num;//同一线上相同颜色棋子数 tX = curX;tY = curY; do//计算落子一边同颜色的棋子数 (比如左边) { if( onTable(tX,tY) onTable(curX,curY) { num = Max(Abs(curX - tX),Abs(curY - tY);//Max求最大值,Abs求求绝对值 break; } tX = tX + x;tY = tY + y; } while (x 15 || j > 15); do//计算落子一边同颜色的棋子数 (比如右边) { if( onTable(tX,tY) onTable(curX,curY) { num = num -1 + Max(Abs(curX - tX),Abs(curY - tY);//Max求最大值,Abs求求绝对值 break; } tX = tX - x;tY = tY - y; } while (x 15 || j > 15); return num; }