예제 #1
0
 /// <summary>
 /// * NAME
 ///     public static void Game::UpdateBoardView(GameDisplay boardView)
 ///     
 /// * DESCRIPTION
 ///     Updates the board display based on board state while retaining Model-View separation.
 ///     That is - the board state lives in Game.cs, while the GUI and its controls live in GameDisplay.cs
 ///     
 /// * RETURNS
 ///     This method returns nothing
 ///     
 /// * AUTHOR
 ///     Daniel Melnikov
 ///     
 /// * DATE
 ///     9/25/15
 ///     
 /// </summary>
 /// <param name="boardView">An object of type GameDisplay that is passed from GameDisplay in the event of a button press
 ///                     that allows Game::UpdateBoardView to call GameDisplay::SetButtonImage corresponding to each square index</param>
 public static void UpdateBoardView(GameDisplay boardView)
 {
     pieceID switchID;
     for (int i = 0; i < 8; i++)
     {
         for (int j = 0; j < 8; j++)
         {
             switchID = (pieceID)Math.Abs(theBoard[i, j]);
             pieceType = theBoard[i, j];
             switch (switchID)
             {
                 case pieceID.Pawn:
                     if (pieceType == 1)
                     {
                         boardView.SetButtonImage(i, j, 1);
                     }
                     else if (pieceType == -1)
                     {
                         boardView.SetButtonImage(i, j, -1);
                     }
                     break;
                 case pieceID.Knight:
                     if (pieceType == 2)
                     {
                         boardView.SetButtonImage(i, j, 2);
                     }
                     else if (pieceType == -2)
                     {
                         boardView.SetButtonImage(i, j, -2);
                     }
                     break;
                 case pieceID.Bishop:
                     if (pieceType == 3)
                     {
                         boardView.SetButtonImage(i, j, 3);
                     }
                     else if (pieceType == -3)
                     {
                         boardView.SetButtonImage(i, j, -3);
                     }
                     break;
                 case pieceID.Rook:
                     if (pieceType == 4)
                     {
                         boardView.SetButtonImage(i, j, 4);
                     }
                     else if (pieceType == -4)
                     {
                         boardView.SetButtonImage(i, j, -4);
                     }
                     break;
                 case pieceID.Queen:
                     if (pieceType == 5)
                     {
                         boardView.SetButtonImage(i, j, 5);
                     }
                     else if (pieceType == -5)
                     {
                         boardView.SetButtonImage(i, j, -5);
                     }
                     break;
                 case pieceID.King:
                     if (pieceType == 6)
                     {
                         boardView.SetButtonImage(i, j, 6);
                     }
                     else if (pieceType == -6)
                     {
                         boardView.SetButtonImage(i, j, -6);
                     }
                     break;
                 default:
                     boardView.SetButtonImage(i, j, 0);
                     break;
             }
         }
     }
 }
예제 #2
0
        /// <summary>
        /// * NAME
        ///     public static void Game::OnClick(int yVal, int xVal, GameDisplay boardView)
        /// 
        /// * DESCRIPTION
        ///     Handles user input from the chessboard GUI by grabbing the appropriate data to pass
        ///     to Game::ValidateMove() and Rules::MakeMove()
        ///     Validates the player move and makes it if valid, initiates the AI master method so it can take its turn
        ///     Checks for pawn conversion after both the player and AI moves
        ///     Calls Game::UpdateBoardView() after both the player and AI moves
        ///     
        /// * RETURNS
        ///     This method returns nothing
        ///     
        /// * AUTHOR
        ///     Daniel Melnikov
        ///     
        /// * DATE
        ///     9/23/15
        ///     
        /// </summary>
        /// <param name="yVal">The Y coordinate of the square whose button was clicked</param>
        /// <param name="xVal">The X coordinate of the square whose button was clicked</param>
        /// <param name="boardView">An object of type GameDisplay that is passed from GameDisplay in the event of a button press
        ///                     that allows Game::UpdateBoardView to call GameDisplay::SetButtonImage corresponding to each square index</param>
        public static void OnClick(int yVal, int xVal, GameDisplay boardView)
        {
            clickvalue = clickcount % 2;
            clickcount++;

            // When clickvalue is even, case 0 is used; when odd, case 1 is used
            // Effectively, the player's first click (selecting the moving piece) triggers case 0
            // While the second click (selecting the target square or piece) triggers case 1
            switch(clickvalue)
            {
                case 0:
                    alphaY = yVal;
                    alphaX = xVal;
                    pieceType = theBoard[alphaY, alphaX];

                    if (pieceType == 0)
                    {
                        clickcount--;
                    }
                    if (pieceType < 0)
                    {
                        clickcount--;
                        MessageBox.Show("Please select a piece of your color");
                    }

                    break;
                case 1:
                    betaY = yVal;
                    betaX = xVal;

                    if(ValidateMove(pieceType, alphaY, alphaX, betaY, betaX))
                    {
                        Rules.MakeMove(pieceType, alphaY, alphaX, betaY, betaX, theBoard);
                        Rules.CheckPawnPromotion(theBoard);
                        UpdateBoardView(boardView);
                        Katana.SwingKatana(theBoard);
                        Rules.CheckPawnPromotion(theBoard);
                        UpdateBoardView(boardView);
                    }
                    else
                    {
                        MessageBox.Show("Invalid move \r\n\r\nPlease try again");
                    }
                    break;
            }
        }