forked from thuxugang/doudizhu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
myclass.py
464 lines (421 loc) · 15.8 KB
/
myclass.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 13 21:55:58 2017
@author: XuGang
"""
#import numpy as np
from myutil import card_show, choose
############################################
# 扑克牌相关类 #
############################################
class Cards(object):
"""
一副扑克牌类,54张排,abcd四种花色,小王14-a,大王15-a
"""
def __init__(self):
#初始化扑克牌类型
self.cards_type = ['1-a-12', '1-b-12','1-c-12','1-d-12',
'2-a-13', '2-b-13','2-c-13','2-d-13',
'3-a-1', '3-b-1','3-c-1','3-d-1',
'4-a-2', '4-b-2','4-c-2','4-d-2',
'5-a-3', '5-b-3','5-c-3','5-d-3',
'6-a-4', '6-b-4','6-c-4','6-d-4',
'7-a-5', '7-b-5','7-c-5','7-d-5',
'8-a-6', '8-b-6','8-c-6','8-d-6',
'9-a-7', '9-b-7','9-c-7','9-d-7',
'10-a-8', '10-b-8','10-c-8','10-d-8',
'11-a-9', '11-b-9','11-c-9','11-d-9',
'12-a-10', '12-b-10','12-c-10','12-d-10',
'13-a-11', '13-b-11','13-c-11','13-d-11',
'14-a-14', '15-a-15']
#初始化扑克牌类
self.cards = self.get_cards()
#初始化扑克牌类
def get_cards(self):
cards = []
for card_type in self.cards_type:
cards.append(Card(card_type))
#打乱顺序
#np.random.shuffle(cards)
return cards
class Card(object):
"""
扑克牌类
"""
def __init__(self, card_type):
self.card_type = card_type
#名称
self.name = self.card_type.split('-')[0]
#花色
self.color = self.card_type.split('-')[1]
#大小
self.rank = int(self.card_type.split('-')[2])
#判断大小
def bigger_than(self, card_instance):
if (self.rank > card_instance.rank):
return True
else:
return False
class PlayRecords(object):
"""
扑克牌记录类
"""
def __init__(self):
#当前手牌
self.cards_left1 = []
self.cards_left2 = []
self.cards_left3 = []
#可能出牌选择
self.next_moves1 = []
self.next_moves2 = []
self.next_moves3 = []
#出牌记录
self.next_move1 = []
self.next_move2 = []
self.next_move3 = []
#出牌记录
self.records = []
#胜利者
#winner=0,1,2,3 0表示未结束,1,2,3表示winner
self.winner = 0
#展示
def show(self, info):
print(info)
card_show(self.cards_left1, "player 1", 1)
card_show(self.cards_left2, "player 2", 1)
card_show(self.cards_left3, "player 3", 1)
card_show(self.records, "record", 3)
############################################
# 出牌相关类 #
############################################
class Moves(object):
"""
出牌类,单,对,三,三带一,三带二,顺子,炸弹
"""
def __init__(self):
#出牌信息
self.dan = []
self.dui = []
self.san = []
self.san_dai_yi = []
self.san_dai_er = []
self.bomb = []
self.shunzi = []
#牌数量信息
self.card_num_info = {}
#牌顺序信息,计算顺子
self.card_order_info = []
#王牌信息
self.king = []
#下次出牌
self.next_moves = []
#下次出牌类型
self.next_moves_type = []
#获取全部出牌列表
def get_moves(self, cards_left):
#统计牌数量/顺序/王牌信息
for i in cards_left:
#王牌信息
if i.rank in [14,15]:
self.king.append(i)
#数量
tmp = self.card_num_info.get(i.name, [])
if len(tmp) == 0:
self.card_num_info[i.name] = [i]
else:
self.card_num_info[i.name].append(i)
#顺序
if i.rank in [13,14,15]: #不统计2,小王,大王
continue
elif len(self.card_order_info) == 0:
self.card_order_info.append(i)
elif i.rank != self.card_order_info[-1].rank:
self.card_order_info.append(i)
#王炸
if len(self.king) == 2:
self.bomb.append(self.king)
#出单,出对,出三,炸弹(考虑拆开)
for k, v in self.card_num_info.items():
if len(v) == 1:
self.dan.append(v)
elif len(v) == 2:
self.dui.append(v)
self.dan.append(v[:1])
elif len(v) == 3:
self.san.append(v)
self.dui.append(v[:2])
self.dan.append(v[:1])
elif len(v) == 4:
self.bomb.append(v)
self.san.append(v[:3])
self.dui.append(v[:2])
self.dan.append(v[:1])
#三带一,三带二
for san in self.san:
for dan in self.dan:
#防止重复
if dan[0].name != san[0].name:
self.san_dai_yi.append(san+dan)
for dui in self.dui:
#防止重复
if dui[0].name != san[0].name:
self.san_dai_er.append(san+dui)
#获取最长顺子
max_len = []
for i in self.card_order_info:
if i == self.card_order_info[0]:
max_len.append(i)
elif max_len[-1].rank == i.rank - 1:
max_len.append(i)
else:
if len(max_len) >= 5:
self.shunzi.append(max_len)
max_len = [i]
#最后一轮
if len(max_len) >= 5:
self.shunzi.append(max_len)
#拆顺子
shunzi_sub = []
for i in self.shunzi:
len_total = len(i)
n = len_total - 5
#遍历所有可能顺子长度
while(n > 0):
len_sub = len_total - n
j = 0
while(len_sub+j <= len(i)):
#遍历该长度所有组合
shunzi_sub.append(i[j:len_sub+j])
j = j + 1
n = n - 1
self.shunzi.extend(shunzi_sub)
#获取下次出牌列表
def get_next_moves(self, last_move_type, last_move):
#没有last,全加上,除了bomb最后加
if last_move_type == "start":
moves_types = ["dan", "dui", "san", "san_dai_yi", "san_dai_er", "shunzi"]
i = 0
for move_type in [self.dan, self.dui, self.san, self.san_dai_yi,
self.san_dai_er, self.shunzi]:
for move in move_type:
self.next_moves.append(move)
self.next_moves_type.append(moves_types[i])
i = i + 1
#出单
elif last_move_type == "dan":
for move in self.dan:
#比last大
if move[0].bigger_than(last_move[0]):
self.next_moves.append(move)
self.next_moves_type.append("dan")
#出对
elif last_move_type == "dui":
for move in self.dui:
#比last大
if move[0].bigger_than(last_move[0]):
self.next_moves.append(move)
self.next_moves_type.append("dui")
#出三个
elif last_move_type == "san":
for move in self.san:
#比last大
if move[0].bigger_than(last_move[0]):
self.next_moves.append(move)
self.next_moves_type.append("san")
#出三带一
elif last_move_type == "san_dai_yi":
for move in self.san_dai_yi:
#比last大
if move[0].bigger_than(last_move[0]):
self.next_moves.append(move)
self.next_moves_type.append("san_dai_yi")
#出三带二
elif last_move_type == "san_dai_er":
for move in self.san_dai_er:
#比last大
if move[0].bigger_than(last_move[0]):
self.next_moves.append(move)
self.next_moves_type.append("san_dai_er")
#出炸弹
elif last_move_type == "bomb":
for move in self.bomb:
#比last大
if move[0].bigger_than(last_move[0]):
self.next_moves.append(move)
self.next_moves_type.append("bomb")
#出顺子
elif last_move_type == "shunzi":
for move in self.shunzi:
#相同长度
if len(move) == len(last_move):
#比last大
if move[0].bigger_than(last_move[0]):
self.next_moves.append(move)
self.next_moves_type.append("shunzi")
else:
print("last_move_type_wrong")
#除了bomb,都可以出炸
if last_move_type != "bomb":
for move in self.bomb:
self.next_moves.append(move)
self.next_moves_type.append("bomb")
return self.next_moves_type, self.next_moves
#展示
def show(self, info):
print(info)
#card_show(self.dan, "dan", 2)
#card_show(self.dui, "dui", 2)
#card_show(self.san, "san", 2)
#card_show(self.san_dai_yi, "san_dai_yi", 2)
#card_show(self.san_dai_er, "san_dai_er", 2)
#card_show(self.bomb, "bomb", 2)
#card_show(self.shunzi, "shunzi", 2)
#card_show(self.next_moves, "next_moves", 2)
############################################
# 玩家相关类 #
############################################
class Player(object):
"""
player类
"""
def __init__(self, player_id):
self.player_id = player_id
self.cards_left = []
#展示
def show(self, info):
self.total_moves.show(info)
card_show(self.next_move, "next_move", 1)
#card_show(self.cards_left, "card_left", 1)
#根据next_move同步cards_left
def record_move(self, playrecords):
#playrecords中records记录[id,next_move]
if self.next_move_type in ["yaobuqi", "buyao"]:
self.next_move = self.next_move_type
playrecords.records.append([self.player_id, self.next_move_type])
else:
playrecords.records.append([self.player_id, self.next_move])
for i in self.next_move:
self.cards_left.remove(i)
#同步playrecords
if self.player_id == 1:
playrecords.cards_left1 = self.cards_left
playrecords.next_moves1.append(self.next_moves)
playrecords.next_move1.append(self.next_move)
elif self.player_id == 2:
playrecords.cards_left2 = self.cards_left
playrecords.next_moves2.append(self.next_moves)
playrecords.next_move2.append(self.next_move)
elif self.player_id == 3:
playrecords.cards_left3 = self.cards_left
playrecords.next_moves3.append(self.next_moves)
playrecords.next_move3.append(self.next_move)
#是否牌局结束
end = False
if len(self.cards_left) == 0:
end = True
return end
#出牌
def go(self, last_move_type, last_move, playrecords, model):
#所有出牌可选列表
self.total_moves = Moves()
#获取全部出牌列表
self.total_moves.get_moves(self.cards_left)
#获取下次出牌列表
self.next_move_types, self.next_moves = self.total_moves.get_next_moves(last_move_type, last_move)
#在next_moves中选择出牌方法
self.next_move_type, self.next_move = choose(self.next_move_types, self.next_moves, last_move_type, model)
#记录
end = self.record_move(playrecords)
#展示
#self.show("Player " + str(self.player_id))
#要不起&不要
yaobuqi = False
if self.next_move_type in ["yaobuqi","buyao"]:
yaobuqi = True
self.next_move_type = last_move_type
self.next_move = last_move
return self.next_move_type, self.next_move, end, yaobuqi
############################################
# 网页展示类 #
############################################
class WebShow(object):
"""
网页展示类
"""
def __init__(self, playrecords):
#胜利者
self.winner = playrecords.winner
#剩余手牌
self.cards_left1 = []
for i in playrecords.cards_left1:
self.cards_left1.append(i.name+i.color)
self.cards_left2 = []
for i in playrecords.cards_left2:
self.cards_left2.append(i.name+i.color)
self.cards_left3 = []
for i in playrecords.cards_left3:
self.cards_left3.append(i.name+i.color)
#可能出牌
self.next_moves1 = []
if len(playrecords.next_moves1) != 0:
next_moves = playrecords.next_moves1[-1]
for move in next_moves:
cards = []
for card in move:
cards.append(card.name+card.color)
self.next_moves1.append(cards)
self.next_moves2 = []
if len(playrecords.next_moves2) != 0:
next_moves = playrecords.next_moves2[-1]
for move in next_moves:
cards = []
for card in move:
cards.append(card.name+card.color)
self.next_moves2.append(cards)
self.next_moves3 = []
if len(playrecords.next_moves3) != 0:
next_moves = playrecords.next_moves3[-1]
for move in next_moves:
cards = []
for card in move:
cards.append(card.name+card.color)
self.next_moves3.append(cards)
#出牌
self.next_move1 = []
if len(playrecords.next_move1) != 0:
next_move = playrecords.next_move1[-1]
if next_move in ["yaobuqi", "buyao"]:
self.next_move1.append(next_move)
else:
for card in next_move:
self.next_move1.append(card.name+card.color)
self.next_move2 = []
if len(playrecords.next_move2) != 0:
next_move = playrecords.next_move2[-1]
if next_move in ["yaobuqi", "buyao"]:
self.next_move2.append(next_move)
else:
for card in next_move:
self.next_move2.append(card.name+card.color)
self.next_move3 = []
if len(playrecords.next_move3) != 0:
next_move = playrecords.next_move3[-1]
if next_move in ["yaobuqi", "buyao"]:
self.next_move3.append(next_move)
else:
for card in next_move:
self.next_move3.append(card.name+card.color)
#记录
self.records = []
for i in playrecords.records:
tmp = []
tmp.append(i[0])
tmp_name = []
#处理要不起
try:
for j in i[1]:
tmp_name.append(j.name+j.color)
tmp.append(tmp_name)
except:
tmp.append(i[1])
self.records.append(tmp)