Exemplo n.º 1
0
def validate(players, n_games):
    game = Game(players=players,
                dealer_type=PerfectDealer,
                verbose=False,
                collect_stats=True)

    # turn training off
    for player in players:
        player.training = False

    for _ in range(n_games):
        game.start_round()

    # turn training back on
    for player in players:
        player.training = True

    # average the dealer reward
    dealer_reward = sum(game.collector.rewards['dealer']) / n_games

    # average player rewards
    player_reward = 0
    for player in players:
        player_reward += sum(game.collector.rewards[player.name]) / n_games
    player_reward /= len(players)

    return player_reward, dealer_reward
Exemplo n.º 2
0
    def test_available_actions_during_play(self):
        game = Game([])

        self.assertEqual((
            game.ACTION_HIT,
            game.ACTION_STAND,
        ), game.get_available_actions())
Exemplo n.º 3
0
def test_deal():
    g = Game()
    assert len(g.player.hand) == 0
    assert len(g.dealer.hand) == 0

    g.deal()
    assert len(g.player.hand) == 2
    assert len(g.dealer.hand) == 2
Exemplo n.º 4
0
def main_game():
    players = [Player('Bob', 500), AiPlayer('Sam', 500)]
    dealer = AiPlayer('Dealer', 5000)
    game = Game(players, dealer)

    print('\nWelcome!\n')
    game.game_loop()
    print('\nThanks for playing!')
Exemplo n.º 5
0
    def test_play_hand_stand(self):
        game = Game()

        game.add_player()

        game.players[-1].hands.append(build_hand('hard'))
        game.players[0].hands.append(build_hand('hard'))

        self.assertEqual(2, len(game.players[0].hands[0].cards))
Exemplo n.º 6
0
    def test_add_player_default(self):
        game = Game()

        bankroll = 100
        game.add_player(player_settings={'bankroll': bankroll})

        self.assertEqual(2, len(game.players))
        self.assertEqual(bankroll, game.players[0].bankroll)
        self.assertEqual(False, game.players[0].dealer)
        self.assertEqual(1, game.players[0].number)
Exemplo n.º 7
0
    def test_add_player_multiple(self):
        game = Game()

        num_players = 3
        for _ in range(num_players):
            game.add_player()

        self.assertEqual(num_players + 1, len(game.players))

        for i in range(num_players):
            self.assertEqual(i + 1, game.players[i].number)
Exemplo n.º 8
0
    def test_deal_card(self):
        dealt_card = Card(1, SUIT_SPADES)
        card_in_deck = Card(13, SUIT_SPADES)

        with patch.object(Game, 'shuffle_deck', autospec=True):
            game = Game([dealt_card, card_in_deck])
        player = Player()

        game.deal(player)

        self.assertEqual([card_in_deck], game.card_pool)
        self.assertEqual([dealt_card], player.hand.cards)
Exemplo n.º 9
0
    def test_player_bet_and_lose(self):
        player = Player()
        game = Game()
        bet = Bet(1)

        player.place_bet(bet, game)

        player.hand = game.deal_cards()

        game.resolve_bust()

        self.assertEquals("House wins!", game.result)
Exemplo n.º 10
0
def play(stake, players):
    game = Game(stake, players)
    print(f"\nGoing to start with ${stake} stake and {len(players)} players")
    game.start()

    r = 1
    while game.winner == None:
        print(f'Round {r}; current player - {game.players[0].name}')
        print(format_game_state(game))
        r += 1
        game.next_turn()

    print(format_game_state(game))
    winner_name = game.winner.name
    game.finish()
    print(
        f'{winner_name} has won the game!\n  Players: {format_players_balance(players)}'
    )

    for p in players:
        if p.balance == 0:
            print(f"{p.name} does not have enough money to continue...")
            sys.exit(0)

    ask_replay(players)
Exemplo n.º 11
0
    def assign_player(self, player):
        game = None
        for gameId, potentialGame in self.games.iteritems():
            if not potentialGame.is_full():
                game = potentialGame
                break
        if game is None:
            gameId = str(uuid.uuid4())
            game = Game(gameId)
            self.games[gameId] = game
            print 'created new game with ID {}'.format(gameId)

        game.add_player(player)
        self.playerAssignments[player.id] = game
        print 'added player {} to game {}'.format(player.id, game.id)
        return game
