예제 #1
0
        // this is where the moves of the game are made, and where the moves are added to history
        public void makeMove(Square targetposition, Board gameBoard, Piece movingPiece)
        {
            PieceSet[] pieceSets   = gameBoard.getPieceSets();
            PieceSet   BlackPieces = pieceSets[0];
            PieceSet   WhitePieces = pieceSets[1];

            //Move object created and populated
            Move aMove = new Move();

            aMove.finalPosition   = targetposition;
            aMove.initialPosition = movingPiece.getPosition();
            aMove.PieceMoved      = movingPiece;

            if (BoardAdmin.game1.getTurn() == BoardAdmin.game1.getPlayer1().getPlayerColour())

            {
                aMove.PlayerMoved = BoardAdmin.game1.getPlayer1();


                if (BoardAdmin.game1.getPlayer1().getPlayerColour() == Piece.Piececolour.WHITE)

                {
                    foreach (Piece piece in gameBoard.getPieceSets()[0].getPieceSet())

                    {
                        if (piece.getPosition() == targetposition)

                        {
                            aMove.pieceCaptured = piece;
                        }
                    }

                    BlackPieces.getPieceSet().Remove(aMove.pieceCaptured);
                }
                if (BoardAdmin.game1.getPlayer1().getPlayerColour() == Piece.Piececolour.BLACK)

                {
                    foreach (Piece piece in gameBoard.getPieceSets()[1].getPieceSet())

                    {
                        if (piece.getPosition() == targetposition)

                        {
                            aMove.pieceCaptured = piece;
                        }
                    }

                    WhitePieces.getPieceSet().Remove(aMove.pieceCaptured);
                }
            }
            else // otherwise it is player 2's turn so do exactly the same but for player 2
            {
                aMove.PlayerMoved = BoardAdmin.game1.getPlayer2();


                if (BoardAdmin.game1.getPlayer2().getPlayerColour() == Piece.Piececolour.WHITE)
                {
                    foreach (Piece piece in gameBoard.getPieceSets()[0].getPieceSet())
                    {
                        if (piece.getPosition() == targetposition)
                        {
                            aMove.pieceCaptured = piece;
                        }
                    }
                    BlackPieces.getPieceSet().Remove(aMove.pieceCaptured);
                }
                if (BoardAdmin.game1.getPlayer2().getPlayerColour() == Piece.Piececolour.BLACK)
                {
                    foreach (Piece piece in gameBoard.getPieceSets()[1].getPieceSet())
                    {
                        if (piece.getPosition() == targetposition)
                        {
                            aMove.pieceCaptured = piece;
                        }
                    }
                    WhitePieces.getPieceSet().Remove(aMove.pieceCaptured);
                }
            }


            if (movingPiece.getType() == Piece.pieceType.KING)
            // if the moving piece is a king, castling may occur
            {
                King movingKing = (King)movingPiece;
                if (movingKing.canCastle(targetposition, gameBoard) == true)
                // invokes the canCastle method of the king class to check whether castling can occur
                {
                    BoardAdmin.isCastling = true;

                    Rook movingRook = null;

                    if (this.getPlayerColour() == Piece.Piececolour.WHITE)

                    {
                        foreach (Piece piece in WhitePieces.getPieceSet())
                        {
                            if (piece.getType() == Piece.pieceType.ROOK && Math.Abs(piece.getPosition().X - movingKing.getPosition().X) == 3)

                            {
                                movingRook = (Rook)piece;
                            }
                        }
                    }
                    if (this.getPlayerColour() == Piece.Piececolour.BLACK)

                    {
                        foreach (Piece piece in BlackPieces.getPieceSet())
                        {
                            if (piece.getType() == Piece.pieceType.ROOK && Math.Abs(piece.getPosition().X - movingKing.getPosition().X) == 3)
                            {
                                movingRook = (Rook)piece;
                            }
                        }
                    }
                    BoardAdmin.KingsideRook = movingRook;
                    movingKing.PerformCastle(BoardAdmin.KingsideRook, movingKing, targetposition, gameBoard);
                    // invokes the performCastle method in the king class to make the castling move.

                    if (BoardAdmin.TempMoving == false)
                    // if temp moving is true, then the move needs to be retracted so the boolean representing castling needs to remain true otherwise the kingside rook will not be moved back aswell as the king
                    {
                        BoardAdmin.isCastling = false;
                        // so if temp moving is false, the castling can be made false aswell as the move is not temporary.
                    }
                }
            }


            // adjusts properties of the piece moving and the squares involved
            movingPiece.getPosition().occ = false;
            movingPiece.setPosition(targetposition);
            movingPiece.getPosition().occ = true;
            movingPiece.IncrementMoveCount(1);

            if (movingPiece.getType() == Piece.pieceType.PAWN) // if the moving piece is a pawn, promototion may occur
            {
                if (movingPiece.getPosition().Y == 0 || movingPiece.getPosition().Y == 7)
                //if either the bottom or the top of the board is reached
                {
                    if (BoardAdmin.TempMoving == false) // only promotes pawn if it is not a temporary move
                    {
                        movingPiece.promotePawn();      // invokes the promotePawn method to perform the move.
                    }
                }
            }

            BoardAdmin.game1.addTomoveHistory(aMove);
        }
