-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBlackJack.py
166 lines (135 loc) · 5.93 KB
/
BlackJack.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
# MIT License
#
# Copyright (c) 2018 David Sprenkle
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import anki_vector
from anki_vector.util import parse_command_args, degrees
class BlackJack(object):
def __init__(self, robot, blackJackActions, readCard, actions):
self._blackJackActions = blackJackActions
self._cardRead = readCard
self._robot = robot
self._actions = actions
def get_next_card(self, seen_cards):
card, percentage = self._cardRead.extract_card()
while card in seen_cards:
card, percentage = self._cardRead.extract_card()
seen_cards.add(card)
return card
def draw(self, d, v, index, results, seen_cards):
while True:
action = self._blackJackActions.player_action(d[0], v, index)
count, ace = self._blackJackActions.get_count(v[index])
if action == 'bu':
self._actions.busted(count)
results.append(count)
return results
if action == 'st':
self._actions.stand(count)
results.append(count)
return results
if action == 'dl':
self._actions.double(count)
self.add_card(seen_cards, v[index])
count, ace = self._blackJackActions.get_count(v[index])
results.append(count)
self._actions.say_count(count)
return results
if action == 'bl':
self._actions.blackjack(count)
results.append(21.5)
return results
if action == 'sp':
self._actions.split(v[index][0])
second = v[index].pop()
split_hand = [second]
self.add_card(seen_cards, split_hand)
v.append(split_hand)
self.draw(d, v, index + 1, results, seen_cards)
if action == 'ht':
self._actions.hit(count)
self.add_card(seen_cards, v[index])
def add_card(self, seen_cards, cards):
card = self.get_next_card(seen_cards)
cards.append(card)
print(card)
self._actions.robot_say(self._cardRead.card_to_name(card))
def run(self):
self._robot.behavior.set_head_angle(degrees(5.0))
deck_playable = True
while deck_playable:
self._actions.robot_say("Play a round")
seen_cards = set([])
v = [[]]
d = []
# Deal first 3 cards
self.add_card(seen_cards, v[0])
self.add_card(seen_cards, d)
self.add_card(seen_cards, v[0])
results = self.draw(d, v, 0, [], seen_cards)
all_busted = True
for end_results in results: # continue if all hands busted
if end_results <= 21.5: all_busted = False
if all_busted: continue
self._actions.robot_say("Show Dealer Card")
# Deal dealer 4th card
self.add_card(seen_cards, d)
d_count, ace = self._blackJackActions.get_count(d)
self._actions.dealer_count(d_count)
if len(d) == 2 and d_count == 21 and ace:
self._actions.robot_say("Dealer Backjack")
self._actions.lose()
continue
# if player blackjack check, note if a split and a bust dealer will still draw, should fix
if len(results) == 1 and results[0] == 21.5:
self._actions.win()
continue
while (d_count < 17 and not ace) or (ace and d_count < 18):
self._actions.robot_say("Dealer Draw")
card = self.get_next_card(seen_cards)
self._actions.robot_say(self._cardRead.card_to_name(card))
d.append(card)
d_count, ace = self._blackJackActions.get_count(d)
self._actions.dealer_count(d_count)
d_count, ace = self._blackJackActions.get_count(d)
if d_count <= 21:
self._actions.robot_say("Dealer Stand")
else:
self._actions.robot_say("Dealer Busted")
self._actions.win()
continue
for values in results:
if values == d_count:
self._actions.tie()
elif values > d_count:
self._actions.win()
else:
self._actions.lose()
if __name__ == '__main__':
from ReadCard import ReadCard
from BlackJackActions import BlackJackActions
from RobotActions import RobotActions
args = anki_vector.util.parse_command_args()
with anki_vector.Robot(args.serial, enable_camera_feed=True, show_viewer=False) as robot:
readCard = ReadCard(robot)
blackJackActions = BlackJackActions()
actions = RobotActions(robot)
blackJack = BlackJack(robot, blackJackActions, readCard, actions)
blackJack.run()