阅读新闻

五字棋算法 转载

[日期:2006-05-08] 来源:  作者: [字体: ]
         
这是我为GBA写的一个五子棋游戏代码,今天测试了一下,居然打败了PC上的一个五子棋高级难度。可是拿到网上和别人对弈,却发现有时输得很惨。。难道这个算法不稳定?

#include
#include
#include
#include
#define max_step 3
u32 com_time,man_time;
u8 who_isset;
extern void (*sys_deamon)();
struct chee_weight//棋点权重
{
u32 weight;//权重值
u8 type;//类型
u8 line_sta[4];
};
struct chee_loc
{
u8 xp;
u8 yp;
u8 danger;
};
//struct chee_weight *comm,*manm;
void cal_weight(struct chee_weight *chee);
void show_time(u8 xp,u8 yp,u32 time)
{
char mes[10];
u8 hou,min,sec;
time=time>>2;
hou=time/3600;
time=time%3600;
min=time/60;
sec=time%60;
mes[0]=hou/10+48;
mes[1]=hou%10+48;
mes[2]=':';
mes[3]=min/10+48;
mes[4]=min%10+48;
mes[5]=':';
mes[6]=sec/10+48;
mes[7]=sec%10+48;
mes[8]='\0';
Q_box(xp,yp,xp+64,yp+10,0);
cwrite(xp,yp,RGB(30,30,30),mes);
}
void chee_deamon()
{
if (who_isset)
{
man_time++;
if (man_time%4==0) show_time(175,100,man_time);
}
else
{
com_time++;
if (com_time%4==0) show_time(175,30,com_time);
}
}
void chee_box(u8 xp,u8 yp,u8 typ)
{
u16 color=typ?(typ==1?RGB(0,20,0):RGB(0,10,20)):RGB(20,10,5);
r_box(20+xp*10,5+yp*10,28+xp*10,13+yp*10,color,(typ<3?1:5));
}
void show_type(u8 type,u32 weight,u8 who)
{
char wei[30];
Q_box(173,10+who*40,239,45+who*40,0);
paint_eng(175,14+who*40,RGB(30,30,30),type+48);
num_to_str(weight,wei);
cwrite(175,30+who*40,RGB(30,30,30),wei);
}
void show_wins(u8 xp,u8 yp,u8 wins)
{
char mes[10]="WIN: ";
mes[5]=wins/10+48;
mes[6]=wins%10+48;
mes[7]='\0';
Q_box(xp,yp,xp+60,yp+10,0);
cwrite(xp,yp,RGB(30,30,30),mes);
}
void show_sta(struct chee_weight wei)
{
char str[12];
u8 i;
struct chee_weight *test=(struct chee_weight*)malloc(sizeof(struct chee_weight));
for (i=0;i<4;i++)
{
num_to_str(wei.line_sta[i],str);
r_box(0,10+i*14,20,23+i*14,0,1);
cwrite(0,10+i*14,RGB(30,30,30),str);
}
num_to_str(wei.type,str);
r_box(0,10+i*14,20,23+4*14,0,1);
cwrite(0,10+4*14,RGB(30,30,30),str);
*test=wei;
cal_weight(test);
num_to_str(test->type,str);
Q_box(0,10+5*14,20,23+5*14,0);
cwrite(0,10+5*14,RGB(30,30,30),str);
}
void show_loc(struct chee_loc loc)
{
char locs[6];
loc.xp=loc.xp%100;
loc.yp=loc.yp%100;
locs[0]=loc.xp/10+48;
locs[1]=loc.xp%10+48;
locs[2]=' ';
locs[3]=loc.yp/10+48;
locs[4]=loc.yp%10+48;
locs[5]='\0';
Q_box(180,100,230,120,0);
cwrite(182,102,RGB(30,30,30),locs);
}