예제 #2
0
        //initiallises all piece objects and fills up the corresponding piece list.

        public void setInitalPieceList(Piece.Piececolour piececolour, Square[,] squares, Piece.Piececolour player1colour)
        {
            int index;     // the index of the pieces on the board (first row pieces - king, rook etc)
            int pawnindex; // pawns are in the second row, so have their own index


            setColour(piececolour);

            //creates all the piece objects neccessary
            Pawn[]   Blackpawns   = new Pawn[8];
            Pawn[]   Whitepawns   = new Pawn[8];
            Rook[]   BlackRooks   = new Rook[2];
            Rook[]   WhiteRooks   = new Rook[2];
            Bishop[] WhiteBishops = new Bishop[2];
            Bishop[] BlackBishops = new Bishop[2];
            Knight[] WhiteKnights = new Knight[2];
            Knight[] BlackKnights = new Knight[2];
            Queen    WhiteQueen   = new Queen();
            King     WhiteKing    = new King();
            Queen    BlackQueen   = new Queen();
            King     BlackKing    = new King();

            int i = 0;

            //Initiallise all piece objects
            for (int j = 0; j < 8; j++)
            {
                Blackpawns[j] = new Pawn();
                Whitepawns[j] = new Pawn();
            }

            for (int j = 0; j < 2; j++)
            {
                WhiteBishops[j] = new Bishop();
                BlackBishops[j] = new Bishop();
            }

            for (int j = 0; j < 2; j++)
            {
                BlackRooks[j] = new Rook();
                WhiteRooks[j] = new Rook();
            }
            for (int j = 0; j < 2; j++)
            {
                WhiteKnights[j] = new Knight();
                BlackKnights[j] = new Knight();
            }


            //fills up the list with the pieces and setting their properties

            if (colour == Piece.Piececolour.BLACK)
            {
                i = 0; // the x coordinate this can be reassigned as the pieces are populated


                //player 1 pieces needs to be at the bottom of the board so assign the indexes accordingly
                if (player1colour == Piece.Piececolour.BLACK)
                {
                    index     = 7;
                    pawnindex = 6;
                }
                else
                {
                    index     = 0;
                    pawnindex = 1;
                }

                // sets properties of pawns
                foreach (Pawn pawn in Blackpawns)
                {
                    pawn.setType(Piece.pieceType.PAWN);
                    pawn.setColour(Piece.Piececolour.BLACK);
                    pawn.setImage(test_chess.Properties.Resources.Chess_pdt60);
                    pawn.setPosition(squares[i, pawnindex]);
                    if (player1colour == Piece.Piececolour.BLACK)
                    {
                        pawn.movedirection = -1;
                    }
                    else
                    {
                        pawn.movedirection = 1;
                    }
                    m_pieceSet.Add(pawn);
                    i++;
                }

                i = 0; // x coordinate value
                // sets properties of rooks
                foreach (Rook rook in BlackRooks)
                {
                    rook.setType(Piece.pieceType.ROOK);
                    rook.setColour(Piece.Piececolour.BLACK);
                    rook.setImage(test_chess.Properties.Resources.Chess_rdt60);
                    rook.setPosition(squares[i, index]);
                    m_pieceSet.Add(rook);

                    i = 7;
                }

                i = 1;// x coordinate value
                foreach (Knight knight in BlackKnights)
                {
                    knight.setType(Piece.pieceType.KNIGHT);
                    knight.setColour(Piece.Piececolour.BLACK);
                    knight.setImage(test_chess.Properties.Resources.Chess_ndt60);
                    knight.setPosition(squares[i, index]);
                    m_pieceSet.Add(knight);
                    i = 6;
                }


                i = 2; // x coordinate value

                foreach (Bishop bishop in BlackBishops)
                {
                    bishop.setType(Piece.pieceType.BISHOP);
                    bishop.setColour(Piece.Piececolour.BLACK);
                    bishop.setImage(test_chess.Properties.Resources.Chess_bdt60);
                    bishop.setPosition(squares[i, index]);
                    m_pieceSet.Add(bishop);
                    i = 5;
                }


                BlackKing.setType(Piece.pieceType.KING);
                BlackKing.setColour(Piece.Piececolour.BLACK);
                BlackKing.setImage(test_chess.Properties.Resources.Chess_kdt60);
                BlackKing.setPosition(squares[3, index]);


                BlackQueen.setType(Piece.pieceType.QUEEN);
                BlackQueen.setColour(Piece.Piececolour.BLACK);
                BlackQueen.setImage(test_chess.Properties.Resources.Chess_qdt60);
                BlackQueen.setPosition(squares[4, index]);

                m_pieceSet.Add(BlackKing);

                m_pieceSet.Add(BlackQueen);
            }
            else
            {
                // otherwise player 1 is black and so black pieces must be at the bottom of the board
                if (player1colour == Piece.Piececolour.BLACK)
                {
                    index     = 0;
                    pawnindex = 1;
                }
                else
                {
                    index     = 7;
                    pawnindex = 6;
                }

                i = 0;

                foreach (Pawn pawn in Whitepawns)
                {
                    pawn.setType(Piece.pieceType.PAWN);
                    pawn.setColour(Piece.Piececolour.WHITE);
                    pawn.setImage(test_chess.Properties.Resources.Chess_plt60);
                    pawn.setPosition(squares[i, pawnindex]);
                    if (player1colour == Piece.Piececolour.WHITE)
                    {
                        pawn.movedirection = -1;
                    }
                    else
                    {
                        pawn.movedirection = 1;
                    }
                    m_pieceSet.Add(pawn);
                    i++;
                }

                i = 0;

                foreach (Rook rook in WhiteRooks)
                {
                    rook.setType(Piece.pieceType.ROOK);
                    rook.setColour(Piece.Piececolour.WHITE);
                    rook.setImage(test_chess.Properties.Resources.Chess_rlt60);
                    rook.setPosition(squares[i, index]);
                    m_pieceSet.Add(rook);
                    i = 7;
                }


                i = 2;

                foreach (Bishop bishop in WhiteBishops)
                {
                    bishop.setType(Piece.pieceType.BISHOP);
                    bishop.setColour(Piece.Piececolour.WHITE);
                    bishop.setImage(test_chess.Properties.Resources.Chess_blt60);
                    bishop.setPosition(squares[i, index]);
                    m_pieceSet.Add(bishop);
                    i = 5;
                }

                i = 1;

                foreach (Knight knight in WhiteKnights)
                {
                    knight.setType(Piece.pieceType.KNIGHT);
                    knight.setColour(Piece.Piececolour.WHITE);
                    knight.setImage(test_chess.Properties.Resources.Chess_nlt60);
                    knight.setPosition(squares[i, index]);
                    m_pieceSet.Add(knight);
                    i = 6;
                }

                WhiteKing.setType(Piece.pieceType.KING);
                WhiteKing.setColour(Piece.Piececolour.WHITE);
                WhiteKing.setImage(test_chess.Properties.Resources.Chess_klt60);
                WhiteKing.setPosition(squares[3, index]);

                WhiteQueen.setType(Piece.pieceType.QUEEN);
                WhiteQueen.setColour(Piece.Piececolour.WHITE);
                WhiteQueen.setImage(test_chess.Properties.Resources.Chess_qlt60);
                WhiteQueen.setPosition(squares[4, index]);

                m_pieceSet.Add(WhiteQueen);
                m_pieceSet.Add(WhiteKing);
            }
        }