Example #1
0
        // performs the castling procedure
        // moves the kingside rook as the standard makeMove algorithm in the player class handles the king piece as that is the primary piece involved in the move and is the piece that is clicked to perform the castle move.
        public void PerformCastle(Rook kingsideRook, King movingKing, Square targetPosition, Board gameBoard)
        {
            // sets the target position for the rook
            Square rookTargetPosition = gameBoard.getSquares()[targetPosition.X + 1, targetPosition.Y];


            //creates a move object for the rook and populates it
            Move aMove = new Move();

            aMove.initialPosition = kingsideRook.getPosition();
            aMove.finalPosition   = rookTargetPosition;
            aMove.PieceMoved      = kingsideRook;

            if (kingsideRook.getMoveCount() == 0)
            {
                aMove.castled = true;
            }
            if (BoardAdmin.game1.getTurn() == BoardAdmin.game1.getPlayer1().getPlayerColour())

            {
                aMove.PlayerMoved = BoardAdmin.game1.getPlayer1();
            }
            else
            {
                aMove.PlayerMoved = BoardAdmin.game1.getPlayer2();
            }

            //sets properties of kingside rook piece
            BoardAdmin.game1.addTomoveHistory(aMove); // adds the move object to the list of moves for the game history
            kingsideRook.getPosition().occ = false;
            kingsideRook.setPosition(rookTargetPosition);
            kingsideRook.getPosition().occ = true;
            kingsideRook.IncrementMoveCount(1);
        }
Example #2
0
        public void updateView()
        {
            //string[,] boardPositions = generateBoardPositions();
            //PictureBox[,] squares = new PictureBox[8, 8];

            //for (int i = 0; i < 8; i++)
            //{
            //    for (int j = 0; j < 8; j++)
            //    {
            //        squares[i, j] = (PictureBox)this.Controls.Find(boardPositions[i, j], true)[0];


            //    }
            //}



            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    if (BlackRook1.getPosition() == squares[i, j].Name)
                    {
                        squares[i, j].Image = test_chess.Properties.Resources.Rook2;
                    }
                    else
                    {
                        squares[i, j].Image = null;
                    }
                }
            }
        }
Example #3
0
        // evaluates whether the king can perform the castle procedure by finding the kingside rook and determining
        // whether both the king and rook havent moved yet and whether the spaces between them are empty
        public bool canCastle(Square targetPosition, Board gameBoard)
        {
            // if the target position is castling then the square will be 2 x - coordinates away
            if (Math.Abs(targetPosition.X - this.getPosition().X) == 2)
            {
                PieceSet blackpieces = gameBoard.getPieceSets()[0];
                PieceSet whitepieces = gameBoard.getPieceSets()[1];

                Rook kingsideRook = null;

                if (this.getColour() == Piececolour.BLACK)

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

                            {
                                kingsideRook = (Rook)piece;
                            }
                        }
                    }
                }
                if (this.getColour() == Piececolour.WHITE)

                {
                    foreach (Piece piece in whitepieces.getPieceSet())

                    {
                        if (piece.getType() == pieceType.ROOK)

                        {
                            if (Math.Abs(this.getPosition().X - piece.getPosition().X) == 3)

                            {
                                kingsideRook = (Rook)piece;
                            }
                        }
                    }
                }

                if (kingsideRook != null)

                {
                    if (kingsideRook.getMoveCount() == 0)

                    {
                        if (this.getMoveCount() == 0)

                        {
                            if (targetPosition.Y == this.getPosition().Y&& targetPosition.occ != true)
                            // checks that the y cooridnate of the target square is the same as the king's and also that the target square is not occupied
                            {
                                for (int i = this.getPosition().X - 1; i > kingsideRook.getPosition().X; i--)
                                // loops through between the rook and the king, to check the squares in between to make sure they are not occupied.
                                {
                                    if (gameBoard.getSquares()[i, this.getPosition().Y].occ == true)

                                    {
                                        return(false);
                                    }
                                }
                                if (targetPosition.X < this.getPosition().X)
                                // ensures that the algorithm only returns true for the correct target square ( in between rook and king ) and not the other side of the king
                                {
                                    return(true);
                                }
                                else
                                {
                                    return(false);
                                }
                            }
                            else // otherwise, if the square is occupied or the y coordinates are not the same
                            {
                                return(false);
                            }
                        }
                        else // if the king has already moved then return false
                        {
                            return(false);
                        }
                    }
                    else // if the rook has already moved before then return false
                    {
                        return(false);
                    }
                }
                else // if the kingside rook variable is null then return false
                {
                    return(false);
                }
            }
            else // if the target square is not 2 x-coordinates away then return false
            {
                return(false);
            }
        }