Beispiel #1
0
        public AbstractPiece clearPiece(int x, int y)
        {
            AbstractPiece clearedPiece = board[x, y];

            board[x, y] = null;
            return(clearedPiece);
        }
Beispiel #2
0
        public AbstractPiece clearPiece(Location position)
        {
            AbstractPiece clearedPiece = board[position.x, position.y];

            board[position.x, position.y] = null;
            return(clearedPiece);
        }
Beispiel #3
0
        //Clears a piece from the 2d array and the master list of pieces
        public AbstractPiece clearPiece(AbstractPiece piece)
        {
            AbstractPiece clearedPiece = board[piece.location.x, piece.location.y];

            board[piece.location.x, piece.location.y] = null;
            return(clearedPiece);
        }
Beispiel #4
0
        ///Methods

        /*
         * A couple of methods to create a deep copy of the object
         */
        public AbstractPiece Copy()
        {
            object[] constructor = { this };

            AbstractPiece newPiece = (AbstractPiece)Activator.CreateInstance(this.GetType(), constructor);

            return(newPiece);
        }
Beispiel #5
0
        //Deep copy of a GameBoard
        public GameBoard Copy()
        {
            //Needs to be a deep copy
            GameBoard newBoard = new GameBoard(this.xLength, this.yLength);

            newBoard.addPieces(AbstractPiece.Copy(this.pieces));

            return(newBoard);
        }
Beispiel #6
0
        /*
         * Gets the original pieces moves then filters them through the filter event which can be handled by any client
         */
        public MoveList getMoves(Location startLocation)
        {
            if (!pieceAtLocation(startLocation))
            {
                return(null);
            }

            AbstractPiece piece = getPiece(startLocation);

            return(OnFilterMoves(piece, piece.getMoves(this)));
        }
Beispiel #7
0
        public AbstractPiece removePiece(AbstractPiece piece)
        {
            if (locationOnBoard(piece.location))
            {
                clearPiece(piece);
                pieces.Remove(piece);

                return(piece);
            }

            return(null);
        }
Beispiel #8
0
        //Adds pieces to both the 2d Array and the master list
        public bool addPiece(AbstractPiece piece)
        {//Might have it update the tile it is added to
            if (locationOnBoard(piece.location) && !pieceAtLocation(piece.location))
            {
                setPiece(piece);
                pieces.Add(piece);

                return(true);
            }

            return(false);
        }
Beispiel #9
0
        protected MoveList OnFilterMoves(AbstractPiece piece, MoveList moves)
        {
            MoveFilteringHandler handler = FilterMoves;

            if (handler != null)
            {
                return(handler(piece, moves));
            }

            //Nothing was handling this event pass on the original
            return(moves);
        }
Beispiel #10
0
        public IList <AbstractPiece> removePieces(IList <AbstractPiece> pieces)
        {
            IList <AbstractPiece> removedPieces = new List <AbstractPiece>();

            foreach (AbstractPiece piece in pieces)
            {
                AbstractPiece removed = removePiece(piece);
                if (removed != null)
                {
                    removedPieces.Add(removed);
                }
            }

            return(removedPieces);
        }
Beispiel #11
0
        public IList <AbstractPiece> getPieces(IList <Location> locals)
        {
            IList <AbstractPiece> returnList = new List <AbstractPiece>();

            foreach (Location local in locals)
            {
                AbstractPiece piece = getPiece(local);
                if (piece != null)
                {
                    returnList.Add(piece);
                }
            }

            return(returnList);
        }
Beispiel #12
0
        public bool isPieceBlockingAttack(AbstractPiece piece, Location blockedAttack)
        {
            GameBoard copy = this.Copy();

            copy.removePiece(piece);//Allows for simulation

            IList <AbstractPiece> attackers = getAttackers(piece);

            foreach (AbstractPiece attacker in attackers)
            {
                MoveList moves = copy.getMoves(attacker);
                if (moves.ContainsAttacked(blockedAttack))
                {
                    return(true);
                }
            }

            return(false);
        }
Beispiel #13
0
        public override bool Equals(object comparison)
        {
            if (comparison == null)
            {
                return(false);
            }

            if (comparison.GetType().IsSubclassOf(typeof(AbstractPiece)))
            {
                AbstractPiece piece = (AbstractPiece)comparison;
                if (piece.getPieceType() == this.getPieceType() &&
                    piece.location == this.location &&
                    piece.color == this.color &&
                    piece.hasMoved == this.hasMoved)
                {
                    return(true);
                }
            }


            return(false);
        }
