没注意代码重用,就是尽兴而写,逻辑都放在文档类了。 AI不是太智能,有胜负以及无路可走的判断。,斜线就赢。敌人是中心没占,优先占中心。然后就随机处理了。代码都没什么难的,看到效果就可以想到算法是怎么样的了。 /** * @author 熊俊程 * @copyright * @document * @history * create 09.10.27 * 对角棋 **/ package com { import com.xjc.AIChess; import com.xjc.Grid; import com.xjc.MyChess; import flash.display.Sprite; import flash.events.MouseEvent; import flash.text.TextField; public class TestDoc extends Sprite { private var gridArr : Array; private var _grid00 : Grid; private var _grid01 : Grid; private var _grid02 : Grid; private var _grid10 : Grid; private var _grid11 : Grid; private var _grid12 : Grid; private var _grid20 : Grid; private var _grid21 : Grid; private var _grid22 : Grid; private var _result_txt : TextField; private var myChessArr : Array; private var aiChessArr : Array; private var lastChess : MyChess; private var curIndex : int; private var isDown : Boolean; private var isYourTurn : Boolean; public function TestDoc() { _grid00 = this.getChildByName("grid00") as Grid; _grid01 = this.getChildByName("grid01") as Grid; _grid02 = this.getChildByName("grid02") as Grid; _grid10 = this.getChildByName("grid10") as Grid; _grid11 = this.getChildByName("grid11") as Grid; _grid12 = this.getChildByName("grid12") as Grid; _grid20 = this.getChildByName("grid20") as Grid; _grid21 = this.getChildByName("grid21") as Grid; _grid22 = this.getChildByName("grid22") as Grid; _result_txt = this.getChildByName("result_txt") as TextField; startGame(); } private function startGame() : void { myChessArr = new Array(); aiChessArr = new Array(); gridArr = new Array(); initData(); isYourTurn = true; _result_txt.visible = false; gridArr.push(_grid00); gridArr.push(_grid01); gridArr.push(_grid02); gridArr.push(_grid10); gridArr.push(_grid11); gridArr.push(_grid12); gridArr.push(_grid20); gridArr.push(_grid21); gridArr.push(_grid22); layPosition(0 , 0 , 1); layPosition(0 , 1 , 1); layPosition(0 , 2 , 1); layPosition(2 , 0 , 0); layPosition(2 , 1 , 0); layPosition(2 , 2 , 0); stage.addEventListener(MouseEvent.MOUSE_MOVE , __move); stage.addEventListener(MouseEvent.MOUSE_UP , __up); } private function layPosition(x : int , y : int , flag : int = 0) : void { if(flag == 0) { var myChess : MyChess = new MyChess(); myChess.x = this["_grid" + String(x) + String(y)].x; myChess.y = this["_grid" + String(x) + String(y)].y; myChess.name = "my" + String(x) + String(y); addChild(myChess); myChess.addEventListener(MouseEvent.MOUSE_DOWN , __down); myChessArr.push(myChess); }else if(flag == 1) { var aiChess : AIChess = new AIChess(); aiChess.x = this["_grid" + String(x) + String(y)].x; aiChess.y = this["_grid" + String(x) + String(y)].y; aiChess.name = "ai" + String(x) + String(y); addChild(aiChess); aiChessArr.push(aiChess); } } private function __down(e : MouseEvent) : void { if(!isYourTurn) return; isDown = true; if(curIndex != -1) { judgePosion(); return; } for(var i in myChessArr) { if((myChessArr[i] as MyChess).isHitPoint(this.mouseX , this.mouseY)) { lastChess.name = (myChessArr[i] as MyChess).name; lastChess.x = (myChessArr[i] as MyChess).x; lastChess.y = (myChessArr[i] as MyChess).y; curIndex = i; } } } private function __move(e : MouseEvent) : void { if(isDown) { (myChessArr[curIndex] as MyChess).x = e.stageX - (myChessArr[curIndex] as MyChess).width/2; (myChessArr[curIndex] as MyChess).y = e.stageY - (myChessArr[curIndex] as MyChess).height/2; e.updateAfterEvent(); } } private function __up(e : MouseEvent) : void { } private function judgePosion() : void { if(isHitAi()) { (myChessArr[curIndex] as MyChess).x = lastChess.x; (myChessArr[curIndex] as MyChess).y = lastChess.y; initData(); return; } var isBreak : Boolean = false; for(var j in gridArr) { if((gridArr[j] as Grid).isHit(myChessArr[curIndex] as MyChess)) { if(int(lastChess.name.substr(2 , 1)) == 1 && int(lastChess.name.substr(3 , 1)) == 1 && !indexMeToPos(int((gridArr[j] as Grid).name.substr(4 , 1)), int((gridArr[j] as Grid).name.substr(5 , 1)))) { updateMe(gridArr[j]); initData(); return; } if(int((myChessArr[curIndex] as MyChess).name.substr(2 , 1)) == int((gridArr[j] as Grid).name.substr(4 , 1)) && !indexMeToPos(int((gridArr[j] as Grid).name.substr(4 , 1)), int((gridArr[j] as Grid).name.substr(5 , 1)))) { if(Math.abs(int((myChessArr[curIndex] as MyChess).name.substr(3 , 1)) - int((gridArr[j] as Grid).name.substr(5 , 1))) == 1) { updateMe(gridArr[j]); initData(); isBreak = true; } }else if(int((myChessArr[curIndex] as MyChess).name.substr(3 , 1)) == int((gridArr[j] as Grid).name.substr(5 , 1)) && !indexMeToPos(int((gridArr[j] as Grid).name.substr(4 , 1)), int((gridArr[j] as Grid).name.substr(5 , 1)))) { if(Math.abs(int((myChessArr[curIndex] as MyChess).name.substr(2 , 1)) - int((gridArr[j] as Grid).name.substr(4 , 1))) == 1) { updateMe(gridArr[j]); initData(); isBreak = true; } }else if(int((gridArr[j] as Grid).name.substr(4 , 1)) == 1 && int((gridArr[j] as Grid).name.substr(5 , 1)) == 1 && !indexMeToPos(int((gridArr[j] as Grid).name.substr(4 , 1)), int((gridArr[j] as Grid).name.substr(5 , 1)))) { updateMe(gridArr[j]); initData(); isBreak = true; } break; } } if(!isBreak) { (myChessArr[curIndex] as MyChess).x = lastChess.x; (myChessArr[curIndex] as MyChess).y = lastChess.y; initData(); } } private function isHitAi() : Boolean { var b : Boolean = false; for(var i in aiChessArr) { if((aiChessArr[i] as AIChess).isHit(myChessArr[curIndex] as MyChess)) { b = true; break; } } return b; } private function initData() : void { curIndex = -1; isDown = false; lastChess = new MyChess(); } private function updateMe(chess : Grid) : void { (myChessArr[curIndex] as MyChess).x = chess.x; (myChessArr[curIndex] as MyChess).y = chess.y; (myChessArr[curIndex] as MyChess).name = "my" + int(chess.name.substr(4,1)) + int(chess.name.substr(5,1)); isYourTurn = false; if(isWin()) { _result_txt.text = "you win"; _result_txt.visible = true; }else { goAi(); } } private function goAi() : void { var isCenter : Boolean = false; for(var j in myChessArr) { if(int((myChessArr[j] as MyChess).name.substr(2 , 1)) == 1 && int((myChessArr[j] as MyChess).name.substr(3 , 1)) == 1) { isCenter = true; break; } } var isSelfCenter : Boolean = false; for(var k in aiChessArr) { if(int(aiChessArr[k].name.substr(2 , 1)) == 1 && int(aiChessArr[k].name.substr(3 , 1)) == 1) { isSelfCenter = true; break; } } var isNoWay : Boolean = true; var randAi : int = Math.floor(Math.random() * 3); if(!isCenter && !isSelfCenter) { isNoWay = false; updateAi(randAi ,this["_grid11"]); }else { var ai : AIChess = aiChessArr[randAi]; aiChessArr.splice(randAi,1); aiChessArr.unshift(ai); for(var i in aiChessArr) { var x : int = int((aiChessArr[i] as AIChess).name.substr(2 , 1)); var y : int = int((aiChessArr[i] as AIChess).name.substr(3 , 1)); if(x + 1 <= 2 && !indexToPos(x + 1 , y)) { isNoWay = false; updateAi(i ,this["_grid" + String(x + 1) + String(y)]); break; }else if(x - 1 >= 0 && !indexToPos(x-1 , y)) { isNoWay = false; updateAi(i ,this["_grid" + String(x - 1) + String(y)]); break; } else if(y + 1 <= 2 && !indexToPos(x , y+1)) { isNoWay = false; updateAi(i ,this["_grid" + String(x) + String(y + 1)]); break; } else if(y - 1 >= 0 && !indexToPos(x , y - 1)) { isNoWay = false; updateAi(i ,this["_grid" + String(x) + String(y - 1)]); break; } } } if(isNoWay) { _result_txt.text = "you win"; _result_txt.visible = true; } } private function indexToPos(x : int, y : int) : Boolean { var b : Boolean = false; for(var i in myChessArr) { if(int((myChessArr[i] as MyChess).name.substr(2 , 1)) == x && int((myChessArr[i] as MyChess).name.substr(3 , 1)) == y) { b = true; } } var isSelf : Boolean = false; for(var k in aiChessArr) { if(int(aiChessArr[k].name.substr(2 , 1)) == x && int(aiChessArr[k].name.substr(3 , 1)) == y) { isSelf = true; break; } } if(!b && !isSelf) { b = false; }else { b = true; } return b; } private function indexMeToPos(x : int , y : int) : Boolean { var b : Boolean = false; for(var j in myChessArr) { if(int((myChessArr[j] as MyChess).name.substr(2 , 1)) == x && int((myChessArr[j] as MyChess).name.substr(3 , 1)) == y) { b = true; break; } } return b; } private function updateAi(index : int , grid : Grid) : void { (aiChessArr[index] as AIChess).x = grid.x; (aiChessArr[index] as AIChess).y = grid.y; (aiChessArr[index] as AIChess).name = "ai" + int(grid.name.substr(4,1)) + int(grid.name.substr(5,1)); if(isAiWin()) { _result_txt.text = "you fail"; _result_txt.visible = true; }else { isYourTurn = true; } } private function isWin() : Boolean { var b : Boolean = false ; var xArr : Array = new Array(); var yArr : Array = new Array(); for(var i in myChessArr) { xArr.push(int((myChessArr[i] as MyChess).name.substr(2 , 1))); yArr.push(int((myChessArr[i] as MyChess).name.substr(3 , 1))); } xArr.sort(); yArr.sort(); if(indexMeToPos(1 , 1) && xArr[0] == 0 && xArr[1] == 1 && xArr[2] == 2 && yArr[0] == 0 && yArr[1] == 1 && yArr[2] == 2) { b = true; } return b; } private function isAiWin() : Boolean { var b : Boolean = false ; var xArr : Array = new Array(); var yArr : Array = new Array(); for(var i in aiChessArr) { xArr.push(int((aiChessArr[i] as AIChess).name.substr(2 , 1))); yArr.push(int((aiChessArr[i] as AIChess).name.substr(3 , 1))); } xArr.sort(); yArr.sort(); var isSelf : Boolean = false; for(var k in aiChessArr) { if(int(aiChessArr[k].name.substr(2 , 1)) == 1 && int(aiChessArr[k].name.substr(3 , 1)) == 1) { isSelf = true; break; } } if(isSelf && xArr[0] == 0 && xArr[1] == 1 && xArr[2] == 2 && yArr[0] == 0 && yArr[1] == 1 && yArr[2] == 2) { b = true; } return b; } } } 源文件 : http://download.csdn.net/source/1993994