Exemplo n.º 12
0
    def test_game_loop_out_of_budget(self, _print_mock,
                                     single_game_mock):
        budget = 500
        player = Player('Bob', budget)
        players = [player]
        dealer = AiPlayer('Dealer', 5000)

        def single_game(self):
            for player in self.players:
                player.budget = 0
        single_game_mock.side_effect = single_game

        game = Game(players, dealer)
        game.game_loop()

        self.assertFalse(game.players)
Exemplo n.º 13
0
 def test_card_pool_is_shuffled(self):
     with patch('blackjack.game.shuffle', autospec=True) as mock_shuffle:
         card_pool = [
             Card(1, SUIT_SPADES),
             Card(2, SUIT_SPADES),
         ]
         Game(card_pool)
         mock_shuffle.assert_called_once_with(card_pool)
Exemplo n.º 14
0
    def test_game_loop_no_continue(self, _print_mock,
                                   get_answer_mock,
                                   single_game_mock):
        budget = 500
        player = Player('Bob', budget)
        players = [player]
        dealer = AiPlayer('Dealer', 5000)

        def single_game(self):
            for player in self.players:
                player.budget = 0
        single_game_mock.side_effect = single_game
        get_answer_mock.return_value = 'n'

        game = Game(players, dealer)
        game.game_loop()

        self.assertFalse(game.players)
Exemplo n.º 15
0
 def test_start_game(self):
     game = Game.create_game()
     game.start()
     player = game.player
     dealer = game.dealer
     self.assertEqual(2, len(player.hand.cards),
                      'The player should be dealt 2 cards')
     self.assertEqual(1, len(dealer.hand.cards),
                      'The dealer should be dealt 1 card')
Exemplo n.º 16
0
    def test_add_player_options(self):
        game = Game()

        bankroll = 100
        bet_limit = 25
        bet_strategy_type = BetStrategyType.MARTINGALE

        game.add_player(player_settings={
            'bet_strategy_type': bet_strategy_type,
            'bet_limit': bet_limit
        })

        self.assertEqual(2, len(game.players))
        self.assertEqual(bankroll, game.players[0].bankroll)
        self.assertEqual(bet_strategy_type,
                         game.players[0].bet_strategy.strategy_type)
        self.assertEqual(False, game.players[0].dealer)
        self.assertEqual(bet_limit, game.players[0].bet_limit)
        self.assertEqual(1, game.players[0].number)
Exemplo n.º 17
0
 def test_card_pool_creation(self):
     expected_card_pool = [
         Card(1, SUIT_SPADES),
         Card(2, SUIT_SPADES),
     ]
     with patch('blackjack.game.generate_deck',
                autospec=True,
                return_value=expected_card_pool):
         game = Game.create_game()
         self.assertCountEqual(expected_card_pool, game.card_pool)
Exemplo n.º 18
0
def main():
    init()

    game = Game(players_file)
    while game.players_in_game() > 0 and game.new_round():
        game.run()

    if game.players_in_game() == 0:
        log_and_print('No more players. Game stops!')
Exemplo n.º 19
0
    def test_player_loses(self, _print_mock,
                         ai_player_make_move_mock, player_make_move_mock,
                         player_make_bet_mock, deal_start_cards_mock):

        player_cards = [
            Card('One', [1], '♣', exposed=True),
            Card('One', [1], '♦', exposed=True),
        ]
        dealer_cards = [
            Card('Ten', [10], '♣', exposed=True),
            Card('Ten', [10], '♦', exposed=False),
        ]

        def deal(self):
            for player in self.players:
                player.cards.extend(player_cards)
            self.dealer.cards.extend(dealer_cards)

        deal_start_cards_mock.side_effect = deal

        budget = 500
        player = Player('Bob', budget)
        players = [player]
        dealer = AiPlayer('Dealer', 5000)

        bet = 100

        def make_bet(self):
            self.current_bet = bet
        player_make_bet_mock.side_effect = make_bet

        stand = 'stand'
        player_make_move_mock.return_value = stand
        ai_player_make_move_mock.return_value = stand

        game = Game(players, dealer)
        game.single_game()

        self.assertEqual(player.budget, budget - bet)
