// Min part of Minimax with AB pruning private int MiniABPruning(PositionManager board, int depth, int a, int b) { if (depth <= 0) { return(board.GetValueOfBoard()); } int min = int.MaxValue; List <(int, int)> allPM = MoveManager.GetAllPossibleMoves(board, false, false); if (allPM.Count == 0) { return(board.GetValueOfBoard()); } foreach ((int, int)iMove in allPM) { PositionManager childBoard = new PositionManager(board); childBoard.MovePiece(iMove.Item1, iMove.Item2); min = Math.Min(min, MaxABPruning(childBoard, depth - 1, a, b)); b = Math.Min(b, min); if (b <= a) { break; } } return(min); }
// Minimax main function, should be the one to call in all cases // Essentialy the same as MinABPruning but store the actual move private (int, int) MiniMaxABPruning(int n) { (int, int)move = (-1, -1); int min = int.MaxValue; List <(int, int)> allPM = MoveManager.GetAllPossibleMoves(realBoard, false, false); foreach ((int, int)iMove in allPM) { PositionManager childBoard = new PositionManager(realBoard); childBoard.MovePiece(iMove.Item1, iMove.Item2); int score = MaxABPruning(childBoard, n, int.MinValue, int.MaxValue); if (score == min && (Random.Range(0, 2) == 0)) //add random to make the AI less predictable { move = iMove; } if (score < min) { min = score; move = iMove; } } if (min > 300) { return(-1, -1); } return(move); }
private (int, int) PlayRandom() { List <(int, int)> allPM = MoveManager.GetAllPossibleMoves(realBoard, false, false); (int, int)move = allPM[Random.Range(0, allPM.Count)]; realBoard.MovePiece(move.Item1, move.Item2); return(move); }