共有回帖数 0 个
-
*0 is player,1 is computer*/
#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],goal[15][15][2],num[15][15][2][4];
char win=2,a,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(beginx0)
{
beginx=0;
beginy=ay+ax;
}
if(beginy14)
{
beginx=ax+ay-14;
beginy=14;
}
endx=ax+4;endy=ay-4;
if(endx14)
{
endx=14;
endy=ax+ay-14;
}
if(endy0)
{
endx=ax+ay;
endy=0;
}
endx=endx-4;endy=endy+4;
for(r=beginx;rendx+1;r++)
{
if(num[r][beginy+beginx-r][am][0]!=6)
{
num[r][beginy+beginx-r][an][0]=6;
for(t=0;t5;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;t5;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;t5;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(beginx0)
{beginx=0;}
endx=ax+4;endy=ay;
if(endx14)
{endx=14;}
endx=endx-4;
for(r=beginx;rendx+1;r++)
{
if(num[r][ay][am][1]!=6)
{
num[r][ay][an][1]=6;
for(t=0;t5;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;t5;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;t5;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(beginx0)
{
beginy=beginy-beginx;
beginx=0;
}
if(beginy0)
{
beginx=beginx-beginy;
beginy=0;
}
endx=ax+4;endy=ay+4;
if(endx14)
{ endy=endy-endx+14;
endx=14;
}
if(endy14)
{
endx=endx-endy+14;
endy=14;
}
endx=endx-4;endy=endy-4;
if(beginx=endx)
{
for(r=beginx;rendx+1;r++)
{
if(num[r][beginy-beginx+r][am][2]!=6)
{
num[r][beginy-beginx+r][an][2]=6;
for(t=0;t5;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;t5;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;t5;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(beginy0)
{beginy=0;}
endy=ay+4;endx=ax;
if(endy14)
{endy=14;}
endy=endy-4;
for(r=beginy;rendy+1;r++)
{
if(num[ax][r][am][3]!=6)
{
num[ax][r][an][3]=6;
for(t=0;t5;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;t5;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;t5;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,j;
a=0;b=0;maxgoal=-1;
for(i=0;i15;i++)
{
for(j=0;j15;j++)
{
if(state[j]==2)
{
if(goal[j][m]maxgoal)
{
a=i;b=j;maxgoal=goal[j][m];
}
else
if(goal[j][m]==maxgoal)
{
if(goal[j][n]goal[a][n])
{
a=i;b=j;maxgoal=goal[j][m];
}
else
if(goal[j][n]==goal[a][n])
{
if(random(2))
{
a=i;b=j;maxgoal=goal[j][m];
}
}
}
}
}
}
}
void menu()
{
setcolor(RED);
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,"MADE BY lilong");
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,"version:1.0");
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;r15;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);
}
main()
{
int gd=DETECT,gm,turn,oa,ob,omax;
char ch,i1,i2,i3,i4;
registerbgidriver(EGAVGA_driver);
initgraph(&gd,&gm,"");
begin:
for(i1=0;i115;i1++)
for(i2=0;i215;i2++)
{
state[i1][i2]=2;
for(i3=0;i32;i3++)
{
goal[i1][i2][i3]=0;
for(i4=0;i44;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]=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&&s225)
{
if(turn==1)
{
fun(1);
findmax(0,1);
omax=maxgoal;oa=a;ob=b;
findmax(1,0);
if(omaxmaxgoal)
{a=oa;b=ob;}
else if(omax==maxgoal)
{
if(goal[oa][ob][1]goal[a][0])
{a=oa;b=ob;}
else if(goal[oa][ob][1]==goal[a][0])
{
if(random(2))
{a=oa;b=ob;}
}
}
ai(a,b,1,0);
state[a]=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(omaxmaxgoal)
{a=oa;b=ob;}
else if(omax==maxgoal)
{
if(goal[oa][ob][0]goal[a][1])
{a=oa;b=ob;}
else if(goal[oa][ob][0]==goal[a][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(b0)b=14;
x=124+28*a;y=44+28*b;
curr(WHITE);
}
else if(ch==down)
{
curr(BLACK);
b++;
if(b14)b=0;
x=124+28*a;y=44+28*b;
curr(WHITE);
}
else if(ch==left)
{
curr(BLACK);
a--;
if(a0)a=14;
x=124+28*a;y=44+28*b;
curr(WHITE);
}
else if(ch==right)
{
curr(BLACK);
a++;
if(a14)a=0;
x=124+28*a;y=44+28*b;
curr(WHITE);
}
ch=getch();
}
}
if(state[a]==2)
{
curr(BLACK);
ai(a,b,0,1);
state[a]=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;
}
楼主 2016-06-09 16:47 回复
Copyright © 2010~2015 直线网 版权所有,All Rights Reserved.沪ICP备10039589号
意见反馈 |
关于直线 |
版权声明 |
会员须知