private List<GamePieceMove> GetAddtionalMoves(GameBoard board, GamePieceMove bestMove) { List<GamePieceMove> moves = new List<GamePieceMove>(); List<GamePieceMove> bestMoves = new List<GamePieceMove>(); int bestBoardValue = board.EvaluateBoard(PlayerColours.Black); GameBoard nextBoard = new GameBoard(); nextBoard = board.CloneBoard(); nextBoard.ApplyMove(bestMove); if (bestMove.IsJump) moves.AddRange(board.GetAllPossibleJumpsForThisPiece(bestMove.MovingPiece)); foreach (GamePieceMove m in moves) { GameBoard currentBoard = new GameBoard(); currentBoard = nextBoard.CloneBoard(); currentBoard.ApplyMove(m); //currentBoard = MakeAddtionalMoves(currentBoard, m); if (currentBoard.EvaluateBoard(PlayerColours.Black) > bestBoardValue) bestMoves.Add(m); bestMoves.AddRange(GetAddtionalMoves(currentBoard, m)); board.UnDoMove(m); } board.UnDoMove(bestMove); return bestMoves; }
private GameBoard CreateCurrentBoard(GameBoard board, GamePieceMove move) { GameBoard currentBoard = new GameBoard(); currentBoard = board.CloneBoard(); currentBoard.ApplyMove(move); currentBoard = MakeAddtionalMoves(currentBoard, move).CloneBoard(); return currentBoard; }
public void MinMaxTest() { AI_Accessor target = new AI_Accessor(); GameBoard_Accessor board = CreateTestBoard(); GameBoard testBoard = new GameBoard(); testBoard = board.CloneBoard(); target.numberOfMovesAhead = 2; List<GamePieceMove> aiMoves = target.GetAIMoves(testBoard); foreach(GamePieceMove move in aiMoves) testBoard.ApplyMove(move); string expected = "\nB_B_B___\n___w____\n______B_\n_w______\n__b___b_\n________\n________\n___W_W__"; string actual = testBoard.ToString(); Assert.AreEqual(expected,actual); }
private int Max(GameBoard board, int step, ref int alpha, ref int beta) { if (board.IsGameOver() || IsDepthReached(step)) return board.EvaluateBoard(PlayerColours.Black); else { int bestValue = 0; List<GamePieceMove> PossibleMoves = board.GetAllPossibleMoves(PlayerColours.Black); foreach (GamePieceMove move in PossibleMoves) { GameBoard currentBoard = new GameBoard(); currentBoard = board.CloneBoard(); currentBoard.ApplyMove(move); currentBoard = MakeAddtionalMoves(currentBoard, move).CloneBoard(); int currentValue = Min(currentBoard, step + 1, ref alpha, ref beta); board.UnDoMove(move); if (currentValue > bestValue) { bestValue = currentValue; alpha = bestValue; } if (beta >= alpha) return bestValue; } return bestValue; } }
private GameBoard MakeAddtionalMoves(GameBoard board, GamePieceMove move) { List<GamePieceMove> addtionalMoves = new List<GamePieceMove>(); GameBoard bestBoard = board.CloneBoard(); int bestBoardValue = bestBoard.EvaluateBoard(PlayerColours.Black); if (move.IsJump && board.IsThereASecondJump(move)) addtionalMoves.AddRange(board.GetAllPossibleJumpsForThisPiece(move.MovingPiece)); foreach (GamePieceMove m in addtionalMoves) { GameBoard currentBoard = new GameBoard(); currentBoard = board.CloneBoard(); currentBoard.ApplyMove(m); currentBoard = MakeAddtionalMoves(currentBoard, m); if (currentBoard.EvaluateBoard(PlayerColours.Black) > bestBoardValue) bestBoard = currentBoard; board.UnDoMove(m); } return bestBoard; }