Ejemplo n.º 1
0
    def player_turn(self):
        deck = Deck()
        deck.shuffle()
        hand = []
        hand.append(deck.draw_card())
        hand.append(deck.draw_card())
        while True:
            print(self.separator)
            print('Ваши карты: ')
            print(hand)
            print('Очков: ', self.calculate_score(hand))
            if self.calculate_score(hand) == 21:
                print('Blackjack!')
                break
            if self.calculate_score(hand) > 21:
                print('Перебор')
                break
            resp = input('Продолжаем? (y/n) ').lower()
            if resp == 'n':
                break
            elif resp == 'y':
                card = deck.draw_card()
                hand.append(card)

        player_score = self.calculate_score(hand)
        return player_score
Ejemplo n.º 2
0
    def computer_turn(self):
        deck = Deck()
        deck.shuffle()
        hand = []
        hand.append(deck.draw_card())
        hand.append(deck.draw_card())
        computer_score = self.calculate_score(hand)

        print(self.separator)
        print('Начальная рука')
        print(hand)
        print(computer_score)

        while True:
            if computer_score > 21:
                print('Перебор')
                break
            # Здесь можно добавить решение, если в руке есть тузы и количество очков 19-20
            odds = self.calculate_odds(deck, hand)
            if odds == -1:
                # Ровно 21 в руке
                print('Blackjack!')
                break
            #print('Вероятность не проиграть', odds)
            if odds < 0.35:
                #print('Хватит')
                break
            #print('беру карту')
            hand.append(deck.draw_card())
            computer_score = self.calculate_score(hand)
            print(self.separator)
            print('Рука')
            print(hand)
            print(computer_score)
        return computer_score
Ejemplo n.º 3
0
def main():
    d = Deck()
    print 'hands:'
    c1 = raw_input('card1?')
    c2 = raw_input('card2?')
    c3 = raw_input('card3?')
    c4 = raw_input('card4?')
    card1 = Card(c1[0], c1[1])
    card2 = Card(c2[0], c2[1])
    card3 = Card(c3[0], c3[1])
    card4 = Card(c4[0], c4[1])
    ps = list()
    ps.append(Hand(card1, card2))
    ps.append(Hand(card3, card4))
    # ps = d.deal_players(N_PLAYERS)
    ps_str = ''
    for p in ps:
        ps_str += str(p) + ', '
    print ps_str

    wins = [0] * N_PLAYERS
    for loop in range(0, N_LOOP):
        d.reset()
        for p in ps:
            d.draw_card(p.card1)
            d.draw_card(p.card2)

        # print "community:"
        com = d.deal_community()
        com_str = ''
        for c in com:
            com_str += str(c) + ', '
        # print com_str

        ss = []
        for i in range(0, N_PLAYERS):
            ss.append(Holdem.showdown_hand_score(ps[i], com))
            # print ps[i], ss[i]
            # # if ss[i][0] == '9':
            # #     exit()
        # print 'best:'
        max_index, max_value = max(enumerate(ss), key=operator.itemgetter(1))
        # print max_index, max_value
        if ss[0] == ss[1]:
            wins[0] += 0.5
            wins[1] += 0.5
        else:
            wins[max_index] += 1  # OCOC what about ties?

    for i_wins in wins:
        print round(float(i_wins) / N_LOOP * 1000) / 10.0
Ejemplo n.º 4
0
def test_shuffle():
    deck = Deck()
    deck.shuffle()
    card1 = deck.draw_card()
    card2 = deck.draw_card()
    assert card1.rank != "King" and card1.suit != "Spades" and card2.rank != "King" and card2.suit != "Hearts"
    
Ejemplo n.º 5
0
def test_draw_card():
    deck = Deck()
    card = deck.draw_card()
    assert card.rank == "King"
    assert card.suit == "Spades"
Ejemplo n.º 6
0
# Game Setup
players = [
    Player(input(f'What is the name of player {n}: ')) for n in range(1, 3)
]
war_pile = []
game_speed = 0  # Number of seconds to pause between rounds
PLAYER_1 = 0
PLAYER_2 = 1

# Deal all of the cards
deck = Deck()
deck.shuffle()
while True:
    try:
        players[PLAYER_1].add_cards_to_bottom([deck.draw_card()])
        players[PLAYER_2].add_cards_to_bottom([deck.draw_card()])
    except IndexError:
        break

# Main game loop
while players[PLAYER_1].cards and players[
        PLAYER_2].cards:  # While both players have cards
    print('-' * 20)
    sleep(game_speed)
    drawn_cards = [player.draw_from_top() for player in players]
    for n, card in enumerate(drawn_cards):
        print(f'{players[n].name} plays the {card}')

    if drawn_cards[PLAYER_1].value() != drawn_cards[PLAYER_2].value(
    ):  # If there is a clear winner