-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathonehand.py
More file actions
executable file
·362 lines (310 loc) · 13.7 KB
/
onehand.py
File metadata and controls
executable file
·362 lines (310 loc) · 13.7 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from __future__ import division
from typing import Optional
import copy
import pydealer # Used to magage the deck of cards so we don't have to write our own module
import uuid # Used to generate a unique "run ID" to store in the database
import pymysql # Only supporting MySQL/MariaDB right now, but should probably expand that
import argparse
import configparser
# Collect the arguments and set up some defaults
ap = argparse.ArgumentParser()
ap.add_argument("-n", "--games", required=False, help="Number of games to play", type=int)
ap.add_argument('-c', '--config', required=False, help='Location of config file to use')
ap.add_argument('--normal', required=False, help='Play games using the normal rules', action='store_true')
ap.add_argument('--reverse', required=False, help='Play games using the reverse rules', action='store_true')
ap.add_argument('--nodb', help='Do not write games to the database', required=False, action='store_true')
ap.add_argument('--debug', help='Print debug info to the screen', required=False, action='store_true')
ap.add_argument('--timing', help='Print program execution timing', required=False, action='store_true')
ap.add_argument('--samedeck', help='Play a single set of Normal/Reverse games, using the same deck for both.',
required=False, action='store_true')
args = ap.parse_args()
use_db = False # Assume we aren't writing to a DB unless we find out differently
if args.timing:
import timing # I used someone else's code for this module, but it seems to work well enough for my needs
# Get the config file
config = configparser.ConfigParser()
if args.config:
config.read(args.config)
else:
config.read('config')
if args.debug: # Check for debug on the command line
debug = True
else:
if config['General'].getboolean('debug'): # Check to see if debug is enabled in the config file
debug = True
else:
debug = False
# Let's find out if we need to write to the DB or not
if not args.nodb: # If the commandline option is set, don't bother setting up a DB
if config['Database'].getboolean('database'):
# Set up a DB connection
db_conn = pymysql.connect(host=config['Database']['host'], user=config['Database']['user'],
password=config['Database']['password'], database=config['Database']['databasename'])
use_db = True
def print_deck(deck):
print("Deck:")
card: pydealer.Card
for card in deck.cards:
print(card.abbrev)
def print_hand(hand):
print(f"Hand: ({len(hand)})")
hand: pydealer.Stack
print(hand)
print("------------------------")
class Run:
def __init__(self):
self.count: int = 1
self.game_type: str = 'both'
self.same_deck: bool = False
self.id: str = str(uuid.uuid4())
self.games = []
def _print_progress_bar(self, iteration, prefix: str = 'Playing'):
total = self.count
prefix += ':'
suffix = 'Complete'
decimals = 1
length = 50
fill = '█'
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filled_length = int(length * iteration // total)
bar = fill * filled_length + '-' * (length - filled_length)
print(f'\r{prefix} |{bar}| {percent}% {suffix}', end='\r', flush=True)
# Print a newline at the end
if iteration == total:
print()
@property
def stats(self):
stats = {
'run': {
'rules': self.game_type.lower(),
'games': len(self.games),
'wins': 0,
'losses': 0
},
'normal': {
'games': 0,
'wins': 0,
'losses': 0,
'win_pct': 0,
'max_cards_left': 0,
'min_cards_left': 0,
'avg_cards_left': 0
},
'reverse': {
'games': 0,
'wins': 0,
'losses': 0,
'win_pct': 0,
'max_cards_left': 0,
'min_cards_left': 0,
'avg_cards_left': 0
}
}
# Set up a list of remaining cards to do some math later
cards_left = {'normal': [], 'reverse': []}
for game in self.games:
if game.win is True:
stats['run']['wins'] += 1
stats[game.type]['wins'] += 1
else:
stats['run']['losses'] += 1
stats[game.type]['losses'] += 1
stats[game.type]['games'] += 1
cards_left[game.type].append(game.cards_left)
for game_type in ['normal', 'reverse']:
stats[game_type]['max_cards_left'] = max(cards_left[game_type])
stats[game_type]['min_cards_left'] = min(cards_left[game_type])
if stats[game_type]['losses'] != 0:
stats[game_type]['avg_cards_left'] = round(sum(cards_left[game_type]) / int(stats[game_type]['games']))
stats[game_type]['win_pct'] = "{:.2%}".format(int(stats[game_type]['wins']) /
int(stats[game_type]['games']))
return stats
def print_stats(self):
# Return the summary of games played
stats = self.stats
print("=== Run Summary ===")
print(f"Run ID: {self.id}")
print(f"Rules: {stats['run']['rules'].title()}")
if stats['run']['rules'] == 'both':
print(f"Same Deck: {str(self.same_deck)}")
print(f"Games played: {stats['run']['games']}")
print(f"Wins: {stats['run']['wins']}")
print(f"Losses: {stats['run']['losses']}")
for game_type in ['normal', 'reverse']:
if game_type == stats['run']['rules'] or stats['run']['rules'] == 'both':
print(f"-- {game_type.title()} Games ---")
print(f"Games: {stats[game_type]['games']}")
print(f"Wins: {stats[game_type]['wins']}")
print(f"Losses: {stats[game_type]['losses']}")
print(f"Win percentage: {stats[game_type]['win_pct']}")
print(f"Max cards left: {stats[game_type]['max_cards_left']}")
print(f"Min cards left: {stats[game_type]['min_cards_left']}")
print(f"Average cards left: {stats[game_type]['avg_cards_left']}")
def start(self):
for x in range(self.count):
if int(self.count) >= 20:
self._print_progress_bar(x + 1)
loop_games = []
for gt in ['normal', 'reverse']:
if self.game_type.lower() == gt or self.game_type.lower() == 'both':
this_game = Game(type=gt, run_id=self.id)
loop_games.append(this_game)
self.games.append(this_game)
# If we are playing with the same deck for each game, build and shuffle a deck and associate it
if self.same_deck is True and self.game_type.lower() == 'both':
deck1 = pydealer.Deck()
deck1.shuffle()
deck2 = copy.deepcopy(deck1)
loop_games[0].deck = deck1
loop_games[1].deck = deck2
for g in loop_games:
g.play()
if use_db is True:
db_conn.commit()
db_conn.close()
return True
class Game:
def __init__(self, type: str, run_id: str, deck: Optional[pydealer.Deck] = None):
self.type: str = type.lower()
self.run_id: str = run_id
self.deck: Optional[pydealer.Deck] = deck
self.played: bool = False
self.win: Optional[bool] = None
self.cards_left: int = 52
self.first_match_card: int = 0
self.first_match_type: str = ''
self.two_matches: int = 0
self.four_matches: int = 0
self.cards_discarded: int = 0
self.fingerprint: str = ''
def play(self):
self.played = True
if self.deck is None:
self.deck = pydealer.Deck()
self.deck.shuffle()
# Set up a stack to deal into
hand = pydealer.Stack()
# Variable to tell us that it needs to draw a card
# Needed because a suit match later doesn't need to draw a card before checking
draw_needed = True
while len(self.deck) > 0:
if draw_needed:
draw_card = self.deck.deal(1)
hand.add(draw_card)
self.fingerprint += '.'
if debug: print("\tDraw - %s" % draw_card)
if debug: print_hand(hand)
else:
draw_needed = True
# Make sure we have at least 4 cards in the hand
if len(hand) < 4:
continue
# Find the last card and the "check" card three down from it
last_card = len(hand) - 1 # The index of the last card
current_card = hand[last_card] # The actual last card
check_card = hand[last_card - 3] # The "check" card
if debug: print("\tChecking - %s against %s" % (current_card, check_card))
# Check for a 4-card-draw match (rank in normal, suit in reverse
if self.type.lower() == 'normal':
if current_card.value == check_card.value:
match4 = True
match_type = 'R'
else:
match4 = False
if self.type.lower() == 'reverse':
if current_card.suit == check_card.suit:
match4 = True
match_type = 'S'
else:
match4 = False
# See if the value matches and remove all four cards from the hand
if match4:
if debug: print("\tFour Card match - %s matches %s" % (current_card, check_card))
if self.first_match_card == 0:
self.first_match_card = len(hand)
self.first_match_type = match_type
self.four_matches += 1
self.fingerprint += match_type
if debug: print("\tDiscard - %s" % hand[last_card])
del hand[last_card]
if debug: print("\tDiscard - %s" % hand[last_card - 1])
del hand[last_card - 1]
if debug: print("\tDiscard - %s" % hand[last_card - 2])
del hand[last_card - 2]
if debug: print("\tDiscard - %s" % hand[last_card - 3])
del hand[last_card - 3]
self.cards_discarded += 4
draw_needed = True
match4 = False
match_type = ''
if debug: print_hand(hand)
if self.type.lower() == 'normal':
if current_card.suit == check_card.suit:
match2 = True
match_type = 'S'
else:
match2 = False
if self.type.lower() == 'reverse':
if current_card.value == check_card.value:
match2 = True
match_type = 'R'
else:
match2 = False
# See if the suit matched and remove the two cards between the last card and the check card
if match2:
if debug: print("\tTwo Card match - %s matches %s" % (current_card, check_card))
if self.first_match_card == 0:
self.first_match_card = len(hand)
self.first_match_type = match_type
self.two_matches += 1
self.fingerprint += match_type
if debug: print("\tDiscard - %s" % hand[last_card - 1])
del hand[last_card - 1]
if debug: print("\tDiscard - %s" % hand[last_card - 2])
del hand[last_card - 2]
self.cards_discarded += 2
draw_needed = False
if debug: print_hand(hand)
self.cards_left = len(hand)
if len(hand) == 0: # Winner winner
self.win = True
else:
self.win = False
# Garbage cleanup: Remove the pydealer.deck from the Game to free up a little memory
del self.deck
if use_db is True:
self._write_db()
return True
def _write_db(self):
with db_conn.cursor() as cursor:
sql = "INSERT INTO games (game_type, win, cards_left, four_matches, two_matches, first_match_type, " \
"first_match_card, fingerprint, run_id) "
sql += "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"
cursor.execute(sql, (self.type.title(), int(self.win), self.cards_left, self.four_matches, self.two_matches,
self.first_match_type, self.first_match_card, self.fingerprint, self.run_id))
if __name__ == '__main__':
run = Run()
if args.games: # Check for number of games on the command line
run_count = args.games
else:
if 'games' in config['General']: # Check for number of games in config file
run_count = config['General']['games']
else: # Prompt the user for the number of games
run_count = input("How many games should I play?\n")
run.count = int(run_count)
if args.normal or (not args.normal and not args.reverse):
normal_game = True
if args.reverse or (not args.normal and not args.reverse):
reverse_game = True
if normal_game is True and reverse_game is True:
run.game_type = 'Both'
elif normal_game is True and reverse_game is False:
run.game_type = 'Normal'
elif normal_game is False and reverse_game is True:
run.game_type = 'Reverse'
if config['Game Rules'].getboolean('SameDeck') is True:
run.same_deck = True
run.start()
run.print_stats()