-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetteEMezzoGame.py
145 lines (119 loc) · 4.56 KB
/
SetteEMezzoGame.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from typing import Dict, Tuple
import math
import numpy as np
from Deck import Deck
MATTA = 40
FIGURES = [10, 20, 30, 40, 8, 18, 28, 38, 9, 19, 29, 39]
# 1 2 3 4 5 6 7 8 9 10
# 1 2 3 4 5 6 7 .5 .5 .5
class SetteEMezzo:
def __init__(self):
self.actions = [0, 1]
self.deck = Deck()
self.win = 0
self.matta_drawn = False
self.policy = (-1, -1)
def observation(self) -> Tuple[float, float]:
return (self.player, self.bust_prob(self.player))
def start_new_episode(self) -> Tuple[float, float]:
if len(self.deck.cards) < 15 or self.matta_drawn:
self.matta_drawn = False
self.deck.shuffle_deck()
self.player = self.card_value(self.deck.draw_card())
self.dealer = self.card_value(self.deck.draw_card())
self.masked_card = self.player
self.cards_count = 1
self.cards_count_dealer = 1
return self.observation()
def is_bust(self, cards_value: int) -> bool:
return cards_value > 7.5
def bust_prob(self, actual_value):
max_card_value = math.floor(7.5 - actual_value)
pos = sum(self.deck.card_count[i] for i in range(1, max_card_value + 1))
pos += sum(self.deck.card_count[i] for i in range(8, 11))
tot = sum(self.deck.card_count[i] for i in range(1, 11))
prob = (tot - pos) / tot
return round(prob * 100 / 5) * 5
def card_value(self, card: int, actual_value: float = 0) -> float:
if card == MATTA:
self.matta_drawn = True
return 7 - actual_value if actual_value < 7 else 0.5
elif card in FIGURES:
return 0.5
else:
return card % 10
def get_action(self, cards_value_bust_prob: Tuple[Tuple[float, float]]) -> str:
if cards_value_bust_prob[0] == 0.5:
return 0 # HIT
if cards_value_bust_prob[0] == 7.5:
return 1 # STAY
if self.policy == (-1, -1):
return np.random.choice([0, 1])
return (
0 # "HIT"
if (cards_value_bust_prob[0] <= self.policy[0] or self.policy[0] == -1)
and (cards_value_bust_prob[1] <= self.policy[1] or self.policy[1] == -1)
else 1 # "STICK"
)
def guessing_card(self):
guessing_list = list(range(math.ceil(7.5 - self.player + self.masked_card)))
guessing_list.append(0.5)
guessing_list.remove(0)
return np.random.choice(guessing_list)
def step(
self, action: str, player_bust_reward=-1
) -> Tuple[Tuple[float, float], float, bool]:
if action == 0:
self.player += self.card_value(self.deck.draw_card())
self.cards_count += 1
if self.is_bust(self.player):
done = True
reward = player_bust_reward
else:
done = False
reward = 0
else:
done = True
reward = 0
hidden_card_value = (
self.guessing_card() if self.player != 7.5 else self.masked_card
)
# print(
# "Dealer guess: "
# + str(self.masked_card)
# + " with: "
# + str(hidden_card_value)
# )
while self.dealer < self.player - self.masked_card + hidden_card_value:
self.dealer += self.card_value(self.deck.draw_card())
self.cards_count_dealer += 1
# player_real_seven_and_half = self.player == 7.5 and self.cards_count == 2
# dealer_real_seven_and_half = (
# self.dealer == 7.5 and self.cards_count_dealer == 2
# )
if self.is_bust(self.dealer):
reward += 1
self.win += 1
else:
if self.dealer >= self.player:
reward -= 1
# if dealer_real_seven_and_half:
# reward -= 1
else:
reward = 1
self.win += 1
# if player_real_seven_and_half:
# reward += 1
return self.observation(), reward, done
def run_episode(self):
states, actions, rewards = [], [], []
obs = self.start_new_episode()
while True:
states.append(obs)
action = self.get_action(obs)
actions.append(action)
obs, reward, is_finished = self.step(action=action)
rewards.append(reward)
if is_finished:
break
return states, actions, rewards