Created
January 21, 2019 04:01
-
-
Save merryt/0eb1d3d4b46e8987fc1450d97de841d3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random | |
| from itertools import permutations, combinations | |
| class Card: | |
| """ | |
| A single card in a 52 card deck | |
| When creating a card the format is value 1(ace) to 13 king followed by 0-4 for a suite | |
| """ | |
| def __init__(self, value, suite): | |
| """both as integers""" | |
| if(value < 1 or value > 13): | |
| raise ValueError("Trying to create a card with an invalid rank", rank) | |
| if(suite < 0 or suite > 3): | |
| raise ValueError("Trying to create a card with an invalid suite", suite) | |
| self.value = value | |
| self.suite = suite | |
| def GetSuite(self): | |
| if(self.suite is 0): | |
| return "Hearts" | |
| elif(self.suite is 1): | |
| return "Diamonds" | |
| elif(self.suite is 2): | |
| return "Spades" | |
| elif(self.suite is 3): | |
| return "Clubs" | |
| def GetValue(self): | |
| if(self.value is 1): | |
| return "A" | |
| elif(self.value is 11): | |
| return "J" | |
| elif(self.value is 12): | |
| return "Q" | |
| elif(self.value is 13): | |
| return "K" | |
| else: | |
| return self.value | |
| def GetPrettyName(self): | |
| return str(self.GetValue()) + " of " + self.GetSuite() | |
| def __str__(self): | |
| return str(self.GetValue()) + self.GetSuite()[:1] | |
| def GetComputerName(self): | |
| return (self.value, self.suite) | |
| class Hand: | |
| """ define a 5 card hand with ranking and stuff""" | |
| def __init__(self, hand): | |
| self.hand = hand | |
| def __str__(self): | |
| string = "" | |
| for card in self.hand: | |
| string = string + str(card) + " " | |
| string = string + "wow, a " + self.FindBestHand() | |
| return string | |
| def FindBestHand(self): | |
| if self.IsStraightFlush(): return "straight flush" | |
| if self.IsQuads(): return "four of a kind" | |
| if self.IsBoat(): return "full house" | |
| if self.IsFlush(): return "flush" | |
| if self.IsStraight(): return "straight" | |
| if self.IsTrips(): return "three of a kind" | |
| if self.IsTwoPair(): return "two pair" | |
| if self.IsPair(): return "one pair" | |
| return "high card" | |
| def SortByRank(self): | |
| return sorted(self.hand, key=self.SortHigh, reverse=True) | |
| def SortHigh(self, card): | |
| """this is internal only""" | |
| if card.GetComputerName()[0] is 1: | |
| return 14 | |
| return card.GetComputerName()[0] | |
| def IsFlush(self): | |
| cardzerosuite = self.hand[0].GetComputerName()[1] | |
| for card in self.hand: | |
| if card.GetComputerName()[1] is not cardzerosuite: | |
| return False | |
| return True | |
| def IsStraight(self): | |
| HighStraight = True | |
| LowStraight = True | |
| # this code sorts the card, makes the ace 14, and checks for an ace high flush | |
| hand = self.SortByRank() | |
| for index, card in enumerate(hand): | |
| # check for ace | |
| if card.GetComputerName()[0] is 1: | |
| cardRank = 14 | |
| else: | |
| cardRank = card.GetComputerName()[0] | |
| if(len(hand) > index+1 and cardRank is not hand[index+1].GetComputerName()[0] + 1 ): | |
| HighStraight = False | |
| # this code seem redundant... it is almost the same as above, just no ace | |
| hand = sorted(self.hand, key=lambda tup: tup.GetComputerName()[0], reverse=True) | |
| for index, card in enumerate(hand): | |
| if(len(hand) > index+1 and card.GetComputerName()[0] is not hand[index+1].GetComputerName()[0] + 1 ): | |
| LowStraight = False | |
| return (LowStraight or HighStraight) | |
| def IsStraightFlush(self): | |
| if self.IsStraight() and self.IsFlush(): | |
| return True | |
| return False | |
| def ValueArray(self): | |
| """Internal only""" | |
| array = [0] * 13 | |
| for card in self.hand: | |
| array[card.GetComputerName()[0] - 1] += 1 | |
| return array | |
| def IsPair(self): | |
| if 2 in self.ValueArray(): | |
| return True | |
| return False | |
| def IsTwoPair(self): | |
| if self.ValueArray().count(2) is 2: | |
| return True | |
| return False | |
| def IsTrips(self): | |
| if 3 in self.ValueArray(): | |
| return True | |
| return False | |
| def IsQuads(self): | |
| if 4 in self.ValueArray(): | |
| return True | |
| return False | |
| def IsBoat(self): | |
| if self.IsTrips() and self.IsPair() : | |
| return True | |
| return False | |
| def newDeck(): | |
| deck = [] | |
| for s in range(0,4): | |
| for v in range(1,14): | |
| deck.append(Card(v,s)) | |
| random.shuffle(deck) | |
| return deck | |
| def bestHand(hands): | |
| currentBestHand = Hand((Card(2,1),Card(3,0),Card(4,0),Card(5,0),Card(7,0))) | |
| scoreMap = ( | |
| 'high card', | |
| 'one pair', | |
| 'two pair', | |
| 'three of a kind', | |
| 'straight', | |
| 'flush', | |
| 'full house', | |
| 'four of a kind', | |
| 'straight flush', | |
| ) | |
| for hand in hands: | |
| if(isinstance(hand, Hand) is False): | |
| hand = Hand(hand) | |
| currentBestHandIndex = scoreMap.index(currentBestHand.FindBestHand()) | |
| handBestHandIndex = scoreMap.index(hand.FindBestHand()) | |
| if(handBestHandIndex > currentBestHandIndex): | |
| currentBestHand = hand | |
| return currentBestHand | |
| def newGame(players, deck): | |
| for player in players: | |
| player["hand"] = [deck.pop(),deck.pop()] | |
| # print(player["name"], player["hand"][0].GetPrettyName(), player["hand"][1].GetPrettyName()) | |
| table = [deck.pop(), deck.pop(),deck.pop(), deck.pop(),deck.pop() ] | |
| # print("and the hards on the table") | |
| # for card in table: | |
| # print(card.GetPrettyName()) | |
| for player in players: | |
| sevenCardHand = player["hand"] + table | |
| sevenCardHands = list(combinations(sevenCardHand, 5)) | |
| for hand in sevenCardHands: | |
| hand = Hand(tuple(hand)) | |
| player["besthand"] = bestHand(sevenCardHands) | |
| players =[ | |
| { | |
| 'name': "player one", | |
| 'hand': [], | |
| 'besthand': [], | |
| }, | |
| { | |
| 'name': "player two", | |
| 'hand': [], | |
| "besthand": [], | |
| }, | |
| ] | |
| from timeit import Timer | |
| t = Timer("""newGame(players,newDeck())""", "from __main__ import newGame, players, newDeck") | |
| print t.timeit(1000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment