-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShoe.py
More file actions
119 lines (89 loc) · 3.76 KB
/
Shoe.py
File metadata and controls
119 lines (89 loc) · 3.76 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
"""
========================================================================================================================
Package
========================================================================================================================
"""
import os, sys
sys.path.append(os.path.abspath(os.path.join(__file__, '..')))
import random
from typing import Dict
from Utils import *
"""
========================================================================================================================
Shoe Manager
========================================================================================================================
"""
class Shoe():
"""
====================================================================================================================
Initialization
====================================================================================================================
"""
def __init__(self, num_decks: int = 6, rng: random.Random = None) -> None:
self.num_decks = num_decks
self.rng = rng or random.Random()
# Counts of Every Rank
self.counts: Dict[Rank, int] = {r: 4 * num_decks for r in RANKS}
return
"""
====================================================================================================================
Number of Remaining Cards
====================================================================================================================
"""
def remaining(self) -> int:
return sum(self.counts.values())
"""
====================================================================================================================
====================================================================================================================
"""
def remove_card(self, rank: Rank) -> None:
# Check Validity
if self.counts.get(rank, 0) <= 0:
raise ValueError("No card {} left to remove".format(rank))
# Remove Specific Rank
self.counts[rank] -= 1
return
"""
====================================================================================================================
====================================================================================================================
"""
def draw_one(self) -> Rank:
total = self.remaining()
if total == 0:
raise ValueError("Shoe is empty")
#
r = self.rng.randint(1, total)
cum = 0
for rank in RANKS:
cum += self.counts.get(rank, 0)
if r <= cum:
self.remove_card(rank)
return rank
#
raise RuntimeError("draw_one failed")
"""
====================================================================================================================
====================================================================================================================
"""
def clone(self) -> 'Shoe':
shoe_temp = Shoe(self.num_decks, rng = random.Random(self.rng.randint(0, 2**31-1)))
shoe_temp.counts = dict(self.counts)
return shoe_temp
"""
========================================================================================================================
Main Function
========================================================================================================================
"""
if __name__ == "__main__":
shoe = Shoe(num_decks = 8)
temp = shoe.clone()
print()
print(temp.draw_one())
print(temp.draw_one())
print(temp.draw_one())
print(temp.draw_one())
print()
print()
print(shoe.counts)
print(temp.counts)
print()