Beispiel #14
0
        private bool moveIndividualPiece(Move move)
        {
            if (pieceAtLocation(move.startLocation) && locationOnBoard(move.endLocation))
            {
                if (move.IgnorePossibleMoves() || checkMove(move))
                {///Once in here execute the steps of the move
                    //Remove anypiece that is sitting at the end location
                    foreach (Location attacked in move.GetAttackedLocations())
                    {
                        removePiece(getPiece(attacked));
                    }

                    //Remove from old position
                    AbstractPiece piece = clearPiece(move.startLocation);

                    //replace onto the board in the new position
                    piece.location = move.endLocation;
                    //Need to let the piece know it has moved
                    piece.setHasMoved(true);
                    setPiece(piece);

                    //Execute subsequent moves
                    foreach (Move subsequentMove in move.GetSubsequentMoves())
                    {
                        //Allows for complex moves involving multiple pieces
                        //Might have bad ramifications that need to be handled
                        moveIndividualPiece(subsequentMove);
                    }

                    //Certain pieces can have added functionality after the move
                    piece.afterMoveAction(this);

                    return(true);
                }
            }

            return(false);
        }
Beispiel #15
0
        public IList <AbstractPiece> getAttackers(Location startLocation, ColorEnum color)
        {
            if (!locationOnBoard(startLocation))
            {
                return(null);
            }

            //Loop through each derived type
            IList <AbstractPiece> attackers = new List <AbstractPiece>();

            foreach (Type type in AbstractPiece.getDerivedTypes())
            {
                Type[]     parameterTypes = { typeof(GameBoard), typeof(Location), typeof(ColorEnum) };
                MethodInfo method         = type.GetMethod("getPieceMoves", parameterTypes);
                if (method == null)
                {
                    continue; // Should have exceptions here
                }
                object[] parameters = { this, startLocation, color }, constructor = { color, startLocation };

                MoveList moves = (MoveList)method.Invoke(Activator.CreateInstance(type, constructor), parameters);
                foreach (Move move in moves)
                {
                    IList <AbstractPiece> pieces = getPieces(move.GetAttackedLocations());
                    foreach (AbstractPiece piece in pieces)//Kinda wonky
                    {
                        if (piece != null && piece.GetType().Equals(type))
                        {
                            //Means the reverse attack met with a piece of the correct type
                            attackers.Add(piece);
                        }
                    }
                }
            }

            return(attackers);
        }
Beispiel #16
0
        /*
         * Modifies the moves that come from GameBoard.getMoves() by handling the FilterMoves event
         * This allows for programmers to have more control over the moves that pieces are allowed to make
         *
         * Currently adds the Castling to a kings moveset
         * As well as disallowing pieces to move in a way the puts the king into check
         */
        public MoveList FilteringMoves(AbstractPiece piece, MoveList moves)
        {
            //Need to filter certain moves out of this and return a new list
            //Need to filter out moves that put the king in check (By moving out of the way), having the king move into check, and that don't stop the king from being in check (Not taking out the current king attacker or blocking the attack path)

            if (piece.getPieceType().Contains("King"))
            {
                MoveList kingMoves = new MoveList();

                //Readding moves with the added specification that they can't be attacked
                foreach (Move move in moves)
                {
                    if (!gameBoard.checkAttacked(move.endLocation, piece.color))
                    {
                        kingMoves.Add(move);
                    }
                }

                //Add castling as a special move
                if (!piece.getHasMoved())
                {
                    //check for rooks in position that have not moved
                    int baseDistance = 2;
                    for (int i = -1; i <= 1; i += 2)
                    {
                        //baseDistance +1 or +0
                        for (int j = 1; j <= 2; j++)
                        {
                            Location rook = piece.location.Shift(i * (baseDistance + j), 0);
                            if (gameBoard.locationOnBoard(rook) && gameBoard.pieceAtLocation(rook))
                            {
                                AbstractPiece checkedPiece = gameBoard.getPiece(rook);
                                if (checkedPiece.getPieceType() == "Rook" && checkedPiece.color == piece.color && !checkedPiece.getHasMoved())
                                {
                                    //The piece itself is fine now check for spaces inbetween
                                    bool allEmpty = true;
                                    for (int x = rook.x - i; x != piece.location.x; x -= i)
                                    {
                                        Location emptyLocation = new Location(x, piece.location.y);
                                        if (!gameBoard.locationOnBoard(emptyLocation) || gameBoard.pieceAtLocation(emptyLocation))
                                        {
                                            allEmpty = false;
                                            break;
                                        }
                                    }

                                    //And check to see if the spaces the king will pass are under attack
                                    bool allNotUnderAttack = true;
                                    for (int x = piece.location.x; x != piece.location.x + (baseDistance + 1) * i; x += i)
                                    {
                                        Location attackLocation = new Location(x, piece.location.y);
                                        if (gameBoard.checkAttacked(attackLocation, piece.color))
                                        {
                                            allNotUnderAttack = false;
                                            break;
                                        }
                                    }

                                    if (allEmpty && allNotUnderAttack)
                                    {
                                        //Need to switch over to a move based system
                                        Move kingMove = new Move(piece.location, piece.location.Shift(i * baseDistance, 0));
                                        Move rookMove = new Move(rook, piece.location.Shift(i * (baseDistance - 1), 0));
                                        rookMove.SetIgnorePossibleMoves(true);
                                        kingMove.AddSubsequentMove(rookMove);
                                        kingMoves.Add(kingMove);
                                    }
                                }
                            }
                        }
                    }
                }

                //A special case that needs to be handled externally
                return(kingMoves);
            }
            //Filter out moves that put the king in check
            else
            {
                MoveList returnMoves = moves.Copy();

                //Filter out moves that don't stop a check
                IList <AbstractPiece> kings     = gameBoard.getPieces(typeof(King), piece.color);
                IList <AbstractPiece> attackers = gameBoard.getAttackers(kings);
                if (attackers.Count > 0)
                {
                    for (int i = 0; i < returnMoves.Count; i++)
                    {
                        Move move = returnMoves[i];

                        bool unhandled = true;
                        foreach (AbstractPiece attacker in attackers)
                        {
                            //Take out the attacking piece or Block the attack
                            if (move.GetAttackedLocations().Contains(attacker.location) || gameBoard.doesMoveBlockMove(attacker.getMoves(gameBoard).GetAttackedMove(kings[0].location), move))
                            {
                                continue;
                            }
                            else
                            {
                                //If any attacker is still attacking the king after the move, move is invalid
                                unhandled = false;
                                break;
                            }
                        }

                        if (!unhandled)
                        {
                            returnMoves.Remove(move);
                            i--;//Needs back down one index to account for the removed object
                        }
                    }
                }

                //Filter out moves that put the king into check
                returnMoves = gameBoard.filterMovesThatBlockAttack(returnMoves, kings[0]);

                return(returnMoves);
            }

            return(moves);
        }
