/
pythonista_poker1.py
140 lines (125 loc) · 4.63 KB
/
pythonista_poker1.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
import random
class TrumpGame:
def make_card_list(self):
# マークのリスト
symbol_list = ['Clubs', 'Hearts', 'Spades', 'Diamonds']
# カードリスト
card_list = []
# カードのデータを作成
for symbol in symbol_list:
for number in range(1, 14):
card = {
'number': number,
'symbol': symbol
}
# マークと数字を合体させる
# 11以上と1は置き換え
if number == 1:
card['string'] = symbol + 'A'
elif number == 11:
card['string'] = symbol + 'J'
elif number == 12:
card['string'] = symbol + 'Q'
elif number == 13:
card['string'] = symbol + 'K'
else:
# 10以下ならそのまま
card['string'] = symbol + str(number)
# カードをリストに追加
card_list.append(card)
self.card_list = card_list
def shuffle(self):
# カードをシャッフルする
random.shuffle(self.card_list)
# 手札を作成する
def reset_draw_cards(self, number):
card_list = self.make_card_list()
self.shuffle()
self.draw_cards = []
for i in range(0, number):
self.draw_cards.append(
self.card_list.pop(0)
)
# 役のチェック処理
def check_poker_hand(self):
# ペア数
pair_count = 0
# 同じ数字のカウント
match_count = 0
# 同じ数字の枚数(3カード,4カードチェック用)
match_number = 0
# フラッシュの有無フラグ
flash_flag = True
# ストレートの有無フラグ
straight_flag = True
# 数字の昇順に並び替える
cards = sorted(self.draw_cards, key=lambda x: x['number'])
# 比較チェックループ
for i in range(1, 5):
# 前の数字が同じかチェック
if cards[i]['number'] == cards[i - 1]['number']:
match_count += 1
# 最終ループチェック
if i == 4:
if match_count == 1:
pair_count += 1
# 3カード以上の場合
elif match_count > 1:
match_number = match_count + 1
else:
# 違う数字の場合
if match_count == 1:
pair_count += 1
# 3カード以上の場合
elif match_count > 1:
match_number = match_count + 1
match_count = 0
# 同じマークが続いているかチェック
if flash_flag == True and cards[i]['symbol'] != cards[i - 1]['symbol']:
flash_flag = False
# 数字が連続しているかチェック
if straight_flag == True and cards[i]['number'] != cards[i - 1]['number'] + 1:
if cards[i]['number'] != 10 or cards[i - 1]['number'] != 1:
straight_flag = False
# 最終手札チェック
if straight_flag == True and flash_flag == True:
if cards[0]['number'] == 1 and cards[4]['number'] == 13:
# ロイヤルストレートフラッシュ
hand = 'ロイヤル\nストレートフラッシュ'
else:
# ストレートフラッシュ
hand = 'ストレートフラッシュ'
elif match_number > 2:
if match_number == 4:
# 4カード
hand = '4カード'
else:
if pair_count > 0:
# フルハウス
hand = 'フルハウス'
else:
# 3カード
hand = '3カード'
elif flash_flag == True:
# フラッシュ
hand = 'フラッシュ'
elif straight_flag == True:
# ストレート
hand = 'ストレート'
elif pair_count > 0:
if pair_count > 1:
# 2ペア
hand = '2ペア'
else:
# 1ペア
hand = '1ペア'
else:
# なし
hand = 'ぶた'
return hand
if __name__ == '__main__':
tg = TrumpGame()
tg.reset_draw_cards(5)
for card in tg.draw_cards:
print(card['string'])
print(tg.check_poker_hand())