Esempio n. 1
0
def start_game(args: argparse.Namespace):
    print('Playing a game with {} players.'.format(args.num_players))
    players = [
        Player('Player {}'.format(i + 1)) for i in range(args.num_players)
    ]
    game = Game(players)
    listener = Listener()
    game.event_log.add_event_listener(listener)
    while True:
        print('-' * 10)
        print('New round!')
        print('Shuffling, dealing, thinking...')
        print('-' * 10)
        round_manager = RoundManager(game.progress_to_next_round())
        print('Trump card is {}'.format(game.round.trump_card))
        while True:
            state = round_manager.state
            if state == RoundState.ROUND_FINISHED:
                debug_log('ROUND_FINISHED')
                break
            elif state == RoundState.AWAITING_TURN:
                debug_log('AWAITING_TURN')
                print('\nAwaiting turn from player {}.'.format(
                    game.round.active_player.identifier))

                playable_indexes = print_active_hand(game)

                choice = None
                while True:
                    choice_str = input('Choose a card index: ')
                    try:
                        choice = int(choice_str)
                    except Exception as e:
                        print(e)
                        print('Please choose a valid card index.')
                        continue
                    if choice not in playable_indexes:
                        print('Please choose a valid card index.')
                    else:
                        break
                card = game.round.hands[game.round.active_player][choice]
                print('Chose to play: {}'.format(card))
                round_manager.play_card(card, game.round.active_player)
            elif state == RoundState.HAND_FINISHED:
                debug_log('HAND_FINISHED')
                round_manager.finish_hand()
                print('Hand finished!')
            elif state == RoundState.AWAITING_BID:
                debug_log('AWAITING_BID')
                print('\nAwaiting bid from player {}.'.format(
                    game.round.active_player.identifier))

                print_active_hand(game)

                choice = None
                while True:
                    bid_str = input('Make a bid: ')
                    try:
                        bid = int(bid_str)
                        round_manager.make_bid(game.round.active_player, bid)
                        break
                    except Exception as e:
                        print(e)
                        print('Please choose a valid bid.')
                        continue
        # TODO(iandioch): Handle finished game.
        print('Round finished!')