예제 #1
0
파일: deck.py 프로젝트: Darthfett/CardGame
def load_deck(file_path):
    """Given a file path, load and return the deck."""
    with open(file_path, 'r') as deck_file:
        # Remove space from the file
        # Every character in the file excluding whitespace is a card
        cards = list(''.join(deck_file.read().split()))

        cards = [card.Card(card.Card[card.name_for_card(card_)]) for card_ in cards]

        return Deck(cards)
예제 #2
0
파일: deck.py 프로젝트: Darthfett/CardGame
 def get(self, card_):
     return next((c for c in self.cards if repr(c) == card_ or repr(c) == card.name_for_card(card_)), None)
예제 #3
0
파일: round.py 프로젝트: Darthfett/CardGame
def start_phase(game, round, phase, player):
    game.current_round = round
    game.current_phase = phase
    game.current_player = player

    phase_name = name_for_phase(phase)
    enemy = game.next_player(player)

    print(ROUND_ANNOUNCEMENT.format(round=round, phase=phase_name))

    while True:
        action = input("Action? (help) ").strip()

        # Commands are case-insensitive, help is default.
        cmd = action.split()
        if not cmd:
            cmd = ["help"]
        cmd[0] = cmd[0].lower()

        # Validate that action is good for current phase
        if cmd[0] in PHASE_ACTIONS and cmd[0] not in ACTIONS_FOR_PHASE[phase]:
            # Invalid action
            phases_for_action = [phase for phase in ACTIONS_FOR_PHASE if cmd[0] in ACTIONS_FOR_PHASE[phase]]
            print(
                "Must be in {phase} phase to perform this action.\n".format(
                    phase=" phase OR ".join(map(name_for_phase, phases_for_action))
                )
            )
            continue

        if cmd[0] in BOARD_ACTIONS:
            # Display board
            print_board(player, enemy)
            continue

        if cmd[0] in DRAW_ACTIONS:
            # Draw card
            drawn_card = player.deck.draw()
            player.hand.add(drawn_card)
            print("Drew {card}\n".format(card=drawn_card.name))
            start_phase(game, round, STRAT_PHASE, player)
            return

        if cmd[0] in HAND_ACTIONS:
            # Display hand
            print_hand(player)
            continue

        if cmd[0] in HELP_ACTIONS:
            # Display help message
            print(HELP_MSG)
            continue

        if cmd[0] in NEXT_ACTIONS:
            start_phase(game, round, (phase + 1) % PHASE_COUNT, player)
            return

        if cmd[0] in PLAY_ACTIONS:
            # Play card cmd[1] on field at cmd[2]
            try:
                card_repr = cmd[1]
            except IndexError:
                print("Must select a card name and an empty spot on the field.\n")
                continue

            card_ = player.hand.get(card_repr)
            if card_ is None:
                print("No {card} card in hand.\n".format(card=card.name_for_card(card_repr)))
                continue

            try:
                field_cell = int(cmd[2])
            except (IndexError, ValueError):
                print("Must select a card name and an empty spot on the field.\n")
                continue

            try:
                current_field_card = player.field[field_cell - 1]
            except IndexError:
                print("Field cell must be in range 1 <= cell <= 9.\n")
                continue

            if current_field_card is not None:
                print("Spot on field is not empty.\n")
                continue

            # Successful play
            player.hand.remove(card_)
            player.field[field_cell - 1] = card_
            print_board(player, enemy)
            continue

        if cmd[0] in QUIT_ACTIONS:
            # Quit game
            print("Thanks for playing!\n")
            break

        print("Unknown command {cmd}\n".format(cmd=cmd[0]))