// This method is called when initializing a new game. It assigns // the specified piece to the ChessSquare identified by the col and row. public void AddNewChessPiece(int col, int row, AbstractChessPiece piece) { // get the target ChessSquare ChessSquare square = GetSquare(col, row); // assign the piece to the square square.SetChessPiece(piece); }
// This utility function will return true if the specified player is in check, or // false otherwise. public bool IsInCheck(PlayerType player) { // to see if player is in check, walk through all opposing player pieces and see if any of // them can move to this player's King position. // first, find this player's king AbstractChessPiece king = null; for (int col = 0; col < 8; col++) { for (int row = 0; row < 8; row++) { // see if there is a piece on this square AbstractChessPiece piece = squares[col, row].ChessPiece; // if there is a piece here, and it belongs to this player, and it's the King if ((piece != null) && (piece.Player == player) && (piece.Name.Equals("King"))) { king = piece; // save the king piece, we found it! } } } // now, walk through all opposing pieces to see if they can capture this player's king PlayerType opposingPlayer = PlayerType.WHITE; if (player == PlayerType.WHITE) { opposingPlayer = PlayerType.BLACK; } for (int col = 0; col < 8; col++) { for (int row = 0; row < 8; row++) { // see if there is a piece on this square AbstractChessPiece piece = squares[col, row].ChessPiece; // if there is a piece here, and it belongs to the opposing player if ((piece != null) && (piece.Player == opposingPlayer)) { // see if opposing piece can capture our King if (piece.CanMoveToLocation(king.Col, king.Row, this)) { return(true); // yikes, player is in check! } } } } // if we get here then no opposing piece can capture player's king, so // player's king is not in check. return(false); }
// This function will establish the specified chess piece on the square. // (Or, if the piece is null, just clear the square of any prior piece!) public void SetChessPiece(AbstractChessPiece piece) { // clear the prior abbreviation button.Text = ""; // set our chess piece to the input if (piece == null) // if the input was null { RemoveChessPiece(); // clear off any prior piece return; // nothing else to do! } // set the new chess piece to the input parameter ChessPiece = piece; // update the Chess Piece so it knows what row and column it now resides on ChessPiece.Col = Col; ChessPiece.Row = Row; // change the text color of the button according to the piece's player if (ChessPiece.Player == PlayerType.WHITE) { button.ForeColor = Color.White; } else { button.ForeColor = Color.Black; } // set the button's text from the piece's 1-character abbreviation button.Text = piece.Abbreviation; // set the font based on the type of piece if (piece.Name.Equals("King")) { button.Font = kingFont; } else { button.Font = pieceFont; } }
// This utility function will move a piece from the selected square to the target square. // It will return any chess piece that was captured from the target square. public AbstractChessPiece MoveChessPiece(ChessSquare selectedSquare, ChessSquare clickedSquare) { // save any piece that is on the target square AbstractChessPiece capturedPiece = clickedSquare.ChessPiece; // move the piece from the original square to the target square clickedSquare.SetChessPiece(selectedSquare.ChessPiece); // mark the chess piece as having been moved, in case the piece needs to know (e.g. Pawn) selectedSquare.ChessPiece.HasMoved = true; // remove the chess piece from the original square selectedSquare.RemoveChessPiece(); // Unselect the currently selected square selectedSquare.Unselect(); // return the captured piece, if any (or null if no piece was captured) return(capturedPiece); }
// This utility function will look at a proposed move from the selectedSquare to // the clickedSquare and determine if the specified player would be in check after // the move. The actual gameboard itself is left unchanged. public bool TestMoveForCheck(PlayerType currentPlayer, ChessSquare selectedSquare, ChessSquare clickedSquare) { // save the piece that may be on the target square AbstractChessPiece capturedPiece = clickedSquare.ChessPiece; // temporarily put the original piece on the target square clickedSquare.SetChessPiece(selectedSquare.ChessPiece); // temporarily remove the original piece from the original square selectedSquare.RemoveChessPiece(); // see if the specified player is in check bool isInCheck = IsInCheck(currentPlayer); // restore the moved piece to the original square selectedSquare.SetChessPiece(clickedSquare.ChessPiece); // restore the original contents of the target square clickedSquare.SetChessPiece(capturedPiece); // return true if the current player would still be in check, or false otherwise return(isInCheck); }
// this method will clear out any trace of the existing chess piece, if any. public void RemoveChessPiece() { ChessPiece = null; button.Text = ""; }
// The student should complete this function. // This method contains all of the game logic to handle user click events. private void handleClick(ChessSquare clickedSquare) { // If there is no piece there then... if (selectedSquare == null) { // If the square they want to move to has a piece then... if (clickedSquare.ChessPiece != null) { // If the piece is one of the players, then stop if (clickedSquare.ChessPiece.Player != currentPlayer) { return; } selectedSquare = clickedSquare; selectedSquare.Select(); } } // If the click the square again, then unselect else if (selectedSquare == clickedSquare) { selectedSquare.Unselect(); selectedSquare = null; } // Otherwise... else { // If there is a piece there... if (clickedSquare.ChessPiece != null) { // If the piece is the players, then stop if (clickedSquare.ChessPiece.Player == currentPlayer) { selectedSquare.Unselect(); selectedSquare = clickedSquare; selectedSquare.Select(); return; } } // If they can't move there, stop if (selectedSquare.ChessPiece.CanMoveToLocation(clickedSquare.Col, clickedSquare.Row, gameBoard) == false) { return; } // Otherwise... else { // If the player trys to kill themself, yell at them if (gameBoard.TestMoveForCheck(currentPlayer, selectedSquare, clickedSquare)) { MessageBox.Show("You are still in check!"); return; } AbstractChessPiece capturedPiece = gameBoard.MoveChessPiece(selectedSquare, clickedSquare); selectedSquare = null; // If the piece was captured, tell the player if (capturedPiece != null) { capturedPiece.ToString(); MessageBox.Show(capturedPiece.ToString() + " was captured"); } changePlayer(); // If the king is in danger, yell at the player if (gameBoard.IsInCheck(currentPlayer)) { MessageBox.Show("Check!"); } } } }