26 楼
Ghost-mjw [专家分:130] 发布于 2004-02-26 17:32:00
我的!有AI,2001年写,TC3.0使用,同一目录下要有EGVVGA.BGI,我想你也不会去看的,你这样要程序是学不好的,我只是灌灌水罢了!呵呵!
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <bios.h>
#include <dos.h>
#define KUp 0x4800
#define KDown 0x5000
#define KRight 0x4d00
#define KLeft 0x4b00
#define KSpace 0x3920
#define KTab 0xf09
#define KBack 0xe08
#define KEsc 0x11b
#define KF1 0x3B00
#define BLeft 170
#define BTop 30
#define GWidth 20
#define GAmount0 14
#define GAmount1 15
#define GLeft 180
#define GTop 40
#define Player1 0
#define Player2 1
#define OneD 1
#define OneU 2
#define TwoD 5
#define TwoU 8
#define ThreeD 15
#define ThreeU 30
#define FourD 30
#define FourU 180
#define IsFive 625
#define IsOneP 2
#define IsOneE 3
#define IsTwoP 4
#define IsTwoE 6
#define IsThreeP 8
#define IsThreeE 9
#define IsFourP 17
#define IsFourE 18
#define IsFiveP 19
#define AIStrLong 3
struct best_step
{
int value;
char row ;
char col ;
}codex={0,0,0};
int row=0,col=0;
int vscomputer=0,forbid=0,attackpiece=0;
char chessboard[GAmount1][GAmount1]={""};
//struct chessman_types codex;
char *ord[2][20]={/*棋子可能组合*/
{"01000","00010","00100",
"01010","00110","01100",
"011010","010110","01110",
"101112","110112","111012","210111","211011","211101","011112","211110","011110",
"11111",
"111111"},
{"02000","00020","00200",
"02020","02200","00220",
"022020","020220","02220",
"022221","202221","220221","222021","122220","120222","122022","122202","022220",
"22222",
"222222"}
};
void drawsquare(void)
{int i=0;
setcolor(14);
setfillstyle(1,6);
rectangle(BLeft,BTop, BLeft+GAmount1*GWidth, BTop+GAmount1*GWidth);
floodfill(BLeft+1,BTop+1,14);
for (i=0;i<GAmount1;i++)
{line(GLeft,i*GWidth+GTop,GLeft+GAmount0*GWidth,i*GWidth+GTop);
line(GLeft+i*GWidth,GTop,i*GWidth+GLeft,GTop+GAmount0*GWidth);
}
circle(GLeft+3*GWidth,GTop+3*GWidth,2);
circle(GLeft+3*GWidth,GTop+11*GWidth,2);
circle(GLeft+7*GWidth,GTop+7*GWidth,2);
circle(GLeft+11*GWidth,GTop+3*GWidth,2);
circle(GLeft+11*GWidth,GTop+11*GWidth,2);
rectangle(10,30,140,330);
rectangle(500,30,630,330);
rectangle(10,350,630,440);
rectangle(70,450,630,470);
rectangle(30,220,120,240);
rectangle(30,280,120,300);
rectangle(520,220,610,240);
rectangle(520,280,610,300);
// rectangle(530,220,620,240);
// rectangle(530,280,620,300);
setfillstyle(11,2);
floodfill(11,31,14);
floodfill(501,31,14);
outtextxy(20,40,"Player1");
outtextxy(30,80,"Name:");
outtextxy(30,120,"Win:");
outtextxy(30,140,"Level:");
outtextxy(20,200,"Use time:");
outtextxy(20,260,"Count time:");
outtextxy(510,40,"Player2");
outtextxy(530,80,"Name:");
outtextxy(530,120,"Win:");
outtextxy(530,140,"Level:");
outtextxy(510,200,"Use time:");
outtextxy(510,260,"Count time:");
setcolor(15);
outtextxy(50,225,"00:00");
outtextxy(50,285,"00:00");
outtextxy(540,225,"00:00");
outtextxy(540,285,"00:00");
// outextxy(140,370,"Capitulate");
// outextxy(140,370,"Standoff ");
// outextxy(140,370,"Restart ");
// outextxy(140,370,"Capitulate");
setfillstyle(9,1);
// rectangle(0,0,getmaxx(),getmaxy());
floodfill(1,1,14);
outtextxy(240,10,"V2.1 Copyright by Ghost" );
}
void chessimage(void *img[])
{int size=0,color[2]={15,0},radius=9,i=0;
size=imagesize(0,0,20,20);
setcolor(7);
setfillstyle(1, 6);
rectangle(0,0,20,20);
floodfill(10,10,7);
for (i=0;i<=1;i++)
{setcolor(8);
setfillstyle(1, color[i]);
circle(10, 10, radius);
floodfill(10,10,8);
img[i]=malloc(size);
getimage(1,1,19,19,img[i]);
}
cleardevice();
}
void *arrowimage()
{void *img;
setcolor(4);
rectangle(0,0,20,20);
img=malloc(imagesize(0,0,20,20));
getimage(0,0,20,20,img);
cleardevice();
return(img);
}
void screen(void)
{int gdriver = 9, gmode=2, errorcode;
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("\nSet Display Mode error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
}
/*
char *timestr(long ntime )
{static char stime[6];
int i=0;
stime[2]=':';
stime[5]=0;
// time1/=18.2;
i=ntime%60;
stime[3]=i/10+48;
stime[4]=i%10+48;
ntime/=60;
i=ntime%60;
stime[0]=i/10+48;
stime[1]=i%10+48;
return (stime);
}
*/
int instr(char *string1,char *string2)
{ char *flag1,*flag2;
flag1=string1;
flag2=string2;
do
{ flag1=string1++;
flag2=string2;
while (*flag1==*flag2 && *flag1 && *flag2) {flag1++;flag2++;}
if (*flag2==0) return(1);
}
while (*flag1);
return(0);
}
int grant(int appointrow ,int appointcol,int appointplayer)
{
int i=0,j=0;
int minrow=0,maxrow=0,mincol=0,maxcol=0;
int doublethree=0,doublefour=0,longsix=0;
int errcode=1;
int p0=0,p1=0,p2=0,p3=0;
char astr[4][12]={""};
if (chessboard[appointrow][appointcol])
errcode=0;
else
{chessboard[appointrow][appointcol]=appointplayer+1;
minrow=appointrow-5>0?appointrow-5:0;
mincol=appointcol-5>0?appointcol-5:0;
maxrow=appointrow+5<GAmount0?appointrow+5:GAmount0;
maxcol=appointcol+5<GAmount0?appointcol+5:GAmount0;
for(i=minrow;i<=maxrow;i++)
for(j=mincol;j<=maxcol;j++)
{
if (i==appointrow) {astr[0][p0]=chessboard[i][j]+48;p0++;}
if (j==appointcol) {astr[1][p1]=chessboard[i][j]+48;p1++;}
if (i+j==appointrow+appointcol) {astr[2][p2]=chessboard[i][j]+48;p2++;}
if (i-j==appointrow-appointcol) {astr[3][p3]=chessboard[i][j]+48;p3++;}
}
astr[0][p0]=0;
astr[1][p1]=0;
astr[2][p2]=0;
astr[3][p3]=0;
for (i=0;i<4;i++)
for (j=19;j>=0;j--)
if (instr(astr[i],ord[appointplayer][j]))
{
if (j<IsOneP) errcode+=OneD;
else if (j<IsOneE) errcode+=OneU;
else if (j<IsTwoP) errcode+=TwoD;
else if (j<IsTwoE) errcode+=TwoU;
else if (j<IsThreeP) {errcode+=ThreeD; doublethree++;}
else if (j<IsThreeE) {errcode+=ThreeU; doublethree++;}
else if (j<IsFourP) {errcode+=FourD; doublefour++;}
else if (j<IsFourE) {errcode+=FourU; doublefour++;}
else if (j<IsFiveP) errcode+=IsFive;
else {errcode+=IsFive;longsix=1;}
break;
}
if (doublethree>=2 || doublefour>=2 || longsix)
if (forbid) errcode=0; else errcode+=10;
/* gotoxy(10,1);
printf("\none=%d",one);
printf("\ntwo=%d",two);
printf("\nthree=%d",three);
printf("\nfour=%d",four);
printf("\nfive=%d",five);
printf("\nsix=%d",six);
printf("\nerrcode=%d ",errcode);
*/
chessboard[appointrow][appointcol]=0;
}
return(errcode);
}
void ComputerAI(int player_man,int layer)
{int i=0,j=0,k_def=0,k_att=0,tmp=0,max_def=0,max_att=0;
char player_ai=(player_man+1)%2;
struct best_step attackstep[AIStrLong],defendstep[AIStrLong];
for (i=0;i<AIStrLong;i++)
{attackstep[i].value=0;attackstep[i].row=0;attackstep[i].col=1;
defendstep[i].value=0;defendstep[i].row=0;defendstep[i].col=1;
}
for (i=0;i<GAmount1;i++)
for (j=0;j<GAmount1;j++)
{
tmp=grant(i,j,player_ai);
if (tmp>=max_att && tmp)
{max_att=tmp;
attackstep[k_att].value=max_att;
attackstep[k_att].row=i;
attackstep[k_att].col=j;
k_att=(k_att+1)%AIStrLong;
}
tmp=grant(i,j,player_man);
if (tmp>=max_def && tmp)
{max_def=tmp;
defendstep[k_def].value=max_def;
defendstep[k_def].row=i;
defendstep[k_def].col=j;
k_def=(k_def+1)%AIStrLong;
}
}
// gotoxy(2,1);
for (i=0;i<AIStrLong;i++)
{
max_att=i;
max_def=i;
for (j=i+1;j<AIStrLong;j++)
{
if (attackstep[max_att].value<attackstep[j].value) max_att=j;
if (defendstep[max_def].value<defendstep[j].value) max_def=j;
}
if (max_att!=i)
{tmp=attackstep[max_att].value;
attackstep[max_att].value=attackstep[i].value;
attackstep[i].value=tmp;
tmp=attackstep[max_att].row;
attackstep[max_att].row=attackstep[i].row;
attackstep[i].row=tmp;
tmp=attackstep[max_att].col;
attackstep[max_att].col=attackstep[i].col;
attackstep[i].col=tmp;
}
if (max_def!=i)
{tmp=defendstep[max_def].value;
defendstep[max_def].value=defendstep[i].value;
defendstep[i].value=tmp;
tmp=defendstep[max_def].row;
defendstep[max_def].row=defendstep[i].row;
defendstep[i].row=tmp;
tmp=defendstep[max_def].col;
defendstep[max_def].col=defendstep[i].col;
defendstep[i].col=tmp;
}
// printf("\nrow=%d,col=%d,value=%d,layer=%d ",defendstep[i].row,defendstep[i].col,defendstep[max_def].value,layer);
}
// getch();
for (i=0;i<AIStrLong;i++)
defendstep[i].value=grant(defendstep[i].row,defendstep[i].col,player_ai);
tmp=0;
i=0;
if (layer>0)
{
for(i=0;i<AIStrLong;i++)
{
chessboard[attackstep[i].row][attackstep[i].col]=player_ai+1;
ComputerAI(player_ai,layer-1);
chessboard[attackstep[i].row][attackstep[i].col]=0;
if (codex.value<=attackstep[i].value) {tmp=0;break;}
}
if (i==AIStrLong)
{for(i=0;i<AIStrLong;i++)
{chessboard[defendstep[i].row][defendstep[i].col]=player_ai+1;
ComputerAI(player_ai,0);
chessboard[defendstep[i].row][defendstep[i].col]=0;
if (codex.value<=defendstep[i].value) {tmp=1;break;}
}
if (i==AIStrLong) {tmp=1;i=0;}
}
}
if (tmp==0)
{codex.value=attackstep[i].value;
codex.row=attackstep[i].row;
codex.col=attackstep[i].col;
}
else
{codex.value=defendstep[i].value;
codex.row=defendstep[i].row;
codex.col=defendstep[i].col;
}
}
void main(int argc,char *argv[])
{
int i=0,j=0;
int inkey=0,stepcode=0;
void *arrow,*cimage[2];
char player=1;
while(argc-->1)
{
argv++;
if (**argv=='c' || **argv=='C') vscomputer=1;
else if(**argv=='f' || **argv=='F') forbid=1;
else if(**argv=='o' || **argv=='O') attackpiece=1;
else
{
clrscr();
printf("\nUSE:WZQ [Command1] [Command2]...\n\tCommand: C Player VS Compuer\n\t\t F Forbid piece\n\t\t O Computer On the offensive");
exit(1);
}
}
// printf("\nc=%d,f=%d,d=%d",vscomputer,forbid,attackpiece);
// getch();
screen();
chessimage(cimage);
arrow=arrowimage();
inkey=KF1;
do
{
switch (inkey)
{case KUp:
if (row>0)
{putimage(col*GWidth+BLeft,row*GWidth+BTop,arrow,XOR_PUT);
row--;
putimage(col*GWidth+BLeft,row*GWidth+BTop,arrow,XOR_PUT);
}
break;
case KDown:
if (row<GAmount0)
{putimage(col*GWidth+BLeft,row*GWidth+BTop,arrow,XOR_PUT);
row++;
putimage(col*GWidth+BLeft,row*GWidth+BTop,arrow,XOR_PUT);
}
break;
case KLeft:
if (col>0)
{putimage(col*GWidth+BLeft,row*GWidth+BTop,arrow,XOR_PUT);
col--;
putimage(col*GWidth+BLeft,row*GWidth+BTop,arrow,XOR_PUT);
}
break;
case KRight:
if (col<GAmount0)
{putimage(col*GWidth+BLeft,row*GWidth+BTop,arrow,XOR_PUT);
col++;
putimage(col*GWidth+BLeft,row*GWidth+BTop,arrow,XOR_PUT);
}
break;
case KSpace:
stepcode=grant(row,col,player);
if (stepcode)
{
chessboard[row][col]=player+1;
putimage(col*GWidth+GLeft-9,row*GWidth+GTop-9,cimage[player],COPY_PUT);
if (vscomputer)
{ComputerAI(player,4);
player=(player+1)%2;
chessboard[codex.row][codex.col]=player+1;
putimage(codex.col*GWidth+GLeft-9,codex.row*GWidth+GTop-9,cimage[player],COPY_PUT);
stepcode=codex.value;
}
player=(player+1)%2;
}
else
outtextxy(30,360,"No! There is forbid piece!");
if (stepcode>=625)
{gotoxy(3,23);
printf("winer is player%d",player+1);
gotoxy(3,24);
printf("inkey to restart!");
getch();
}
else
break;
case KF1:
{
cleardevice();
for (i=0;i<GAmount1;i++)
for (j=0;j<GAmount1;j++)
chessboard[i][j]=0;
row=7;
col=7;
drawsquare();
putimage(col*GWidth+BLeft,row*GWidth+BTop,arrow,XOR_PUT);
setcolor(15);
setfillstyle(1,0);
player=0;
if (vscomputer && !attackpiece)
{
chessboard[row][col]=player+1;
putimage(col*GWidth+GLeft-9,row*GWidth+GTop-9,cimage[player],COPY_PUT);
player=(player+1)%2;
}
}
break;
case KTab:
// if (inkey
break;
default :break;
}
/* if (biostime(0,0L)-lasttime>18)
{
if (clearstart)
{clear--;
if (!clear) {floodfill(30,360,14);clearstart=0;}
}
lasttime=biostime(0,0L);
} */
inkey=bioskey(1)?bioskey(0):0;
}
while (inkey!=KEsc);
free(arrow);
free(cimage[0]);
free(cimage[1]);
closegraph();
printf ("\nAbout This Program! Welcome to 303 Classroom.\n");
}