コード例 #1
0
        // 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);
        }
コード例 #2
0
ファイル: ChessForm.cs プロジェクト: InformalLeaf571/Chess
 // 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!");
             }
         }
     }
 }