Exemplo n.º 1
0
    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))
Exemplo n.º 2
0
 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())
Exemplo n.º 3
0
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
Exemplo n.º 4
0
    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)
Exemplo n.º 5
0
    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)
Exemplo n.º 6
0
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):
        """
Exemplo n.º 7
0
    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)
Exemplo n.º 8
0
def test_shuffed_deck_is_not_ordered():
    deck = Deck()
    deck.shuffle()
    assert not deck.is_ordered()
Exemplo n.º 9
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("")