Esempio n. 1
0
        // updates the textbox containing the list of moves made in unicode format
        // uses unicode hexadecimal codes for chess piece icons
        // the move object from the game's history is used as a reference
        private void updateUnicodeMoveBox(Move aMove)
        {
            string tempText = UnicodeMoveBox.Text;

            UnicodeMoveBox.Clear();

            UnicodeMoveBox.AppendText(BoardAdmin.game1.getMoveHistory().Count + ". ");

            if (aMove.PieceMoved.getType() == Piece.pieceType.BISHOP)
            {
                if (BoardAdmin.game1.getTurn() == Piece.Piececolour.BLACK)
                {
                    UnicodeMoveBox.AppendText("\x265D");
                }
                else
                {
                    UnicodeMoveBox.AppendText("\x2657");
                }
            }
            if (aMove.PieceMoved.getType() == Piece.pieceType.KING)
            {
                if (BoardAdmin.game1.getTurn() == Piece.Piececolour.BLACK)
                {
                    UnicodeMoveBox.AppendText("\x265A");
                }
                else
                {
                    UnicodeMoveBox.AppendText("\x2654");
                }
            }
            if (aMove.PieceMoved.getType() == Piece.pieceType.KNIGHT)
            {
                if (BoardAdmin.game1.getTurn() == Piece.Piececolour.BLACK)
                {
                    UnicodeMoveBox.AppendText("\x265E");
                }
                else
                {
                    UnicodeMoveBox.AppendText("\x2658");
                }
            }
            if (aMove.PieceMoved.getType() == Piece.pieceType.PAWN)
            {
                if (BoardAdmin.game1.getTurn() == Piece.Piececolour.BLACK)
                {
                    UnicodeMoveBox.AppendText("\x265F");
                }
                else
                {
                    UnicodeMoveBox.AppendText("\x2659");
                }
            }
            if (aMove.PieceMoved.getType() == Piece.pieceType.QUEEN)
            {
                if (BoardAdmin.game1.getTurn() == Piece.Piececolour.BLACK)
                {
                    UnicodeMoveBox.AppendText("\x265B");
                }
                else
                {
                    UnicodeMoveBox.AppendText("\x2655");
                }
            }
            if (aMove.PieceMoved.getType() == Piece.pieceType.ROOK)
            {
                if (BoardAdmin.game1.getTurn() == Piece.Piececolour.BLACK)
                {
                    UnicodeMoveBox.AppendText("\x265C");
                }
                else
                {
                    UnicodeMoveBox.AppendText("\x2656");
                }
            }

            string initialPosition = squares[aMove.initialPosition.X, aMove.initialPosition.Y].Name;
            string finalPosition   = squares[aMove.finalPosition.X, aMove.finalPosition.Y].Name;



            UnicodeMoveBox.AppendText(" " + initialPosition + "\x02C3\x02C3 " + finalPosition);

            if (aMove.pieceCaptured != null)
            {
                UnicodeMoveBox.AppendText(Environment.NewLine + "Captured: ");

                if (aMove.pieceCaptured.getType() == Piece.pieceType.BISHOP)
                {
                    if (aMove.pieceCaptured.getColour() == Piece.Piececolour.BLACK)
                    {
                        UnicodeMoveBox.AppendText("\x265D");
                    }
                    else
                    {
                        UnicodeMoveBox.AppendText("\x2657");
                    }
                }
                if (aMove.pieceCaptured.getType() == Piece.pieceType.KING)
                {
                    if (aMove.pieceCaptured.getColour() == Piece.Piececolour.BLACK)
                    {
                        UnicodeMoveBox.AppendText("\x265A");
                    }
                    else
                    {
                        UnicodeMoveBox.AppendText("\x2654");
                    }
                }
                if (aMove.pieceCaptured.getType() == Piece.pieceType.KNIGHT)
                {
                    if (aMove.pieceCaptured.getColour() == Piece.Piececolour.BLACK)
                    {
                        UnicodeMoveBox.AppendText("\x265E");
                    }
                    else
                    {
                        UnicodeMoveBox.AppendText("\x2658");
                    }
                }
                if (aMove.pieceCaptured.getType() == Piece.pieceType.PAWN)
                {
                    if (aMove.pieceCaptured.getColour() == Piece.Piececolour.BLACK)
                    {
                        UnicodeMoveBox.AppendText("\x265F");
                    }
                    else
                    {
                        UnicodeMoveBox.AppendText("\x2659");
                    }
                }
                if (aMove.pieceCaptured.getType() == Piece.pieceType.QUEEN)
                {
                    if (aMove.pieceCaptured.getColour() == Piece.Piececolour.BLACK)
                    {
                        UnicodeMoveBox.AppendText("\x265B");
                    }
                    else
                    {
                        UnicodeMoveBox.AppendText("\x2655");
                    }
                }
                if (aMove.pieceCaptured.getType() == Piece.pieceType.ROOK)
                {
                    if (aMove.pieceCaptured.getColour() == Piece.Piececolour.BLACK)
                    {
                        UnicodeMoveBox.AppendText("\x265C");
                    }
                    else
                    {
                        UnicodeMoveBox.AppendText("\x2656");
                    }
                }
            }

            UnicodeMoveBox.AppendText(Environment.NewLine);
            UnicodeMoveBox.AppendText(tempText);
        }
