-
Notifications
You must be signed in to change notification settings - Fork 62
/
d4YC.py
118 lines (96 loc) · 2.88 KB
/
d4YC.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
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 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():
print('デバッグログ:play()')
load_image()
create_cards()
players.append( Human() )
players.append( Computer() )
deal_card( players[0] )
deal_card( players[1] )
deal_card( players[0] )
show_cards( players[0].cards )
choice()
if(players[0].total_number == 21):
win()
play()