C++做一个简单的五子棋

你可以去我的博客上看看,有一个类似的游戏,借鉴一下。我在帮你修改一下代码,改好了发上。

现在把代码给你,代码如下:

#include

#include

#include

#include

#include

#defineN14

#definePi3.1415926//圆周率π的值

#defineM1521106

#defineU72

#defineD80

#defineL75

#defineR77

#include

charmm[20]=\0,pw[20]=\0;//密码

charzi,name[50],name2[50]={\0},named[50]={\0};

inttm,v=0,slct=0;

unsignedlongtimed;

intmap[N+1][N+1];

inti,j,x=0,y=0;

intstep=0,con=0;

FILE*fp;

clock_tstart,end;

usingnamespacestd;

voidstore(void);

intfind(inta)//反向解密阶段

{

doublen,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;

}

voiddec()//解密

{

FILE*p,*q;intn;

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);

elsebreak;

}

for(i=0;i<20;i++)

{

fscanf(p,%d,&tm);

if(tm!=M)

name2[i]=find(tm);

elsebreak;

}

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);

{

chardel[50];

sprintf(del,del%s,name2);

system(del);

}

}

voiddraw(void)//绘制

{

intcon=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:case1:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)

,FOREGROUND_INTENSITY|FOREGROUND_RED);printf(★);continue;//红色

L2:case2:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)

,FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN);printf(★);continue;//黄色

default:if(slct-1)gotoL2;elsegotoL1;

}

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)

,FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);

if(!(i%2)&&!(j%2))

switch(map[i/2][j/2])

{

case0:gotolab;

case1:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE)

,FOREGROUND_INTENSITY|FOREGROUND_RED);printf(●);continue;//红色

case2: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(┏);

elseif(j==2*N)

printf(┓);

else

printf(┳);

}

elseif(i==2*N)

{

if(j==0)

printf(┗);

elseif(j==2*N)

printf(┛);

else

printf(┻);

}

else

{

if(j==0)

printf(┣);

elseif(j==2*N)

printf(┫);

else

printf(╋);

}

}

elseif(!(i%2))

printf(━);

elseif((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;

}

voidinitialize(void)//初始化

{

for(i=0;i<=N;i++)

for(j=0;j<=N;j++)

map[i][j]=0;

}

intcheck(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])

{

gotolab2;

}

elseif(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])

{

gotolab2;

}

elseif(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])

{

gotolab2;

}

elseif(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])

{

gotolab2;

}

}

return1;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);}

return0;

}

intenc()//加密

{

FILE*p,*q;intm,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);

gotore;

}

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);

{

chardel[50];

sprintf(del,del%s,name2);

system(del);

}

return0;

}

voidstore(void)

{

FILE*p,*q;

intm,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);

{

chardel[50];

sprintf(del,del%s,name2);

system(del);

cout<<Stored!<<endl;

exit(0);

}

}

voidStart(void)

{

dec();

}

voidRestart(void)

{

enc();initialize();

}

intcontrol(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();return2;

case'a':

case'A':gotol;

case's':

case'S':gotod;

case'd':

case'D':gotor;

case'w':

case'W':gotou;

case'q':

case'Q':end=clock();timed+=end-start;store();

}

switch(getch())

{

l:caseL:system(cls);x-=2;x%=(N*2+2);if(x<0)x+=N*2+2;break;

d:caseD:system(cls);y+=2;y%=(N*2+2);break;

r:caseR:system(cls);x+=2;x%=(N*2+2);break;

u:caseU:system(cls);y-=2;y%=(N*2+2);if(y<0)y+=N*2+2;break;

}

check();

draw();

return0;

}

intmain()

{

start=clock();

cout<<按N创建新游戏,按E继续上次的游戏。<<endl;

re1:

charc=getch();

if(c=='e'||c=='E')

Start();

elseif(c=='n'||c=='N')

Restart();

elsegotore1;

system(cls);

draw();

while(1)control();

return0;

}

免责声明:本站发布的游戏攻略(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场。
如果本文侵犯了您的权益,请联系站长邮箱进行举报反馈,一经查实,我们将在第一时间处理,感谢您对本站的关注!