Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import Counter
- class parsepoker():
- def __init__(self, input, player = 1):
- input = input.replace("T", "10")
- input = input.replace("J", "11")
- input = input.replace("Q", "12")
- input = input.replace("K", "13")
- input = input.replace("A", "14")
- if player == 1:
- self.hand = input.split()[:5]
- else:
- self.hand = input.split()[5:]
- self.suits = [x[1] for x in self.hand]
- self.ranks = [int(x[0:-1]) for x in self.hand]
- self.suitscount = dict(Counter(self.suits))
- self.rankscount = dict(Counter(self.ranks))
- self.tiebreaker = [(y, x) for (x, y) in Counter(self.ranks).items()]
- def fourofakind(self):
- return 4 in self.rankscount.values()
- def straight(self):
- if (max(self.ranks) - min(self.ranks)) == 4 and max(self.rankscount.values()) == 1:
- return True
- elif sorted(self.ranks) == [2, 3, 4, 5, 14]:
- return True
- else:
- return False
- def threeofakind(self):
- return 3 in self.rankscount.values()
- def flush(self):
- return 5 in self.suitscount.values()
- def fullhouse(self):
- return 2 in self.rankscount.values() and 3 in self.rankscount.values()
- def highcard(self):
- return max(self.ranks)
- def twopair(self):
- return "22" in "".join(sorted(map(str, self.rankscount.values())))
- def onepair(self):
- return 2 in self.rankscount.values()
- def straightflush(self):
- return parsepoker.straight(self) and parsepoker.flush(self)
- def royalflush(self):
- return parsepoker.flush(self) and min(self.ranks) == 10
- def testhand(self):
- if parsepoker.royalflush(self): self.score = 10
- elif parsepoker.straightflush(self): self.score = 9
- elif parsepoker.fourofakind(self): self.score = 8
- elif parsepoker.fullhouse(self): self.score = 7
- elif parsepoker.flush(self): self.score = 6
- elif parsepoker.straight(self): self.score = 5
- elif parsepoker.threeofakind(self): self.score = 4
- elif parsepoker.twopair(self): self.score = 3
- elif parsepoker.onepair(self): self.score = 2
- else: self.score = 1
- pokerfile = open("poker1.txt", "r")
- temp = pokerfile.read().splitlines()
- player1wins = 0
- for pokerhand in temp:
- player1 = parsepoker(pokerhand, 1)
- player2 = parsepoker(pokerhand, 2)
- player1.testhand()
- player2.testhand()
- if player1.score > player2.score: player1wins += 1
- elif player1.score == player2.score and sorted(player1.tiebreaker, reverse=True) > sorted(player2.tiebreaker, reverse=True):
- player1wins += 1
- print player1wins
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement