-
Notifications
You must be signed in to change notification settings - Fork 0
/
ridethebus.py
220 lines (182 loc) · 5.87 KB
/
ridethebus.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
from random import shuffle, random
from statistics import mean, stdev, median
import matplotlib.pyplot as plt
from datetime import datetime
DEBUG = False
SMART = False
LOGGING = False
LOGFILE = open('log.csv', 'a')
deck = []
class Card:
def __init__(self, suit, value, face):
self.suit = suit
self.value = value
self.face = face
def __repr__(self):
return f"{self.face} of {self.suit}"
def __str__(self):
return self.__repr__()
def __eq__(self, other):
return self.value == other.value
def __lt__(self, other):
return self.value < other.value
def __gt__(self, other):
return self.value > other.value
def get_card() -> Card:
# cards += 1
if not deck:
makeDeck()
return deck.pop()
def driver():
attempts = 0
while True:
attempts += 1
if DEBUG:
print(f'\nattempt: {attempts}')
c1 = get_card()
if not rb(c1):
continue
c2 = get_card()
if not hilo(c1,c2):
continue
c3 = get_card()
if not inout(c1,c2,c3):
continue
c4 = get_card()
if suit(c4):
break
return attempts
def makeDeck():
for suit in ["Spades", "Hearts", "Clubs", "Diamonds"]:
for value in range(1, 14):
if value == 1:
face = "Ace"
elif value == 11:
face = "Jack"
elif value == 12:
face = "Queen"
elif value == 13:
face = "King"
else:
face = str(value)
deck.append(Card(suit, value, face))
shuffle(deck)
def __main__():
makeDeck()
s1 = []
iterations = 10000
for _ in range(iterations):
s1.append(driver())
print(f'Random mode: {mean(s1)=:.2f}, {stdev(s1)=:.2f}')
makeDeck()
global SMART
SMART = True
s2 = []
for _ in range(iterations):
s2.append(driver())
print(f'SMART mode: {mean(s2)=:.2f}, {stdev(s2)=:.2f}')
graphit(s1,s2)
def rb(c1: Card) -> bool:
# guessing algo
guess = None
if random() > 0.5:
guess = "red"
else:
guess = "black"
if SMART:
guess = "red" if sum(map(lambda c: 1 if c.suit in ("Hearts", "Diamonds") else -1, deck + [c1])) > 0 else "black"
if DEBUG:
print(f'RB: {guess} vs {c1.suit}')
# logic
if guess == "red":
result = c1.suit in ("Hearts", "Diamonds")
if guess == "black":
result = c1.suit in ("Spades", "Clubs")
if LOGGING:
log_step('rb', guess, result, sum(map(lambda c: 1 if c.suit in ("Hearts", "Diamonds") else -1, deck + [c1])))
return result
def hilo(c1: Card, c2: Card) -> bool:
# guessing algo
guess = None
if random() > 0.5:
guess = "higher"
else:
guess = "lower"
if SMART:
guess = "higher" if sum(map(lambda c: 1 if c > c1 else -1, deck + [c2])) > 0 else "lower"
if DEBUG:
print(f'HILO: {guess} vs {c1.value} , {c2.value}')
# logic
if guess == "higher":
result = c1.value < c2.value
elif guess == "lower":
result = c1.value > c2.value
if LOGGING:
log_step('hilo', guess, result, sum(map(lambda c: 1 if c > c1 else -1, deck + [c2])))
return result
def inout(c1: Card, c2: Card, c3: Card) -> bool:
guess = None
if random() > 0.5:
guess = "inside"
else:
guess = "outside"
# logic
bounds = sorted([c1,c2])
if SMART:
guess = "inside" if sum(map(lambda c: 1 if c > bounds[0] and c < bounds[1] else -1, deck + [c3])) > 0 else "outside"
if DEBUG:
print(f'INOUT: {guess} vs {bounds}, {c3}')
if guess == "inside":
result = c3 > bounds[0] and c3 < bounds[1]
elif guess == "outside":
result = c3 < bounds[0] or c3 > bounds[1]
if LOGGING:
log_step('inout', guess, result, sum(map(lambda c: 1 if c > bounds[0] and c < bounds[1] else -1, deck + [c3])))
return result
def suit(c4: Card) -> bool:
guess = None
if random() < 0.25:
guess = "Clubs"
elif random() < 0.5:
guess = "Spades"
elif random() < 0.75:
guess = "Hearts"
else:
guess = "Diamonds"
d = {"Clubs": 0, "Spades": 0, "Hearts": 0, "Diamonds": 0}
for c in deck:
d[c.suit] += 1
if SMART:
guess = sorted(d.items(), key=lambda x: -x[1])[0][0]
if DEBUG:
print(f'SUIT: {guess} vs {c4.suit}')
result = guess == c4.suit
if LOGGING:
log_step('suit', guess, result, ':'.join(f'{d[k]}' for k in sorted(d)))
return result
def log_step(step_name, guess, result, deck_status):
LOGFILE.write(f'{SMART}, {step_name}, {deck_status}, {guess}, {result}\n')
def graphit(s1,s2):
plt.hist(s1, bins=20, range=(0,200), alpha=0.5, label='Random', color='Red')
plt.hist(s2, bins=20, range=(0,200), alpha=0.5, label='Smart', color='Blue')
rmean = mean(s1)
rstd = stdev(s1)
rmed = median(s1)
smean = mean(s2)
sstd = stdev(s2)
smed = median(s2)
# Add text annotations
plt.text(50, plt.ylim()[1]*0.9, f'Mean: {rmean:.2f}', color='Red', ha='left')
plt.text(50, plt.ylim()[1]*0.85, f'1 Sigma: {rmean + rstd:.2f}', color='Red', ha='left')
plt.text(50, plt.ylim()[1]*0.8, f'Max: {max(s1):.2f}', color="Red", ha='left')
plt.text(100, plt.ylim()[1]*0.9, f'Mean: {smean:.2f}', color='Blue', ha='left')
plt.text(100, plt.ylim()[1]*0.85, f'1 Sigma: {smean + sstd:.2f}', color='Blue', ha='left')
plt.text(100, plt.ylim()[1]*0.8, f'Max: {max(s2):.2f}', color="Blue", ha='left')
plt.axvline(rmean, color='Red', linestyle='dashed', linewidth=1)
plt.axvline(smean, color = 'Blue', linestyle='dashed', linewidth=1)
plt.legend()
plt.title("Ride the Bus Distribution")
plt.xlabel("Score")
plt.ylabel("Frequency")
plt.savefig('ridethebus_smart.png')
__main__()