【正文】
][]。 m_curPosition[][] = 。 return chessID。 } //判斷游戲時(shí)候結(jié)束 public static int isGameOver(int position[][], int depth) { int i, j。 boolean redLive = false, blackLive = false。 for(i = 0。 i 3。 ++i) for(j = 3。 j 6。 ++j) { if(position[i][j] == 1) blackLive = true。 } for(i = 7。 i 10。 ++i) for(j = 3。 j 6。 ++j) { if(position[i][j] == 8) redLive = true。 } i = (m_maxDepth depth + 1 )%2。//取當(dāng)前奇偶標(biāo)志 if(!redLive)//紅將不在了 { if(i != 0) return 19990 + depth。 else return 19990depth。 } if(!blackLive) if(0 != i) return 19990depth。 else return 19990+depth。 return 0。//兩將都在,返回0 } //取消移動(dòng) public static void unMakeMove(ChessMove move, int chessID) { m_curPosition[][] = m_curPosition[][]。 m_curPosition[][] = chessID。 } }package 。public class NegamaxEngine extends SearchEngine{ public NegamaxEngine(){ } public static ChessMove Search(int[][] position){ searchAgoodMove(position)。 return bestMove。 } public static void searchAgoodMove(int[][] position) { m_maxDepth = m_searchDepth。 int i, j。 for(i = 0。 i 10。 ++i) for(j = 0。 j 9。 ++j) m_curPosition[i][j] = position[i][j]。 negaMax(m_maxDepth)。 //調(diào)用負(fù)極大值搜索引擎 try { (4000)。 } catch (InterruptedException e) { // TODO Autogenerated catch block ()。 } makeMove(bestMove)。 } //負(fù)極大值搜索引擎 public static int negaMax(int depth) { int current = 20000。 int score = 0。 int count = 0,i。 int type。 i = isGameOver(m_curPosition, depth)。 if(i != 0)//不等于0表示某方的王不存在了,直接返回 { return i。 } boolean side。 if((m_maxDepthdepth)%2 == 1) side = true。 else side = false。 if(depth = 0){//搜索到葉子節(jié)點(diǎn),返回估值信息 return (m_curPosition,side)。 } //生產(chǎn)所有可能的走法, count = (m_curPosition, depth,side)。 for(i = 0。 i count 。 ++i) { //嘗試每種走法 type = makeMove([depth][i])。 //返回此種走法的估值分?jǐn)?shù) score = negaMax(depth1)。 //取消上次走法 unMakeMove([depth][i],type)。 //若當(dāng)前走法更優(yōu),則記錄下來 if(score current) { current = score。 //如果到了根節(jié)點(diǎn)S if(depth == m_maxDepth) {//記錄最優(yōu)的走棋,把它保存到m_bestMove中 bestMove = [depth][i]。 } } } return current。 }}package 。public class ConstData { public final static int NOCHESS = 0。 //無子 //黑方 public final static int B_KING = 1。 //黑帥 public final static int B_CAR = 2。 //黑車 public final static int B_HORSE = 3。 //黑馬 public final static int B_CANON = 4。 //黑炮 public final static int B_BISHOP = 5。 //黑士 public final static int B_ELEPHANT = 6。 //黑象 public final static int B_PAWN = 7。 //黑兵 public final static int B_BEGIN = 1。 public final static int B_END = 7。 //紅方 public final static int R_KING = 8。 //紅將 public final static int R_CAR = 9。 //紅車 public final static int R_HORSE = 10。 //紅馬 public final static int R_CANON = 11。 //紅炮 public final static int R_BISHOP = 12。 //紅仕 public final static int R_ELEPHANT = 13。 //紅相 public final static int R_PAWN = 14。 //紅卒 public final static int R_BEGIN = 8。 public final static int R_END = 14。 //定義每種棋的價(jià)值 //兵100,士250,象250,馬350,炮350車500 public final static int BASEVALUE_PAWN = 100。 public final static int BASEVALUE_BISHOP = public final static int BASEVALUE_ELEPHANT = public final static int BASEVALUE_HORSE = 350。 public final static int BASEVALUE_CANON = 350。 public final static int BASEVALUE_CAR = 500。 public final static int BASEVALUE_KING = 10000。 //定義各種棋子的靈活性 //也就是每多一個(gè)可走位置應(yīng)加上的值 //兵15,士1,象1,車6,馬12,炮6,王0 public final static int FLEXIBLE_PAWN = 15。 public final static int FLEXIBLE_BISHOP = 1。 public final static int FLEXIBLE_ELEPHANT = 1。 public final static int FLEXIBLE_HORSE = 12。 public final static int FLEXIBLE_CANON = 6。 public final static int FLEXIBLE_CAR = 6。 public final static int FLEXIBLE_KING = 0。 //初始棋盤 public final static int INITCHESSBOARD[][]= { {2,3,6,5,1,5,6,3,2}, {0,0,0,0,0,0,0,0,0}, {0,4,0,0,0,0,0,4,0}, {7,0,7,0,7,0,7,0,7}, {0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0}, {14,0,14,0,14,0,14,0,14}, {0,11,0,0,0,0,0,11,0}, {0,0,0,0,0,0,0,0,0}, {9,10,13,12,8,12,13,10,9} }。 //判斷chessID是否是黑棋 public static boolean isBlack(int chessID) { return (chessID = B_BEGIN amp。amp。 chessID = B_END)。 } //判斷chessID是否是紅棋 public static boolean isRed(int chessID) { return (chessID = R_BEGIN amp。amp。 chessID = R_END)。 } //判斷chessID1和chessID2是否是同色 public static boolean isSameSide(int chessID1, int chessID2) { return ( (isBlack(chessID1)amp。amp。isBlack(chessID2))||(isRed