一个简易的游戏程序
2010-12-31 09:04
495 查看
#include"stdio.h"
#include"stdlib.h"
#include"conio.h"
#include"graphics.h"
/*自定义方向键与功能键*/
#define up 72
#define down 80
#define left 75
#define right 77
/*帮助热键*/
#define enter 13
#define esc 27
#define h_key 104
#define y_key 121
int quan[5][2]={0,0,1,2,7,9,35,40,125,180};
int state[15][15];/*棋盘大小*/
int goal[15][15][2];
int num[15][15][2][4];
char win=2;
char a;
char b;
int maxgoal,x,y,s=0;
void ai(char ax,char ay,char am,char an)
{
char r,t;
int beginx,beginy,endx,endy;
/*0方向判断*/
if((ax+ay)>=4&&(ax+ay)<=24)
{
beginx=ax-4;beginy=ay+4;
if(beginx<0)
{
beginx=0;
beginy=ay+ax;
}
if(beginy>14)
{
beginx=ax+ay-14;
beginy=14;
}
endx=ax+4;endy=ay-4;
if(endx>14)
{
endx=14;
endy=ax+ay-14;
}
if(endy<0)
{
endx=ax+ay;
endy=0;
}
endx=endx-4;
endy=endy+4;
for(r=beginx;r<endx+1;r++)
{
if(num[r][beginy+beginx-r][am][0]!=6)
{
num[r][beginy+beginx-r][an][0]=6;
for(t=0;t<5;t++)
{
goal[r+t][beginy+beginx-r-t][am]=goal[r+t][beginy+beginx-r-t][am]-quan[num[r][beginy+beginx-r][am][0]][am];
}
num[r][beginy+beginx-r][am][0]++;
if(num[r][beginy+beginx-r][am][0]==5)
{
win=am;
}
for(t=0;t<5;t++)
{
goal[r+t][beginy+beginx-r-t][am]=goal[r+t][beginy+beginx-r-t][am]+quan[num[r][beginy+beginx-r][am][0]][am];
}
}
else if(num[r][beginy+beginx-r][an][0]!=6)
{
for(t=0;t<5;t++)
{
goal[r+t][beginy+beginx-r-t][an]=goal[r+t][beginy+beginx-r-t][an]-quan[num[r][beginy+beginx-r][an][0]][an];
}
num[r][beginy+beginx-r][am][0]=6;
num[r][beginy+beginx-r][an][0]=6;
}
}
}
/*1方向判断*/
{
beginx=ax-4;beginy=ay;
if(beginx<0)
{
beginx=0;
}
endx=ax+4;endy=ay;
if(endx>14)
{
endx=14;
}
endx=endx-4;
for(r=beginx;r<endx+1;r++)
{
if(num[r][ay][am][1]!=6)
{
num[r][ay][an][1]=6;
for(t=0;t<5;t++)
{
goal[r+t][ay][am]=goal[r+t][ay][am]-quan[num[r][ay][am][1]][am];
}
num[r][ay][am][1]++;
if(num[r][ay][am][1]==5)
{
win=am;
}
for(t=0;t<5;t++)
{
goal[r+t][ay][am]=goal[r+t][ay][am]+quan[num[r][ay][am][1]][am];
}
}
else if(num[r][ay][an][1]!=6)
{
for(t=0;t<5;t++)
{
goal[r+t][ay][an]=goal[r+t][ay][an]-quan[num[r][ay][an][1]][an];
}
num[r][ay][am][1]=6;
num[r][ay][an][1]=6;
}
}
}
/*2方向判断*/
beginx=ax-4;beginy=ay-4;
if(beginx<0)
{
beginy=beginy-beginx;
beginx=0;
}
if(beginy<0)
{
beginx=beginx-beginy;
beginy=0;
}
endx=ax+4;endy=ay+4;
if(endx>14)
{
endy=endy-endx+14;
endx=14;
}
if(endy>14)
{
endx=endx-endy+14;
endy=14;
}
endx=endx-4;endy=endy-4;
if(beginx<=endx)
{
for(r=beginx;r<endx+1;r++)
{
if(num[r][beginy-beginx+r][am][2]!=6)
{
num[r][beginy-beginx+r][an][2]=6;
for(t=0;t<5;t++)
{
goal[r+t][beginy-beginx+r+t][am]=goal[r+t][beginy-beginx+r+t][am]-quan[num[r][beginy-beginx+r][am][2]][am];
}
num[r][beginy-beginx+r][am][2]++;
if(num[r][beginy-beginx+r][am][2]==5)
{
win=am;
}
for(t=0;t<5;t++)
{
goal[r+t][beginy-beginx+r+t][am]=goal[r+t][beginy-beginx+r+t][am]+quan[num[r][beginy-beginx+r][am][2]][am];
}
}
else if(num[r][beginy-beginx+r][an][2]!=6)
{
for(t=0;t<5;t++)
{
goal[r+t][beginy-beginx+r+t][an]=goal[r+t][beginy-beginx+r+t][an]-quan[num[r][beginy-beginx+r][an][2]][an];
}
num[r][beginy-beginx+r][am][2]=6;
num[r][beginy-beginx+r][an][2]=6;
}
}
}
/*3方向判断*/
{
beginy=ay-4;
beginx=ax;
if(beginy<0)
{
beginy=0;
}
endy=ay+4;
endx=ax;
if(endy>14)
{
endy=14;
}
endy=endy-4;
for(r=beginy;r<endy+1;r++)
{
if(num[ax][r][am][3]!=6)
{
num[ax][r][an][3]=6;
for(t=0;t<5;t++)
{
goal[ax][r+t][am]=goal[ax][r+t][am]-quan[num[ax][r][am][3]][am];
}
num[ax][r][am][3]++;
if(num[ax][r][am][3]==5)
{
win=am;
}
for(t=0;t<5;t++)
{
goal[ax][r+t][am]=goal[ax][r+t][am]+quan[num[ax][r][am][3]][am];
}
}
else if(num[ax][r][an][3]!=6)
{
for(t=0;t<5;t++)
{
goal[ax][r+t][an]=goal[ax][r+t][an]-quan[num[ax][r][an][3]][an];
}
num[ax][r][am][3]=6;
num[ax][r][an][3]=6;
}
}
}
}
void findmax(char m,char n)
{
char i;
char j;
a=0;
b=0;
maxgoal=-1;
for(i=0;i<15;i++)
{
for(j=0;j<15;j++)
{
if(state[i][j]==2)
{
if(goal[i][j][m]>maxgoal)
{
a=i;
b=j;
maxgoal=goal[i][j][m];
}
else if(goal[i][j][m]==maxgoal)
{
if(goal[i][j]
>goal[a][b]
)
{
a=i;
b=j;
maxgoal=goal[i][j][m];
}
else if(goal[i][j]
==goal[a][b]
)
{
if(random(2))
{
a=i;
b=j;
maxgoal=goal[i][j][m];
}
}
}
}
}
}
}
setcolor(RED);
/*menu 位置设置*/
void menu()
{
settextstyle(0,0,3);
outtextxy(150,150,"Player first");
outtextxy(150,180,"Computer first");
outtextxy(150,210,"Exit");
}
/*显示设置*/
int show(int y)
{
int oy=0;
char ch=0;
cleardevice();
setbkcolor(BLACK);
setcolor(BLUE);
settextstyle(0,0,4);
outtextxy(200,40,"GOBANG");
settextstyle(0,0,2);
outtextxy(350,90,"welcome to play");
setcolor(WHITE);
settextstyle(0,0,2);
outtextxy(40,280,"operate handbook:");
outtextxy(100,310,"Up,Down,Left,Right key to move");
outtextxy(100,340,"Enter key for sure");
outtextxy(100,370,"H key for help");
outtextxy(100,400,"Esc key to exit");
outtextxy(440,430,"this part made ");
while(ch!=enter)
{
if((ch==down||ch==right)&&y!=210) /*移动*/
y+=30;
else if((ch==up||ch==left)&&y!=150)
y-=30;
if(oy!=y)
{
setfillstyle(0,BLACK);
bar(140,oy-2,490,oy+24);
setfillstyle(1,BLUE);
bar(140,y-2,490,y+24);
menu();
oy=y;
}
ch=getch();
}
return y;
}
/*棋局走法与步骤*/
void step(int step)
{
char s[4];
sprintf(s,"%d",step);
setcolor(BLACK);
setfillstyle(1,BLACK);
bar(22,239,100,269);
setcolor(BLUE);
settextstyle(0,0,3);
outtextxy(10,210,"STEP");
outtextxy(23,240,s);
}
void fun(int t)
{
setcolor(BLACK);
setfillstyle(1,BLACK);
bar(539,100,639,300);
setcolor(BLUE);
settextstyle(0,0,3);
if(t==1)
{
outtextxy(540,210," ");
outtextxy(540,240," ");
}
else if(t==0)
{
outtextxy(540,195,"Your");
outtextxy(540,225,"turn");
setcolor(RED);
setfillstyle(1,RED);
pieslice(590,270,0,360,11);
}
else if(t==2)
{
outtextxy(540,210,"Exit");
outtextxy(540,240,"y/n ");
}
else if(t==3)
{
setcolor(RED);
outtextxy(540,210,"You ");
outtextxy(540,240,"win ");
}
else if(t==4)
{
setcolor(RED);
outtextxy(540,210,"You ");
outtextxy(540,240,"lose");
}
else if(t==5)
{
setcolor(RED);
outtextxy(540,210,"GAME");
outtextxy(540,240,"OVER");
}
}
void pan()
{
int r;
cleardevice();
setcolor(WHITE);
for(r=1;r<15;r++)
{
rectangle(124,44,124+r*28,44+r*28);
rectangle(516,436,516-r*28,436-r*28);
}
rectangle(106,26,534,454);
rectangle(105,25,535,455);
rectangle(107,27,533,453);
}
void curr(int co)
{
setcolor(co);
line(x-15,y-10,x-10,y-10);
line(x-10,y-10,x-10,y-15);
line(x+10,y-15,x+10,y-10);
line(x+10,y-10,x+15,y-10);
line(x-15,y+10,x-10,y+10);
line(x-10,y+10,x-10,y+15);
line(x+10,y+10,x+10,y+15);
line(x+10,y+10,x+15,y+10);
}
orgn();
srms()//双人模式
{
printf("OK");
int gd=DETECT;
int gm;
int turn;
int oa;
int ob;
int omax;
char ch;
char i1;
char i2;
char i3;
char i4;
/*registerbgidriver(EG***GA_driver);*/
initgraph(&gd,&gm,"");
begin:
for(i1=0;i1<15;i1++)/*画图*/
for(i2=0;i2<15;i2++)
{
state[i1][i2]=2;
for(i3=0;i3<2;i3++)
{
goal[i1][i2][i3]=0;
for(i4=0;i4<4;i4++)
num[i1][i2][i3][i4]=0;
}
}
win=2;s=0;
turn=(show(150)-150)/30;
if(turn==2)
exit(0);
else
{
pan();
if(turn==1)
{
randomize();
a=random(7)+4;
randomize();
b=random(7)+4;
ai(a,b,1,0);
state[a][b]=turn;
s++;
step(s);
turn=0;
x=124+28*a;y=44+28*b;
setcolor(GREEN);
setfillstyle(1,GREEN);
pieslice(x,y,0,360,11);
curr(WHITE);
}
else
{
a=7;
b=7;
x=124+28*a;
y=44+28*b;
curr(WHITE);
step(0);
}
}
while(win==2&&s<225)
{
if(turn==1)
{
fun(1);
findmax(0,1);
omax=maxgoal;oa=a;ob=b;
findmax(1,0);
if(omax>maxgoal)
{
a=oa;
b=ob;
}
else if(omax==maxgoal)
{
if(goal[oa][ob][1]>goal[a][b][0])
{
a=oa;
b=ob;
}
else if(goal[oa][ob][1]==goal[a][b][0])
{
if(random(2))
{
a=oa;
b=ob;
}
}
}
ai(a,b,1,0);
state[a][b]=turn;
s++;
step(s);
turn=0;
x=124+28*a;
y=44+28*b;
setcolor(GREEN);
setfillstyle(1,GREEN);
pieslice(x,y,0,360,11);
curr(WHITE);
}
else
{
fun(0);
ch=getch();
while(ch!=enter)
{
if(ch==esc)
{
fun(2);
if(getch()==y_key)
goto begin;
fun(0);
}
else if(ch==h_key)
{
curr(BLACK);
findmax(1,0);
omax=maxgoal;
oa=a;
ob=b;
findmax(0,1);
x=a;
y=b;
if(omax>maxgoal)
{
a=oa;
b=ob;
}
else if(omax==maxgoal)
{
if(goal[oa][ob][0]>goal[a][b][1])
{
a=oa;
b=ob;
}
else if(goal[oa][ob][0]==goal[a][b][1])
{
if(random(2))
{
a=oa;
b=ob;
}
}
}
x=124+28*a;
y=44+28*b;
curr(WHITE);
}
else if(ch==up)
{
curr(BLACK);
b--;
if(b<0)
b=14;
x=124+28*a;
y=44+28*b;
curr(WHITE);
}
else if(ch==down)
{
curr(BLACK);
b++;
if(b>14)
b=0;
x=124+28*a;
y=44+28*b;
curr(WHITE);
}
else if(ch==left)
{
curr(BLACK);
a--;
if(a<0)
a=14;
x=124+28*a;
y=44+28*b;
curr(WHITE);
}
else if(ch==right)
{
curr(BLACK);
a++;
if(a>14)
a=0;
x=124+28*a;
y=44+28*b;
curr(WHITE);
}
ch=getch();
}
}
if(state[a][b]==2)
{
curr(BLACK);
ai(a,b,0,1);
state[a][b]=turn;
x=124+28*a;
y=44+28*b;
turn=1;
s++;
step(s);
setcolor(RED);
setfillstyle(1,RED);
pieslice(x,y,0,360,11);
}
}
curr(WHITE);
fun(win+3);
getch();
goto begin;
}
}
drms()int gd=DETECT;
int gm;
int turn;
int oa;
int ob;
int omax;
char ch;
char i1;
char i2;
char i3;
char i4;
/*registerbgidriver(EG***GA_driver);*/
initgraph(&gd,&gm,"");
begin:
for(i1=0;i1<15;i1++)/*画图*/
for(i2=0;i2<15;i2++)
{
state[i1][i2]=2;
for(i3=0;i3<2;i3++)
{
goal[i1][i2][i3]=0;
for(i4=0;i4<4;i4++)
num[i1][i2][i3][i4]=0;
}
}
win=2;s=0;
turn=(show(150)-150)/30;
if(turn==2)
exit(0);
else
{
pan();
if(turn==1)
{
randomize();
a=random(7)+4;
randomize();
b=random(7)+4;
ai(a,b,1,0);
state[a][b]=turn;
s++;
step(s);
turn=0;
x=124+28*a;y=44+28*b;
setcolor(GREEN);
setfillstyle(1,GREEN);
pieslice(x,y,0,360,11);
curr(WHITE);
}
else
{
a=7;
b=7;
x=124+28*a;
y=44+28*b;
curr(WHITE);
step(0);
}
}
while(win==2&&s<225)
{
if(turn==1)
{
fun(1);
findmax(0,1);
omax=maxgoal;oa=a;ob=b;
findmax(1,0);
if(omax>maxgoal)
{
a=oa;
b=ob;
}
else if(omax==maxgoal)
{
if(goal[oa][ob][1]>goal[a][b][0])
{
a=oa;
b=ob;
}
else if(goal[oa][ob][1]==goal[a][b][0])
{
if(random(2))
{
a=oa;
b=ob;
}
}
}
ai(a,b,1,0);
state[a][b]=turn;
s++;
step(s);
turn=0;
x=124+28*a;
y=44+28*b;
setcolor(GREEN);
setfillstyle(1,GREEN);
pieslice(x,y,0,360,11);
curr(WHITE);
}
else
{
fun(0);
ch=getch();
while(ch!=enter)
{
if(ch==esc)
{
fun(2);
if(getch()==y_key)
goto begin;
fun(0);
}
else if(ch==h_key)
{
curr(BLACK);
findmax(1,0);
omax=maxgoal;
oa=a;
ob=b;
findmax(0,1);
x=a;
y=b;
if(omax>maxgoal)
{
a=oa;
b=ob;
}
else if(omax==maxgoal)
{
if(goal[oa][ob][0]>goal[a][b][1])
{
a=oa;
b=ob;
}
else if(goal[oa][ob][0]==goal[a][b][1])
{
if(random(2))
{
a=oa;
b=ob;
}
}
}
x=124+28*a;
y=44+28*b;
curr(WHITE);
}
else if(ch==up)
{
curr(BLACK);
b--;
if(b<0)
b=14;
x=124+28*a;
y=44+28*b;
curr(WHITE);
}
else if(ch==down)
{
curr(BLACK);
b++;
if(b>14)
b=0;
x=124+28*a;
y=44+28*b;
curr(WHITE);
}
else if(ch==left)
{
curr(BLACK);
a--;
if(a<0)
a=14;
x=124+28*a;
y=44+28*b;
curr(WHITE);
}
else if(ch==right)
{
curr(BLACK);
a++;
if(a>14)
a=0;
x=124+28*a;
y=44+28*b;
curr(WHITE);
}
ch=getch();
}
}
if(state[a][b]==2)
{
curr(BLACK);
ai(a,b,0,1);
state[a][b]=turn;
x=124+28*a;
y=44+28*b;
turn=1;
s++;
step(s);
setcolor(RED);
setfillstyle(1,RED);
pieslice(x,y,0,360,11);
}
}
curr(WHITE);
fun(win+3);
getch();
goto begin;
}/*单人模式*/
{
printf("OK");
}
xzms()//选择模式
{
char ch;
system("color E");
puts("/n /n");
puts("/t ▲△▲△▲△▲△▲△▲△▲△▲");
puts("/t *****************************");
puts("/t ********输入1 or 2:**********");
puts("/t *****************************");
printf("/t 1.双人模式 ");
printf("/n");
printf("/t 2.单人模式 /n");
printf("/t 3.返回上一级 /n");
printf("请做出选择:");
scanf("%c",&ch);
switch(ch)
{
case '1':
srms();
break;
case '2':
drms();
break;
case '3':
orgn();
break;
default:
scanf("%c",&ch);
break;
}
}
}
main()
{ char data[2];
printf("please input password ");
gets(data);
if(strcmp("123",data)==0)
{ xzms();
}
else
printf("the password is fault");
}
#include"stdlib.h"
#include"conio.h"
#include"graphics.h"
/*自定义方向键与功能键*/
#define up 72
#define down 80
#define left 75
#define right 77
/*帮助热键*/
#define enter 13
#define esc 27
#define h_key 104
#define y_key 121
int quan[5][2]={0,0,1,2,7,9,35,40,125,180};
int state[15][15];/*棋盘大小*/
int goal[15][15][2];
int num[15][15][2][4];
char win=2;
char a;
char b;
int maxgoal,x,y,s=0;
void ai(char ax,char ay,char am,char an)
{
char r,t;
int beginx,beginy,endx,endy;
/*0方向判断*/
if((ax+ay)>=4&&(ax+ay)<=24)
{
beginx=ax-4;beginy=ay+4;
if(beginx<0)
{
beginx=0;
beginy=ay+ax;
}
if(beginy>14)
{
beginx=ax+ay-14;
beginy=14;
}
endx=ax+4;endy=ay-4;
if(endx>14)
{
endx=14;
endy=ax+ay-14;
}
if(endy<0)
{
endx=ax+ay;
endy=0;
}
endx=endx-4;
endy=endy+4;
for(r=beginx;r<endx+1;r++)
{
if(num[r][beginy+beginx-r][am][0]!=6)
{
num[r][beginy+beginx-r][an][0]=6;
for(t=0;t<5;t++)
{
goal[r+t][beginy+beginx-r-t][am]=goal[r+t][beginy+beginx-r-t][am]-quan[num[r][beginy+beginx-r][am][0]][am];
}
num[r][beginy+beginx-r][am][0]++;
if(num[r][beginy+beginx-r][am][0]==5)
{
win=am;
}
for(t=0;t<5;t++)
{
goal[r+t][beginy+beginx-r-t][am]=goal[r+t][beginy+beginx-r-t][am]+quan[num[r][beginy+beginx-r][am][0]][am];
}
}
else if(num[r][beginy+beginx-r][an][0]!=6)
{
for(t=0;t<5;t++)
{
goal[r+t][beginy+beginx-r-t][an]=goal[r+t][beginy+beginx-r-t][an]-quan[num[r][beginy+beginx-r][an][0]][an];
}
num[r][beginy+beginx-r][am][0]=6;
num[r][beginy+beginx-r][an][0]=6;
}
}
}
/*1方向判断*/
{
beginx=ax-4;beginy=ay;
if(beginx<0)
{
beginx=0;
}
endx=ax+4;endy=ay;
if(endx>14)
{
endx=14;
}
endx=endx-4;
for(r=beginx;r<endx+1;r++)
{
if(num[r][ay][am][1]!=6)
{
num[r][ay][an][1]=6;
for(t=0;t<5;t++)
{
goal[r+t][ay][am]=goal[r+t][ay][am]-quan[num[r][ay][am][1]][am];
}
num[r][ay][am][1]++;
if(num[r][ay][am][1]==5)
{
win=am;
}
for(t=0;t<5;t++)
{
goal[r+t][ay][am]=goal[r+t][ay][am]+quan[num[r][ay][am][1]][am];
}
}
else if(num[r][ay][an][1]!=6)
{
for(t=0;t<5;t++)
{
goal[r+t][ay][an]=goal[r+t][ay][an]-quan[num[r][ay][an][1]][an];
}
num[r][ay][am][1]=6;
num[r][ay][an][1]=6;
}
}
}
/*2方向判断*/
beginx=ax-4;beginy=ay-4;
if(beginx<0)
{
beginy=beginy-beginx;
beginx=0;
}
if(beginy<0)
{
beginx=beginx-beginy;
beginy=0;
}
endx=ax+4;endy=ay+4;
if(endx>14)
{
endy=endy-endx+14;
endx=14;
}
if(endy>14)
{
endx=endx-endy+14;
endy=14;
}
endx=endx-4;endy=endy-4;
if(beginx<=endx)
{
for(r=beginx;r<endx+1;r++)
{
if(num[r][beginy-beginx+r][am][2]!=6)
{
num[r][beginy-beginx+r][an][2]=6;
for(t=0;t<5;t++)
{
goal[r+t][beginy-beginx+r+t][am]=goal[r+t][beginy-beginx+r+t][am]-quan[num[r][beginy-beginx+r][am][2]][am];
}
num[r][beginy-beginx+r][am][2]++;
if(num[r][beginy-beginx+r][am][2]==5)
{
win=am;
}
for(t=0;t<5;t++)
{
goal[r+t][beginy-beginx+r+t][am]=goal[r+t][beginy-beginx+r+t][am]+quan[num[r][beginy-beginx+r][am][2]][am];
}
}
else if(num[r][beginy-beginx+r][an][2]!=6)
{
for(t=0;t<5;t++)
{
goal[r+t][beginy-beginx+r+t][an]=goal[r+t][beginy-beginx+r+t][an]-quan[num[r][beginy-beginx+r][an][2]][an];
}
num[r][beginy-beginx+r][am][2]=6;
num[r][beginy-beginx+r][an][2]=6;
}
}
}
/*3方向判断*/
{
beginy=ay-4;
beginx=ax;
if(beginy<0)
{
beginy=0;
}
endy=ay+4;
endx=ax;
if(endy>14)
{
endy=14;
}
endy=endy-4;
for(r=beginy;r<endy+1;r++)
{
if(num[ax][r][am][3]!=6)
{
num[ax][r][an][3]=6;
for(t=0;t<5;t++)
{
goal[ax][r+t][am]=goal[ax][r+t][am]-quan[num[ax][r][am][3]][am];
}
num[ax][r][am][3]++;
if(num[ax][r][am][3]==5)
{
win=am;
}
for(t=0;t<5;t++)
{
goal[ax][r+t][am]=goal[ax][r+t][am]+quan[num[ax][r][am][3]][am];
}
}
else if(num[ax][r][an][3]!=6)
{
for(t=0;t<5;t++)
{
goal[ax][r+t][an]=goal[ax][r+t][an]-quan[num[ax][r][an][3]][an];
}
num[ax][r][am][3]=6;
num[ax][r][an][3]=6;
}
}
}
}
void findmax(char m,char n)
{
char i;
char j;
a=0;
b=0;
maxgoal=-1;
for(i=0;i<15;i++)
{
for(j=0;j<15;j++)
{
if(state[i][j]==2)
{
if(goal[i][j][m]>maxgoal)
{
a=i;
b=j;
maxgoal=goal[i][j][m];
}
else if(goal[i][j][m]==maxgoal)
{
if(goal[i][j]
>goal[a][b]
)
{
a=i;
b=j;
maxgoal=goal[i][j][m];
}
else if(goal[i][j]
==goal[a][b]
)
{
if(random(2))
{
a=i;
b=j;
maxgoal=goal[i][j][m];
}
}
}
}
}
}
}
setcolor(RED);
/*menu 位置设置*/
void menu()
{
settextstyle(0,0,3);
outtextxy(150,150,"Player first");
outtextxy(150,180,"Computer first");
outtextxy(150,210,"Exit");
}
/*显示设置*/
int show(int y)
{
int oy=0;
char ch=0;
cleardevice();
setbkcolor(BLACK);
setcolor(BLUE);
settextstyle(0,0,4);
outtextxy(200,40,"GOBANG");
settextstyle(0,0,2);
outtextxy(350,90,"welcome to play");
setcolor(WHITE);
settextstyle(0,0,2);
outtextxy(40,280,"operate handbook:");
outtextxy(100,310,"Up,Down,Left,Right key to move");
outtextxy(100,340,"Enter key for sure");
outtextxy(100,370,"H key for help");
outtextxy(100,400,"Esc key to exit");
outtextxy(440,430,"this part made ");
while(ch!=enter)
{
if((ch==down||ch==right)&&y!=210) /*移动*/
y+=30;
else if((ch==up||ch==left)&&y!=150)
y-=30;
if(oy!=y)
{
setfillstyle(0,BLACK);
bar(140,oy-2,490,oy+24);
setfillstyle(1,BLUE);
bar(140,y-2,490,y+24);
menu();
oy=y;
}
ch=getch();
}
return y;
}
/*棋局走法与步骤*/
void step(int step)
{
char s[4];
sprintf(s,"%d",step);
setcolor(BLACK);
setfillstyle(1,BLACK);
bar(22,239,100,269);
setcolor(BLUE);
settextstyle(0,0,3);
outtextxy(10,210,"STEP");
outtextxy(23,240,s);
}
void fun(int t)
{
setcolor(BLACK);
setfillstyle(1,BLACK);
bar(539,100,639,300);
setcolor(BLUE);
settextstyle(0,0,3);
if(t==1)
{
outtextxy(540,210," ");
outtextxy(540,240," ");
}
else if(t==0)
{
outtextxy(540,195,"Your");
outtextxy(540,225,"turn");
setcolor(RED);
setfillstyle(1,RED);
pieslice(590,270,0,360,11);
}
else if(t==2)
{
outtextxy(540,210,"Exit");
outtextxy(540,240,"y/n ");
}
else if(t==3)
{
setcolor(RED);
outtextxy(540,210,"You ");
outtextxy(540,240,"win ");
}
else if(t==4)
{
setcolor(RED);
outtextxy(540,210,"You ");
outtextxy(540,240,"lose");
}
else if(t==5)
{
setcolor(RED);
outtextxy(540,210,"GAME");
outtextxy(540,240,"OVER");
}
}
void pan()
{
int r;
cleardevice();
setcolor(WHITE);
for(r=1;r<15;r++)
{
rectangle(124,44,124+r*28,44+r*28);
rectangle(516,436,516-r*28,436-r*28);
}
rectangle(106,26,534,454);
rectangle(105,25,535,455);
rectangle(107,27,533,453);
}
void curr(int co)
{
setcolor(co);
line(x-15,y-10,x-10,y-10);
line(x-10,y-10,x-10,y-15);
line(x+10,y-15,x+10,y-10);
line(x+10,y-10,x+15,y-10);
line(x-15,y+10,x-10,y+10);
line(x-10,y+10,x-10,y+15);
line(x+10,y+10,x+10,y+15);
line(x+10,y+10,x+15,y+10);
}
orgn();
srms()//双人模式
{
printf("OK");
int gd=DETECT;
int gm;
int turn;
int oa;
int ob;
int omax;
char ch;
char i1;
char i2;
char i3;
char i4;
/*registerbgidriver(EG***GA_driver);*/
initgraph(&gd,&gm,"");
begin:
for(i1=0;i1<15;i1++)/*画图*/
for(i2=0;i2<15;i2++)
{
state[i1][i2]=2;
for(i3=0;i3<2;i3++)
{
goal[i1][i2][i3]=0;
for(i4=0;i4<4;i4++)
num[i1][i2][i3][i4]=0;
}
}
win=2;s=0;
turn=(show(150)-150)/30;
if(turn==2)
exit(0);
else
{
pan();
if(turn==1)
{
randomize();
a=random(7)+4;
randomize();
b=random(7)+4;
ai(a,b,1,0);
state[a][b]=turn;
s++;
step(s);
turn=0;
x=124+28*a;y=44+28*b;
setcolor(GREEN);
setfillstyle(1,GREEN);
pieslice(x,y,0,360,11);
curr(WHITE);
}
else
{
a=7;
b=7;
x=124+28*a;
y=44+28*b;
curr(WHITE);
step(0);
}
}
while(win==2&&s<225)
{
if(turn==1)
{
fun(1);
findmax(0,1);
omax=maxgoal;oa=a;ob=b;
findmax(1,0);
if(omax>maxgoal)
{
a=oa;
b=ob;
}
else if(omax==maxgoal)
{
if(goal[oa][ob][1]>goal[a][b][0])
{
a=oa;
b=ob;
}
else if(goal[oa][ob][1]==goal[a][b][0])
{
if(random(2))
{
a=oa;
b=ob;
}
}
}
ai(a,b,1,0);
state[a][b]=turn;
s++;
step(s);
turn=0;
x=124+28*a;
y=44+28*b;
setcolor(GREEN);
setfillstyle(1,GREEN);
pieslice(x,y,0,360,11);
curr(WHITE);
}
else
{
fun(0);
ch=getch();
while(ch!=enter)
{
if(ch==esc)
{
fun(2);
if(getch()==y_key)
goto begin;
fun(0);
}
else if(ch==h_key)
{
curr(BLACK);
findmax(1,0);
omax=maxgoal;
oa=a;
ob=b;
findmax(0,1);
x=a;
y=b;
if(omax>maxgoal)
{
a=oa;
b=ob;
}
else if(omax==maxgoal)
{
if(goal[oa][ob][0]>goal[a][b][1])
{
a=oa;
b=ob;
}
else if(goal[oa][ob][0]==goal[a][b][1])
{
if(random(2))
{
a=oa;
b=ob;
}
}
}
x=124+28*a;
y=44+28*b;
curr(WHITE);
}
else if(ch==up)
{
curr(BLACK);
b--;
if(b<0)
b=14;
x=124+28*a;
y=44+28*b;
curr(WHITE);
}
else if(ch==down)
{
curr(BLACK);
b++;
if(b>14)
b=0;
x=124+28*a;
y=44+28*b;
curr(WHITE);
}
else if(ch==left)
{
curr(BLACK);
a--;
if(a<0)
a=14;
x=124+28*a;
y=44+28*b;
curr(WHITE);
}
else if(ch==right)
{
curr(BLACK);
a++;
if(a>14)
a=0;
x=124+28*a;
y=44+28*b;
curr(WHITE);
}
ch=getch();
}
}
if(state[a][b]==2)
{
curr(BLACK);
ai(a,b,0,1);
state[a][b]=turn;
x=124+28*a;
y=44+28*b;
turn=1;
s++;
step(s);
setcolor(RED);
setfillstyle(1,RED);
pieslice(x,y,0,360,11);
}
}
curr(WHITE);
fun(win+3);
getch();
goto begin;
}
}
drms()int gd=DETECT;
int gm;
int turn;
int oa;
int ob;
int omax;
char ch;
char i1;
char i2;
char i3;
char i4;
/*registerbgidriver(EG***GA_driver);*/
initgraph(&gd,&gm,"");
begin:
for(i1=0;i1<15;i1++)/*画图*/
for(i2=0;i2<15;i2++)
{
state[i1][i2]=2;
for(i3=0;i3<2;i3++)
{
goal[i1][i2][i3]=0;
for(i4=0;i4<4;i4++)
num[i1][i2][i3][i4]=0;
}
}
win=2;s=0;
turn=(show(150)-150)/30;
if(turn==2)
exit(0);
else
{
pan();
if(turn==1)
{
randomize();
a=random(7)+4;
randomize();
b=random(7)+4;
ai(a,b,1,0);
state[a][b]=turn;
s++;
step(s);
turn=0;
x=124+28*a;y=44+28*b;
setcolor(GREEN);
setfillstyle(1,GREEN);
pieslice(x,y,0,360,11);
curr(WHITE);
}
else
{
a=7;
b=7;
x=124+28*a;
y=44+28*b;
curr(WHITE);
step(0);
}
}
while(win==2&&s<225)
{
if(turn==1)
{
fun(1);
findmax(0,1);
omax=maxgoal;oa=a;ob=b;
findmax(1,0);
if(omax>maxgoal)
{
a=oa;
b=ob;
}
else if(omax==maxgoal)
{
if(goal[oa][ob][1]>goal[a][b][0])
{
a=oa;
b=ob;
}
else if(goal[oa][ob][1]==goal[a][b][0])
{
if(random(2))
{
a=oa;
b=ob;
}
}
}
ai(a,b,1,0);
state[a][b]=turn;
s++;
step(s);
turn=0;
x=124+28*a;
y=44+28*b;
setcolor(GREEN);
setfillstyle(1,GREEN);
pieslice(x,y,0,360,11);
curr(WHITE);
}
else
{
fun(0);
ch=getch();
while(ch!=enter)
{
if(ch==esc)
{
fun(2);
if(getch()==y_key)
goto begin;
fun(0);
}
else if(ch==h_key)
{
curr(BLACK);
findmax(1,0);
omax=maxgoal;
oa=a;
ob=b;
findmax(0,1);
x=a;
y=b;
if(omax>maxgoal)
{
a=oa;
b=ob;
}
else if(omax==maxgoal)
{
if(goal[oa][ob][0]>goal[a][b][1])
{
a=oa;
b=ob;
}
else if(goal[oa][ob][0]==goal[a][b][1])
{
if(random(2))
{
a=oa;
b=ob;
}
}
}
x=124+28*a;
y=44+28*b;
curr(WHITE);
}
else if(ch==up)
{
curr(BLACK);
b--;
if(b<0)
b=14;
x=124+28*a;
y=44+28*b;
curr(WHITE);
}
else if(ch==down)
{
curr(BLACK);
b++;
if(b>14)
b=0;
x=124+28*a;
y=44+28*b;
curr(WHITE);
}
else if(ch==left)
{
curr(BLACK);
a--;
if(a<0)
a=14;
x=124+28*a;
y=44+28*b;
curr(WHITE);
}
else if(ch==right)
{
curr(BLACK);
a++;
if(a>14)
a=0;
x=124+28*a;
y=44+28*b;
curr(WHITE);
}
ch=getch();
}
}
if(state[a][b]==2)
{
curr(BLACK);
ai(a,b,0,1);
state[a][b]=turn;
x=124+28*a;
y=44+28*b;
turn=1;
s++;
step(s);
setcolor(RED);
setfillstyle(1,RED);
pieslice(x,y,0,360,11);
}
}
curr(WHITE);
fun(win+3);
getch();
goto begin;
}/*单人模式*/
{
printf("OK");
}
xzms()//选择模式
{
char ch;
system("color E");
puts("/n /n");
puts("/t ▲△▲△▲△▲△▲△▲△▲△▲");
puts("/t *****************************");
puts("/t ********输入1 or 2:**********");
puts("/t *****************************");
printf("/t 1.双人模式 ");
printf("/n");
printf("/t 2.单人模式 /n");
printf("/t 3.返回上一级 /n");
printf("请做出选择:");
scanf("%c",&ch);
switch(ch)
{
case '1':
srms();
break;
case '2':
drms();
break;
case '3':
orgn();
break;
default:
scanf("%c",&ch);
break;
}
}
}
main()
{ char data[2];
printf("please input password ");
gets(data);
if(strcmp("123",data)==0)
{ xzms();
}
else
printf("the password is fault");
}
相关文章推荐
- 简易恋爱游戏程序 C++
- 一个关于生命游戏的小小程序
- Servlet学习:(一)使用IDEA搭建一个简易的servlet程序
- 使用Java编写一个简易的本地文件的检索相关字的程序
- 一个游戏控制台的基本程序
- 一个简易的聊天程序(Socket)
- simple-todo: 一个简易的 todo 程序 - django版
- Python中使用wxPython开发的一个简易笔记本程序实例
- 一个简易的51单片机串口接收和发送程序
- 和程序有关的一个游戏<<mu complex>> 攻略
- 给学生物的一个同学写的计算原子距离的简易程序
- python的一个简易web程序index.py
- 一个简易的计算器程序
- 用C#做一个简单纸牌游戏的程序
- ASP.NET一个简易的WebServer,用控制台程序模拟IIS 托起web服务
- 编写一个程序求解字谜游戏
- 简易游戏修改器(C#实现,提供程序下载)
- 一个简易的日志程序
- 一个标准数独游戏的题目生成和解题的程序
- ASP.NET一个简易的WebServer,用控制台程序模拟IIS服务器 托起web服务