//极小窗口算法 //blackclick bl = new blackclick(); // IEnumerator Robot(){ // yield return new WaitForSeconds (0.2f); // blackclick.threm(); // } int PrincipalVariation(int depth, int alpha, int beta) { int score; int Count, i; int type; int best; i = IsGameOver(Curposition, depth); if (i != 0) { return(i); } if (depth <= 0) { return(Eveluate(Curposition, (m_nMaxDepth - depth) % 2 != 0)); } Count = m.CreatePossibleMove(Curposition, depth, (m_nMaxDepth - depth) % 2 != 0); type = MakeMove(m.m_MoveList[depth, 0]); best = -PrincipalVariation(depth - 1, -beta, -alpha); UnMakeMove(m.m_MoveList[depth, 0], type); if (depth == m_nMaxDepth) { m_cmBestMove = m.m_MoveList [depth, i]; } for (i = 1; i < Count; i++) { // StartCoroutine(Robot); if (best < beta) { //如果不能被beta 剪枝 if (best < beta) { if (best > alpha) { alpha = best; } } type = MakeMove(m.m_MoveList[depth, i]); score = -PrincipalVariation(depth - 1, -alpha - 1, -alpha); if (score > alpha && score < beta) { best = -PrincipalVariation(depth - 1, -beta, -score); if (depth == m_nMaxDepth) { m_cmBestMove = m.m_MoveList[depth, i]; } } else if (score > best) { best = score; if (depth == m_nMaxDepth) { m_cmBestMove = m.m_MoveList[depth, i]; } } UnMakeMove(m.m_MoveList[depth, i], type); } } return(best); }
// 根据走法恢复棋盘 //move 是要恢复的走法 //nChessID是原棋盘上move位置的棋子类型 void UnMakeMove(Blackmove.CHESSMOVE move, int nChessID) { //将目标位置和棋子拷贝会原位 Curposition [move.From.y, move.From.x] = Curposition [move.To.y, move.To.x]; //恢复目标位置的棋子 Curposition [move.To.y, move.To.x] = nChessID; }
public void threm() { //str="对方正在思考"; if (ChessMove == false) { //print("yes"); //ChessChongzhi chzh = new ChessChongzhi(); //SearchEngine see = new SearchEngine(); //Blackmove mo = new Blackmove(); chere = see.SearchAGoodMove(board.chess); string s1 = ""; string s2 = ""; string s3 = ""; string s4 = ""; s1 = see.Itemfirname(chere); s2 = see.Itemsconname(chere); //print(s1+" "+s2); GameObject one = GameObject.Find(s1); GameObject two = GameObject.Find(s2); foreach (Transform child in one.transform) { s3 = child.name; //第一个象棋名字 } foreach (Transform child in two.transform) { s4 = child.name; //吃到的子的象棋名字 } if (s4 == "") { int a = board.chess [chere.From.y, chere.From.x]; int b = board.chess [chere.To.y, chere.To.x]; chzh.AddChess(ChessChongzhi.Count, chere.From.x, chere.From.y, chere.To.x, chere.To.y, false, a, b); IsMove(s3, two, chere.From.x, chere.From.y, chere.To.x, chere.To.y); renji.transform.localPosition = one.transform.localPosition; } else { int a = board.chess [chere.From.y, chere.From.x]; int b = board.chess [chere.To.y, chere.To.x]; chzh.AddChess(ChessChongzhi.Count, chere.From.x, chere.From.y, chere.To.x, chere.To.y, false, a, b); IsEat(s3, s4, chere.From.x, chere.From.y, chere.To.x, chere.To.y); renji.transform.localPosition = one.transform.localPosition; } RedName = null; BlackName = null; GameObject obj1 = GameObject.Find(s3); tog = obj1.GetComponent <UIToggle> (); tog.value = true; str = "红方走"; KingPosition.JiangJunCheck(); ChessMove = true; } }
//根据传入的走法 改变棋盘 //move是要进行的走法 //public int[,] CurPosition = new int[10, 9]; public int MakeMove(Blackmove.CHESSMOVE move) { int nChessID; nChessID = Curposition [move.To.y, move.To.x]; //取目标棋子 //把棋子移动到目标位置 Curposition [move.To.y, move.To.x] = Curposition [move.From.y, move.From.x]; //将原位清空 Curposition [move.From.y, move.From.x] = NOCHESS; //清空原来位置 //shuzi (Curposition); return(nChessID); //返回被吃掉的棋子 }
public string Itemfirname(Blackmove.CHESSMOVE move) //得到开始位置gameobject的对象名字 { string s3 = ""; for (int i = 1; i <= 90; i++) { obj = GameObject.Find("item" + i.ToString()); int x = System.Convert.ToInt32((obj.transform.localPosition.x) / 130); int y = System.Convert.ToInt32(Mathf.Abs((obj.transform.localPosition.y) / 128)); if (x == move.From.x && y == move.From.y) { s3 = obj.name; } } return(s3); }
//负极大值函数 //depth表示节点离叶子节点的层数 int NegaMax(int depth) { int current = -20000; int score; int Count, i; int type; i = IsGameOver(Curposition, depth); //检查棋局是否结束 if (i != 0) { return(i); //棋局没有结束,返回极大极小值 } if (depth <= 0) { return(Eveluate(Curposition, ((m_nMaxDepth - depth) % 2) != 0)); // } //返回估值 //列举出当前局面下一步所有可能的走法 Count = m.CreatePossibleMove(Curposition, depth, ((m_nMaxDepth - depth) % 2) != 0); // for (i = 0; i < Count; i++) { Blackmove.CHESSMOVE a = m.m_MoveList[depth, i]; //根据走法产生新局面 type = MakeMove(a); //print (a.From.x+","+a.From.y+"--"+a.To.x+","+a.To.y); //递归调用负极大值搜索下一层节点 score = -NegaMax(depth - 1); //Debug.Log("当前搜索层数是"+m_nSearchDepth+"层 "+board.chess[a.From.y,a.From.x]+" "+a.From.x+","+a.From.y+"--"+a.To.x+","+a.To.y+":"+score); //恢复当前局面 UnMakeMove(m.m_MoveList[depth, i], type); if (score > current) //如果score大于一直的最大值 { current = score; if (depth == m_nMaxDepth) { //靠近根部保存最佳走法 m_cmBestMove = m.m_MoveList[depth, i]; } } } return(current); //返回极大值 }
public int Falphabeta(int depth, int alpha, int beta) { int type; int score; int Count, i; int current = -20000; i = IsGameOver(Curposition, depth); if (i != 0) { return(i); } if (depth <= 0) { return(Eveluate(Curposition, (m_nMaxDepth - depth) % 2 != 0)); } Count = m.CreatePossibleMove(Curposition, depth, (m_nMaxDepth - depth) % 2 != 0); for (i = 0; i < Count; i++) { //Blackmove.CHESSMOVE a = m.m_MoveList[depth,i]; type = MakeMove(m.m_MoveList[depth, i]); score = -Falphabeta(depth - 1, -beta, -alpha); //Debug.Log("当前搜索层数是"+m_nSearchDepth+"层 "+board.chess[a.From.y,a.From.x]+" "+a.From.x+","+a.From.y+"--"+a.To.x+","+a.To.y+":"+score); UnMakeMove(m.m_MoveList[depth, i], type); if (score > current) { current = score; if (depth == m_nMaxDepth) { m_cmBestMove = m.m_MoveList[depth, i]; } if (score >= alpha) { alpha = score; } if (score >= beta) { break; } } } //CeShi+="\n"; return(current); }