Esempio n. 1
0
        ///Methods
        //Overidden with King piece logic
        //Does not include that a king cannot move to an attacked square, that is added in FilteringMoves
        public override MoveList getPieceMoves(GameBoard board, Location startLocation, ColorEnum color)
        {
            MoveList moves = new MoveList();

            for (int i = 0; i < 8; i++)
            {
                Location possible = startLocation.Shift(Location.directionIndex(i));
                if (board.locationOnBoard(possible))
                {// Cannot include the idea that a king cant move to an attack space as it links to the king and creates an infinite loop
                    //Needs to be filtered on the outside of the gameboard
                    if (!board.pieceAtLocation(possible))
                    {
                        Move newMove = new Move(startLocation, possible);
                        newMove.AddNeededEmtpyLocations(possible.Copy());
                        moves.Add(newMove);
                    }
                    else if (board.getPiece(possible).color != color)
                    {
                        Move attack = new Move(startLocation, possible);
                        attack.AddAttackedLocation(possible);
                        moves.Add(attack);
                    }
                }
            }

            return(moves);
        }
Esempio n. 2
0
        ///Methods
        //Overidden with Knight piece logic
        public override MoveList getPieceMoves(GameBoard board, Location startLocation, ColorEnum color)
        {
            MoveList moves = new MoveList();

            for (int i = 0; i <= 1; i++)
            {
                for (int j = -1; j <= 1; j += 2)
                {
                    for (int k = -1; k <= 1; k += 2)
                    {
                        int      xShift = (1 + i) * j, yShift = (2 - i) * k;
                        Location poss = startLocation.Shift(xShift, yShift);
                        if (board.locationOnBoard(poss))
                        {
                            if (!board.pieceAtLocation(poss))
                            {
                                Move newMove = new Move(startLocation, poss);
                                newMove.AddNeededEmtpyLocations(poss.Copy());
                                moves.Add(newMove);
                            }
                            else if (board.getPiece(poss).color != color)
                            {
                                Move attack = new Move(startLocation, poss);
                                attack.AddAttackedLocation(poss);

                                moves.Add(attack);
                            }
                        }
                    }
                }
            }

            return(moves);
        }
Esempio n. 3
0
        ///Methods
        //Overidden with Queen piece logic
        public override MoveList getPieceMoves(GameBoard board, Location startLocation, ColorEnum color)
        {
            MoveList moves = new MoveList();

            Location local;

            for (int i = 0; i < 8; i++)
            {
                local = new Location(startLocation);


                IList <Location> previousEmpty = new List <Location>();
                while (true)
                {
                    local = local.Shift(Location.directionIndex(i));

                    //Conditional for stopping the array
                    if (!board.locationOnBoard(local))
                    {
                        break;
                    }
                    if (board.pieceAtLocation(local))
                    {
                        if (board.getPiece(local).color != color)
                        {
                            Move attack = new Move(startLocation, local);
                            attack.AddAttackedLocation(local);

                            foreach (Location empty in previousEmpty)
                            {
                                attack.AddNeededEmtpyLocations(empty.Copy());
                            }

                            moves.Add(attack);
                        }

                        break;
                    }

                    previousEmpty.Add(local);

                    Move nonAttack = new Move(startLocation, local);

                    //Includes the end location as it is non attack
                    foreach (Location empty in previousEmpty)
                    {
                        nonAttack.AddNeededEmtpyLocations(empty.Copy());
                    }

                    moves.Add(nonAttack);
                }
            }

            return(moves);
        }
Esempio n. 4
0
        ///Methods
        //Overidden with Pawn piece logic
        public override MoveList getPieceMoves(GameBoard board, Location startLocation, ColorEnum color)
        {
            MoveList moves = new MoveList();

            int      forward    = getYForward();
            Location forwardLoc = startLocation.Shift(0, forward);

            if (board.locationOnBoard(forwardLoc) && !board.pieceAtLocation(forwardLoc))
            {
                Move newMove = new Move(startLocation, forwardLoc);
                newMove.AddNeededEmtpyLocations(forwardLoc.Copy());
                moves.Add(newMove);


                //Needs to be able to do the first forward in order to do the second one
                Location doubleForwardLoc = forwardLoc.Shift(0, forward);
                if (board.locationOnBoard(doubleForwardLoc) && !board.pieceAtLocation(doubleForwardLoc) && !getHasMoved())
                {
                    Move doubleForwardNewMove = new Move(startLocation, doubleForwardLoc);
                    doubleForwardNewMove.AddNeededEmtpyLocations(forwardLoc.Copy());
                    doubleForwardNewMove.AddNeededEmtpyLocations(doubleForwardLoc.Copy());
                    moves.Add(doubleForwardNewMove);
                }
            }


            //Two attacks
            for (int i = -1; i <= 1; i += 2)
            {
                Location sideAttack = startLocation.Shift(i, forward);
                if (board.locationOnBoard(sideAttack) && board.pieceAtLocation(sideAttack) && board.getPiece(sideAttack).color != color)
                {
                    Move sideAttackMove = new Move(startLocation, sideAttack);
                    sideAttackMove.AddAttackedLocation(sideAttack);
                    moves.Add(sideAttackMove);
                }
            }

            return(moves);
        }
Esempio n. 5
0
        /*
         * Handles the GameBoard Tile Selecting event and creates the neccasary feedback for players to see what moves they can make and what they have selected
         */
        public void TileSelecting(GameBoard sender, TileSelectingEventArgs e)
        {
            if (aPlayerHasWon)
            {
                e.CancelEvent();
                return;
            }
            //The selected tile must be either a piece or a move of the previously selected piece

            if (sender.getSelectedTile() != null && sender.getSelectedTile() == e.tile) //Reselected the previous tile
            {                                                                           //Deselect the previous move and prevent the event
                DeselectTile(sender.getSelectedTile());
                sender.DeselectTile();


                e.CancelEvent();
                return;
            }
            else if (sender.getSelectedTile() != null && sender.getSelectedPiece() != null && sender.getSelectedMoves().Contains(sender.getSelectedLocation(), e.location)) //Selecting a move from the previous selected piece
            {                                                                                                                                                               //Execute the move and clean up the GUI
                //Removes the gui elements
                DeselectTile(sender.getSelectedTile());


                //Execute the planned move on the board
                if (gameBoard.movePiece(sender.getSelectedLocation(), e.location))
                {
                    //Get ready for next move
                    sender.DeselectTile();
                }
                else
                {//Moving failed, reset to last position
                    SelectTile(sender.getSelectedTile());
                }

                e.CancelEvent();
                return;
            }
            else if (sender.pieceAtLocation(e.location) && sender.getPiece(e.location).color == PlayerColor) //Selecting a new piece
            {                                                                                                //Deselect the previous tile
                if (sender.getSelectedTile() != null)
                {
                    DeselectTile(sender.getSelectedTile());
                }
            }
            else
            {//Else invalid selection
                e.CancelEvent();
                return;
            }
        }