def test_draw_cards(self): mock_game = Mock() deck = Deck(mock_game) card1 = Card('Sample Card', Suit.FOX) card2 = Card('Sample Card 2', Suit.FOX) deck.cards = [card1, card2] self.assertEqual(deck.draw_cards(2), [card2, card1])
def test_draw_card_empty_deck(self): mock_game = Mock() deck = Deck(mock_game) card = Card('Sample Card', Suit.FOX) deck.cards = [] deck.discard_pile = [card] self.assertEqual(deck.draw_card(), card)
def test_draw_cards_partially_empty_deck_empty_discard_pile(self): mock_game = Mock() deck = Deck(mock_game) card1 = Card('Sample Card', Suit.FOX) card2 = Card('Sample Card 2', Suit.FOX) deck.cards = [card1, card2] deck.discard_pile = [] self.assertEqual(deck.draw_cards(3), [card2, card1])
def test_draw_cards_empty_deck(self, mock_random): mock_game = Mock() deck = Deck(mock_game) card1 = Card('Sample Card', Suit.FOX) card2 = Card('Sample Card 2', Suit.FOX) deck.cards = [] deck.discard_pile = [card1, card2] self.assertEqual(deck.draw_cards(2), [card2, card1])
def test_hand_storing(): deck = Deck() hand = Hand() for i in range(4): hand.receive(deck.deal()) sorted_hand = hand.sorted() for i in range(1, len(hand)): assert sorted_hand[i] >= sorted_hand[i - 1]
def test_hand_picking(): deck = Deck() original_size = len(deck) hand = Hand() card = deck.deal() hand.receive(card) assert deck.dealed[0] == hand[0] assert len(hand) == 1 assert len(deck) == original_size - 1
def test_replace_card_remain_at_same_place(): deck = Deck() card1 = deck.deal() card2 = deck.deal() card3 = deck.deal() new_card = deck.replace(card2) assert new_card != card2 assert card1 == deck.dealed[0] assert new_card == deck.dealed[1] assert card3 == deck.dealed[2]
class PokerGame(object): def __init__(self, user_name, balance): self.user = user_name self.balance = balance self.hand = None self.deck = None self.bet = 0 self.reset() def get_balance(self): return self.balance def get_json_cards(self): c = [] for i in range(len(self.hand.cards)): card = self.hand[i] c.append({ 'rank': card.get_rank(), 'suit': card.get_suit(), 'index': i }) return c def make_turn(self, bet): self.bet = bet for i in range(5): self.deck.deal() self.hand.cards = self.deck.dealed def show_hand(self): for i in range(len(self.hand)): print("#%d: %s" % (i, self.hand[i])) def test_hand(self): i = 0 while i < len(SCORES): if SCORES[i]['tester'](self.hand): return SCORES[i] i += 1 return SCORES[i] def validate_hand(self): score = self.test_hand() self.balance -= self.bet self.balance += score['payout'] * self.bet return score def replace(self, card): self.deck.replace(self.hand[int(card)]) def reset(self): self.hand = Hand() self.deck = Deck() self.deck.shuffle() self.bet = 0
def test_replaced_card(): deck = Deck() original_size = len(deck) card = deck.deal() new_card = deck.replace(card) assert len(deck) == original_size - 2 assert len(deck.dealed) == 1 assert len(deck.burned) == 1 assert card != new_card assert deck.dealed[0] == new_card assert deck.burned[0] == card
def __init__(self, players): self.deck = Deck() self.numPlayers = players self.cards = [] for i in range(players): self.cards.append(self.deck.deal(1)[0]) for i in range(players): self.cards[i].append(self.deck.deal(1)[0]) print(self.cards)
def __init__(self, num_agents, min_buy_in, max_buy_in, buy_ins): """ Used to set environment variables """ self.deck = Deck() self.deck.shuffle() self.player_count = num_agents self.players = [] self.minBuyIn = min_buy_in self.maxBuyIn = max_buy_in for i in range(num_agents): players.append(TexasPlayer(buy_ins[i], i + 1))
class BlackJack: def __init__(self, players): self.deck = Deck() self.numPlayers = players self.cards = [] for i in range(players): self.cards.append(self.deck.deal(1)[0]) for i in range(players): self.cards[i].append(self.deck.deal(1)[0]) print(self.cards) def step(self): pass
def test_deck_has_unique_cards(): deck = Deck() i = 0 while i < len(deck): j = 0 while j < len(deck): if i != j: assert deck[i] != deck[j] j += 1 i += 1
def test_round_end(self): '''This test check that a winner is found''' test_input = { 'decks': 1, 'rounds': 2, 'players': 1, 'human players': 1, 'comp_levels': {} } state = GameState(test_input) state.start_new_round() player_obj = state.players.get_player_by_id(state.current_player) player_obj.hand = Deck(1).cards state.print_round_state_to_cli() test_cmd_strs_1 = [ str(i) + s + "L" + str(suit_layout_dict[s]) for s in suit_layout_dict.keys() for i in range(7, 15) ] test_cmd_strs_2 = [ str(i) + s + "L" + str(suit_layout_dict[s]) for s in suit_layout_dict.keys() for i in range(2, 7) ] test_cmd_strs_2.reverse() test_cmd_strs = test_cmd_strs_1 + test_cmd_strs_2 test_commands = [Command(c, state.layouts) for c in test_cmd_strs] player_obj = state.players.get_player_by_id(state.current_player) while not state.check_round_winner(): for test_command in test_commands: state.current_command = test_command state.update() state.print_round_state_to_cli() state.start_new_round() self.assertEqual(state.round_number, 2) self.assertEqual(state.dealer_id, 0) while not state.check_round_winner(): for test_command in test_commands: state.current_command = test_command state.update() state.print_round_state_to_cli() print("Total rounds: ", state.total_rounds) print("round_number: ", state.round_number) self.assertTrue(state.check_game_end())
def test_deck_burned_dealed_are_correct_size(): deck = Deck() original_size = len(deck) card1 = deck.deal() card2 = deck.deal() deck.replace(card1) deck.replace(card2) assert len(deck) == 48 assert len(deck) + deck.burned_size() + deck.dealed_size() == original_size
def start_new_round(self): '''Updates class variable to start a new round.''' # Notify user of new round print("\nRound number '{}' starting ...".format(self.round_number)) # Clear players cards self.players.clear_hands() # Create deck self.game_deck = Deck(self.deck_num) # Shuffle deck self.game_deck.shuffle() # Deal deck to players self.game_deck.deal(self.players) # Create layouts self.layouts = Layouts(self.deck_num)
def test_valid_command(self): '''This test ensures that a valid command is correctly checked''' deck = Deck(1) player_1 = Player(0) player_2 = Player(1) players = [player_1] layouts = Layouts(1) deck.shuffle() deck.deal(players) seven_suits = [c.suit for c in player_1.hand if c.rank == 7] test_suit = seven_suits[0] card_cmd_str = "7" + test_suit layout_dict = { "C": "L0", "D": "L1", "H": "L2", "S": "L3" } layout_cmd_str = layout_dict[test_suit] cmd_str = card_cmd_str + layout_cmd_str cmd = Command(cmd_str, layouts) self.assertTrue(cmd.is_valid(player_1.hand))
class Texas: def __init__(self, num_agents, min_buy_in, max_buy_in, buy_ins): """ Used to set environment variables """ self.deck = Deck() self.deck.shuffle() self.player_count = num_agents self.players = [] self.minBuyIn = min_buy_in self.maxBuyIn = max_buy_in for i in range(num_agents): players.append(TexasPlayer(buy_ins[i], i + 1)) def step(self, move): """ Player uses this function to make a move """ def reset(self): """
def test_reshuffle_discard_pile_into_deck(self): mock_game = Mock() deck = Deck(mock_game) card = Card('Sample Card', Suit.FOX) deck.cards = [] deck.discard_pile = [card] deck.reshuffle_discard_pile_into_deck() self.assertEqual(deck.cards, [card]) self.assertEqual(deck.discard_pile, [])
def test_card_ten(self): '''This test check that a card can be played with a double digit rank''' test_input = { 'decks': 1, 'rounds': 1, 'players': 1, 'human players': 1, 'comp_levels': {} } state = GameState(test_input) state.start_new_round() player_obj = state.players.get_player_by_id(state.current_player) player_obj.hand = [c for c in Deck(1)] state.print_round_state_to_cli() test_commands = [] test_commands.append(Command("7CL0", state.layouts)) test_commands.append(Command("8CL0", state.layouts)) test_commands.append(Command("9CL0", state.layouts)) test_commands.append(Command("10CL0", state.layouts)) test_commands.append(Command("JCL0", state.layouts)) test_commands.append(Command("QCL0", state.layouts)) test_commands.append(Command("KCL0", state.layouts)) test_commands.append(Command("ACL0", state.layouts)) test_commands.append(Command("6CL0", state.layouts)) test_commands.append(Command("5CL0", state.layouts)) test_commands.append(Command("4CL0", state.layouts)) test_commands.append(Command("3CL0", state.layouts)) test_commands.append(Command("2CL0", state.layouts)) for test_command in test_commands: state.current_command = test_command state.update() state.print_round_state_to_cli() extra_command = Command("7HL2", state.layouts) state.current_command = extra_command state.update() state.print_round_state_to_cli()
def test_discard_card(): deck = Deck() original_size = len(deck) card1 = deck.deal() card2 = deck.deal() card3 = deck.deal() deck.discard(card2) assert len(deck) == original_size - 3 assert len(deck.burned) == 1 assert len(deck.dealed) == 2 assert deck.dealed[0] == card1 assert deck.dealed[1] == card3
def test_player(self): ''' Ensures that both players have 26 cards in their hand after dealing 1 deck. ''' deck = Deck(1) player_1 = Player(0) player_2 = Player(1) players = [player_1, player_2] deck.shuffle() deck.deal(players) self.assertEqual(len(player_1.hand), 26)
def test_doubledeck(self): ''' Ensures that both players have 52 cards in their hand after dealing 2 decks. ''' deck = Deck(2) player_1 = Player(0) player_2 = Player(1) players = [player_1, player_2] deck.shuffle() deck.deal(players) self.assertEqual(len(player_1.hand), 52)
def test_hand(self): x = Hand() base_deck = Deck() #case 1: add some cards, make sure the hand goes to the right length x.add_cards([2, 3, 4, 5], base_deck) self.assertTrue(len(x) == 4) #case 2: add some illegal cards, make sure they are not added x.add_cards([-1, 55, 'q'], base_deck) self.assertTrue(len(x) == 4) #case 4: add an initialized card object base_deck.reset() c1 = Card('As') x.add_cards([c1], base_deck) self.assertTrue(len(x) == 5) #case 5: test adding cards with string names base_deck.reset() y = Hand() y.add_cards(['Ah', 'As', '5d'], base_deck) self.assertTrue(len(y) == 3) #case 6: insure duplicate strings don't add y.add_cards(['Ah'], base_deck) self.assertTrue(len(y) == 3) #case 7: add from a string base_deck.reset() y = Hand() y.add_cards('AhAdAs2d', base_deck) self.assertTrue(len(y) == 4) #case 8: make sure we can't pull a card twice from a deck z = Hand() z.add_cards('AhAdAs2d5d', base_deck) self.assertTrue(len(z) == 1)
def reset(self): self.hand = Hand() self.deck = Deck() self.deck.shuffle() self.bet = 0
class GameState(): ''' The Game State object contains all variables needed to represent a game. ''' def __init__(self, user_input): '''Creates GameState according to user_input''' # Set starting values for class members self.layouts = [] self.round_number = 1 self.dealer_id = 0 self.current_player = 0 self.current_command = None self.game_deck = None # Set class members based on user input self.total_rounds = user_input["rounds"] self.deck_num = user_input["decks"] self.players = Players( user_input["players"], user_input["human players"], user_input["comp_levels"] ) def start_new_round(self): '''Updates class variable to start a new round.''' # Notify user of new round print("\nRound number '{}' starting ...".format(self.round_number)) # Clear players cards self.players.clear_hands() # Create deck self.game_deck = Deck(self.deck_num) # Shuffle deck self.game_deck.shuffle() # Deal deck to players self.game_deck.deal(self.players) # Create layouts self.layouts = Layouts(self.deck_num) def process_command(self): ''' Requests command from current player and updates the class variables. ''' # Get player instance from the id in the GameState current_player_obj = self.players.get_player_by_id(self.current_player) print( "Player {}'s Turn ({})\n".format( self.current_player, current_player_obj.__class__.__name__ ) ) # Request and validate command from the current player self.current_command = current_player_obj.request_command(self.layouts) print( "Command: {} given by Player: {}_{}".format( self.current_command, current_player_obj.__class__.__name__, self.current_player ) ) # Skip update if command is a pass if not self.current_command.pass_cmd: # Update game state with validated command self.update() def update(self): '''Updates the players cards and layouts''' # Remove card from current players hand self.current_command.card.remove_from_list( self.players.get_player_by_id(self.current_player).hand ) # Add card to Layout layout = self.layouts.get_layout_by_id( self.current_command.layout.layout_id ) layout.cards.append(self.current_command.card) # Update layout valid_cards layout.calculate_valid_cards() def end_turn(self): '''Sets current player to next player_id.''' # Change current player if self.current_player != len(self.players) - 1: self.current_player += 1 else: self.current_player = 0 def end_round(self): '''Increments round_number''' # Increment round number self.round_number += 1 # Increment dealer_id if self.dealer_id != len(self.players) - 1: self.dealer_id += 1 else: self.dealer_id = 0 def check_game_end(self): '''Checks if specified number of game rounds have been exceeded.''' # Check if current round > the number of total desired rounds return self.round_number > self.total_rounds def check_round_winner(self): '''Check if current player has played their last card.''' # Get player instance from the id in the GameState current_player_obj = self.players.get_player_by_id(self.current_player) # Check whether player just played winning move winner = current_player_obj.check_if_winner() if winner: print("Round has been won!") self.end_round() return winner def print_round_state_to_cli(self): ''' Prints current layout state and human player's hand if current player. ''' # Print layouts table print(self.layouts) print("") # Get current Player from current_player_id current_player_obj = self.players.get_player_by_id(self.current_player) # Print Player cards if human if isinstance(current_player_obj, Human): print( "Printing Human Player {} cards:".format( current_player_obj.player_id ) ) print(current_player_obj.get_hand_table()) print("")
def test_deck(self): x = Deck() x.shuffle() #case 1: make sure a shuffled deck has all the cards self.assertTrue(len(x) == 52) self.assertTrue(len(x.deck_state()) == 52) #case 2: pull a single card and make sure the deck shrinks c = x.deal_one() self.assertTrue(len(x) == 51) self.assertTrue(len(x.deck_state()) == 51) #case 3: reshuffle and make sure the deck is whole x.reset() self.assertTrue(len(x) == 52) self.assertTrue(len(x.deck_state()) == 52) #case 4: reshuffle, take a specific card, and insure the deck shrinks x.reset() c = Card('Ah') x.take_card(c) self.assertTrue(len(x) == 51) #case 5: make sure we can't take the same card twice self.assertTrue(x.take_card(c) == -1) #case 6: get a five card hand x.reset() l = x.deal_hand(5) self.assertTrue(len(l) == 5) self.assertTrue(len(x) == 52-5) #case 7: shuffle, make sure the length is unchanged x.shuffle() self.assertTrue(len(x) == 52-5)
def test_hand_cannot_be_greater_than_5(): deck = Deck() hand = Hand() with pytest.raises(HandFullException): for i in range(10): hand.receive(deck.deal())
def test_restore(self): x = Deck() x.shuffle() y = x.deck_state() z = Deck() self.assertTrue(z.restore_deck(y) == True) self.assertTrue(z.deck_state() == x.deck_state()) z.shuffle() self.assertFalse(z.deck_state() == x.deck_state())
def test_new_deck_is_ordered(): deck = Deck() assert deck.is_ordered()
def test_deal_card(): deck = Deck() original_size = len(deck) card = deck.deal() assert len(deck) == original_size - 1 assert deck.dealed[0] == card
def test_deck_has_52_cards(): deck = Deck() assert len(deck) == 52
def test_shuffed_deck_is_not_ordered(): deck = Deck() deck.shuffle() assert not deck.is_ordered()