private int AlphBeta(int depth, int alpha, int beta) { int score, count, willKillKing, chessID; willKillKing = IsGameOver(unrealBoard, depth); if (willKillKing != 0) { return(willKillKing); } if (depth <= 0) { return(Eveluate(unrealBoard, (searchDepth - depth) % 2 != 0)); } count = gameManager.movingOfChess.CreatePossibleMove(unrealBoard, depth, (searchDepth - depth) % 2 != 0); for (int i = 0; i < count; i++) { chessID = MakeMove(gameManager.movingOfChess.moveList[depth, i]); score = -AlphBeta(depth - 1, -beta, -alpha); UnMakeMove(gameManager.movingOfChess.moveList[depth, i], chessID); if (score >= beta) { return(beta); } if (score > alpha) { alpha = score; if (depth == searchDepth) { bestStep = gameManager.movingOfChess.moveList[depth, i]; } } } return(alpha); }
private int NegaMax(int depth) { int best = -20000; int score; int count; int willKillKing; int chessID; willKillKing = IsGameOver(unrealBoard, depth); if (willKillKing != 0) { return(willKillKing); } if (depth <= 0) { return(Eveluate(unrealBoard, (searchDepth - depth) % 2 != 0)); } count = gameManager.movingOfChess.CreatePossibleMove(unrealBoard, depth, (searchDepth - depth) % 2 != 0); for (int i = 0; i < count; i++) { chessID = MakeMove(gameManager.movingOfChess.moveList[depth, i]); score = -NegaMax(depth - 1); UnMakeMove(gameManager.movingOfChess.moveList[depth, i], chessID); if (score > best) { best = score; if (depth == searchDepth) { bestStep = gameManager.movingOfChess.moveList[depth, i]; } } } return(best); }
/// <summary> /// AIMove /// </summary> public void HaveGoodMove(ChessReseting.Chess aChessStep) { if (aChessStep.chessTwo == null) { gameManager.chessReseting.AddChess(gameManager.chessReseting.resetCount, aChessStep.gridOne.GetComponent <ChessOrGrid>().xIndex, aChessStep.gridOne.GetComponent <ChessOrGrid>().yIndex, aChessStep.gridTwo.GetComponent <ChessOrGrid>().xIndex, aChessStep.gridTwo.GetComponent <ChessOrGrid>().yIndex, aChessStep.chessOneID, aChessStep.chessTwoID); IsMove(aChessStep.chessOne, aChessStep.gridTwo, aChessStep.gridOne.GetComponent <ChessOrGrid>().xIndex, aChessStep.gridOne.GetComponent <ChessOrGrid>().yIndex, aChessStep.gridTwo.GetComponent <ChessOrGrid>().xIndex, aChessStep.gridTwo.GetComponent <ChessOrGrid>().yIndex); } else { gameManager.chessReseting.AddChess(gameManager.chessReseting.resetCount, aChessStep.gridOne.GetComponent <ChessOrGrid>().xIndex, aChessStep.gridOne.GetComponent <ChessOrGrid>().yIndex, aChessStep.gridTwo.GetComponent <ChessOrGrid>().xIndex, aChessStep.gridTwo.GetComponent <ChessOrGrid>().yIndex, aChessStep.chessOneID, aChessStep.chessTwoID); IsEat(aChessStep.chessOne, aChessStep.chessTwo, aChessStep.gridOne.GetComponent <ChessOrGrid>().xIndex, aChessStep.gridOne.GetComponent <ChessOrGrid>().yIndex, aChessStep.gridTwo.GetComponent <ChessOrGrid>().xIndex, aChessStep.gridTwo.GetComponent <ChessOrGrid>().yIndex); } }
private int MakeMove(ChessReseting.Chess move) { int chessID = 0; chessID = unrealBoard[move.to.x, move.to.y]; unrealBoard[move.to.x, move.to.y] = unrealBoard[move.from.x, move.from.y]; unrealBoard[move.from.x, move.from.y] = 0; return(chessID); }
public void AddHistoryScore(ChessReseting.Chess move, int depth) { if (historyDic.TryGetValue(move, out int score)) { historyDic[move] += 2 << depth; } else { historyDic.Add(move, 2 << depth); } }
private int PrincipalVariation(int depth, int alpha, int beta) { int score, count, willKillKing, chessID; int best; willKillKing = IsGameOver(unrealBoard, depth); if (willKillKing != 0) { return(willKillKing); } if (depth <= 0) { return(Eveluate(unrealBoard, ((searchDepth - depth) % 2) != 0)); } count = gameManager.movingOfChess.CreatePossibleMove(unrealBoard, depth, ((searchDepth - depth) % 2) != 0); chessID = MakeMove(gameManager.movingOfChess.moveList[depth, 0]); best = -PrincipalVariation(depth - 1, -beta, -alpha); UnMakeMove(gameManager.movingOfChess.moveList[depth, 0], chessID); if (depth == searchDepth) { bestStep = gameManager.movingOfChess.moveList[depth, 0]; } for (int i = 0; i < count; i++) { if (best < beta) { if (best > alpha) { alpha = best; } chessID = MakeMove(gameManager.movingOfChess.moveList[depth, i]); score = -PrincipalVariation(depth - 1, -alpha - 1, -alpha); if (score > alpha && score < beta) { best = -PrincipalVariation(depth - 1, -beta, -score); if (depth == searchDepth) { bestStep = gameManager.movingOfChess.moveList[depth, i]; } } else if (score > best) { best = score; if (depth == searchDepth) { bestStep = gameManager.movingOfChess.moveList[depth, i]; } } UnMakeMove(gameManager.movingOfChess.moveList[depth, i], chessID); } } return(best); }
private void UnMakeMove(ChessReseting.Chess move, int chessID) { unrealBoard[move.from.x, move.from.y] = unrealBoard[move.to.x, move.to.y]; unrealBoard[move.to.x, move.to.y] = chessID; }
private void MergeSort(ChessReseting.Chess[,] move, int count, int depth) { ChessReseting.Chess[,] temp = new ChessReseting.Chess[8, 80]; Sort(move, 0, count, temp, depth); }
private int GetHistoryScore(ChessReseting.Chess move) { historyDic.TryGetValue(move, out int score); return(score); }