-
Notifications
You must be signed in to change notification settings - Fork 62
/
B5vM.py
165 lines (136 loc) · 3.58 KB
/
B5vM.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
import requests
import cv2 as cv
import os
import matplotlib.pyplot as plt
import numpy as np
import random
card_images = []
cards = []
players = []
marks = ['ハート', 'スペード', 'ダイヤ', 'クローバー']
display_names = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
numbers = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
def load_image():
image_name = 'cards.jpg'
vsplit_number = 4
hsplit_number = 13
if not os.path.isfile(image_name):
response = requests.get('https://raw.githubusercontent.com/techgymjp/techgym_python/master/cards.jpg', allow_redirects=False)
with open(image_name, 'wb') as image:
image.write(response.content)
img = cv.imread('./'+image_name)
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
h, w = img.shape[:2]
crop_img = img[:h // vsplit_number * vsplit_number, :w // hsplit_number * hsplit_number]
card_images.clear()
for h_image in np.vsplit(crop_img, vsplit_number):
for v_image in np.hsplit(h_image, hsplit_number):
card_images.append(v_image)
class Card:
def __init__(self, mark, display_name, number, image):
self.mark = mark
self.display_name = display_name
self.number = number
self.image = image
self.is_dealt = False
class Player:
def __init__(self, name):
self.name = name
self.cards = []
self.total_number = 0
class Human(Player):
def __init__(self):
super().__init__('自分')
class Computer(Player):
def __init__(self):
super().__init__('コンピューター')
def create_cards():
cards.clear()
for i, mark in enumerate(marks):
for j, number in enumerate(numbers):
cards.append( Card(mark, display_names[j], number, card_images[i*len(numbers)+j]) )
def show_cards(cards):
for i, card in enumerate(cards):
print(f"{card.mark}{card.display_name}")
plt.subplot(1, 6, i + 1)
plt.axis('off')
plt.imshow(card.image)
plt.show()
def deal_card(player):
tmp_cards = list(filter(lambda n: n.is_dealt == False, cards))
assert (len(tmp_cards) != 0), "残りカードなし"
tmp_card = random.choice( tmp_cards )
tmp_card.is_dealt = True
player.cards.append( tmp_card )
player.total_number += tmp_card.number
def win():
print('勝ち')
def lose():
print('負け')
def choice():
message = 'ヒット[1] or スタンド[2]'
choice_key = input(message)
while not enable_choice(choice_key):
choice_key = input(message)
return int(choice_key)
def enable_choice(string):
if string.isdigit():
number = int(string)
if number >= 1 and number <= 2:
return True
else:
return False
else:
return False
def play_once():
deal_card( players[0] )
deal_card( players[1] )
deal_card( players[0] )
show_cards( players[0].cards )
if is_blackjack():
win()
else:
choice_key = choice()
if choice_key == 1:
hit()
elif choice_key == 2:
stand()
def is_blackjack():
if(players[0].total_number == 21):
return True
else:
return False
def is_burst():
if(players[0].total_number >= 22):
return True
else:
return False
def hit():
deal_card( players[0] )
show_cards( players[0].cards )
if is_blackjack():
win()
elif is_burst():
lose()
else:
choice_key = choice()
if choice_key == 1:
hit()
elif choice_key == 2:
stand()
def stand():
deal_card( players[1] )
if players[1].total_number < 17:
stand()
else:
judge()
def judge():
print('結果')
def play():
print('デバッグログ:play()')
load_image()
create_cards()
players.append( Human() )
players.append( Computer() )
play_once()
play()