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; }
public void ApplyNormalWhitePieceMoveTest() { GameBoard_Accessor target = CreateTestBoard(); GamePieceMove move = new GamePieceMove(target.GameBoardSpaces[1, 1], 2, 0); string expected = "\n__B_B___\n___w____\nw_____B_\n_w_w____\n__b_b_b_\n________\n________\n___W_W__"; target.ApplyMove(move); string actual = target.ToString(); Assert.AreEqual(expected, actual); }
public void ApplyMove(GamePieceMove move) { if(IsMoveLegal(move)) { if (move.IsJump) RemoveJumpedPiece(move); gameBoardSpaces[move.NewHorizontalPostion, move.NewVerticalPostion] = move.MovingPiece; gameBoardSpaces[move.MovingPiece.HorizontalPostion, move.MovingPiece.VerticalPostion] = null; gameBoardSpaces[move.NewHorizontalPostion, move.NewVerticalPostion].HorizontalPostion = move.NewHorizontalPostion; gameBoardSpaces[move.NewHorizontalPostion, move.NewVerticalPostion].VerticalPostion = move.NewVerticalPostion; if (IsPieceToBeCrowned(move.MovingPiece)) move.MovingPiece.IsKing = true; } }
public void MiniMax(GameBoard board) { startTime = System.DateTime.Now; maxCalls = 0; minCalls = 0; int bestValue = 0; List<GamePieceMove> PossibleMoves = board.GetAllPossibleMoves(PlayerColours.Black); foreach (GamePieceMove move in PossibleMoves) { int currentValue = Min(CreateCurrentBoard(board, move), FIRSTSTEP, ref alpha, ref beta); board.UnDoMove(move); if (currentValue > bestValue) { bestMove = move; bestValue = currentValue; } } endTime = System.DateTime.Now; }
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; }
public Boolean IsThereASecondJump(GamePieceMove move) { if (GetAllPossibleJumpsForThisPiece(move.MovingPiece).Count > 0) return true; else return false; }
public Boolean IsMoveLegal(GamePieceMove move) { if (IsNewLocationAnEmptySquare(move)) { if (move.IsNewLocationLegal(move.MovingPiece)) { if (move.IsJump && IsOpponentPieceInNextSquare(move)) return true; else if (!move.IsJump) return true; else return false; } else return false; } else return false; }
public List<GamePieceMove> GetAllPossibleNormalMovesForThisPiece(DraughtPiece piece) { List<GamePieceMove> moves = new List<GamePieceMove>(); GamePieceMove move; move = new GamePieceMove(piece, piece.HorizontalPostion - 1, piece.VerticalPostion - 1); if (IsMoveLegal(move)) moves.Add(move); move = new GamePieceMove(piece, piece.HorizontalPostion - 1, piece.VerticalPostion + 1); if (IsMoveLegal(move)) moves.Add(move); move = new GamePieceMove(piece, piece.HorizontalPostion + 1, piece.VerticalPostion - 1); if (IsMoveLegal(move)) moves.Add(move); move = new GamePieceMove(piece, piece.HorizontalPostion + 1, piece.VerticalPostion + 1); if (IsMoveLegal(move)) moves.Add(move); return moves; }
public void IsMoveIllegalForWhitePieceMovingToTheBottomLeftTest() { GameBoard_Accessor target = CreateTestBoard(); GamePieceMove move = new GamePieceMove(target.GameBoardSpaces[3, 1], 2, 2); bool expected = false; bool actual = target.IsMoveLegal(move); Assert.AreEqual(expected, actual); }
public List<GamePieceMove> GetAllPossibleJumpsForThisPiece(DraughtPiece piece) { List<GamePieceMove> jumps = new List<GamePieceMove>(); GamePieceMove jump; jump = new GamePieceMove(piece, piece.HorizontalPostion - 2, piece.VerticalPostion - 2); if (IsMoveLegal(jump)) jumps.Add(jump); jump = new GamePieceMove(piece, piece.HorizontalPostion - 2, piece.VerticalPostion + 2); if (IsMoveLegal(jump)) jumps.Add(jump); jump = new GamePieceMove(piece, piece.HorizontalPostion + 2, piece.VerticalPostion - 2); if (IsMoveLegal(jump)) jumps.Add(jump); jump = new GamePieceMove(piece, piece.HorizontalPostion + 2, piece.VerticalPostion + 2); if (IsMoveLegal(jump)) jumps.Add(jump); return jumps; }
private bool IsNewLocationAnEmptySquare(GamePieceMove move) { if (IsLocationInsideOfBoard(move)) { if (gameBoardSpaces[move.NewHorizontalPostion, move.NewVerticalPostion] == null) return true; else return false; } else return false; }
private Boolean IsPieceToBeUnCrowned(GamePieceMove move) { if (!move.IsKingBeforeMove && move.IsKingAfterMove) return true; else return false; }
public void IsOpponentPieceInSquareToTheTopLeftTest() { GameBoard_Accessor target = CreateTestBoard(); GamePieceMove move = new GamePieceMove(target.GameBoardSpaces[4, 2], 3, 1); bool expected = true; bool actual = target.IsOpponentPieceInNextSquare(move); Assert.AreEqual(expected, actual); }
public void IsOpponentPieceNotInSquareToTheTopRightTest() { GameBoard_Accessor target = CreateTestBoard(); GamePieceMove move = new GamePieceMove(target.GameBoardSpaces[1, 1], 2, 0); bool expected = false; bool actual = target.IsOpponentPieceInNextSquare(move); Assert.AreEqual(expected, actual); }
public void IsNewLocationAnEmptySquareWhenAnEmptySquareIsSelectedTest() { GameBoard_Accessor target = CreateTestBoard(); GamePieceMove move = new GamePieceMove(target.GameBoardSpaces[1, 1], 2, 0); bool expected = true; bool actual = target.IsNewLocationAnEmptySquare(move); Assert.AreEqual(expected, actual); }
public void IsMoveLegalForWhitePieceMovingToTheTopRightTest() { GameBoard_Accessor target = CreateTestBoard(); GamePieceMove move = new GamePieceMove(target.GameBoardSpaces[1, 1], 2, 0); bool expected = true; bool actual = target.IsMoveLegal(move); Assert.AreEqual(expected, actual); }
public void IsMoveLegalForBlackPieceMovingToTheTopLeftTest() { GameBoard_Accessor target = CreateTestBoard(); GamePieceMove move = new GamePieceMove(target.GameBoardSpaces[4, 6], 3, 5); bool expected = true; bool actual = target.IsMoveLegal(move); Assert.AreEqual(expected, actual); }
public void UnDoMove(GamePieceMove move) { GameBoardSpaces[move.NewHorizontalPostion, move.NewVerticalPostion] = null; GameBoardSpaces[move.OriginalHorizontalPostion, move.OriginalVerticalPostion] = move.MovingPiece; move.MovingPiece.HorizontalPostion = move.OriginalHorizontalPostion; move.MovingPiece.VerticalPostion = move.OriginalVerticalPostion; if (move.IsJump) ReplaceJumpedPiece(move); if (IsPieceToBeUnCrowned(move)) move.MovingPiece.IsKing = false; }
private void ReplaceJumpedPiece(GamePieceMove move) { gameBoardSpaces[move.JumpedPiece.HorizontalPostion, move.JumpedPiece.VerticalPostion] = move.JumpedPiece; }
private bool IsLocationInsideOfBoard(GamePieceMove move) { if (move.NewHorizontalPostion >= 0 && move.NewHorizontalPostion <= 7 && move.NewVerticalPostion >= 0 && move.NewVerticalPostion <= 7) return true; else return false; }
public void IsMoveIllegalForBlackPieceMovingToTheTopRightTest() { GameBoard_Accessor target = CreateTestBoard(); GamePieceMove move = new GamePieceMove(target.GameBoardSpaces[4, 2], 5, 1); bool expected = false; bool actual = target.IsMoveLegal(move); Assert.AreEqual(expected, actual); }
private bool IsOpponentPieceInNextSquare(GamePieceMove move) { switch(move.MoveDirection) { case MoveDirections.TopLeft: if (IsOpponentToTheTopLeft(move.MovingPiece)) return true; break; case MoveDirections.BottomLeft: if (IsOpponentToTheBottomLeft(move.MovingPiece)) return true; break; case MoveDirections.TopRight: if (IsOpponentToTheTopRight(move.MovingPiece)) return true; break; case MoveDirections.BottomRight: if (IsOpponentToTheBottomRight(move.MovingPiece)) return true; break; default: return false; } return false; }
public void IsPieceToBeUnCrownedIfPieceWasNotKingBeforeMoveButIsAfterTest() { GameBoard_Accessor target = CreateTestBoard(); GamePieceMove move = new GamePieceMove(target.GameBoardSpaces[1, 1], 0, 0); move.IsKingBeforeMove = false; move.IsKingAfterMove = true; bool expected = true; bool actual = target.IsPieceToBeUnCrowned(move); Assert.AreEqual(expected, actual); }
private void RemoveJumpedPiece(GamePieceMove move) { switch (move.MoveDirection) { case MoveDirections.TopLeft: move.JumpedPiece = gameBoardSpaces[move.MovingPiece.HorizontalPostion - 1, move.MovingPiece.VerticalPostion - 1]; gameBoardSpaces[move.MovingPiece.HorizontalPostion - 1, move.MovingPiece.VerticalPostion - 1] = null; break; case MoveDirections.BottomLeft: move.JumpedPiece = gameBoardSpaces[move.MovingPiece.HorizontalPostion - 1, move.MovingPiece.VerticalPostion + 1]; gameBoardSpaces[move.MovingPiece.HorizontalPostion - 1, move.MovingPiece.VerticalPostion + 1] = null; break; case MoveDirections.TopRight: move.JumpedPiece = gameBoardSpaces[move.MovingPiece.HorizontalPostion + 1, move.MovingPiece.VerticalPostion - 1]; gameBoardSpaces[move.MovingPiece.HorizontalPostion + 1, move.MovingPiece.VerticalPostion - 1] = null; break; case MoveDirections.BottomRight: move.JumpedPiece = gameBoardSpaces[move.MovingPiece.HorizontalPostion + 1, move.MovingPiece.VerticalPostion + 1]; gameBoardSpaces[move.MovingPiece.HorizontalPostion + 1, move.MovingPiece.VerticalPostion + 1] = null; break; } }
public void IsThereASecondJumpWhenThereIsTest() { GameBoard_Accessor target = CreateTestBoard(); GamePieceMove move = new GamePieceMove(target.GameBoardSpaces[4, 4], 2, 2); target.ApplyMove(move); bool expected = true; bool actual; actual = target.IsThereASecondJump(move); Assert.AreEqual(expected, actual); }
public void ReplaceJumpedPieceTest() { GameBoard_Accessor target = CreateTestBoard(); GamePieceMove move = new GamePieceMove(target.GameBoardSpaces[0, 2], 2, 0); DraughtPiece expected = target.GameBoardSpaces[1,1]; target.RemoveJumpedPiece(move); target.ReplaceJumpedPiece(move); DraughtPiece actual = target.GameBoardSpaces[1, 1]; Assert.AreEqual(expected, actual); }
private GameBoard CreateCurrentBoard(GameBoard board, GamePieceMove move) { GameBoard currentBoard = new GameBoard(); currentBoard = board.CloneBoard(); currentBoard.ApplyMove(move); currentBoard = MakeAddtionalMoves(currentBoard, move).CloneBoard(); return currentBoard; }
private Boolean MakeMove(XNAGameBoardPostion selectedSquare) { GamePieceMove move = new GamePieceMove(selectedPiece, selectedSquare.GameBoardSpace.X, selectedSquare.GameBoardSpace.Y); gameBoard.ApplyMove(move); if(move.IsJump) if(gameBoard.IsThereASecondJump(move)) return false; return true; }
private void MiniMax(GameBoard board) { int bestValue = 0; List<GamePieceMove> PossibleMoves = board.GetAllPossibleMoves(PlayerColours.Black); foreach (GamePieceMove move in PossibleMoves) { GameBoard currentBoard = board.CloneBoard(); currentBoard.ApplyMove(move); currentBoard = MakeAddtionalMoves(currentBoard, move).CloneBoard(); int currentValue = Min(currentBoard, FIRSTSTEP,ref alpha,ref beta); board.UnDoMove(move); if (currentValue > bestValue) { bestMove = move; bestValue = currentValue; } } }