Ejemplo n.º 1
0
    def results(self):
        while 0 < get_score(self.dealer_cards) < 17 or is_soft_17(
                self.dealer_cards):
            self.dealer_cards.append(self.deck.next_card())

        self.dealer_score = get_score(self.dealer_cards)

        for i in range(self.number_of_players):
            current = []
            for j in range(len(self.hands[i])):
                current.append(self.evaluate(i, j))
            self.hand_rewards[i] = current

        shuffle = False
        if self.deck.index > self.shuffle_every:
            shuffle = True

        return {
            "shuffle": shuffle,
            "done": True,
            "dealer_cards": self.dealer_cards,
            "dealer_score": self.dealer_score,
            "hands": self.hands,
            "rewards": self.hand_rewards,
            "count": self.deck.counter.get_rc()
        }
Ejemplo n.º 2
0
def simple_play_2(dealer, strategy):
    res = dealer.reset()
    while not res["done"]:

        player_playing = res["player_playing"]
        hand_playing = res["hand_playing"]
        dealer_cards = res["dealer_cards"]
        hand = res["hands"][player_playing][hand_playing]
        can_split = len(res["hands"][player_playing]) == 1

        action = choose_action(hand,
                               dealer_cards,
                               strategy=strategy,
                               can_split=can_split)
        res = dealer.step(action)

    player_blackjacks = [get_score(cards) for cards in res["hands"][0]]
    player_blackjacks = [s == 22 for s in player_blackjacks]
    player_blackjacks = [1 if s else 0 for s in player_blackjacks]
    dealer_blackjack = 1 if get_score(res["dealer_cards"]) == 22 else 0
    dealer_burst = 1 if get_score(res["dealer_cards"]) == 0 else 0
    rewards = res["rewards"][0]

    return np.mean(rewards), np.mean(
        player_blackjacks), dealer_blackjack, dealer_burst
Ejemplo n.º 3
0
    def test_get_score(self):
        cards = [[1, 5, 5], [1, 3, 8], [2, 3, 8], [10, 1], [1, 10], [1, 10, 4],
                 [4, 5, 12, 10], [13, 4], [1, 1, 1, 1, 1]]
        truth = [21, 12, 13, 22, 22, 15, 0, 14, 15]

        scores = [get_score(c) for c in cards]
        print(scores)

        for i in range(len(truth)):
            self.assertEqual(scores[i], truth[i])
Ejemplo n.º 4
0
    def evaluate(self, player_id, hand_id):
        doubled = hand_id in self.doubled_hands[player_id]
        x = 2 if doubled else 1
        player_score = get_score(self.hands[player_id][hand_id])

        if player_score == 22 and len(self.hands[player_id]) >= 2:
            player_score = 21

        if player_score == 0:
            return -x
        if player_score == 22 and self.dealer_score < 22:
            return 1.5 * x
        if self.dealer_score > player_score:
            return -x
        if self.dealer_score == player_score:
            return 0
        return x
Ejemplo n.º 5
0
    def reset(self):
        self.shuffle_if_needed()
        self.hands = {}
        self.dealer_cards = []
        self.doubled_hands = {}
        self.hand_rewards = {}
        self.dealer_score = None

        for i in range(self.number_of_players):
            self.doubled_hands[i] = []
            self.hand_rewards[i] = []

        self.player_playing = 0
        self.hand_playing = 0

        for i in range(self.number_of_players):
            self.hands[i] = [[self.deck.next_card()]]

        self.dealer_cards.append(self.deck.next_card())

        for i in range(self.number_of_players):
            # TODO: check that this is the way to do this
            self.hands[i][0].append(self.deck.next_card())

        # pour le blackjack américain il faut rajouter ça ici:
        # self.dealer_cards.append(self.deck.next_card())

        for i in range(self.number_of_players):
            self.player_playing = i
            if get_score(self.hands[i][0]) not in [0, 22]:
                done = False
                break
            else:
                done = True

        if self.player_playing == self.number_of_players - 1 and done:
            return self.results()

        return {
            "done": False,
            "dealer_cards": self.dealer_cards,
            "hands": self.hands,
            "hand_playing": self.hand_playing,
            "player_playing": self.player_playing
        }
Ejemplo n.º 6
0
def play(n):
    """
    play n games of blackjack
    """
    for i in range(n):
        print("\nNew game:")
        res = dealer.reset()
        while not res["done"]:
            print("hands:")
            print(res["hands"])
            print("dealer:")
            print(res["dealer_cards"])
            player_playing = res["player_playing"]
            hand_playing = res["hand_playing"]
            hand = res["hands"][player_playing][hand_playing]
            score = get_score(hand)
            print("hand playing: "+str(hand))
            print("your score: "+str(score))
            action = ""
            while action not in action_space:
                action = input("please enter an action\n")
            res = dealer.step(action)
        print(res)
Ejemplo n.º 7
0
 def hit(self, player_id, hand_id):
     self.hands[player_id][hand_id].append(self.deck.next_card())
     score = get_score(self.hands[player_id][hand_id])
     # TODO: remplacer par score in [0, 21, 22] ?
     return score in [0, 22]
Ejemplo n.º 8
0
def naive_strategy(hand, dealer_hand, strategy):
    if get_score(hand) > strategy["k"]:
        return "stick"
    return "hit"