Flash动画:
历史飞行棋》移动控制上采用了纯AS运算移动,另外本游戏采用了双筛子和随机地图,所以需要进行电脑选择筛子的判断,这里选择最优化的筛子进行移动就是电脑AI的所在.
5.控制棋子移动 使用了setInterval来控制时间,这里将每个title按顺序命名:t1,t2......
参数_who表示要移动的mc,如:c1,c2,c3,_step表示要移动的步数,该函数支持步数为负,即退.
function movecast(_who, _step) { trace("事件:移动"+_step); flashcast(_who, false); var dpos = _who.pos+_step; var dr = true; if (_step<0) { dr = false; } clearInterval(moveid); moveid = setInterval(function () { if (dr) { if (_who.pos<dpos) { if (_who.pos<=titleMaxnum) { _who.pos += 1; if (_who.pos == titleMaxnum+1) { _who._x = _who.x00; _who._y = _who.y00; //执行后退 if (_who.pos-dpos<>0) { movecast(_who, _who.pos-dpos); } else { _who.overflag = true; trace("事件:"+_who+"胜利!"); } } else { _who._x = eval("t"+_who.pos)._x+18; _who._y = eval("t"+_who.pos)._y+11; } } } else { exeevent(_who); clearInterval(moveid); } } else { if (_who.pos>dpos) { _who.pos -= 1; _who._x = eval("t"+_who.pos)._x+18; _who._y = eval("t"+_who.pos)._y+11; } else { exeevent(_who); clearInterval(moveid); } } }, 500); }
6、AI设定,电脑的AI是人思维的体现,一个完善的AI应该是一个系统,应该是很多人智慧的结晶!
这不仅仅需要良好的程序结构,更需要总结人的操作过程。这里仅总结一下个人对AI的理解。
我常把电脑的智能操作分成几个等级,这样才能提高游戏的娱乐性,电脑什么都想到了游戏的价值就没有了,
何况人也有操作和分析失误的时候。
本游戏我将AI分为三个等级:
AI-1:这是一种随机的操作,从两个数中任意选择一个数。
//简单随机 var rnd = int(Math.random()*2)+1; curstep = this["b"+rnd].values; movecast(this["c"+curplayer], curstep);
AI-2:只简单的做一次判断,是否目的地是友好的。
if (b1.values<>b2.values) { var maxnum; var minnum; if (b1.values>b2.values) { maxnum = b1; minnum = b2; } else { maxnum = b2; minnum = b1; } trace("tt"+maxnum.values); var good_array = [1, 4, 5, 7, 9]; for (var i = 0; i<good_array.length; i++) { if (this["t"+this["c"+curplayer].pos+maxnum.values].types == good_array[i]) { curstep = maxnum.values; movecast(this["c"+curplayer], curstep); return; } } for (var i = 0; i<good_array.length; i++) { if (this["t"+this["c"+curplayer].pos+minnum.values].types == good_array[i]) { curstep = minnum.values; movecast(this["c"+curplayer], curstep); return; } } curstep = maxnum.values; movecast(this["c"+curplayer], curstep); } else { curstep = b1.values; movecast(this["c"+curplayer], curstep); }
AI-3:比较完善的分析,分别计算出选择每一个筛子后走出的最终效果得分,最后选择效果分高执行。
//ai2 对两个筛子的最后结果大小进行比较 /*2:-1 ,3:-3,4:+1,5:+3,6:-5,7:+n,8:-n,碰撞其他人得分*/ //基础分等于2个筛子的大小 可以提炼成为一个函数,根据who,_step就可以得出最终效果分数 var score1 = b1.values; var score2 = b2.values; //对b1进行判断最终得分 var pos0 = this["c"+curplayer].pos+b1.values; if (pos0<>titleMaxnum+1) { if (pos0>titleMaxnum+1) { var p0 = (titleMaxnum+1)-this["c"+curplayer].pos; pos0 = (titleMaxnum+1)-(b1.values-p0); delete p0; } //是否存在对手 score1 += checkhit_pos(pos0); //退1 if (this["t"+pos0].types == 2) { score1 -= 1; score1 += checkhit_pos(pos0-1); if (this["t"+(pos0-1)].types == 6) { score1 -= 5; } else if (this["t"+(pos0-1)].types == 7) { score1 += checksd_pos(pos0-1); } else if (this["t"+(pos0-1)].types == 8) { score1 -= this["c"+curplayer].pos; } } else if (this["t"+pos0].types == 3) { score1 -= 3; score1 += checkhit_pos(pos0-3); if (this["t"+(pos0-3)].types == 6) { score1 -= 5; } else if (this["t"+(pos0-3)].types == 7) { score1 += checksd_pos(pos0-3); } else if (this["t"+(pos0-3)].types == 8) { score1 -= this["c"+curplayer].pos; } } else if (this["t"+pos0].types == 4) { score1 += 1; score1 += checkhit_pos(pos0+1); if (this["t"+(pos0+1)].types == 6) { score1 -= 5; } else if (this["t"+(pos0+1)].types == 7) { score1 += checksd_pos(pos0+1); } else if (this["t"+(pos0+1)].types == 8) { score1 -= this["c"+curplayer].pos; } } else if (this["t"+pos0].types == 5) { score1 += 3; score1 += checkhit_pos(pos0+3); if (this["t"+(pos0+3)].types == 6) { score1 -= 5; } else if (this["t"+(pos0+3)].types == 7) { score1 += checksd_pos(pos0+3); } else if (this["t"+(pos0+3)].types == 8) { score1 -= this["c"+curplayer].pos; } } else if (this["t"+pos0].types == 6) { score1 -= 5; } else if (this["t"+pos0].types == 7) { score1 += checksd_pos(pos0); } else if (this["t"+pos0].types == 8) { score1 -= this["c"+curplayer].pos; } } else { //表示达到终点 score1 += 10000; } //对b2进行判断最终得分 var pos0 = this["c"+curplayer].pos+b2.values; if (pos0<>titleMaxnum+1) { if (pos0>titleMaxnum+1) { var p0 = (titleMaxnum+1)-this["c"+curplayer].pos; pos0 = (titleMaxnum+1)-(b2.values-p0); delete p0; } //是否存在对手 score2 += checkhit_pos(pos0); //退1 if (this["t"+pos0].types == 2) { score2 -= 1; score2 += checkhit_pos(pos0-1); if (this["t"+(pos0-1)].types == 6) { score2 -= 5; } else if (this["t"+(pos0-1)].types == 7) { score2 += checksd_pos(pos0-1); } else if (this["t"+(pos0-1)].types == 8) { score2 -= this["c"+curplayer].pos; } } else if (this["t"+pos0].types == 3) { score2 -= 3; score2 += checkhit_pos(pos0-3); if (this["t"+(pos0-3)].types == 6) { score2 -= 5; } else if (this["t"+(pos0-3)].types == 7) { score2 += checksd_pos(pos0-3); } else if (this["t"+(pos0-3)].types == 8) { score2 -= this["c"+curplayer].pos; } } else if (this["t"+pos0].types == 4) { score2 += 1; score2 += checkhit_pos(pos0+1); if (this["t"+(pos0+1)].types == 6) { score2 -= 5; } else if (this["t"+(pos0+1)].types == 7) { score2 += checksd_pos(pos0+1); } else if (this["t"+(pos0+1)].types == 8) { score2 -= this["c"+curplayer].pos; } } else if (this["t"+pos0].types == 5) { score2 += 3; score2 += checkhit_pos(pos0+3); if (this["t"+(pos0+3)].types == 6) { score2 -= 5; } else if (this["t"+(pos0+3)].types == 7) { score2 += checksd_pos(pos0+3); } else if (this["t"+(pos0+3)].types == 8) { score2 -= this["c"+curplayer].pos; } } else if (this["t"+pos0].types == 6) { score2 -= 5; } else if (this["t"+pos0].types == 7) { score2 += checksd_pos(pos0); } else if (this["t"+pos0].types == 8) { score2 -= this["c"+curplayer].pos; } } else { //表示达到终点 score2 += 10000; } //判断分数大小,并决定采用那个筛子 trace("______________利用ai-2结果如下_______________"); trace("第1个筛子的效果分:"+score1); trace("第2个筛子的效果分:"+score2); if (score1>=score2) { trace("采用第1个筛子进行命令"); trace("______________利用ai-2结果如上_______________"); curstep = b1.values; movecast(this["c"+curplayer], curstep); } else { trace("采用第2个筛子进行命令"); trace("______________利用ai-2结果如上_______________"); curstep = b2.values; movecast(this["c"+curplayer], curstep); }