-
Notifications
You must be signed in to change notification settings - Fork 62
/
Rp48.py
100 lines (80 loc) · 2.49 KB
/
Rp48.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
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://github.com/techgymjp/techgym_python/blob/master/Rp48.py', 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 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 )
if(players[0].total_number == 21):
win()
play()