本部分为电脑的人工智能。
为了加快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://tiaodangan
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