-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
157 lines (129 loc) · 5.12 KB
/
utils.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
import time
import itertools
import numpy as np
def wait(t):
time.sleep(t)
def pick_a_card(cards, remove = True, manual = False):
if manual:
return input('name a card to pick:')
else:
suit_pick = list(cards.keys())[np.random.randint(0, 100) % len(cards)]
card_pick = cards[suit_pick][np.random.randint(0, 100) % len(cards[suit_pick])]
if remove:
del cards[suit_pick][cards[suit_pick].index(card_pick)]
if len(cards[suit_pick]) == 0:
del cards[suit_pick]
return suit_pick + '_' + card_pick
def pick_a_card_hard(cards, number_of_points, remove=True, manual=False):
if manual:
return input('name a card to pick:')
else:
suit_pick = list(cards.keys())[np.random.randint(0, 100) % len(cards)]
card_pick = cards[suit_pick][np.random.randint(0, 100) % len(cards[suit_pick])]
keys = list(cards.keys())
values = list(cards.values())
for i in range(len(keys)):
for l in range(len(values[i])):
if values[i][l] == str(number_of_points):
suit_pick = keys[i]
card_pick = cards[suit_pick][l]
break
else:
continue
break
if remove:
del cards[suit_pick][cards[suit_pick].index(card_pick)]
if len(cards[suit_pick]) == 0:
del cards[suit_pick]
return suit_pick + '_' + card_pick
# blackjack
def calc_points(cards, base_val = 21):
card_values = {'A' : 11, 'J' : 10, 'Q' : 10, 'K' : 10, '2' : 2, '3' : 3, '4' : 4, '5' : 5, '6' : 6, '7' : 7, '8' : 8, '9' : 9, '10' : 10}
points = 0
for i in range(len(cards)):
points += card_values[cards[i].split('_')[-1]]
return points
def card_inc(card_box, n, cnt, shuffle_point, ndeck):
if cnt + n >= shuffle_point:
card_box = {
'heart' : (['A', 'J', 'Q', 'K'] + ['%d'%i for i in range(2, 11)]) * ndeck,
'diamond' : (['A', 'J', 'Q', 'K'] + ['%d'%i for i in range(2, 11)]) * ndeck,
'club' : (['A', 'J', 'Q', 'K'] + ['%d'%i for i in range(2, 11)]) * ndeck,
'spade' : (['A', 'J', 'Q', 'K'] + ['%d'%i for i in range(2, 11)]) * ndeck
}
shuffle_point = np.random.randint((ndeck * 52) // 4, (ndeck * 52 * 3) // 4)
cnt = n
else:
cnt += n
return card_box, cnt, shuffle_point
class Player:
pl_cnt = 0
def __init__(self, name=''):
self.money = 500
Player.pl_cnt += 1
self.name = name if name.replace(' ', '') != '' else 'Player-%d'%Player.pl_cnt
def new_session(self, card_box):
self.bj_state = False
self.cards = [pick_a_card(card_box, True), pick_a_card(card_box, True)]
self.points = calc_points(self.cards)
def set_bet(self, lowest_bet, type_of_player):
if type_of_player =="p":
self.bet = int(input('fund : %d, enter the initial bet : '%self.money))
while True:
if self.bet < lowest_bet:
self.bet = int(input('invalid !! lowest required bed is : %d, enter the bet again : '%lowest_bet))
continue
if self.bet > self.money:
self.bet = int(input('invalid !! fund : %d, enter the bet again : '%self.money))
else:
self.money -= self.bet
break
else:
self.bet = self.money
self.money -= self.bet
def set_bet_player(self, number):
self.bet = number
self.money = 500 - number
print("bet: " + str(self.bet))
print("money: " + str(self.money))
def show_state(self):
print('your cards : ', self.cards, '\t\t total points : ', self.points)
def get_stats(self):
return self.cards
def get_points(self):
return calc_points(self.cards)
def get_bet(self):
return self.bet
def check_blackjack(self, base_point):
if self.points == base_point:
self.bj_state = True
print('!!!!!!!! BLACKJACK !!!!!!!!!!')
return self.bj_state
def check_A(self):
numbers = 0
for i in range(len(self.cards)):
if self.cards[i].split('_')[-1] == 'A':
numbers+=1
return numbers
def hit(self, card_box):
self.cards += [pick_a_card(card_box, True)]
self.points = calc_points(self.cards)
self.show_state()
def last_card(self):
return self.cards[len(self.cards)-1]
def last_card1(self):
return self.cards[len(self.cards)-2]
def hit_hard(self, card_box, number_of_points):
self.cards += [pick_a_card_hard(card_box, number_of_points, True)]
self.points = calc_points(self.cards)
self.show_state()
def update_money(self, win_flag):
if win_flag == 'w':
if self.bj_state:
self.money += (1.5 + 1) * self.bet
else:
self.money += self.bet * 2
elif win_flag == 'd':
self.money += self.bet
if self.money <= 0:
self.money = 0