// evaluates whether the king can perform the castle procedure by finding the kingside rook and determining // whether both the king and rook havent moved yet and whether the spaces between them are empty public bool canCastle(Square targetPosition, Board gameBoard) { // if the target position is castling then the square will be 2 x - coordinates away if (Math.Abs(targetPosition.X - this.getPosition().X) == 2) { PieceSet blackpieces = gameBoard.getPieceSets()[0]; PieceSet whitepieces = gameBoard.getPieceSets()[1]; Rook kingsideRook = null; if (this.getColour() == Piececolour.BLACK) { foreach (Piece piece in blackpieces.getPieceSet()) { if (piece.getType() == pieceType.ROOK) { if (Math.Abs(this.getPosition().X - piece.getPosition().X) == 3) { kingsideRook = (Rook)piece; } } } } if (this.getColour() == Piececolour.WHITE) { foreach (Piece piece in whitepieces.getPieceSet()) { if (piece.getType() == pieceType.ROOK) { if (Math.Abs(this.getPosition().X - piece.getPosition().X) == 3) { kingsideRook = (Rook)piece; } } } } if (kingsideRook != null) { if (kingsideRook.getMoveCount() == 0) { if (this.getMoveCount() == 0) { if (targetPosition.Y == this.getPosition().Y&& targetPosition.occ != true) // checks that the y cooridnate of the target square is the same as the king's and also that the target square is not occupied { for (int i = this.getPosition().X - 1; i > kingsideRook.getPosition().X; i--) // loops through between the rook and the king, to check the squares in between to make sure they are not occupied. { if (gameBoard.getSquares()[i, this.getPosition().Y].occ == true) { return(false); } } if (targetPosition.X < this.getPosition().X) // ensures that the algorithm only returns true for the correct target square ( in between rook and king ) and not the other side of the king { return(true); } else { return(false); } } else // otherwise, if the square is occupied or the y coordinates are not the same { return(false); } } else // if the king has already moved then return false { return(false); } } else // if the rook has already moved before then return false { return(false); } } else // if the kingside rook variable is null then return false { return(false); } } else // if the target square is not 2 x-coordinates away then return false { return(false); } }
//initiallises all piece objects and fills up the corresponding piece list. public void setInitalPieceList(Piece.Piececolour piececolour, Square[,] squares, Piece.Piececolour player1colour) { int index; // the index of the pieces on the board (first row pieces - king, rook etc) int pawnindex; // pawns are in the second row, so have their own index setColour(piececolour); //creates all the piece objects neccessary Pawn[] Blackpawns = new Pawn[8]; Pawn[] Whitepawns = new Pawn[8]; Rook[] BlackRooks = new Rook[2]; Rook[] WhiteRooks = new Rook[2]; Bishop[] WhiteBishops = new Bishop[2]; Bishop[] BlackBishops = new Bishop[2]; Knight[] WhiteKnights = new Knight[2]; Knight[] BlackKnights = new Knight[2]; Queen WhiteQueen = new Queen(); King WhiteKing = new King(); Queen BlackQueen = new Queen(); King BlackKing = new King(); int i = 0; //Initiallise all piece objects for (int j = 0; j < 8; j++) { Blackpawns[j] = new Pawn(); Whitepawns[j] = new Pawn(); } for (int j = 0; j < 2; j++) { WhiteBishops[j] = new Bishop(); BlackBishops[j] = new Bishop(); } for (int j = 0; j < 2; j++) { BlackRooks[j] = new Rook(); WhiteRooks[j] = new Rook(); } for (int j = 0; j < 2; j++) { WhiteKnights[j] = new Knight(); BlackKnights[j] = new Knight(); } //fills up the list with the pieces and setting their properties if (colour == Piece.Piececolour.BLACK) { i = 0; // the x coordinate this can be reassigned as the pieces are populated //player 1 pieces needs to be at the bottom of the board so assign the indexes accordingly if (player1colour == Piece.Piececolour.BLACK) { index = 7; pawnindex = 6; } else { index = 0; pawnindex = 1; } // sets properties of pawns foreach (Pawn pawn in Blackpawns) { pawn.setType(Piece.pieceType.PAWN); pawn.setColour(Piece.Piececolour.BLACK); pawn.setImage(test_chess.Properties.Resources.Chess_pdt60); pawn.setPosition(squares[i, pawnindex]); if (player1colour == Piece.Piececolour.BLACK) { pawn.movedirection = -1; } else { pawn.movedirection = 1; } m_pieceSet.Add(pawn); i++; } i = 0; // x coordinate value // sets properties of rooks foreach (Rook rook in BlackRooks) { rook.setType(Piece.pieceType.ROOK); rook.setColour(Piece.Piececolour.BLACK); rook.setImage(test_chess.Properties.Resources.Chess_rdt60); rook.setPosition(squares[i, index]); m_pieceSet.Add(rook); i = 7; } i = 1;// x coordinate value foreach (Knight knight in BlackKnights) { knight.setType(Piece.pieceType.KNIGHT); knight.setColour(Piece.Piececolour.BLACK); knight.setImage(test_chess.Properties.Resources.Chess_ndt60); knight.setPosition(squares[i, index]); m_pieceSet.Add(knight); i = 6; } i = 2; // x coordinate value foreach (Bishop bishop in BlackBishops) { bishop.setType(Piece.pieceType.BISHOP); bishop.setColour(Piece.Piececolour.BLACK); bishop.setImage(test_chess.Properties.Resources.Chess_bdt60); bishop.setPosition(squares[i, index]); m_pieceSet.Add(bishop); i = 5; } BlackKing.setType(Piece.pieceType.KING); BlackKing.setColour(Piece.Piececolour.BLACK); BlackKing.setImage(test_chess.Properties.Resources.Chess_kdt60); BlackKing.setPosition(squares[3, index]); BlackQueen.setType(Piece.pieceType.QUEEN); BlackQueen.setColour(Piece.Piececolour.BLACK); BlackQueen.setImage(test_chess.Properties.Resources.Chess_qdt60); BlackQueen.setPosition(squares[4, index]); m_pieceSet.Add(BlackKing); m_pieceSet.Add(BlackQueen); } else { // otherwise player 1 is black and so black pieces must be at the bottom of the board if (player1colour == Piece.Piececolour.BLACK) { index = 0; pawnindex = 1; } else { index = 7; pawnindex = 6; } i = 0; foreach (Pawn pawn in Whitepawns) { pawn.setType(Piece.pieceType.PAWN); pawn.setColour(Piece.Piececolour.WHITE); pawn.setImage(test_chess.Properties.Resources.Chess_plt60); pawn.setPosition(squares[i, pawnindex]); if (player1colour == Piece.Piececolour.WHITE) { pawn.movedirection = -1; } else { pawn.movedirection = 1; } m_pieceSet.Add(pawn); i++; } i = 0; foreach (Rook rook in WhiteRooks) { rook.setType(Piece.pieceType.ROOK); rook.setColour(Piece.Piececolour.WHITE); rook.setImage(test_chess.Properties.Resources.Chess_rlt60); rook.setPosition(squares[i, index]); m_pieceSet.Add(rook); i = 7; } i = 2; foreach (Bishop bishop in WhiteBishops) { bishop.setType(Piece.pieceType.BISHOP); bishop.setColour(Piece.Piececolour.WHITE); bishop.setImage(test_chess.Properties.Resources.Chess_blt60); bishop.setPosition(squares[i, index]); m_pieceSet.Add(bishop); i = 5; } i = 1; foreach (Knight knight in WhiteKnights) { knight.setType(Piece.pieceType.KNIGHT); knight.setColour(Piece.Piececolour.WHITE); knight.setImage(test_chess.Properties.Resources.Chess_nlt60); knight.setPosition(squares[i, index]); m_pieceSet.Add(knight); i = 6; } WhiteKing.setType(Piece.pieceType.KING); WhiteKing.setColour(Piece.Piececolour.WHITE); WhiteKing.setImage(test_chess.Properties.Resources.Chess_klt60); WhiteKing.setPosition(squares[3, index]); WhiteQueen.setType(Piece.pieceType.QUEEN); WhiteQueen.setColour(Piece.Piececolour.WHITE); WhiteQueen.setImage(test_chess.Properties.Resources.Chess_qlt60); WhiteQueen.setPosition(squares[4, index]); m_pieceSet.Add(WhiteQueen); m_pieceSet.Add(WhiteKing); } }
// this is where the moves of the game are made, and where the moves are added to history public void makeMove(Square targetposition, Board gameBoard, Piece movingPiece) { PieceSet[] pieceSets = gameBoard.getPieceSets(); PieceSet BlackPieces = pieceSets[0]; PieceSet WhitePieces = pieceSets[1]; //Move object created and populated Move aMove = new Move(); aMove.finalPosition = targetposition; aMove.initialPosition = movingPiece.getPosition(); aMove.PieceMoved = movingPiece; if (BoardAdmin.game1.getTurn() == BoardAdmin.game1.getPlayer1().getPlayerColour()) { aMove.PlayerMoved = BoardAdmin.game1.getPlayer1(); if (BoardAdmin.game1.getPlayer1().getPlayerColour() == Piece.Piececolour.WHITE) { foreach (Piece piece in gameBoard.getPieceSets()[0].getPieceSet()) { if (piece.getPosition() == targetposition) { aMove.pieceCaptured = piece; } } BlackPieces.getPieceSet().Remove(aMove.pieceCaptured); } if (BoardAdmin.game1.getPlayer1().getPlayerColour() == Piece.Piececolour.BLACK) { foreach (Piece piece in gameBoard.getPieceSets()[1].getPieceSet()) { if (piece.getPosition() == targetposition) { aMove.pieceCaptured = piece; } } WhitePieces.getPieceSet().Remove(aMove.pieceCaptured); } } else // otherwise it is player 2's turn so do exactly the same but for player 2 { aMove.PlayerMoved = BoardAdmin.game1.getPlayer2(); if (BoardAdmin.game1.getPlayer2().getPlayerColour() == Piece.Piececolour.WHITE) { foreach (Piece piece in gameBoard.getPieceSets()[0].getPieceSet()) { if (piece.getPosition() == targetposition) { aMove.pieceCaptured = piece; } } BlackPieces.getPieceSet().Remove(aMove.pieceCaptured); } if (BoardAdmin.game1.getPlayer2().getPlayerColour() == Piece.Piececolour.BLACK) { foreach (Piece piece in gameBoard.getPieceSets()[1].getPieceSet()) { if (piece.getPosition() == targetposition) { aMove.pieceCaptured = piece; } } WhitePieces.getPieceSet().Remove(aMove.pieceCaptured); } } if (movingPiece.getType() == Piece.pieceType.KING) // if the moving piece is a king, castling may occur { King movingKing = (King)movingPiece; if (movingKing.canCastle(targetposition, gameBoard) == true) // invokes the canCastle method of the king class to check whether castling can occur { BoardAdmin.isCastling = true; Rook movingRook = null; if (this.getPlayerColour() == Piece.Piececolour.WHITE) { foreach (Piece piece in WhitePieces.getPieceSet()) { if (piece.getType() == Piece.pieceType.ROOK && Math.Abs(piece.getPosition().X - movingKing.getPosition().X) == 3) { movingRook = (Rook)piece; } } } if (this.getPlayerColour() == Piece.Piececolour.BLACK) { foreach (Piece piece in BlackPieces.getPieceSet()) { if (piece.getType() == Piece.pieceType.ROOK && Math.Abs(piece.getPosition().X - movingKing.getPosition().X) == 3) { movingRook = (Rook)piece; } } } BoardAdmin.KingsideRook = movingRook; movingKing.PerformCastle(BoardAdmin.KingsideRook, movingKing, targetposition, gameBoard); // invokes the performCastle method in the king class to make the castling move. if (BoardAdmin.TempMoving == false) // if temp moving is true, then the move needs to be retracted so the boolean representing castling needs to remain true otherwise the kingside rook will not be moved back aswell as the king { BoardAdmin.isCastling = false; // so if temp moving is false, the castling can be made false aswell as the move is not temporary. } } } // adjusts properties of the piece moving and the squares involved movingPiece.getPosition().occ = false; movingPiece.setPosition(targetposition); movingPiece.getPosition().occ = true; movingPiece.IncrementMoveCount(1); if (movingPiece.getType() == Piece.pieceType.PAWN) // if the moving piece is a pawn, promototion may occur { if (movingPiece.getPosition().Y == 0 || movingPiece.getPosition().Y == 7) //if either the bottom or the top of the board is reached { if (BoardAdmin.TempMoving == false) // only promotes pawn if it is not a temporary move { movingPiece.promotePawn(); // invokes the promotePawn method to perform the move. } } } BoardAdmin.game1.addTomoveHistory(aMove); }
// this occurs when the pawn piece reaches the other side of the board, the user may promote the piece to a // better type of piece public override void promotePawn() { // opens the promote pawn sections and awaits a user selection PromotePawn promotingForm = new PromotePawn(); if (promotingForm.ShowDialog() != System.Windows.Forms.DialogResult.OK) { //Waits until a selection has occured on the other form } // checks that a selection has been made and stored if (BoardAdmin.promotingPawnType == pieceType.BISHOP || BoardAdmin.promotingPawnType == pieceType.QUEEN || BoardAdmin.promotingPawnType == pieceType.ROOK || BoardAdmin.promotingPawnType == pieceType.KNIGHT) { PieceSet[] pieceSets = BoardAdmin.game1.getGameBoard().getPieceSets(); Piece promotedPiece = null; //sets properties of new piece to replace the pawn piece. if (BoardAdmin.promotingPawnType == pieceType.QUEEN) { promotedPiece = new Queen(); promotedPiece.setType(pieceType.QUEEN); if (this.getColour() == Piececolour.BLACK) { promotedPiece.setImage(test_chess.Properties.Resources.Chess_qdt60); } else { promotedPiece.setImage(test_chess.Properties.Resources.Chess_qlt60); } } if (BoardAdmin.promotingPawnType == pieceType.BISHOP) { promotedPiece = new Bishop(); promotedPiece.setType(pieceType.BISHOP); if (this.getColour() == Piececolour.BLACK) { promotedPiece.setImage(test_chess.Properties.Resources.Chess_bdt60); } else { promotedPiece.setImage(test_chess.Properties.Resources.Chess_blt60); } } if (BoardAdmin.promotingPawnType == pieceType.KNIGHT) { promotedPiece = new Knight(); promotedPiece.setType(pieceType.KNIGHT); if (this.getColour() == Piececolour.BLACK) { promotedPiece.setImage(test_chess.Properties.Resources.Chess_ndt60); } else { promotedPiece.setImage(test_chess.Properties.Resources.Chess_nlt60); } } if (BoardAdmin.promotingPawnType == pieceType.ROOK) { promotedPiece = new Rook(); promotedPiece.setType(pieceType.ROOK); if (this.getColour() == Piececolour.BLACK) { promotedPiece.setImage(test_chess.Properties.Resources.Chess_rdt60); } else { promotedPiece.setImage(test_chess.Properties.Resources.Chess_rlt60); } } promotedPiece.setColour(this.getColour()); promotedPiece.setPosition(this.getPosition()); // remove the pawn and add the new promoted piece if (this.getColour() == Piececolour.BLACK) { pieceSets[0].getPieceSet().Remove(this); pieceSets[0].getPieceSet().Add(promotedPiece); } else { pieceSets[1].getPieceSet().Remove(this); pieceSets[1].getPieceSet().Add(promotedPiece); } } }