Esempio n. 1
0
        private void GridButton_Click(object sender, RoutedEventArgs e)
        {
            // Get info about Cell
            Button clickedButton = (sender as Button);
            Cell   clickedCell   = clickedButton.Tag as Cell;

            List <Pieces> whitePieces;
            List <Pieces> blackPieces;

            // If user intends to move
            if (clickedCell.IsLegal)
            {
                // Move the piece and update the board
                PrintMoveHistory(currentCell, clickedCell);
                chessboard.MovePiece(currentCell, clickedCell);

                if (clickedCell.Piece.Name == "Pawn" && (clickedCell.Row == 7 || clickedCell.Row == 0))
                {
                    PromotionBackground.Visibility = Visibility.Visible;
                    Promotion.Visibility           = Visibility.Visible;
                }

                UpdateBoardState();

                // Check if game over after white move
                whitePieces = chessboard.SearchForPieces(true);
                blackPieces = chessboard.SearchForPieces(false);

                if (chessboard.KingInCheckForOtherSide(whitePieces))
                {
                    Message.Text = "Black King in check";
                }

                // Start new game if it's game over, else go to next turn
                if (IsGameOver(whitePieces, blackPieces))
                {
                    chessboard.NewGame();
                }
                else
                {
                    ComputerMove(blackPieces);
                }

                // Check if game over after black move
                blackPieces = chessboard.SearchForPieces(false);
                whitePieces = chessboard.SearchForPieces(true);

                if (chessboard.KingInCheckForOtherSide(blackPieces))
                {
                    Message.Text = "White King in check";
                }

                if (IsGameOver(whitePieces, blackPieces))
                {
                    chessboard.NewGame();
                }
            }

            else if (clickedCell.IsOccupied)
            {
                // Find legal moves if user clicks on white piece, do nothing if black piece that is not legal
                if (clickedCell.Piece.IsWhite)
                {
                    chessboard.FindLegalMoves(clickedCell.Piece);
                }
                else
                {
                    return;
                }
            }

            // If user didn't click on legal or occupied
            else
            {
                chessboard.ClearMarkedLegalMoves();
            }

            currentCell = clickedCell;

            UpdateBoardState();
        }