回答編集履歴
2
修正
test
CHANGED
@@ -246,7 +246,7 @@
|
|
246
246
|
|
247
247
|
yr_hand = janken.get_random_hand()
|
248
248
|
|
249
|
-
result = my_hand.do_game(yr
|
249
|
+
result = my_hand.do_game(yr_hand)
|
250
250
|
|
251
251
|
|
252
252
|
|
@@ -288,6 +288,6 @@
|
|
288
288
|
|
289
289
|
|
290
290
|
|
291
|
-
|
291
|
+
GUIに関しましては、~~面倒なので~~テストしていません。悪しからず。
|
292
292
|
|
293
293
|
また、何度もninshoを呼び出すようならば、画像の入出力は関数外に括りだした方が良いでしょう。
|
1
追記
test
CHANGED
@@ -41,3 +41,253 @@
|
|
41
41
|
また、namedWindowを複数回呼ぶのは無駄です。
|
42
42
|
|
43
43
|
**何を実現しようとしているのか、自分自身で理解したうえでコードを書いてください。**
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
書いてみた
|
48
|
+
|
49
|
+
---
|
50
|
+
|
51
|
+
じゃんけんだけですけど。
|
52
|
+
|
53
|
+
```Python
|
54
|
+
|
55
|
+
import enum
|
56
|
+
|
57
|
+
import itertools
|
58
|
+
|
59
|
+
import random
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
Result = enum.Enum('Result', ['WIN', 'DRAW', 'LOOSE'])
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
class __Hand:
|
68
|
+
|
69
|
+
def do_game(self, other):
|
70
|
+
|
71
|
+
raise NotImplementedError
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
def wins(self, other):
|
76
|
+
|
77
|
+
if self.do_game(other) is Result.WIN:
|
78
|
+
|
79
|
+
return True
|
80
|
+
|
81
|
+
return False
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
def looses(self, other):
|
86
|
+
|
87
|
+
return other.wins(self)
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
def draws(self, other):
|
92
|
+
|
93
|
+
return not (self.wins(other) or self.looses(other))
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
class __Guh(__Hand):
|
98
|
+
|
99
|
+
def do_game(self, other):
|
100
|
+
|
101
|
+
if other is choki:
|
102
|
+
|
103
|
+
return Result.WIN
|
104
|
+
|
105
|
+
if other is pah:
|
106
|
+
|
107
|
+
return Result.LOOSE
|
108
|
+
|
109
|
+
return Result.DRAW
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
def __str__(self):
|
114
|
+
|
115
|
+
return 'guh'
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
class __Choki(__Hand):
|
120
|
+
|
121
|
+
def do_game(self, other):
|
122
|
+
|
123
|
+
if other is pah:
|
124
|
+
|
125
|
+
return Result.WIN
|
126
|
+
|
127
|
+
if other is guh:
|
128
|
+
|
129
|
+
return Result.LOOSE
|
130
|
+
|
131
|
+
return Result.DRAW
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
def __str__(self):
|
136
|
+
|
137
|
+
return 'choki'
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
class __Pah(__Hand):
|
142
|
+
|
143
|
+
def do_game(self, other):
|
144
|
+
|
145
|
+
if other is guh:
|
146
|
+
|
147
|
+
return Result.WIN
|
148
|
+
|
149
|
+
if other is choki:
|
150
|
+
|
151
|
+
return Result.LOOSE
|
152
|
+
|
153
|
+
return Result.DRAW
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
def __str__(self):
|
158
|
+
|
159
|
+
return 'pah'
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
guh = __Guh()
|
166
|
+
|
167
|
+
choki = __Choki()
|
168
|
+
|
169
|
+
pah = __Pah()
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
hands = (guh, choki, pah)
|
174
|
+
|
175
|
+
def get_random_hand():
|
176
|
+
|
177
|
+
return random.choice(hands)
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
def __simple_test():
|
182
|
+
|
183
|
+
result = {
|
184
|
+
|
185
|
+
Result.WIN: 'wins',
|
186
|
+
|
187
|
+
Result.DRAW: 'draws',
|
188
|
+
|
189
|
+
Result.LOOSE: 'looses'
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
for mine, other in itertools.product(hands, hands):
|
196
|
+
|
197
|
+
print(
|
198
|
+
|
199
|
+
mine, result[mine.do_game(other)], other
|
200
|
+
|
201
|
+
)
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
if __name__ == '__main__':
|
206
|
+
|
207
|
+
__simple_test()
|
208
|
+
|
209
|
+
```
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
```Python
|
214
|
+
|
215
|
+
import janken
|
216
|
+
|
217
|
+
Result = janken.Result
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
def ninsho():
|
222
|
+
|
223
|
+
hand_img = {
|
224
|
+
|
225
|
+
janken.guh: 'guh.png',
|
226
|
+
|
227
|
+
janken.choki: 'choki.png',
|
228
|
+
|
229
|
+
janken.pah: 'pah.png'
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
result_img = {
|
234
|
+
|
235
|
+
Result.WIN: 'win.png',
|
236
|
+
|
237
|
+
Result.LOOSE: 'loose.png',
|
238
|
+
|
239
|
+
Result.DRAW: 'draw.png'
|
240
|
+
|
241
|
+
}
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
my_hand = janken.get_random_hand()
|
246
|
+
|
247
|
+
yr_hand = janken.get_random_hand()
|
248
|
+
|
249
|
+
result = my_hand.do_game(yr.hand)
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
# make window
|
254
|
+
|
255
|
+
cv2.namedWindow('screen', cv2.WINDOW_NORMAL)
|
256
|
+
|
257
|
+
cv2.setWindowProperty('screen', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
# show my hand
|
262
|
+
|
263
|
+
img = cv2.imread(hand_img[my_hand])
|
264
|
+
|
265
|
+
assert img is not None
|
266
|
+
|
267
|
+
cv2.imshow('screen', img)
|
268
|
+
|
269
|
+
cv2.waitKey(2000)
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
# show result
|
274
|
+
|
275
|
+
img = cv2.imread(result_img[result])
|
276
|
+
|
277
|
+
assert img is not None
|
278
|
+
|
279
|
+
cv2.imshow('screen', img)
|
280
|
+
|
281
|
+
cv2.waitKey(2000)
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
return yr_hand
|
286
|
+
|
287
|
+
```
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
関数ninshoに関しましては、~~面倒なので~~テストしていません。悪しからず。
|
292
|
+
|
293
|
+
また、何度もninshoを呼び出すようならば、画像の入出力は関数外に括りだした方が良いでしょう。
|