Ejemplo n.º 1
0
    def draw_cards(
        self,
        n: int,
        scr: Screen,
        verbose: bool = True,
    ) -> None:
        """
        Tries to take n cards from the player's drawpile, and places them into the player's hand.
        If the drawpile is empty, the discard pile will be added onto the drawpile and shuffled.
        """
        # What are you doin trying to draw negative cards
        if (n <= 0):
            scr.log("Uh, you tried to draw {0} cards.".format(n), 2)
            return

        scr.log("Drawing {0} cards...".format(n), 1)
        # Repeat n times
        for _ in range(n):

            # If the draw pile is empty, and the discard pile is empty, we're can't do any more.
            if (len(self.drawpile) == 0 and len(self.discardpile) == 0):
                scr.log("Nothing left to draw!")
                break

            # If the draw pile is empty, and the discard pile isn't, put everything into the draw pile.
            if (len(self.drawpile) == 0):
                self.discard_to_draw()

            # Pop a card from the draw pile, and add it onto the current hand
            card_drawn = load_card(self.drawpile.pop())
            if verbose: scr.log("Drew card: {0}".format(card_drawn.name))
            self.current_hand.append(card_drawn.name)

        # Update hand value
        self.current_hand_balance = self.get_hand_value()
Ejemplo n.º 2
0
def perform_action(p: Player, card_name: str, scr: Screen) -> None:
    """
    Plays a card in the player's hand.
    Once a card is successfully played, the card gets put on the player's discard pile.
    """
    for card in p.current_hand:

        # Found the card that we want to play
        if "action" in load_card(card).cardtype and card == card_name:
            # try:
            p.current_hand.remove(card)

            scr.log("Played card: {0}".format(card_name))
            action_performed: bool = globals()[load_card(card).action](p, scr)

            if action_performed:
                p.cards_played.append(card)
                p.actions_left -= 1
            else:
                p.current_hand.append(card)
            break
Ejemplo n.º 3
0
 def get_deck_score(self) -> int:
     """
     Gets the points of the player.
     This is used as the player's final score.
     """
     res = 0
     for i in self.deck:
         card = load_card(i)
         # Check if the card actually counts towards the final points
         if "point" in card.cardtype:
             res += card.value
     return res
Ejemplo n.º 4
0
    def get_hand_value(self) -> int:
        """
        Gets the hand value (money) of the player.
        This is not equal to the value that the player has left to spend.
        """
        res = 0

        for i in self.current_hand:
            card = load_card(i)
            if "money" in card.cardtype:
                res += card.value

        return res
Ejemplo n.º 5
0
    def trash_hand_card(self, card: str, scr: Screen) -> None:
        """
        Remove a card from the player's hand and deck.
        """
        try:

            # Remove card from player
            self.current_hand.remove(card)
            self.deck.remove(card)

            # Add card into trashpile
            session_objects.s_trashpile.append(card)

            scr.log("Removed {0} from deck.".format(load_card(card).name))

        except ValueError:

            scr.log("Can't remove this card!")
Ejemplo n.º 6
0
def format_cards(cards: [str],
                 width,
                 show_description: bool = False,
                 show_type: bool = False,
                 show_cost: bool = False,
                 custom_lines: [str] = None) -> [str]:

    res = []

    if len(cards) == 0:
        res.append("No available cards!")
        return res

    i = 1
    for card_name in cards:
        card = load_card(card_name)

        res.append("{0}. {1} {2} {3}".format(
            # Number
            i,

            # Name
            card.name,

            # Card type
            "({0})".format(card.cardtype) if show_type else "",

            # Card cost
            "- {0}$".format(card.cost) if show_cost else ""))

        if show_description:
            wrapper = textwrap.TextWrapper(width=width)
            res = res + wrapper.wrap("  - " + card.description)

        # Custom lines can be added per card
        if custom_lines is not None:
            res.append(custom_lines[i - 1])

        res.append("")

        i += 1

    return res
