/** This is what is called when a player is castling. It is called from Move(). * It first moves the king to the square it is moving to and then moves the * rook to the square next to the king. * @param a_move - The castle that is being executed * @author Thomas Hooper * @date April 2019 */ public void Castle(Move a_move) { //Moving king here MovePiece(a_move); #region Actually changing data for rook and it's destination BoardSquare rookSquare = ChessBoard.ReturnSquare(a_move.CastlingRook.Row, a_move.CastlingRook.Column); rookSquare.Occupied = false; BoardSquare destSquare = new BoardSquare(); if (a_move.Destination.Column == 2) { destSquare = ChessBoard.ReturnSquare(a_move.Destination.Row, a_move.Destination.Column + 1); } else if (a_move.Destination.Column == 6) { destSquare = ChessBoard.ReturnSquare(a_move.Destination.Row, a_move.Destination.Column - 1); } a_move.CastlingRook.Row = destSquare.Row; a_move.CastlingRook.Column = destSquare.Column; a_move.CastlingRook.Square = destSquare; a_move.CastlingRook.HasMoved = true; destSquare.Occupied = true; destSquare.PieceColor = a_move.CastlingRook.Color; #endregion }
/** Constructor for Move that is used when the piece is moving and * not doing anything else. * @param a_square - The square the piece is moving to. * @param a_piece - The piece that is moving. * @author Thomas Hooper * @date April 2019 */ public Move(BoardSquare a_square, Piece a_piece) { Destination = a_square; MovingPiece = a_piece; OriginalSquare = a_piece.Square; Castle = false; EnPassant = false; Capture = false; Value = 0; }
/** Constructor for Move that is used when the piece is capturing another piece. * @param a_square - The square the king is moving to. * @param a_piece - The king that is castling. * @param a_rook - The rook taking part in the castle * @param a_castle - This signifies we are castling * @author Thomas Hooper * @date April 2019 */ public Move(BoardSquare a_square, Piece a_piece, Piece a_rook, bool a_castle) { Destination = a_square; MovingPiece = a_piece; OriginalSquare = a_piece.Square; CastlingRook = a_rook; Castle = true; EnPassant = false; Capture = false; Value = 0; }
/** This method takes a castling move and checks its validity. If it is a valid * castle then it returns true and otherwise it returns false. * @param a_move - the castle we are checking the validity of * @returns true if the castle is valid and false otherwise * @author Thomas Hooper * @date April 2019 */ public bool ValidCastle(Move a_move) { //If king or rook have moved then no bueno if (a_move.MovingPiece.HasMoved || a_move.CastlingRook.HasMoved) { return(false); } BoardSquare s1 = new BoardSquare(); BoardSquare s2 = new BoardSquare(); BoardSquare s3 = new BoardSquare(); #region Getting Squares to left or right of king if (a_move.MovingPiece.Column > a_move.CastlingRook.Column) { s1 = ChessBoard.ReturnSquare(a_move.MovingPiece.Row, a_move.MovingPiece.Column - 1); s2 = ChessBoard.ReturnSquare(a_move.MovingPiece.Row, a_move.MovingPiece.Column - 2); s3 = ChessBoard.ReturnSquare(a_move.MovingPiece.Row, a_move.MovingPiece.Column - 3); if (s1.Occupied || s2.Occupied || s3.Occupied) { return(false); } } else { s1 = ChessBoard.ReturnSquare(a_move.MovingPiece.Row, a_move.MovingPiece.Column + 1); s2 = ChessBoard.ReturnSquare(a_move.MovingPiece.Row, a_move.MovingPiece.Column + 2); if (s1.Occupied || s2.Occupied) { return(false); } } #endregion //Getting Square king is on BoardSquare movPieceSquare = ChessBoard.ReturnSquare(a_move.MovingPiece.Row, a_move.MovingPiece.Column); #region Making sure no squares are attacked if (a_move.MovingPiece.Color == Color.White) { if (BlackPlayer.AttackSquares.Contains(s1) || BlackPlayer.AttackSquares.Contains(s2) || BlackPlayer.AttackSquares.Contains(movPieceSquare)) { return(false); } } else if (a_move.MovingPiece.Color == Color.Black) { if (WhitePlayer.AttackSquares.Contains(s1) || WhitePlayer.AttackSquares.Contains(s2) || WhitePlayer.AttackSquares.Contains(movPieceSquare)) { return(false); } } #endregion return(true); }
/** Constructor for Piece that also takes a BoardSquare object * @param a_row - The row the piece is on * @param a_column - The column the piece is on * @param a_value - the value of the piece * @param a_color - the color of the piece (Black or White) * @param a_name - the name of the piece * @author Thomas Hooper * @date February 2019 */ public Piece(int a_row, int a_column, int a_value, Color a_color, string a_name, BoardSquare a_square) { Row = a_row; Column = a_column; Value = a_value; Color = a_color; HasMoved = false; ValidSquares = new List <BoardSquare>(); InvalidSquares = new List <BoardSquare>(); AttackingSquares = new List <BoardSquare>(); History = new List <string>(); Name = a_name; Square = a_square; }
public ChessBoard() { Board = new List <List <BoardSquare> >(); for (int i = 0; i < 8; i++) { var row = new List <BoardSquare>(); for (int j = 0; j < 8; j++) { var square = new BoardSquare(); row.Add(square); } Board.Add(row); } }
/// <summary> /// Attemps to lift the piece from the given square as long as it is that players turn /// </summary> /// <param name="?"></param> private void attemptToLiftPiece(BoardSquare square) { ChessPiece pieceToLift = square.getPiece(); if (pieceToLift == null || pieceToLift.getColour() != this.turn) { return; } this.liftedPiece = square.liftPiece(); this.liftedFrom = square.getLocation(); setCursorToLiftedPiece(); }
private void attemptToPlacePiece(BoardSquare square) { ChessMove attemptedMove = new ChessMove(liftedFrom, square.getLocation(), liftedPiece); if (square.getLocation() == this.liftedFrom || !gameBoard.getState().isLegalMove(attemptedMove, lastMove)) { // If the piece is getting placed back to where it was lifted from just put it back, or if the move is illegal placePieceBack(); return; } if (checkForAndApplyPawnPromotion(attemptedMove)) { return; } checkForAndApplyenPassant(attemptedMove); gameBoard.getSquare(square.getLocation()).placePiece(this.liftedPiece); setCursorToDefault(); checkForAndApplyCastle(attemptedMove); forceUIUpdate(); updateCastleingPossibilities(); this.liftedPiece = null; this.liftedFrom = null; nextTurn(); gameState currentState = this.gameBoard.getState().calculateGameState(this.turn, lastMove); if (currentState == gameState.lightWin) { gameCompleteAndRestart(currentState); return; } else if (currentState == gameState.darkWin) { gameCompleteAndRestart(currentState); return; } else if (currentState == gameState.draw) { gameCompleteAndRestart(currentState); return; } lastMove = attemptedMove; }
/// <summary> /// Function that is called by every button on the MainWindow representing a board square. /// </summary> /// <param name="sender">Button object of the button that was pressed</param> /// <param name="e"></param> private void Square_Pressed(object sender, RoutedEventArgs e) { Button Sender = (System.Windows.Controls.Button)sender; string squarePressedUid = Sender.Uid; BoardLocation locationPressed; locationPressed = new BoardLocation(squarePressedUid[0], squarePressedUid[1]); if (turn == pieceColour.dark) { locationPressed.rotate180Degrees(); } BoardSquare squarePressed = gameBoard.getSquare(locationPressed); if (this.liftedPiece == null) { attemptToLiftPiece(squarePressed); } else if (this.liftedPiece != null) { attemptToPlacePiece(squarePressed); } }
/** Constructor for King that also takess a BoardSquare object * @param a_row - The row the king is on * @param a_column - The column the king is on * @param a_value - the value of the piece * @param a_color - the color of the piece (Black or White) * @param a_name - the name of the piece * @author Thomas Hooper * @date February 2019 */ public King(int a_row, int a_column, int a_value, Color a_color, string a_name, BoardSquare a_square) : base(a_row, a_column, a_value, a_color, a_name, a_square) { }
/** This method generates all the possible moves the player is able to make * It goes through each piece and their valid squares and determines if it can * make each move for each piece and square. It also evaluates if the piece can capture, * castle or en passant * @param a_player - The opposing player * @param a_board - The board the game is played on * @param a_previousMove - the previous move that was played * @author Thomas Hooper * @date July 2019 */ public void GenerateMoves(Player a_player, Board a_board, Move a_previousMove) { List <Move> moves = new List <Move>(); foreach (Piece p in Pieces) { foreach (BoardSquare s in p.ValidSquares) { #region Just Moving if (!s.Occupied) { moves.Add(new Move(s, p)); } #endregion #region Capturing else { if (a_player.Pieces.Exists(x => x.Row == s.Row && x.Column == s.Column)) //BoardSquare Color { if (p.AttackingSquares.Exists(x => x.Row == s.Row && x.Column == s.Column) && s.PieceColor != p.Color) { Piece capturedPiece = a_player.Pieces.Find(x => x.Row == s.Row && x.Column == s.Column); moves.Add(new Move(s, p, capturedPiece)); } } } #endregion } } #region En Passant if (a_previousMove != null) { if (a_previousMove.MovingPiece is Pawn && (a_previousMove.MovingPiece.Row == 3 || a_previousMove.MovingPiece.Row == 4)) { if (Pieces.Exists(x => x is Pawn && x.Row == a_previousMove.MovingPiece.Row)) { if (a_previousMove.MovingPiece.Color == Color.White && a_previousMove.MovingPiece.Row == 4) { if (a_previousMove.OriginalSquare.Row == 6) { List <Piece> pawns = Pieces.FindAll(x => x.Row == 4); foreach (Piece p in pawns) { if (Math.Abs(p.Column - a_previousMove.MovingPiece.Column) == 1) { BoardSquare square = a_board.ReturnSquare(a_previousMove.OriginalSquare.Row + 1, a_previousMove.OriginalSquare.Column); moves.Add(new Move(square, p) { EnPassant = true }); } } } } else if (a_previousMove.MovingPiece.Color == Color.Black && a_previousMove.MovingPiece.Row == 3) { if (a_previousMove.OriginalSquare.Row == 1) { List <Piece> pawns = Pieces.FindAll(x => x.Row == 3); foreach (Piece p in pawns) { if (Math.Abs(p.Column - a_previousMove.MovingPiece.Column) == 1) { BoardSquare square = a_board.ReturnSquare(a_previousMove.OriginalSquare.Row - 1, a_previousMove.OriginalSquare.Column); moves.Add(new Move(square, p) { EnPassant = true }); } } } } } } } #endregion #region Castling King k = (King)Pieces.Find(x => x is King); if (!k.HasMoved && !Check) { BoardSquare s1 = new BoardSquare(); BoardSquare s2 = new BoardSquare(); BoardSquare s3 = new BoardSquare(); if (Pieces.Exists(x => x is Rook && !x.HasMoved && x.Column == 7)) { Rook r = (Rook)Pieces.Find(x => x is Rook && !x.HasMoved && x.Column == 7); s1 = a_board.ReturnSquare(k.Row, k.Column + 1); s2 = a_board.ReturnSquare(k.Row, k.Column + 2); if (!s1.Occupied && !s2.Occupied) { moves.Add(new Move(s2, k, r, true)); } } if (Pieces.Exists(x => x is Rook && !x.HasMoved && x.Column == 0)) { Rook r = (Rook)Pieces.Find(x => x is Rook && !x.HasMoved && x.Column == 0); s1 = a_board.ReturnSquare(k.Row, k.Column - 1); s2 = a_board.ReturnSquare(k.Row, k.Column - 2); s3 = a_board.ReturnSquare(k.Row, k.Column - 3); if (!s1.Occupied && !s2.Occupied && !s3.Occupied) { moves.Add(new Move(s2, k, r, true)); } } } #endregion Moves = moves; }
/** This function is called for white pawns which in this app * start in row 6 and move in the negative direction. It gets * the squares from the board directly in front of it and if * it hasn't moved it can move two rows up. If the squares on its * diagonals in front of it are occupied by a piece of the opposite color * then those squares are also added because the pawn is able to * capture them * @param a_board - The chessboard the pawn is on * @author Thomas Hooper * @date February 2019 */ private void WhitePawnSquares(Board a_board) { List <BoardSquare> squares = new List <BoardSquare>(); List <BoardSquare> attSquares = new List <BoardSquare>(); BoardSquare square1; BoardSquare square2; square1 = a_board.ReturnSquare(Row - 1, Column); square2 = a_board.ReturnSquare(Row - 2, Column); #region Getting Move Squares try { if (!square1.Occupied) { squares.Add(square1); } } catch (Exception) { } try { if (Row == 6) { if (!(square1.Occupied && square2.Occupied)) { squares.Add(square2); } } } catch (Exception) { } #endregion #region Getting Attack Squares BoardSquare attSquare1 = a_board.ReturnSquare(Row - 1, Column - 1); BoardSquare attSquare2 = a_board.ReturnSquare(Row - 1, Column + 1); try { if (attSquare1.Occupied) { squares.Add(attSquare1); } attSquares.Add(attSquare1); } catch (Exception) { } try { if (attSquare2.Occupied) { squares.Add(attSquare2); } attSquares.Add(attSquare2); } catch (Exception) { } #endregion AttackingSquares = attSquares; ValidSquares = squares; }