Exemplo n.º 1
0
    def test_hand(self):
        hand = Hand()
        board = Hand()
        # board.add_card(Card(0, '0'))
        deck = Deck()

        ace_hearts = Card(14, '♥')
        two_spades = Card(2, '♠')

        pair_comb = Hand()
        for _ in range(2):
            pair_comb.add_card(two_spades)

        two_pairs_comb = Hand()
        for _ in range(2):
            two_pairs_comb.add_card(two_spades)
            two_pairs_comb.add_card(ace_hearts)

        three_comb = Hand()
        for _ in range(3):
            three_comb.add_card(two_spades)

        straight_comb = Hand()
        for i in range(5):
            straight_comb.add_card(Card(i + 1, suits[i % 4]))

        flash_comb = Hand()
        for i in range(5):
            flash_comb.add_card(Card(i * 2 + 2, suits[0]))

        full_house_comb = Hand()
        for _ in range(3):
            full_house_comb.add_card(ace_hearts)
        for _ in range(2):
            full_house_comb.add_card(two_spades)

        four_comb = Hand()
        for _ in range(4):
            four_comb.add_card(two_spades)

        straight_flash_comb = Hand()
        for i in range(5):
            straight_flash_comb.add_card(Card(i + 2, suits[0]))

        royal_flash_comb = Hand()
        for i in range(5):
            royal_flash_comb.add_card(Card(14 - i, suits[0]))

        self.assertEqual(pair_comb.check_combination(board), 14 + 1,
                         "Expected pair")
        self.assertEqual(two_pairs_comb.check_combination(board), 14 + 2,
                         "Expected two pairs")
        self.assertEqual(three_comb.check_combination(board), 14 + 3,
                         "Expected three of a kind")
        self.assertEqual(straight_comb.check_combination(board), 14 + 4,
                         "Expected straight")
        self.assertEqual(flash_comb.check_combination(board), 14 + 5,
                         "Expected flash")
        self.assertEqual(full_house_comb.check_combination(board), 14 + 6,
                         "Expected full house")
        self.assertEqual(four_comb.check_combination(board), 14 + 7,
                         "Expected four of a kind")
        self.assertEqual(straight_flash_comb.check_combination(board), 14 + 8,
                         "Expected straight flash")
        self.assertEqual(royal_flash_comb.check_combination(board), 14 + 9,
                         "Expected royal flash")
Exemplo n.º 2
0
    def create_train(self):
        train = []
        open_cards_quantity = random.randint(2, 5)
        deck = Deck()
        deck.shuffle()

        hand1 = Hand()
        hand2 = Hand()
        board = Hand()

        for i in range(2):
            hand1.add_card(deck.get_card())
        for i in range(2):
            hand2.add_card(deck.get_card())
        for i in range(5):
            board.add_card(deck.get_card())

        round_result = 0
        if hand1.better_than(hand2, board):
            round_result = 2  # Player wins
        elif hand1.worse_than(hand2, board):
            round_result = 0  # Opponent wins
        elif hand1.equal_to(hand2, board):
            round_result = 1  # Draw

        values = []
        suits = []

        # In hand 1
        for card in hand1.cards:
            values.append(card.value)

            # suits = ["♠", "♣", "♥", "♦"]  # "spades", " clubs", "hearts", "diamonds"
            if card.suit == '0':
                suits.append(0)
            elif card.suit == "♠":
                suits.append(1)
            elif card.suit == "♣":
                suits.append(2)
            elif card.suit == "♥":
                suits.append(3)
            elif card.suit == "♦":
                suits.append(4)

        # In board
        for i in range(open_cards_quantity):
            values.append(board.cards[i].value)

            # suits = ["♠", "♣", "♥", "♦"]  # "spades", " clubs", "hearts", "diamonds"

            if board.cards[i].suit == "♠":
                suits.append(1)
            elif board.cards[i].suit == "♣":
                suits.append(2)
            elif board.cards[i].suit == "♥":
                suits.append(3)
            elif board.cards[i].suit == "♦":
                suits.append(4)
        for i in range(5 - open_cards_quantity):
            values.append(0)
            suits.append(0)

        numpy_train = np.array(values + suits)  # Convert our array to numpy array
        # numpy_train = np.array(values)
        # print(numpy_train.shape)
        return numpy_train, round_result
class Round:
    def __init__(self,
                 players,
                 dealer_turn,
                 players_number=2,
                 cards_in_hand_number=2,
                 deal_amount=50,
                 raise_size=25):
        # Round parameters
        self.player_turn = dealer_turn
        self.deal_amount = deal_amount
        self.raise_size = raise_size

        # Players
        self.players = players

        # Initialization
        self.bank = 0
        self.curr_bet = 0
        self.board = Hand()
        self.winner = 0

        # Prepare items
        self.deck = Deck()  # Make deck
        self.deck.shuffle()  # Shuffle
        self.hands = []  # Make two hands

        # Deal cards to players
        for i in range(players_number):
            curr_hand = Hand()
            for j in range(cards_in_hand_number):
                curr_hand.add_card(self.deck.get_card())
            self.hands.append(curr_hand)

        self.players[0].hand = self.hands[0]
        self.players[1].hand = self.hands[1]

        # Deal two first cards to board
        for i in range(2):
            self.board.add_card(self.deck.get_card())

    ## First round steps
    def start(self):
        # Deal points by dealer
        self.bank = 0
        self.bank += self.deal_amount
        self.curr_bet = self.deal_amount
        self.players[self.player_turn].points -= self.deal_amount
        self.players[self.player_turn].player_bet = self.deal_amount

        # Deal points by second player
        self.bank += self.deal_amount / 2
        self.players[1 - self.player_turn].points -= self.deal_amount / 2
        self.players[1 - self.player_turn].player_bet = self.deal_amount

    ## Makes changes to the playing position in accordance with the perfect action
    # @input Player action
    def take_action(self, curr_player, action):
        # 0 - pass
        # 1 - call
        # 2 - raise
        if action == 0:
            self.players[1 - curr_player].points += self.bank
            self.winner = 1 - curr_player
        elif action == 1:
            self.bank += self.curr_bet - self.players[curr_player].player_bet
            self.players[curr_player].points -= self.curr_bet - self.players[
                curr_player].player_bet
            self.players[curr_player].player_bet = self.curr_bet
        elif action == 2:
            self.curr_bet = self.players[
                curr_player].player_bet + self.raise_size
            self.bank += self.curr_bet - self.players[curr_player].player_bet
            self.players[curr_player].points -= self.curr_bet - self.players[
                curr_player].player_bet
            self.players[curr_player].player_bet = self.curr_bet

    ## Opens one more card on board
    def open_card(self):
        self.board.add_card(self.deck.get_card())

    ## Calculates the results of round, makes changes in points
    def summarize(self):
        if self.hands[0].better_than(self.hands[1], self.board):
            self.winner = 2
            self.players[0].points += self.bank
        elif self.hands[0].worse_than(self.hands[1], self.board):
            self.winner = 0
            self.players[1].points += self.bank
        elif self.hands[0].equal_to(self.hands[1], self.board):
            self.winner = 1
            self.players[0].points += self.bank / 2
            self.players[1].points += self.bank / 2

        return self.winner