Exemplo n.º 20
0
    def test_play_hand_split(self):
        game = Game()

        game.add_player()

        game.players[-1].hands.append(build_hand('hard'))
        game.players[0].hands.append(build_hand('hard_pair'))

        self.assertEqual(game.settings.min_bet, game.players[0].hands[0].bet)
        game.play_hand(game.players[0], game.players[0].hands[0])
        self.assertEqual(game.settings.min_bet, game.players[0].hands[1].bet)
        game.play_hand(game.players[0], game.players[0].hands[1])

        self.assertLessEqual(2, len(game.players[0].hands[0].cards))
        self.assertLessEqual(2, len(game.players[0].hands[1].cards))
Exemplo n.º 21
0
    def test_start(self):
        game = Game(GameSettings(num_decks=1))

        game.add_player()

        num_rounds = 10
        game.start(rounds=num_rounds)

        self.assertEqual(num_rounds, game.round_count)
Exemplo n.º 22
0
    def test_winner_has_highest_score(self):
        game = Game([])
        game.player.add_card(Card(10, SUIT_SPADES))
        game.dealer.add_card(Card(5, SUIT_SPADES))
        self.assertEqual(game.player, game.get_winner())

        game = Game([])
        game.player.add_card(Card(5, SUIT_SPADES))
        game.dealer.add_card(Card(10, SUIT_SPADES))
        self.assertEqual(game.dealer, game.get_winner())
Exemplo n.º 23
0
    def test_play_hand_hit(self):
        game = Game()

        game.add_player()

        game.players[-1].hands.append(build_hand('hard'))
        game.players[0].hands.append(build_hand('soft'))

        game.play_hand(game.players[0], game.players[0].hands[0])

        self.assertLess(2, len(game.players[0].hands[0].cards))
Exemplo n.º 24
0
    def test_game_finishes_if_player_collects_21(self):
        p1 = FakePlayer("p1", 100, [STAY, STAY])
        p2 = FakePlayer("DEALER", 100)

        game = Game(50, [p1, p2])
        deck = Deck()
        deck.deck = [
            (2, Deck.HEARTS),  # p1
            (8, Deck.CLUBS),  # p2
            (3, Deck.HEARTS),  # p1
            (2, Deck.CLUBS),  # p2
            (Deck.ACE, Deck.CLUBS),  # p2
            (Deck.ACE, Deck.HEARTS)  # game must be finished here
        ]

        play(game, deck)
        self.assertEqual(game.winner, p2)
        self.assertEqual(p2.balance, 150)
        self.assertEqual(p1.balance, 50)
Exemplo n.º 25
0
    def test_game_finishes_when_one_player_left(self):
        p1 = FakePlayer("p1", 100)
        p2 = FakePlayer("p2", 100)

        game = Game(50, [p1, p2])
        deck = Deck()
        deck.deck = [
            (9, Deck.HEARTS),  # p1
            (8, Deck.CLUBS),  # p2
            (8, Deck.HEARTS),  # p1
            (2, Deck.CLUBS),  # p2
            (Deck.JACK, Deck.CLUBS),  # p2
            (Deck.ACE, Deck.HEARTS)  # game must be finished here
        ]

        play(game, deck)

        self.assertEqual(game.winner, p2)
        self.assertEqual(p2.balance, 150)
        self.assertEqual(p1.balance, 50)
Exemplo n.º 26
0
    def test_play_round(self):
        game = Game()

        game.add_player()

        pre_round_count = game.round_count
        game.play_round()

        self.assertEqual(pre_round_count + 1, game.round_count)

        for player in game.players:
            self.assertLess(0, len(player.hands[0].cards))

        for player in game.players[:-1]:
            self.assertNotEqual(None, player.hands[0].result)
Exemplo n.º 27
0
    def test_available_actions_at_end_of_game(self):
        # Player has won.
        game = Game([])

        game.player.add_card(Card(1, SUIT_SPADES))
        game.player.add_card(Card(13, SUIT_SPADES))

        self.assertEqual((), game.get_available_actions())

        # Player has gone bust.
        game = Game([])

        game.player.add_card(Card(10, SUIT_SPADES))
        game.player.add_card(Card(10, SUIT_SPADES))
        game.player.add_card(Card(10, SUIT_SPADES))

        self.assertEqual((), game.get_available_actions())