Ejemplo n.º 7
0
def mine(p: Player, scr: Screen) -> bool:
    money_cards = {}
    for card in p.current_hand:
        if "money" in load_card(card).cardtype:
            money_cards.update({card: card})

    if len(money_cards.keys()) == 0:
        scr.log("No money cards to upgrade!", 2)
        return False

    scr.show_main_content(
        ["Which card do you want to upgrade?"] +
        format_cards(money_cards.keys(), r_main_content.width))

    chosen_card = input_card_selection(list(money_cards.keys()), scr)

    if chosen_card is None: return

    card_name = chosen_card.name

    upgrade_card = ""
    if card_name == "platinum coin":
        scr.log("Sorry, the fun stops here.", 2)
        return False
    elif card_name == "gold coin":
        upgrade_card = "platinum coin"

    elif card_name == "silver coin":
        upgrade_card = "gold coin"

    elif card_name == "copper coin":
        upgrade_card = "silver coin"
    else:
        return False

    if store.gift_card(p, upgrade_card, scr, pile="hand"):
        p.trash_hand_card(money_cards[card_name], scr)
        return True
    else:
        return False
Ejemplo n.º 8
0
def purchase_card(p: Player, card: str, scr: Screen) -> bool:
    """
    The transaction method. Buys a card for a player using their balance.
    The purchased card gets taken out of the shop, and placed into the player's discard pile.
    """
    # Card doesn't exist in the store
    if card not in s_store:
        scr.log("This card doesn't exist in the store!", 2)
        return False

    # Card has 0 left in the store
    elif s_store.get(card) == 0:
        scr.log("This card cannot be bought anymore!", 2)
        return False

    # Player can't purchase any more cards
    elif p.purchases_left == 0:
        scr.log("You can't buy any more cards this turn!", 2)
        return False

    else:
        # Load in the card
        card_bought = load_card(card)

        if card_bought.cost > (p.current_hand_balance + p.bonus_coins -
                               p.amount_spent):
            scr.log("Insufficient funds!", 2)
            return False

        # Confirm purchase
        p.purchases_left -= 1
        scr.log("Bought card: {0}".format(card), 1)

        p.add_discardpile_card(card_bought.name)

        # Subtract cost from balance
        p.amount_spent += card_bought.cost

        remove_card(card, scr)
        return True
Ejemplo n.º 9
0
def routine(p: Player, scr: Screen) -> None:
    """
    Action routine. Allows player to play action cards.
    """
    if p.actions_left <= 0:
        scr.log("You don't have any actions left.", 2)
        return
    action_cards: [str] = [
        card for card in p.current_hand if "action" in load_card(card).cardtype
    ]
    if len(action_cards) == 0:
        scr.log("You don't have any action cards to play.", 2)
        return

    scr.show_main_content(["Which action do you want to play?"] + format_cards(
        action_cards, r_main_content.width, show_description=True))

    card: Card = input_card_selection(action_cards, scr)

    if card is None: return

    perform_action(p, card.name, scr)
Ejemplo n.º 10
0
                attackingCard = None
                currentPlayerIndex = not currentPlayerIndex
                turnInit = False
            else:
                turnInit = False

    # Render the game. #
    screen.fill(utils.BACKGROUND)

    if trump_card is not None:
        screen.blit(utils.load_trump_card(trump_card), utils.TRUMP_POSITION)
    if len(deck) > 0:
        screen.blit(utils.load_card_back(), utils.DECK_POSITION)
    if len(discard) > 0:
        screen.blit(utils.load_card(discard[len(discard) - 1]),
                    utils.DISCARD_POSITION)

    if len(cardsInPlay) > 0:
        for index in range(0, len(cardsInPlay)):
            screen.blit(utils.load_card(cardsInPlay[index]),
                        utils.get_play_position(index, len(cardsInPlay)))

    for index in range(0, len(players[0].hand)):
        screen.blit(
            utils.load_card(players[0].hand[index]),
            utils.get_card_position(index, len(players[0].hand), False))
    for index in range(0, len(players[1].hand)):
        screen.blit(utils.load_card_back(),
                    utils.get_card_position(index, len(players[1].hand), True))