-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_logic.py
42 lines (34 loc) · 1.2 KB
/
game_logic.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
import hashlib
import random
import time
class BlackJackGame:
def __init__(self):
self.deck = self.create_deck()
self.shuffle_deck()
def create_deck(self):
suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades']
ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King', 'Ace']
deck = [{'suit': suit, 'rank': rank} for suit in suits for rank in ranks]
return deck
def shuffle_deck(self):
hash_object = hashlib.sha256()
hash_object.update(str(time.time()).encode())
seed = int(hash_object.hexdigest(), 16)
random.seed(seed)
random.shuffle(self.deck)
def deal_card(self):
return self.deck.pop()
def get_card_value(self, card):
if card['rank'] in ['Jack', 'Queen', 'King']:
return 10
elif card['rank'] == 'Ace':
return 11
else:
return int(card['rank'])
def calculate_hand_value(self, hand):
value = sum([self.get_card_value(card) for card in hand])
aces = sum([1 for card in hand if card['rank'] == 'Ace'])
while value > 21 and aces:
value -= 10
aces -= 1
return value