import random
class PokerHandsSimulator:
def __init__(self, n):
suits = '◆♣♥♠'
nums = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K', 'A']
self.deck_of_cards = []
self.n = n
for suit in suits:
for num in nums:
self.deck_of_cards.append(suit + str(num))
self.log = {}
def main(self):
for i in range(self.n):
random.shuffle(self.deck_of_cards)
my_cards = self.deck_of_cards[:5]
print(my_cards)
self.check_hand(my_cards)
print('==========통계==========')
print('총 반복 수 : {}회'.format(self.n))
for k, v in self.log.items():
print('{} : {} ({:.2f}%)'.format(k, v, v/self.n*100))
def check_hand(self, cards):
result = ''
if self.is_royal_flush(cards):
result = self.is_royal_flush(cards)
elif self.is_four_of_a_kind(cards):
result = self.is_four_of_a_kind(cards)
elif self.is_full_house(cards):
result = self.is_full_house(cards)
elif self.is_straight_flush(cards):
result = self.is_straight_flush(cards)
elif self.is_flush(cards):
result = self.is_flush(cards)
elif self.is_straight(cards):
result = self.is_straight(cards)
else:
result = self.check_rest(cards)
print(result)
def is_four_of_a_kind(self, cards):
check = [i[1:] for i in cards]
set_check = list(set(check))
for num in set_check:
if check.count(num) == 4:
return 'FOUR OF A KIND'
else:
return False
def is_flush(self, cards):
suits = [x[0] for x in cards]
if len(set(suits)) == 1:
return True
else:
return False
def is_royal_flush(self, cards):
suits = [x[0] for x in cards]
nums = [x[1:] for x in cards]
if (sorted(nums) == sorted(['10', 'J', 'Q', 'K', 'A']) and
self.is_flush(suits)):
return 'ROYAL FLUSH'
else:
return False
def is_full_house(self, cards):
nums = [x[1:] for x in cards]
set_check = list(set(nums))
if len(set_check) > 2:
return False
else:
if (nums.count(set_check[0]) == 3 and
nums.count(set_check[1]) == 2 or
nums.count(set_check[0]) == 2 and
nums.count(set_check[1]) == 3):
return 'FULL HOUSE'
def is_straight(self, cards):
nums = sorted([x[1:] for x in cards])
faces = ['J', 'Q', 'K', 'A']
if any(True for face in faces if face in nums):
return self.is_special_straight(nums)
else:
return self.is_normal_straight(nums)
def is_special_straight(self, cards):
possibilities = [
sorted(['7', '8', '9', '10', 'J']),
sorted(['8', '9', '10', 'J', 'Q']),
sorted(['9', '10', 'J', 'Q', 'K']),
sorted(['10', 'J', 'Q', 'K', 'A']),
sorted(['A', '2', '3', '4', '5'])
]
if cards in possibilities:
return 'STRAIGHT'
else:
return False
def is_normal_straight(self, cards):
nums = sorted([int(x) for x in cards])
index = 1
straight = True
while index < len(nums) and straight:
straight = False
if nums[index] - nums[index - 1] == 1:
straight = True
index += 1
if straight:
return 'STRAIGHT'
else:
return False
def is_straight_flush(self, cards):
if (self.is_straight(cards) and
self.is_flush(cards)):
return 'STRAIGHT FLUSH'
else:
return False
def check_rest(self, cards):
nums = [x[1:] for x in cards]
card_count = {}
for card in nums:
if card not in card_count.keys():
card_count[card] = 1
else:
card_count[card] += 1
if 3 in card_count.values():
return 'THREE OF A KIND'
else:
if len(card_count) == 3:
return 'TWO PAIRS'
elif len(card_count) == 4:
return 'ONE PAIR'
else:
return 'HIGH CARD'
def write_log(self, result):
if result not in self.log.keys():
self.log[result] = 1
else:
self.log[result] += 1
if __name__ == "__main__":
print("==시행 회수를 입력하시오==")
n = int(input('>>>'))
app = PokerHandsSimulator(n)
app.main()
시행 횟수 넣고
반복 시키고 결과까지 다 보여주는디
HIGH CARD
['♣J', '♣7', '♥4', '◆4', '♠9']
ONE PAIR
['♠5', '♣J', '♥4', '◆7', '♥5']
ONE PAIR
==========통계==========
총 반복 수 : 5회
이러기만 하고 통계 내주는 부분이 안나온다.
def main(self):for i in range(self.n):
random.shuffle(self.deck_of_cards)
my_cards = self.deck_of_cards[:5]
print(my_cards)
self.check_hand(my_cards)
print('==========통계==========')
print('총 반복 수 : {}회'.format(self.n))
for k, v in self.log.items():
print('{} : {} ({:.2f}%)'.format(k, v, v/self.n*100))
이부분이 통계내는 부분인디 여기 문젠지 어딘지 모르겠다
https://m.blog.naver.com/dukalee/221267500091
이거보고 따라하고 있음
일단 write_log를 정의만 해놓고 안 쓴거 같은데
check_hand 함수에서 self.write_log(result) 이거 빼먹음