Esempio n. 2
0
        // event that is run when a piece is being moved
        private void PieceMovingClickEvent(PictureBox currentPicBox)
        {
            Square clickedSquare = new Square();

            // gets the square that was clicked by mapping the picture box array index onto the square array.
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    if (squares[i, j] == currentPicBox)
                    {
                        clickedSquare = gameBoard.getSquares()[i, j];
                    }
                }
            }



            if (possiblemoves != null)
            {
                // if the clicked square is one of the possible moves then make the move
                if (possiblemoves.Contains(clickedSquare) == true)
                {
                    if (BoardAdmin.game1.getPlayer1().getPlayerColour() == BoardAdmin.game1.getTurn())
                    {
                        BoardAdmin.game1.getPlayer1().makeMove(clickedSquare, gameBoard, clickedPiece);
                    }
                    if (BoardAdmin.game1.getPlayer2().getPlayerColour() == BoardAdmin.game1.getTurn())
                    {
                        BoardAdmin.game1.getPlayer2().makeMove(clickedSquare, gameBoard, clickedPiece);
                    }

                    // update the move history text box with the latest move
                    updateUnicodeMoveBox(BoardAdmin.game1.getMoveHistory().Last <Move>());

                    if (BoardAdmin.game1.getTurn() == Piece.Piececolour.WHITE)
                    {
                        BoardAdmin.game1.setTurn(Piece.Piececolour.BLACK);
                    }
                    else
                    {
                        BoardAdmin.game1.setTurn(Piece.Piececolour.WHITE);
                    }
                }

                //This is where the maths question is called - a check is needed to determine that the click event
                //corresponds to a moving piece of the current player.
                if (clickedPiece != null)
                {
                    if (clickedPiece.getColour() != BoardAdmin.game1.getTurn())
                    {
                        if (BoardAdmin.game1.getGameType() == Game.gameType.Maths)
                        // if it is a maths game then a question must be presented to the player
                        {
                            AskQuestion aAskQuestionForm = new AskQuestion();

                            UpdateViewTimer.Enabled = false;

                            if (aAskQuestionForm.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                            {
                                //Waits until the question form has been dealt with
                            }
                            UpdateViewTimer.Enabled = true;
                        }
                    }
                }

                clickedPiece = null;
                Array.Clear(possiblemoves, 0, possiblemoves.Length);

                // checks whether this move has resulted in check mate
                if (isCheckMated() == true)
                {
                    UpdateViewTimer.Enabled = false;

                    foreach (PictureBox square in squares)
                    {
                        square.Enabled = false;
                    }
                    string TempText = UnicodeMoveBox.Text;
                    UnicodeMoveBox.Clear();

                    // updates the unicode move box with the game result
                    if (BoardAdmin.game1.getTurn() == Piece.Piececolour.BLACK)
                    {
                        BoardAdmin.game1.setGameResult(Game.result.WhiteWon);
                        MessageBox.Show("WHITE WON! \x2654  (CheckMate)");
                        UnicodeMoveBox.AppendText("WHITE WON! \x2654  (CheckMate)" + Environment.NewLine + TempText);
                    }
                    else
                    {
                        BoardAdmin.game1.setGameResult(Game.result.BlackWon);
                        MessageBox.Show("BLACK WON! \x265A  (CheckMate)");
                        UnicodeMoveBox.AppendText("BLACK WON! \x265A  (CheckMate)" + Environment.NewLine + TempText);
                    }
                    // disable the undo move button as the game has finished
                    // the user may still look throught the list f moves made in the text box
                    undoMoveButton.Enabled = false;
                }
            }
        }