Beispiel #17
0
 public void setPiece(AbstractPiece piece, int x, int y)
 {
     board[x, y] = piece;
 }
Beispiel #18
0
 //Takes a list of moves and removes any that allow an attack to pass
 public MoveList filterMovesThatBlockAttack(MoveList moves, AbstractPiece blockedPiece)
 {
     return(filterMovesThatBlockAttack(moves, blockedPiece.location));
 }
Beispiel #19
0
 //Checks if a move is blocking a specific attack
 //May have different results from isPieceBlockingAttack as the move may make a piece end up still blocking the attack
 public bool isMoveBlockingAttack(Move move, AbstractPiece blockedPiece)
 {
     return(isMoveBlockingAttack(move, blockedPiece.location));
 }
Beispiel #20
0
 //Checks if a pieces current position is blocking an attack to a specific location or piece
 public bool isPieceBlockingAttack(AbstractPiece piece, AbstractPiece blockedPiece)
 {
     return(isPieceBlockingAttack(piece, blockedPiece.location));
 }
Beispiel #21
0
 //Returns a list of all attackers of a piece or location
 public IList <AbstractPiece> getAttackers(AbstractPiece piece)
 {
     return(getAttackers(piece.location, piece.color));
 }
Beispiel #22
0
 public void setPiece(AbstractPiece piece, Location position)
 {
     board[position.x, position.y] = piece;
 }
Beispiel #23
0
 public bool pieceOnBoard(AbstractPiece piece)
 {
     return(pieces.Contains(piece));
 }
Beispiel #24
0
 public Queen(AbstractPiece piece)
     : base(piece)
 {
     whiteVersion = Chess_Program.Properties.Resources.WhiteQueen;
     blackVersion = Chess_Program.Properties.Resources.BlackQueen;
 }
Beispiel #25
0
 public King(AbstractPiece piece)
     : base(piece)
 {
     whiteVersion = Chess_Program.Properties.Resources.WhiteKing;
     blackVersion = Chess_Program.Properties.Resources.BlackKing;
 }
Beispiel #26
0
 public MoveList getMoves(AbstractPiece piece)
 {
     return(OnFilterMoves(piece, piece.getMoves(this)));
 }
Beispiel #27
0
 public AbstractPiece(AbstractPiece copyPiece) // Copy constructor
 {
     this.color    = copyPiece.color;
     this.location = copyPiece.location.Copy();
     this.hasMoved = copyPiece.hasMoved;
 }
Beispiel #28
0
 public bool checkMove(AbstractPiece piece, Move move)
 {
     return(getMoves(piece).Contains(move));
 }
Beispiel #29
0
 public static AbstractPiece Copy(AbstractPiece piece)
 {
     return(piece.Copy());
 }
Beispiel #30
0
 //Methods to allow the client to create complex gameplay
 //Determines if a location or piece is "Attacked" (Has move with a target location of the piece or location)
 public bool checkAttacked(AbstractPiece piece)
 {
     return(checkAttacked(piece.location, piece.color));
 }