Пример #1
0
        /*
         *  A bit of an odd workaround for the issue of having no way to handle exceptions from buttons firing these
         *      methods.
         */
        public void exec(object sender, EventArgs e)
        {
            //Check
            switch (state)
            {
            case 0:
                if (sender is NumberChip)
                {
                    NumberChip nc = (NumberChip)sender;

                    //Check if the player has selected the desert
                    if (Board.THIEF_MUST_MOVE && nc.isBlocked())
                    {
                        throw new ThiefException(ThiefException.THIEF_CANNOT_STAY);
                    }

                    if (Board.THIEF_CANNOT_GO_HOME && nc.getNumber() == 0)
                    {
                        throw new ThiefException(ThiefException.THIEF_CANNOT_GO_DESERT);
                    }

                    theBoard.addEventText(UserMessages.RobberHasMoved(theBoard.currentPlayer));
                    oldThiefLocation.removeThief();
                    nc.placeThief();
                    oldThiefLocation = nc;
                    if (chitHasPlayers())
                    {
                        state++;
                        theBoard.addEventText(UserMessages.PLAYER_STEAL_RESOURCES);
                    }
                    else
                    {
                        endExecution();
                    }
                }
                break;

            case 1:
                if (!(sender is Settlement))
                {
                    //We only run this check in case something did not clean up properly from a previous event.
                    return;
                }

                //We check if the settlement is actually adjascent to the thief.
                if (!terrainTileWithThief.hasSettlement((Settlement)sender))
                {
                    throw new ThiefException(ThiefException.CANT_STEAL_FROM_PLAYER);
                }

                //Get the player at the chosen location (sender is that location)
                Player playerToStealFrom = ((Settlement)sender).getOwningPlayer();

                //Check if there is a player at the location.
                if (playerToStealFrom == null)
                {
                    throw new ThiefException(ThiefException.NO_PLAYER);
                }

                //Check if the player is not the current player.
                if (playerToStealFrom == theBoard.currentPlayer)
                {
                    throw new ThiefException(ThiefException.YOU_OWN_THIS_SETTLEMENT);
                }

                //At this point we can safely assume a card can be taken.
                ResourceCard rCard = playerToStealFrom.takeRandomResource();
                //We check if the card is null because if the player has no cards to give takeRandomResource() returns null.
                if (rCard != null)
                {
                    //What happens if there were cards to steal
                    theBoard.currentPlayer.giveResource(playerToStealFrom.takeRandomResource());
                    theBoard.addEventText(UserMessages.PlayerGotResourceFromPlayer(theBoard.currentPlayer, playerToStealFrom, rCard.getResourceType()));
                    state++;
                    endExecution();
                }
                else
                {
                    //What happens if there were no cards to steal.
                    theBoard.addEventText(UserMessages.PlayerGoNoResourceFromPlayer(theBoard.currentPlayer, playerToStealFrom));
                    state++;
                    endExecution();
                }
                break;
            }
        }