public void AddLateralSquares() { for (int i = 0; i < 8; i++) { //create new squares where row remains const and col iterates from 0 - 7 Squares.Add(Square.At(Current.Row, i)); //create new squares where column remains const and row iterates from 0 - 7 Squares.Add(Square.At(i, Current.Col)); } }
public void AddForwardSquares(List <int> numberOfRowsToMove) { foreach (int i in numberOfRowsToMove) { // create new position Square newPosition = Square.At(Current.Row + i, Current.Col); // Add move to list Squares.Add(newPosition); } }
private void AddSquareForEachColAtSpecifiedRows(List <int> rows) { for (int i = 0; i < 8; i++) { // only add square if row value is within board if (rows[i] >= 0 && rows[i] < 8) { Squares.Add(Square.At(rows[i], i)); } } }
private static bool RookCanCastle(Board board, Rook rook) { var rookPos = board.FindPiece(rook); var king = board.GetPiece(Square.At(rookPos.Row, 4)); if (king is King) { return(CanCastle(board, rook, (King)king)); } return(false); }
public Square FindPiece(Piece piece) { for (var row = 0; row < GameSettings.BoardSize; row++) { for (var col = 0; col < GameSettings.BoardSize; col++) { if (_board[row, col] == piece) { return(Square.At(row, col)); } } } throw new ArgumentException("The supplied piece is not on the board.", "piece"); }
public void SetEnPassantTarget(Pawn piece, Square from, Square to) { if (Math.Abs(to.Row - from.Row) == 2 && piece != null) { EnPassantTarget = new EnPassantTarget { TargetPiece = piece, TargetSquare = Square.At((to.Row + from.Row) / 2, from.Col) }; } else { EnPassantTarget = null; } }
public Square FindPiece(Piece piece) { for (var row = 0; row < GameSettings.BoardSize; row++) { for (var col = 0; col < GameSettings.BoardSize; col++) { if (board[row, col] == piece) { return(Square.At(row, col)); } } } throw new ArgumentException(Constants.MissingPieceExceptionMsg, "piece"); }
public void MovePiece(Square from, Square to) { var movingPiece = board[from.Row, from.Col]; if (movingPiece == null) { return; } // condition for potential en passant isDoubleStepActive = true; if (movingPiece.IsPawn && Math.Abs(from.Row - to.Row) == 2) { movingPiece.DoubleStep = true; isDoubleStepActive = true; } if (movingPiece.Player != CurrentPlayer) { throw new ArgumentException("The supplied piece does not belong to the current player."); } //en passant victim piece var enPassantVictim = GetPiece(Square.At(Pawn.Operation(to.Row, -1, movingPiece.Player), to.Col)); //If the space we're moving to is occupied, we need to mark it as captured. if (board[to.Row, to.Col] != null) { OnPieceCaptured(board[to.Row, to.Col]); } else if (enPassantVictim != null) { if (enPassantVictim.Player != movingPiece.Player && enPassantVictim.DoubleStep && isDoubleStepActive) { OnPieceCaptured(board[Pawn.Operation(to.Row, -1, movingPiece.Player), to.Col]); board[Pawn.Operation(to.Row, -1, movingPiece.Player), to.Col] = null; } } //Move the piece and set the 'from' square to be empty. board[to.Row, to.Col] = board[from.Row, from.Col]; board[from.Row, from.Col] = null; CurrentPlayer = movingPiece.Player == Player.White ? Player.Black : Player.White; OnCurrentPlayerChanged(CurrentPlayer); }
private static Square findOwnKingLocation(Board board, Piece[,] boardstate) { var kingSquare = new Square(); for (var row = 0; row < GameSettings.BoardSize; row++) { for (var col = 0; col < GameSettings.BoardSize; col++) { if (boardstate[row, col] is King && boardstate[row, col].Player == board.CurrentPlayer) { kingSquare = Square.At(row, col); } } } return(kingSquare); }
public bool PlayerHasNoMoves(Player player) { for (int i = 0; i < GameSettings.BoardSize; i++) { for (int j = 0; j < GameSettings.BoardSize; j++) { if (ContainsOpposingPiece(Square.At(i, j), player.opposingPlayer())) { if (GetPiece(Square.At(i, j)).GetAvailableMoves(this).Any()) { return(false); } } } } return(true); }
private IEnumerable <Square> GetMovesInLine(Board board, Square currentPos, Diagonal dir) { var availableMoves = new List <Square>(); var crossedPiece = false; // decides if the piece we intend to move has been crossed for (var u = 0; u < GameSettings.BoardSize; u++) { Square square; if (dir == Diagonal.LeadingBlackDiagonal) { square = Square.At(u - currentPos.Col + currentPos.Row, u); } else { square = Square.At(currentPos.Col + currentPos.Row - u, u); } if (!board.IsPositionValid(square)) { continue; } if (square == currentPos) { crossedPiece = true; continue; } if (!board.IsSquareEmpty(square)) { if (HandleBlockingPieceAndBreak(board, square, crossedPiece, ref availableMoves)) { break; } } else { availableMoves.Add(square); } } return(availableMoves); }
private IEnumerable <Square> GoInDirection(Square location, Direction direction) { List <Square> moveList = new List <Square>(); DirectionIndicators indicators = new DirectionIndicators(true, false, MyBoard, MyPlayer); int i = 1; while (indicators.KeepGoing) { indicators.SetDirectionIndicator(location.Row + i * direction.changeInRow, location.Col + i * direction.changeInCol); if (indicators.Add) { moveList.Add(Square.At(location.Row + i * direction.changeInRow, location.Col + i * direction.changeInCol)); } i++; } return(moveList); }
public bool PlayerIsInCheck(Player player) { for (int i = 0; i < GameSettings.BoardSize; i++) { for (int j = 0; j < GameSettings.BoardSize; j++) { if (ContainsOpposingPiece(Square.At(i, j), player)) { foreach (var move in GetPiece(Square.At(i, j)).GetAvailableMovesPreCheck(this)) { if (GetPiece(move) is King) { return(true); } } } } } return(false); }
public static List <Square> GetRookCastleMoves(Board board, Rook rook) { var rookPos = board.FindPiece(rook); var moves = new List <Square>(); if (RookCanCastle(board, rook)) { switch (rookPos.Col) { case 0: moves.Add(Square.At(rookPos.Row, 3)); break; case 7: moves.Add(Square.At(rookPos.Row, 5)); break; } } return(moves); }
public static List <Square> GetKingCastleMoves(Board board, King king) { var moves = new List <Square>(); var kingPos = board.FindPiece(king); if (!board.PlayerIsInCheck(king.Player)) { var rook = board.GetPiece(Square.At(kingPos.Row, 0)); if ((rook is Rook) && CanCastle(board, (Rook)rook, king)) { moves.Add(Square.At(kingPos.Row, 2)); } rook = board.GetPiece(Square.At(kingPos.Row, 7)); if ((rook is Rook) && CanCastle(board, (Rook)rook, king)) { moves.Add(Square.At(kingPos.Row, 6)); } } return(moves); }
public static Square[] GetCastlingMoveRook(Board board, Square kingFrom, Square kingTo) { int rookFrom = 0; int rookTo = 0; switch (kingFrom.Col - kingTo.Col) { //left rook case 2: rookFrom = 0; rookTo = 3; break; // right rook case -2: rookFrom = 7; rookTo = 5; break; } return(new Square[] { Square.At(kingFrom.Row, rookFrom), Square.At(kingFrom.Row, rookTo) }); }
private IEnumerable <Square> GetMovesInLine(Board board, Square currentPos, Direction dir) { var availableMoves = new List <Square>(); var crossedPiece = false; // decides if the piece we intend to move has been crossed for (var i = 0; i < GameSettings.BoardSize; i++) { Square square; if (dir == Direction.Horizontal) { square = Square.At(currentPos.Row, i); } else { square = Square.At(i, currentPos.Col); } if (square == currentPos) { crossedPiece = true; continue; } if (!board.IsSquareEmpty(square)) { if (HandleBlockingPieceAndBreak(board, square, crossedPiece, ref availableMoves)) { break; } } else { availableMoves.Add(square); } } return(availableMoves); }
public static void AddMoves( Board board, List <Direction> directions, Square currentSquare, int spacesAway, List <Square> moves, Player player ) { foreach (var direction in directions) { var potentialMove = Square.At( currentSquare.Row + spacesAway * direction.RowOffset, currentSquare.Col + spacesAway * direction.ColOffset ); var pieceOnNewSquare = board.GetPiece(potentialMove); if (pieceOnNewSquare == null || pieceOnNewSquare.Player != player) { moves.AddIfOnBoard(potentialMove); } } }
private static bool CanCastle(Board board, Rook rook, King king) { var rookPos = board.FindPiece(rook); var kingPos = board.FindPiece(king); if (!rook.hasMoved && !king.hasMoved) { var min = rookPos.Col < 4 ? rookPos.Col + 1 : 5; var max = rookPos.Col > 4 ? rookPos.Col : 4; var allEmptyAndSafe = true; for (int i = min; i < max; i++) { var square = Square.At(rookPos.Row, i); if (board.GetPiece(square) != null || board.MovePutsPlayerInCheck(king.Player, kingPos, square)) { allEmptyAndSafe = false; } } return(allEmptyAndSafe); } return(false); }
public bool EmptySpace(int row, int col) { return(GetPiece(Square.At(row, col)) == null); }
public bool IsValidCapture(int row, int col) { return(row < GameSettings.BoardSize && row >= 0 && col < GameSettings.BoardSize && col >= 0 && (GetPiece(Square.At(row, col)) == null || GetPiece(Square.At(row, col)).Player != CurrentPlayer)); }
public bool IsValidPosition(int row, int col) { return(row < GameSettings.BoardSize && row >= 0 && col < GameSettings.BoardSize && col >= 0 && GetPiece(Square.At(row, col)) == null); }
private bool CastleThroughCheck(Square from, Square to) { var columns = Enumerable.Range(to.Col > from.Col ? from.Col : to.Col, 3); return(columns.Any(i => MoveIntoCheck(@from, Square.At(@from.Row, i)))); }