private bool canCaptureLocation(int targetCol, int targetRow, ChessBoard gameBoard) { ChessSquare targetSquare = gameBoard.GetSquare(targetCol, targetRow); // If there is no piece on the square, stop if (targetSquare.ChessPiece == null) { return(false); } // If the piece is the players, stop if (targetSquare.ChessPiece.Player == Player) { return(false); } bool validMove = false; // If the player is white, make sure they can only go up if (Player == PlayerType.WHITE) { if (targetRow == Row - 1 && targetCol > -Col) { validMove = true; } } // If the player is black (don't disciminate the chess pieces because of their color) then make sure they can only go down if (Player == PlayerType.BLACK) { if (targetRow == Row + 1 && targetCol > -Col) { validMove = true; } } return(validMove); }
// 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); }
// The student should complete this function. // This method resets the game data to the initial configuration private void buttonRestart_Click(object sender, EventArgs e) { initializeGame(); if (selectedSquare != null) { selectedSquare.Unselect(); selectedSquare = null; } }
public Bishop(Color c, ChessSquare s) : base(Piece.eBISHOP, c, s) { if (c == Color.eWHITE) { m_image = Chess.Properties.Resources.w_Bishop; } else { m_image = Chess.Properties.Resources.b_Bishop; } }
public Queen(Color c, ChessSquare s) : base(Piece.eQUEEN, c, s) { if (c == Color.eWHITE) { m_image = Chess.Properties.Resources.w_Queen; } else { m_image = Chess.Properties.Resources.b_Queen; } }
public Pawn(Color c, ChessSquare s) : base(Piece.ePAWN, c, s) { if (c == Color.eWHITE) { m_image = Chess.Properties.Resources.w_Pawn; } else { m_image = Chess.Properties.Resources.b_Pawn; } }
public King(Color c, ChessSquare s) : base(Piece.eKING, c, s) { if (c == Color.eWHITE) { m_image = Chess.Properties.Resources.w_King; } else { m_image = Chess.Properties.Resources.b_King; } }
// This function is provided complete as part of the starter code // This method identifies the ChessSquare clicked by using the button name. public void gameSquare_Click(object sender, EventArgs e) { // get the button object that the user clicked on ButtonBase button = (System.Windows.Forms.ButtonBase)sender; // identify the ChessSquare the user clicked on by using the button name ChessSquare clickedSquare = gameBoard.GetClickedSquare(button.Name); // run the game logic for this click event handleClick(clickedSquare); }
public Knight(Color c, ChessSquare s) : base(Piece.eKNIGHT, c, s) { if (c == Color.eWHITE) { m_image = Chess.Properties.Resources.w_Knight; } else { m_image = Chess.Properties.Resources.b_Knight; } }
public Rook(Color c, ChessSquare s) : base(Piece.eROOK, c, s) { if (c == Color.eWHITE) { m_image = Chess.Properties.Resources.w_Rook; } else { m_image = Chess.Properties.Resources.b_Rook; } }
public ChessBoard(PictureBox[,] pictureBoxes) { for (int r = 0; r < 8; r++) { for (int c = 0; c < 8; c++) { m_b[r, c] = new ChessSquare(r + 1, c + 1); System.Console.WriteLine(m_b[r, c]); } } me = this; }
// Pawn movement rules public override bool CanMoveToLocation(int targetCol, int targetRow, ChessBoard gameBoard) { // If the capture location is valid, then capture if (canCaptureLocation(targetCol, targetRow, gameBoard) == true) { return(true); } LinkedList <ChessSquare> path = new LinkedList <ChessSquare>(); // If the player is white, make sure they are going up if (Player == PlayerType.WHITE) { path = gameBoard.GetSquaresUp(Col, Row, targetCol, targetRow); } // If the player is black, make sure they are going down else if (Player == PlayerType.BLACK) { path = gameBoard.GetSquaresDown(Col, Row, targetCol, targetRow); } // If there are no steps allowed, stop if (path.Count == 0) { return(false); } int maxMoves = 2; // If the pawn has moved, reduce the amount of steps they can take to one if (HasMoved == true) { maxMoves = 1; } for (int i = 0; i < maxMoves; i++) { ChessSquare step = path.ElementAt(i); // If there is a chess piece there then stop moving if (step.ChessPiece != null) { return(false); } // If you can capture, capture! if (step.Col == targetCol && step.Row == targetRow) { return(true); } } return(false); }
// 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); }
//////////// public properties // This constructor needs a reference to the parent Form so it // can configure the individual buttons that make up the squares. public ChessBoard(ChessForm myForm) { // create a new 8x8 array of squares squares = new ChessSquare[8, 8]; // iterate over each column and row for (int col = 0; col < 8; col++) { for (int row = 0; row < 8; row++) { // create a new ChessSquare object squares[col, row] = new ChessSquare(row, col); // add the ChessSquare's button control to the Form myForm.Controls.Add(squares[col, row].button); // link the button's click event to the Form's click handler function squares[col, row].button.Click += new System.EventHandler(myForm.gameSquare_Click); } } }
// 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); }
// Redraw the visible board from the internal chess board public void RedrawBoard() { foreach (Square ChessSquare in Squares) { if (ChessSquare.BackgroundImage == null) // background image doesn't exists { ChessSquare.SetBackgroundSquare(ChessImages); } if (ChessGame.Board[ChessSquare.Name] != null) // Valid board square { ChessSquare.DrawPiece(ChessImages.GetImageForPiece(ChessGame.Board[ChessSquare.Name].piece)); // draw the chess piece image } if (ChessSquare.Name == SelectedSquare && ShowMoveHelp == true) // selected check box { ChessSquare.BackgroundImage = null; ChessSquare.BackColor = System.Drawing.Color.Thistle; } } // Check if need to show the possible legal moves for the current selected piece if (SelectedSquare != null && SelectedSquare != "" && ShowMoveHelp == true && ChessGame.Board[SelectedSquare].piece != null && !ChessGame.Board[SelectedSquare].piece.IsEmpty() && ChessGame.Board[SelectedSquare].piece.Side.type == /*ChessGame.*/ Game.GameTurn) { // Get all legal moves for the current selected item ArrayList moves = ChessGame.GetLegalMoves(ChessGame.Board[SelectedSquare]); // Hightlight all the possible moves for the current player foreach (Cell cell in moves) { Square sqr = GetBoardSquare(cell.ToString()); // get the board by cell position sqr.BackgroundImage = null; // Show a semi-transparent, saddle color sqr.BackColor = System.Drawing.Color.FromArgb(200, System.Drawing.Color.SaddleBrown); } } SelectedSquare = ""; // Reset the selected square position }
public override bool CanMoveToLocation(int targetCol, int targetRow, ChessBoard gameBoard) { bool validMove = false; int moveColumn = Math.Abs(Col - targetCol); int moveRow = Math.Abs(Row - targetRow); if ((moveColumn == 2 & moveRow == 1) || (moveColumn == 1 & moveRow == 2)) { validMove = true; } if (validMove) { ChessSquare targetSquare = gameBoard.GetSquare(targetCol, targetRow); if (targetSquare.ChessPiece != null) { if (targetSquare.ChessPiece.Player == Player) { return(false); } } } return(validMove); }
public override ChessSquare[] GetSquares() { ChessBoard b = ChessBoard.GetBoard(); ChessSquare[] move = new ChessSquare[8]; if (m_square.GetColumn() > 1) { move[0] = b.GetSquare(m_square.GetRow(), m_square.GetColumn() - 1); } else { move[0] = null; } if (m_square.GetColumn() < 8) { move[1] = b.GetSquare(m_square.GetRow(), m_square.GetColumn() + 1); } else { move[0] = null; } if (m_square.GetRow() > 1 && m_square.GetColumn() > 1) { move[2] = b.GetSquare(m_square.GetRow() - 1, m_square.GetColumn() - 1); } else { move[0] = null; } if (m_square.GetRow() < 8 && m_square.GetColumn() < 8) { move[3] = b.GetSquare(m_square.GetRow() + 1, m_square.GetColumn() + 1); } else { move[0] = null; } if (m_square.GetRow() < 8 && m_square.GetColumn() > 1) { move[4] = b.GetSquare(m_square.GetRow() + 1, m_square.GetColumn() - 1); } else { move[0] = null; } if (m_square.GetColumn() > 1 && m_square.GetRow() < 8) { move[5] = b.GetSquare(m_square.GetRow() - 1, m_square.GetColumn() + 1); } else { move[0] = null; } if (m_square.GetRow() < 8) { move[6] = b.GetSquare(m_square.GetRow() + 1, m_square.GetColumn()); } else { move[0] = null; } if (m_square.GetRow() > 1) { move[7] = b.GetSquare(m_square.GetRow() - 1, m_square.GetColumn()); } else { move[0] = null; } m_square.GetRow(); return(null); }
// 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!"); } } } }
public ChessPiece(Piece unit, Color color, ChessSquare s) { m_square = s; s.SetChessPiece(this); }