服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java游戏直棋(二)


  本部分为电脑的人工智能。

为了加快ai的计算速度,必须对前面的设计进行少量的修改,并最终向vc平台转移。在用vc实现的游戏中,棋盘将采用bitboard技术,棋子的显示将彻底和逻辑分开。目前java版本仍然采用数组表示棋盘,主要是便于逻辑分析和设计。

先对第一个ai进行总结。firstai:

package nicholas.game.chess;

class firstai extends chessplayer {

private chessmodel model[][][];
private stepstack stack;
private step step;
private int layer;
private int turn;
protected gamerule gamerule;

public firstai(int t) {
super(false);
stack = new stepstack();
layer = 7;
// layer = 3;
turn = t;
}

public string getname() {
return "colinrobot";
}

public step getnextstep(chessmodel m[][][]) {
//algo
model = m;
system.out.println("max="+getlayervalue(0));
stack.removeall();
return step;
}

//get largest value
private int getlayervalue(int lay) {
if(lay>layer) {
//no recursion
return -1*getmodelvalue();
}
int value = 0;
int max = -2000;
int decision;
for(int z=0;z<3;z++) {
for(int y=0;y<3;y++) {
for(int x=0;x<3;x++) {
if((x==1&&y==1)||model[z][y][x].isoccupied()) continue;
//assume lay chessman here
model[z][y][x].acceptchessman(chessman.chess[(turn+lay)%2]);
decision = gamerule.checkstep(model[z][y][x], model);
switch(decision) {
case 0://win
stack.add(new step(model[z][y][x],decision));
value = 1000;
break;
case 3://tiaodan|gan
gamerule.checkdecision(model[z][y][x],1,model);
stack.add(new step(model[z][y][x],1));
value = 660;
/* value = -1*getlayervalue(lay+1);
//roll back
gamerule.undostep(stack.remove(),model);
model[z][y][x].acceptchessman(chessman.chess[(turn+lay)%2]);
//another
gamerule.checkdecision(model[z][y][x],2,model);
stack.add(new step(model[z][y][x],2));
int b = -1*getlayervalue(lay+1);
//choose better
if(value<b) {
value = b;
} else {
//roll back
gamerule.undostep(stack.remove(),model);
model[z][y][x].acceptchessman(chessman.chess[(turn+lay)%2]);
//redo first
gamerule.checkdecision(model[z][y][x],1,model);
stack.add(new step(model[z][y][x],1));
}
*/ break;
case 1://tiaodan
stack.add(new step(model[z][y][x],decision));
value = 660;
break;
case 2://gan
stack.add(new step(model[z][y][x],decision));
value = 320;
break;
default://tiaodan,gan,none
stack.add(new step(model[z][y][x],decision));
value = -1*getlayervalue(lay+1);
}
if(value>max) {
max = value;
if(lay==0) {
//first layer, save step
system.out.println("max="+max);
step = stack.gettop();
}
}
//remove chessman
gamerule.undostep(stack.remove(),model);
if(max==1000) return max;
}
}
}
return max;
}

private int getmodelvalue() {
return 3;
}

public void setgamerule(gamerule rule) {
gamerule = rule;
}
}

firstai直接继承chessplayer,以后将转为间接继承。采用最大最小深度优先搜索,结束对某分支(仅当前层次)的搜索的两个条件为:a.该层次玩家赢。b.最深搜索层次。最深搜索层次时将返回对局面的评估值(未设计,一律返回3,表示落子得3分。)。

后面的设计,除bitboard实现棋盘外需要考虑几个问题:
1)搜索的层次。针对第一步着法,强制去除部分无关分支(x+y>2),再将搜索层次设置为7,即可得到正确的着法。因此估计最大的搜索层次设置为7即可。
2)算法的改进。即使搜索层次仅为7,计算一步也要考虑46亿种可能性,假设每种可能性需要60次运算,以我的本本的配置需要三分钟。是否设计开局库(计算表明部分落子仅有唯一应手);另外将考虑采取其它的搜索技巧;破直将有额外的奖励(局面值>3);考虑可杠可单时,单是否一定比杠有利。
3)局面的估值。比较复杂,考虑中。

扫描关注微信公众号