void seek_sort(struct chee_loc *seek,struct chee_weight *com,struct chee_weight *man)
{
u8 i,j;
u8 loc;
u32 max,now;
u8 mp;
struct chee_loc temp;
for (i=1;i {
loc=seek[i].xp+15*seek[i].yp;
max=com[loc].weight+man[loc].weight>>1;
mp=i;
for (j=i+1;j<=seek[0].xp;j++)
{
loc=seek[j].xp+15*seek[j].yp;
now=com[loc].weight+man[loc].weight>>1;
if (now>max)
{
max=now;
mp=j;
}
}
temp=seek[i];
seek[i]=seek[mp];
seek[mp]=temp;
}
}

u8 get_line_type(u8 *plate,u8 xp,u8 yp,u8 dre)
{
u8 loc;
int xsp,ysp;
u8 nostop,start,lblock,rblock,dev,noblock;
u8 rjoin=0,rdev=0,rsets=0,rstart=0,rstop=0;
u8 ljoin=0,ldev=0,lsets=0,lstart=0,lstop=0;
int xd,yd;
u8 i;
u8 type=40;
u8 block=0;
switch (dre)
{
case 0://水平方向
xd=1;yd=0;
break;
case 1://竖直方向
xd=0;yd=1;
break;
case 2://右下
xd=1;yd=1;
break;
case 3://左下
xd=-1;yd=1;
break;
default:
write(30,30,RGB(30,30,30),"ERROR DRECTION!");
}
xsp=xp;
ysp=yp;
for (i=1;i<5;i++)
{
xsp=xsp+xd;
ysp=ysp+yd;
loc=ysp*15+xsp;
if (xsp<0 ||ysp>14 || ysp<0 || xsp>14)
{
if (!rstop && rstart) rstop=i;
break;
}
if (plate[loc]==2)
{
if (!rstop && rstart) rstop=i;
break;
}
else if(plate[loc]==0)
{
if (rstart && !rstop) rstop=i;
rdev++;
}
else if(plate[loc]==1)
{
rdev++;
rsets++;
if (!rstart) rstart=i;
}
}
xsp=xp;
ysp=yp;
for (i=1;i<5;i++)
{
xsp=xsp-xd;
ysp=ysp-yd;
loc=ysp*15+xsp;
if (xsp<0 ||ysp>14 || ysp<0 || xsp>14)
{
if (!lstop && lstart) lstop=i;
break;
}
if (plate[loc]==2)
{
if (!lstop && lstart) lstop=i;
break;
}
else if(plate[loc]==0)
{
if (lstart && !lstop) lstop=i;
ldev++;
}
else if(plate[loc]==1)
{
ldev++;
lsets++;
if (!lstart) lstart=i;
//show_float(i,RGB(30,30,30));
}
}
ljoin=(lstart==1?lstop-lstart:0);
rjoin=(rstart==1?rstop-rstart:0);
lblock=lstop?lstop>ldev:!ldev;
rblock=rstop?rstop>rdev:!rdev;
noblock=!(rblock||lblock);
dev=ldev+rdev;
if (dev<4) type=40;//阻碍
else
{
if (ljoin+rjoin>=4) type=0;//成四
else if(ljoin+rjoin==3 && noblock) type=1;//活三
else if(ljoin+rjoin==3) type=3;//冲三
else if( (ljoin==2 && rstart==2 && rsets) || (lstart==1 && lsets>=2 && lstop<5 && rjoin) || lsets==3
|| (rjoin==2 && lstart==2 && lsets) || (rstart==1 && rsets>=2 && rstop<5 && ljoin) || rsets==3)
type=2;//离三
else if(ljoin+rjoin==2 && dev>4 && noblock) type=4;//活二
else if( (lsets>=2 && lstop==4 && noblock) || (lsets && lstart==2 && rjoin && noblock)
|| (lsets>=2 && lstart==1 && noblock && ldev==4 && plate[xp-3*xd+15*(yp-3*yd)])
|| (rsets>=2 && rstop==4 && noblock) || (rsets && rstart==2 && ljoin && noblock)
|| (rsets>=2 && rstart==1 && noblock && rdev==4 && plate[xp+3*xd+15*(yp+3*yd)]))
type=5;//离二
else if(rjoin+ljoin && noblock) type=6;//单一
else if (noblock) type=7+rsets+lsets;//其它离散分布情况
}
return type;
}
void cal_weight(struct chee_weight *chee)
{
u8 types[6]={0,0,0,0,0,0};
u8 type;
u8 i;
u32 weight=0;
for (i=0;i<4;i++)
{
type=chee->line_sta[i];
if (type==0)
{
types[0]++;//连四
weight+=100000000;
}
else if(type==1)
{
types[1]++;//活三
weight+=1000000;
}
else if(type==2 || type==3)
{
types[2]++;//冲三
weight=weight+999900;
}
else if(type==4)
{
types[3]++;//活二
weight+=10000;
}
else if(type==5)
{
types[4]++;//离二
weight+=9000;
}
else if(type==6)
{
types[5]++;
weight+=20;
}
else if(type>=7 && type<40) weight+=(type-6);
else weight++;
}
chee->type=7;
chee->weight=weight;
if (types[0])//连四(致命态)
{
chee->type=0;
chee->weight=weight+500000000;
}
else if(types[1] || types[2]>1)//下步必死态
{
chee->type=1;
chee->weight=weight+5000000;
}
else if(types[2] && (types[3]+types[4])) //下步可能必死态
{
chee->type=2;
chee->weight=weight+2000000;
}
else if(types[3]+types[4]>1) //下两步可能必死态
{
chee->type=3;
chee->weight=weight+10000;
}
else if(types[2])//可控对方下子态
{
chee->type=4;
chee->weight=weight+5000;
}
else if(types[3]+types[4])//有前途态
{
chee->type=5;
chee->weight=weight+500;
}
else if(types[5]>1) weight+=150;
}
void recal_weight(u8 xp,u8 yp,u8 *plate,struct chee_weight *w_plate)//放子后重建受影响的点的权重表
{
u8 i,j;
int xd[8]={1,0,1,-1,-1,0,-1,1};
int yd[8]={0,1,1,1,0,-1,-1,-1};
int xsp,ysp;
u8 dre;
u8 loc;
for (i=0;i<8;i++)//八个方向上的点受影响
{
xsp=xp;
ysp=yp;
dre=i%4;
for (j=0;j<4;j++)//每个方向上四个点受影响
{
xsp=xsp+xd[i];
ysp=ysp+yd[i];
if (xsp<0 || ysp<0 || xsp>14 || ysp>14) break;
loc=xsp+15*ysp;
if (plate[loc]==2) break;
if (plate[loc]) continue;//该点已有棋子
w_plate[loc].line_sta[dre]=get_line_type(plate,xsp,ysp,dre);
cal_weight(w_plate+loc);//重新计算该点权重
}
}
}
struct chee_loc chee_fix_set(u8 xp,u8 yp,u8 *plate,struct chee_weight *w_plate)
{
u8 i,j;
int xd[8]={1,0,1,-1,-1,0,-1,1};
int yd[8]={0,1,1,1,0,-1,-1,-1};
int xsp,ysp;
u8 dre;
u8 loc;
struct chee_loc res;
for (i=0;i<8;i++)//八个方向上的点受影响
{
xsp=xp;
ysp=yp;
dre=i%4;
for (j=0;j<4;j++)//每个方向上四个点受影响
{
xsp=xsp+xd[i];
ysp=ysp+yd[i];
if (xsp<0 || ysp<0 || xsp>14 || ysp>14) break;
loc=xsp+15*ysp;
if (plate[loc]==2) break;
if (plate[loc]) continue;//该点已有棋子
if (w_plate[loc].type==0)
{
res.xp=xsp;
res.yp=ysp;
return res;
}
}
}
}
void chee_back(u8 xp,u8 yp,u8 *plate,struct chee_weight *wei)
{
plate[xp+15*yp]=0;
recal_weight(xp,yp,plate,wei);
}
void history_back(u8 *com,u8 *man,struct chee_weight *comw,struct chee_weight *manw,struct chee_loc *history)
{
u8 loc;
if (!history[0].danger)
{
loc=history[1].xp+15*history[1].yp;
chee_back(history[1].xp,history[1].yp,com,comw);
chee_back(history[1].xp,history[1].yp,man,manw);
chee_box(history[1].xp,history[1].yp,0);
loc=history[0].xp+15*history[0].yp;
chee_back(history[0].xp,history[0].yp,com,comw);
chee_back(history[0].xp,history[0].yp,man,manw);
chee_box(history[0].xp,history[0].yp,0);
history[0].danger=1;
}
else arlt("系统警告:","尚无历史记录,无法悔棋!");

}
struct chee_loc com_set(u8 *com,u8 *man,struct chee_weight *w_com,struct chee_weight *w_man,u8 step)//电脑下子
{
u8 i,j;
u8 xp,yp;
u8 loc,loc1;
u8 xsp=16,ysp=16;//最大权重点
u32 now=0,max=0;//最大权重
u8 ctype,mtype;
u8 safe=1;//下子是否安全
u8 done=0;
struct chee_loc res,temp;
struct chee_loc c_seek[6][10];//重点考察点
struct chee_loc m_seek[6][10];//重点考察点
if (step>max_step)
{
res.xp=16;
res.yp=16;
return res;
}
//show_float(step,RGB(30,30,30));
for (i=0;i<6;i++)//初始化
{
c_seek[i][0].xp=0;
m_seek[i][0].xp=0;
}
for (i=0;i<15;i++)//扫描棋盘
for (j=0;j<15;j++)
{
loc=i+j*15;
if (com[loc]) continue;//该点已经有棋子
ctype=w_com[loc].type;
mtype=w_man[loc].type;
if (ctype==0)//敌方已经致命
{
res.xp=100+i;//+100标志胜利
res.yp=j;
return res;
}
if (ctype<6)
{
if (c_seek[ctype][0].xp<9)
{
c_seek[ctype][0].xp++;
c_seek[ctype][c_seek[ctype][0].xp].xp=i;
c_seek[ctype][c_seek[ctype][0].xp].yp=j;
}
}
if (mtype<6)
{
if (m_seek[mtype][0].xp<9)
{
m_seek[mtype][0].xp++;
m_seek[mtype][m_seek[mtype][0].xp].xp=i;
m_seek[mtype][m_seek[mtype][0].xp].yp=j;
}
}
now=w_com[loc].weight+w_man[loc].weight>>1;
if (now>max || (now==max && CS_Rand()%2))
{
max=now;
xsp=i;
ysp=j;
}
}//扫描完毕,下面决定攻守策略
if (m_seek[0][0].xp)//敌方连四
{
if (m_seek[0][0].xp>1 || step<1)//必败
{
res.xp=m_seek[0][1].xp;
res.yp=100+m_seek[0][1].yp;//+100表必败
return res;
}
xp=m_seek[0][1].xp;
yp=m_seek[0][1].yp;
loc=xp+15*yp;
com[loc]=1;
man[loc]=2;
recal_weight(xp,yp,com,w_com);
recal_weight(xp,yp,man,w_man);//我方下子
temp=com_set(man,com,w_man,w_com,step+1);//模拟敌方下子
com[loc]=0;
man[loc]=0;
recal_weight(xp,yp,com,w_com);
recal_weight(xp,yp,man,w_man);//恢复棋盘
if (temp.xp>=100)//敌方胜利
{
res.xp=xp;
res.yp=100+yp;
}
else if(temp.yp>=100)//我方胜利
{
res.xp=100+xp;
res.yp=yp;
}
else
{
res.xp=xp;
res.yp=yp;
}
return res;
}
if (c_seek[1][0].xp)//我方必胜
{
res.xp=c_seek[1][1].xp+100;
res.yp=c_seek[1][1].yp;
return res;
}
if (c_seek[2][0].xp)//我方可能胜利
{
if (m_seek[1][0].xp+m_seek[2][0].xp+m_seek[4][0].xp==0)//无反抗能力
{
res.xp=c_seek[2][1].xp+100;
res.yp=c_seek[2][1].yp;
return res;
}
seek_sort(c_seek[2],w_com,w_man);
for (i=1;i<=c_seek[2][0].xp;i++)
{
xp=c_seek[2][i].xp;
yp=c_seek[2][i].yp;
loc=xp+15*yp;
com[loc]=1;
man[loc]=2;
recal_weight(xp,yp,com,w_com);
recal_weight(xp,yp,man,w_man);//我方下子
temp=com_set(man,com,w_man,w_com,step+1);//模拟敌方下子
com[loc]=0;
man[loc]=0;
recal_weight(xp,yp,com,w_com);
recal_weight(xp,yp,man,w_man);//恢复棋盘
if (temp.xp<100)//胜利啦
{
res.xp=xp+(temp.yp>=100?100:0);
res.yp=yp;
return res;
}
else c_seek[2][i].danger=1;
}
}
if (c_seek[4][0].xp)//我方可控态
{
seek_sort(c_seek[4],w_com,w_man);
for (i=1;i<=c_seek[4][0].xp;i++)
{
xp=c_seek[4][i].xp;
yp=c_seek[4][i].yp;
loc=xp+15*yp;
com[loc]=1;
man[loc]=2;
recal_weight(xp,yp,com,w_com);
recal_weight(xp,yp,man,w_man);//我方下子
res=chee_fix_set(xp,yp,com,w_com);//获得敌方被迫下子点
loc1=res.xp+res.yp*15;
com[loc1]=2;
man[loc1]=1;
recal_weight(res.xp,res.yp,com,w_com);
recal_weight(res.xp,res.yp,man,w_man);
temp=com_set(com,man,w_com,w_man,step+1);//模拟敌方下子
com[loc1]=0;
man[loc1]=0;
recal_weight(res.xp,res.yp,com,w_com);
recal_weight(res.xp,res.yp,man,w_man);
com[loc]=0;
man[loc]=0;
recal_weight(xp,yp,com,w_com);
recal_weight(xp,yp,man,w_man);//恢复棋盘
if (temp.xp>=100)//胜利啦
{
res.xp=100+xp;
res.yp=yp;
return res;
}
else if (temp.yp>=100)//有危险
{
c_seek[4][i].danger=1;
}
else c_seek[4][i].danger=0;//没什么
}
}
if (m_seek[1][0].xp)//我方致命点
{
seek_sort(m_seek[1],w_com,w_man);
if (m_seek[1][0].xp==1) return m_seek[1][1];
for (i=1;i<=m_seek[1][0].xp;i++)
{
xp=m_seek[1][i].xp;
yp=m_seek[1][i].yp;
loc=xp+15*yp;
com[loc]=1;
man[loc]=2;
recal_weight(xp,yp,com,w_com);
recal_weight(xp,yp,man,w_man);//我方下子
temp=com_set(man,com,w_man,w_com,step+1);//模拟敌方下子
com[loc]=0;
man[loc]=0;
recal_weight(xp,yp,com,w_com);
recal_weight(xp,yp,man,w_man);//恢复棋盘
if (temp.xp<100)//解除危险
{
res.xp=xp+(temp.yp<100?0:100);
res.yp=yp;
return res;
}
else m_seek[1][i].danger=1;
}
}
if (m_seek[2][0].xp)//我方可能致命点
{
if (!(c_seek[4][0].xp))
{
res.xp=m_seek[2][1].xp;
res.yp=m_seek[2][1].yp+(m_seek[2][0].xp>1?100:0);
return res;
}
if (m_seek[2][0].xp==1) return m_seek[2][1];
seek_sort(m_seek[2],w_com,w_man);
for (i=1;i<=m_seek[2][0].xp;i++)
{
xp=m_seek[2][i].xp;
yp=m_seek[2][i].yp;
loc=xp+15*yp;
com[loc]=1;
man[loc]=2;
recal_weight(xp,yp,com,w_com);
recal_weight(xp,yp,man,w_man);//我方下子
temp=com_set(man,com,w_man,w_com,step+1);//模拟敌方下子
com[loc]=0;
man[loc]=0;
recal_weight(xp,yp,com,w_com);
recal_weight(xp,yp,man,w_man);//恢复棋盘
if(temp.xp<100)//解除危险
{
res.xp=xp+(temp.yp<100?0:100);
res.yp=yp;
return res;
}
else m_seek[2][i].danger=1;
}
}
if (c_seek[3][0].xp)//敌方两步可能致死态
{
if (!m_seek[4][0].xp)
{
res.xp=c_seek[3][1].xp+100;
res.yp=c_seek[3][1].yp;
return res;
}
seek_sort(c_seek[3],w_com,w_man);
for (i=1;i<=c_seek[3][0].xp;i++)
{
xp=c_seek[3][i].xp;
yp=c_seek[3][i].yp;
loc=xp+15*yp;
com[loc]=1;
man[loc]=2;
recal_weight(xp,yp,com,w_com);
recal_weight(xp,yp,man,w_man);//我方下子
temp=com_set(man,com,w_man,w_com,step+1);//模拟敌方下子
com[loc]=0;
man[loc]=0;
recal_weight(xp,yp,com,w_com);
recal_weight(xp,yp,man,w_man);//恢复棋盘
if (temp.yp>=100)//胜利啦
{
res.xp=100+xp;
res.yp=yp;
return res;
}
if(temp.xp>=100) c_seek[3][i].danger=1;
else c_seek[3][i].danger=0;
}
}
if (m_seek[3][0].xp)//我方两步可能致死态
{
seek_sort(m_seek[3],w_com,w_man);
if (m_seek[3][0].xp==1) return m_seek[3][1];
for (i=1;i<=m_seek[3][0].xp;i++)
{
xp=m_seek[3][i].xp;
yp=m_seek[3][i].yp;
loc=xp+15*yp;
com[loc]=1;
man[loc]=2;
recal_weight(xp,yp,com,w_com);
recal_weight(xp,yp,man,w_man);//我方下子
temp=com_set(man,com,w_man,w_com,step+1);//模拟敌方下子
com[loc]=0;
man[loc]=0;
recal_weight(xp,yp,com,w_com);
recal_weight(xp,yp,man,w_man);//恢复棋盘
if(temp.xp<100)//解除危险
{
res.xp=xp+(temp.yp<100?0:100);
res.yp=yp;
return res;
}
else m_seek[3][i].danger=1;
}
}
if (m_seek[4][0].xp)//敌方可控态
{
seek_sort(m_seek[4],w_man,w_com);
for (i=1;i<=m_seek[4][0].xp;i++)
{
xp=m_seek[4][i].xp;
yp=m_seek[4][i].yp;
loc=xp+15*yp;
com[loc]=1;
man[loc]=2;
recal_weight(xp,yp,com,w_com);
recal_weight(xp,yp,man,w_man);//我方下子
temp=com_set(man,com,w_man,w_com,step+1);//模拟敌方下子
com[loc]=0;
man[loc]=0;
recal_weight(xp,yp,com,w_com);
recal_weight(xp,yp,man,w_man);//恢复棋盘
if(temp.yp>=100)//我方胜利
{
res.xp=xp+100;
res.yp=yp;
return res;
}
if (temp.xp>=100) m_seek[4][i].danger=1;
else m_seek[4][i].danger=0;
}
}
if (c_seek[5][0].xp && step<3)//我方可发展态
{
seek_sort(c_seek[5],w_com,w_man);
for (i=1;i<=c_seek[5][0].xp;i++)
{
if (i>4 && step) break;
xp=c_seek[5][i].xp;
yp=c_seek[5][i].yp;
loc=xp+15*yp;
com[loc]=1;
man[loc]=2;
recal_weight(xp,yp,com,w_com);
recal_weight(xp,yp,man,w_man);//我方下子
temp=com_set(man,com,w_man,w_com,step+1);//模拟敌方下子
com[loc]=0;
man[loc]=0;
recal_weight(xp,yp,com,w_com);
recal_weight(xp,yp,man,w_man);//恢复棋盘
if (temp.yp>=100)//胜利啦
{
res.xp=100+xp;
res.yp=yp;
return res;
}
else if(temp.xp>=100) c_seek[5][i].danger=1;
else c_seek[5][i].danger=0;
}
}
if (m_seek[5][0].xp && step<3)//敌方可发展态
{
seek_sort(m_seek[5],w_com,w_man);
for (i=1;i<=m_seek[5][0].xp;i++)
{
if (i>4 && step) break;
xp=m_seek[5][i].xp;
yp=m_seek[5][i].yp;
loc=xp+15*yp;
com[loc]=1;
man[loc]=2;
recal_weight(xp,yp,com,w_com);
recal_weight(xp,yp,man,w_man);//我方下子
temp=com_set(man,com,w_man,w_com,step+1);//模拟敌方下子
com[loc]=0;
man[loc]=0;
recal_weight(xp,yp,com,w_com);
recal_weight(xp,yp,man,w_man);//恢复棋盘
if(temp.yp>=100)//我方胜利
{
res.xp=xp+100;
res.yp=yp;
return res;
}
if (temp.xp>=100) m_seek[5][i].danger=1;
else m_seek[5][i].danger=0;
}
}

if (xsp>14 || ysp>14)
{
res.xp=16;
res.yp=16;
return res;
}
loc=xsp+15*ysp;//考察最大点
if (w_com[loc].weight>5 && w_man[loc].weight>5 && !step)
{
com[loc]=1;
man[loc]=2;
recal_weight(xsp,ysp,com,w_com);
recal_weight(xsp,ysp,man,w_man);//我方下子
temp=com_set(man,com,w_man,w_com,step+2);//模拟敌方下子
com[loc]=0;
man[loc]=0;
recal_weight(xsp,ysp,com,w_com);
recal_weight(xsp,ysp,man,w_man);//恢复棋盘
if (temp.xp<100)//没问题
{
res.xp=xsp+(temp.yp<100?0:100);
res.yp=ysp;
return res;
}
else safe=0;
}
for (i=0;i<6;i++)
{
for (j=1;j<=c_seek[i][0].xp;j++)
{
if (!c_seek[i][j].danger) return c_seek[i][j];
}
}
for (i=0;i<6;i++)
{
for (j=1;j<=m_seek[i][0].xp;j++)
{
if (!m_seek[i][j].danger) return m_seek[i][j];
}
}
res.xp=xsp;
res.yp=ysp+(safe?0:100);
return res;
}
struct chee_loc man_set(u8 *plate,u8 *man,struct chee_weight *wcom,struct chee_weight *wman,struct chee_loc *history)
{
struct chee_loc set,comsets;
u8 xp,yp;
u8 xsp,ysp,i=0,done=0;
u16 wait;
u8 loc;
if (history[0].danger==1)
{
xsp=7;
ysp=7;
xp=7;
yp=7;
}
else
{
xsp=history[1].xp;
ysp=history[1].yp;
xp=xsp;
yp=ysp;
}
while (TRUE)
{
for (i=0;i<10;i++)
{
for (wait=0;wait<2500;wait++) xp=xp;
chee_box(xsp,ysp,i%2?0:(*(plate+15*ysp+xsp)?3:2));
}
CS_ReadKey();
if (CS_IsKeyDown(KEY_UP))
{
done=1;
ysp--;
if (ysp>14) ysp=14;
}
else if(CS_IsKeyDown(KEY_DOWN))
{
done=1;
ysp++;
if (ysp>14) ysp=0;
}
else if(CS_IsKeyDown(KEY_LEFT))
{
done=1;
xsp--;
if (xsp>14) xsp=14;
}
else if(CS_IsKeyDown(KEY_RIGHT))
{
done=1;
xsp++;
if (xsp>14) xsp=0;
}
else if(CS_IsKeyDown(KEY_A)) done=2;
else if(CS_IsKeyDown(KEY_B)) done=3;
else if(CS_IsKeyDown(KEY_R)) done=4;
else if(CS_IsKeyDown(KEY_L)) done=5;
switch (done)
{
case 1:
done=0;
chee_box(xp,yp,*(plate+15*yp+xp));
xp=xsp;
yp=ysp;
loc=xsp+15*ysp;
//show_type(wcom[loc].type,wcom[loc].weight,0);
//show_type(wman[loc].type,wman[loc].weight,1);
break;
case 2:
if (*(plate+15*ysp+xsp))
{
arlt("出错啦!","该点已有棋子,请重新选放!");
done=0;
}
else
{
chee_box(xsp,ysp,2);
set.xp=xsp;
set.yp=ysp;
return set;
}
break;
case 3:
if (arlt("退出五子棋游戏","您真的要退出五子棋游戏吗?\n[A:确定 ] [B:取消 ]"))
{
set.xp=16;
set.yp=16;
return set;
}
done=0;
break;
case 4:
history_back(plate,man,wcom,wman,history);
done=0;
break;
case 5:
done=0;
break;
default:
}
}
}
void chee_clean(u8 *com,u8 *man,struct chee_weight *wcom,struct chee_weight *wman)
{
u8 i,j,k,loc;
for (i=0;i<15;i++)
for (j=0;j<15;j++)
{
loc=i+15*j;
com[loc]=0;
man[loc]=0;
wcom[loc].weight=0;
wcom[loc].type=6;
wman[loc].weight=0;
wman[loc].type=6;
for (k=0;k<4;k++)
{
wcom[loc].line_sta[k]=7;
wman[loc].line_sta[k]=7;
}
}
}

u8 check_win(u8 xp,u8 yp,struct chee_weight *wei)
{
return wei[xp+15*yp].type==0;
}

void save_history(u8 xp,u8 yp,struct chee_loc *history,u8 who)
{
history[who].xp=xp;
history[who].yp=yp;
history[0].danger=0;
}
void chee()
{
u8 com[15*15];
u8 man[15*15];
u8 i,j,loc;
u8 xp,yp;
u8 comwins=0,manwins=0;
u8 who_first=0;
struct chee_weight w_com[15*15];
struct chee_weight w_man[15*15];
struct chee_loc set;
struct chee_loc history[2];
again:
com_time=0;
man_time=0;
show_time(175,100,man_time);
show_time(175,30,com_time);
show_wins(175,50,comwins);
show_wins(175,120,manwins);
for (i=0;i<15;i++)
for (j=0;j<15;j++)
chee_box(i,j,0);
history[0].danger=1;
chee_clean(com,man,w_com,w_man);
com[7+7*15]=who_first?2:1;
man[7+7*15]=who_first?1:2;
recal_weight(7,7,com,w_com);
recal_weight(7,7,man,w_man);
chee_box(7,7,who_first?2:1);
if (who_first)
{
history[0].xp=7;
history[0].yp=7;
goto comstart;
}
history[1].xp=7;
history[1].yp=7;
while (1)
{
who_isset=1;
set=man_set(com,man,w_com,w_man,history);
xp=set.xp%100;
yp=set.yp%100;
if (xp==16) return;
if (check_win(xp,yp,w_man))
{
arlt("恭喜你赢啦!","我被你打败了,不错嘛。不过不要骄傲噢 ^_^");
who_first=!who_first;
manwins++;
goto again;
}
save_history(xp,yp,history,0);
loc=xp+15*yp;
com[loc]=2;
man[loc]=1;
recal_weight(xp,yp,com,w_com);
recal_weight(xp,yp,man,w_man);
chee_box(xp,yp,2);
comstart:
who_isset=0;
set=com_set(com,man,w_com,w_man,0);
xp=set.xp%100;
yp=set.yp%100;
loc=xp+15*yp;
if (xp>14 || yp>14 ||com[loc])
{
arlt("系统提示:","系统检测到游戏数据异常,请退出后重试!");
return;
}
if (check_win(xp,yp,w_com))
{
arlt("你败咯!","哈哈,我赢你啦。要好好努力噢 ^_^");
who_first=!who_first;
comwins++;
chee_box(xp,yp,1);
while (1)
{
CS_ReadKey();
if (CS_IsAnyKeyDown(KEY_ALL)) goto again;
}
}
save_history(xp,yp,history,1);
com[loc]=1;
man[loc]=2;
recal_weight(xp,yp,com,w_com);
recal_weight(xp,yp,man,w_man);
//show_type(w_com[loc].type,w_com[loc].weight,0);
//show_type(w_man[loc].type,w_man[loc].weight,1);
//show_loc(set);
chee_box(xp,yp,1);
}
}
chee_test()
{
com_time=0;
man_time=0;
who_isset=0;
Q_box(0,0,239,159,0);
cwrite(175,10,RGB(30,30,30),"COMPUTER");
cwrite(175,80,RGB(30,30,30),"PLAYER");
cwrite(175,140,RGB(30,30,30),"R:悔棋");
Q_box(18,3,170,155,RGB(30,30,0));
sys_deamon=chee_deamon;
chee();
sys_deamon=NULL;
}


阅读:
录入:gg

评论 】 【 推荐 】 【 打印
上一篇:纹理混合和模板
下一篇:浅谈游戏程序设计入门 - 基础篇
相关新闻      
本文评论       全部评论
发表评论
字数
姓名:

  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款