Exemplo n.º 28
0
    def test_play_hand_double_down(self):
        game = Game()

        bankroll = 100
        game.add_player(player_settings={'bankroll': bankroll})

        game.players[-1].hands.append(build_hand('soft'))
        game.players[0].hands.append(build_hand('double_down'))
        game.players[0].bankroll -= game.settings.min_bet

        game.play_hand(game.players[0], game.players[0].hands[0])

        self.assertEqual(bankroll - (game.settings.min_bet * 2),
                         game.players[0].bankroll)
        self.assertEqual(game.settings.min_bet * 2,
                         game.players[0].hands[0].bet)
        self.assertEqual(3, len(game.players[0].hands[0].cards))
Exemplo n.º 29
0
    def test_complete_dealers_hand(self):
        # Cards given to the player.
        players_cards = [
            Card(1, SUIT_SPADES),
            Card(2, SUIT_SPADES),
        ]
        # Cards given to the dealer.
        dealers_cards = [
            Card(3, SUIT_SPADES),
            Card(7, SUIT_SPADES),
            Card(8, SUIT_SPADES),
        ]

        deck = players_cards + dealers_cards + [
            Card(9, SUIT_SPADES),
        ]
        with patch.object(Game, 'shuffle_deck', autospec=True):
            game = Game(deck)
            game.start()
            game.complete_dealers_hand()
            dealer = game.dealer
            self.assertEqual(dealers_cards, dealer.hand.cards)
Exemplo n.º 30
0
    def test_complete_dealers_hand_does_not_hit_on_hard_17(self):
        # Cards given to the player.
        players_cards = [
            Card(4, SUIT_SPADES),
            Card(5, SUIT_SPADES),
        ]
        # Cards given to the dealer.
        dealers_cards = [
            Card(10, SUIT_SPADES),
            Card(6, SUIT_SPADES),
            Card(1, SUIT_SPADES),
        ]

        deck = players_cards + dealers_cards + [
            Card(9, SUIT_SPADES),
        ]
        with patch.object(Game, 'shuffle_deck', autospec=True):
            game = Game(deck)
            game.start()
            game.complete_dealers_hand()
            dealer = game.dealer
            self.assertEqual(dealers_cards, dealer.hand.cards,
                             'Dealer must not hit on a hard 17')
Exemplo n.º 31
0
def run():
    # start first game
    print("Welcome to Black Jack!")

    # prompt user for name and chips
    name = input("Enter your name: ")
    while True:
        chip_total = input("Enter how many chips you have [Enter an integer]: ")
        if chip_total.isdigit():
            chip_total = int(chip_total)
            print()
            break
        else:
            print('Please enter an integer amount. \n')
            
    # initialize game
    game = Game(name, chip_total)
    game.play_round()

    while input("Would you like to play another round? Type 'y' to play another round, or any other character to stop. ") == 'y':
        game.play_round()

    print('\nThanks for playing Black Jack!')
Exemplo n.º 32
0
    def test_blackjack_always_wins(self):
        # If a player has a blackjack (e.g. their two first cards total
        # 21 points) they automatically win.
        game = Game([])

        game.player.add_card(Card(1, SUIT_SPADES))
        game.player.add_card(Card(13, SUIT_SPADES))

        game.dealer.add_card(Card(1, SUIT_SPADES))
        game.dealer.add_card(Card(13, SUIT_SPADES))

        self.assertEqual(game.player, game.get_winner())

        # Player may have 21 points without having a blackjack.
        game = Game([])

        game.player.add_card(Card(9, SUIT_SPADES))
        game.player.add_card(Card(10, SUIT_SPADES))
        game.player.add_card(Card(2, SUIT_SPADES))

        game.dealer.add_card(Card(1, SUIT_SPADES))
        game.dealer.add_card(Card(13, SUIT_SPADES))

        self.assertIsNone(game.get_winner(), 'Game should be a draw')