// Private Methods private void addSimpleMovesUpside(List <Move> i_MovesList, Board i_Board) { if (MoveValidator.IsInBorders(i_Board, this.Location.X + 1, this.Location.Y + 1) && i_Board.GameBoard[this.Location.X + 1, this.Location.Y + 1].CurrentPiece == null) { i_MovesList.Add(new Move(this.Location.Y, this.Location.X, this.Location.Y + 1, this.Location.X + 1)); } if (MoveValidator.IsInBorders(i_Board, this.Location.X + 1, this.Location.Y - 1) && i_Board.GameBoard[this.Location.X + 1, this.Location.Y - 1].CurrentPiece == null) { i_MovesList.Add(new Move(this.Location.Y, this.Location.X, this.Location.Y - 1, this.Location.X + 1)); } // Capture moves if (MoveValidator.IsInBorders(i_Board, this.Location.X + 2, this.Location.Y + 2) && i_Board.GameBoard[this.Location.X + 2, this.Location.Y + 2].CurrentPiece == null) { if (i_Board.GameBoard[this.Location.X + 1, this.Location.Y + 1].CurrentPiece != null && i_Board.GameBoard[this.Location.X + 1, this.Location.Y + 1].CurrentPiece.Side == ePlayerSide.Down) { i_MovesList.Add(new Move(this.Location.Y, this.Location.X, this.Location.Y + 2, this.Location.X + 2)); } } if (MoveValidator.IsInBorders(i_Board, this.Location.X + 2, this.Location.Y - 2) && i_Board.GameBoard[this.Location.X + 2, this.Location.Y - 2].CurrentPiece == null) { if (i_Board.GameBoard[this.Location.X + 1, this.Location.Y - 1].CurrentPiece != null && i_Board.GameBoard[this.Location.X + 1, this.Location.Y - 1].CurrentPiece.Side == ePlayerSide.Down) { i_MovesList.Add(new Move(this.Location.Y, this.Location.X, this.Location.Y - 2, this.Location.X + 2)); } } }
// Returns a list of all possible moves public List <Move> GetPossibleMovesList(Player i_Player, Board i_Board, ref bool capturePossible) { List <Move> movesList = new List <Move>(); if (MoveValidator.IsPieceHaveMoves(i_Board, this)) { addNonKingsMovesToList(i_Player, i_Board, movesList, ref capturePossible); if (this.IsKing) { addKingsMovesToList(i_Player, i_Board, movesList, ref capturePossible); } } return(movesList); }
// Returns a capture move from all possible moves of all pieces private Move findCaptureMove(Board i_Board, List <List <Move> > i_AllMovesList) { foreach (List <Move> list in i_AllMovesList) { foreach (Move move in list) { if (MoveValidator.IsCaptureMovePossible(this, i_Board, move)) { return(move); } } } return(null); }
private static bool isSimpleMovePossible(Player i_Player, Board i_Board, Move i_Move) { bool isPossible = false; Piece movingPiece = i_Board.GameBoard[i_Move.FromRow, i_Move.FromCol].CurrentPiece; if (movingPiece != null) { isPossible = isMoveSimpleDiagonalLine(i_Player.Side, i_Move); // if king- also check opposite direction if (movingPiece.IsKing) { ePlayerSide oppositeSide = MoveValidator.GetOtherSide(i_Player.Side); isPossible = isPossible || isMoveSimpleDiagonalLine(oppositeSide, i_Move); } } return(isPossible); }
private bool checkKingCapture(Board i_Board, int i_Direction, Move i_Move) { bool captureMove = false; if (MoveValidator.IsInBorders(i_Board, i_Move.ToRow, i_Move.ToCol)) { if (i_Board.GameBoard[i_Move.ToRow, i_Move.ToCol].CurrentPiece == null) { if (i_Board.GameBoard[i_Move.FromRow + 1, i_Move.FromCol + i_Direction].CurrentPiece != null) { if (i_Board.GameBoard[i_Move.FromRow + 1, i_Move.FromCol + i_Direction].CurrentPiece.Side == MoveValidator.GetOtherSide(this.Side)) { captureMove = true; } } } } return(captureMove); }
// Returns true if the given move is a capture move private bool checkCapture(Board i_Board, int i_Direction, Move i_Move) { // check if destinations is in board's borders if (MoveValidator.IsInBorders(i_Board, i_Move.ToRow, i_Move.ToCol)) { Piece pieceToBeCaptured = i_Board.GameBoard[(i_Move.FromRow + i_Move.ToRow) / 2, (i_Move.FromCol + i_Move.ToCol) / 2].CurrentPiece; // check destination is empty if (i_Board.GameBoard[i_Move.ToRow, i_Move.ToCol].CurrentPiece == null) { if (pieceToBeCaptured != null) { if (pieceToBeCaptured.r_Side == MoveValidator.GetOtherSide(this.Side)) { return(true); } } } } return(false); }
// Adds all possible king moves to the given moves list private void addKingsMovesToList(Player i_Player, Board i_Board, List <Move> i_MovesList, ref bool capturePossible) { if (MoveValidator.IsSimpleMove(i_Player, i_Board, new Move(this.Location.Y, this.Location.X, this.Location.Y + 1, this.Location.X + 1))) { i_MovesList.Add(new Move(this.Location.Y, this.Location.X, this.Location.Y + 1, this.Location.X + 1)); } if (MoveValidator.IsSimpleMove(i_Player, i_Board, new Move(this.Location.Y, this.Location.X, this.Location.Y - 1, this.Location.X + 1))) { i_MovesList.Add(new Move(this.Location.Y, this.Location.X, this.Location.Y - 1, this.Location.X + 1)); } if (checkKingCapture(i_Board, -1, new Move(this.Location.Y, this.Location.X, this.Location.Y - 2, this.Location.X + 2))) { i_MovesList.Add(new Move(this.Location.Y, this.Location.X, this.Location.Y - 2, this.Location.X + 2)); capturePossible = true; } if (checkKingCapture(i_Board, 1, new Move(this.Location.Y, this.Location.X, this.Location.Y + 2, this.Location.X + 2))) { i_MovesList.Add(new Move(this.Location.Y, this.Location.X, this.Location.Y + 2, this.Location.X + 2)); capturePossible = true; } }
// Public Methods public bool HasPossibleMoves(Player i_Player, Board i_CurrentBoard) { return(MoveValidator.IsPieceHaveMoves(i_CurrentBoard, this)); }
// Execute the given move, returns true if it was executed successfully public void ExecuteMove(Move i_Move) { eMoveFeedback moveFeedback = eMoveFeedback.Failed; Piece piece = m_Board.GameBoard[i_Move.FromRow, i_Move.FromCol].CurrentPiece; if (piece != null) { // User chose simple move if (MoveValidator.IsSimpleMove(CurrentPlayer, m_Board, i_Move)) { // If the player can capture- the move fails (he must choose to capture) if (MoveValidator.IsPlayerHasCapture(CurrentPlayer, m_Board)) { moveFeedback = eMoveFeedback.FailedCouldCapture; } else { // If the moving piece is NOT a king if (piece.IsKing == false) { // If tries to move in the opposite direction- the move fails if (MoveValidator.IsMovingInValidDirection(CurrentPlayer, i_Move, piece) == false) { moveFeedback = eMoveFeedback.Failed; } else { m_Board.MovePiece(CurrentPlayer, piece, new Point(i_Move.ToRow, i_Move.ToCol)); moveFeedback = eMoveFeedback.Success; } } // moving piece is a king else { m_Board.MovePiece(CurrentPlayer, piece, new Point(i_Move.ToRow, i_Move.ToCol)); moveFeedback = eMoveFeedback.Success; } } } // User chose capture move else if (MoveValidator.IsCaptureMovePossible(CurrentPlayer, m_Board, i_Move)) { Player enemyPlayer = CurrentPlayer == PlayerOne ? PlayerTwo : PlayerOne; m_Board.MoveCapturingPiece(CurrentPlayer, enemyPlayer, piece, new Point(i_Move.ToRow, i_Move.ToCol)); moveFeedback = eMoveFeedback.Success; // check double capture option if (MoveValidator.CanPieceCapture(m_Board, m_Board.GameBoard[i_Move.ToRow, i_Move.ToCol].CurrentPiece)) { moveFeedback = eMoveFeedback.CanDoubleCapture; } } } if (moveFeedback == eMoveFeedback.Success) { m_TurnCount++; } // Notify listeners of move execution completed MoveExecuted?.Invoke(moveFeedback); }