Advertisement
Guest User

Stuff

a guest
Apr 16th, 2014
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.44 KB | None | 0 0
  1. #Ian Estabrook
  2. #estabro3
  3. #No extra credit features have been implemented. Sadly.
  4.  
  5. import wx, random
  6.  
  7. #Part 1: Basic Game Components
  8.  
  9. class Card(object):
  10. def __init__(self,suit,rank):
  11. self.rank = rank
  12. self.suit = suit
  13. file_name = "cards_gif/"+self.suit
  14. if rank < 11:
  15. file_name+=str(rank)
  16. elif rank == 11:
  17. file_name+="j"
  18. elif rank == 12:
  19. file_name+="q"
  20. elif rank ==13:
  21. file_name+="k"
  22. file_name+=".gif"
  23. self.image = wx.Image(file_name,wx.BITMAP_TYPE_ANY).ConvertToBitmap()
  24. def __str__(self):
  25. suit = {"c":"Clubs","d":"Diamonds","h":"Hearts","s":"Spades"} #These are dictionaries containing the suits and ranks so we can index them later
  26. rank = {1:"Ace",2:"Two",3:"Three",4:"Four",5:"Five",6:"Six",7:"Seven",8:"Eight",9:"Nine",10:"Ten",11:"Jack",12:"Queen",13:"King"}
  27. mysuit = suit[self.suit] #Index to find the card's suit
  28. myrank = rank[self.rank] #Index to find the card's rank
  29. return myrank + " of "+mysuit
  30. def get_img(self):
  31. return self.image #Return the image attribute of the card, which should have something to do with intializing the image in the __init__ constructor
  32. def __cmp__(self,other):
  33. return self.rank-other.rank #For use in the is_straight comparison
  34.  
  35. class Pile(object):
  36. def __init__(self):
  37. self.cards = []
  38. def add_card(self,card):
  39. self.cards.append(card)
  40. def draw_card(self):
  41. return self.cards.pop()
  42. def top_card(self):
  43. return self.cards[-1]
  44. def shuffle(self):
  45. random.shuffle(self.cards)
  46.  
  47. class Deck(Pile):
  48. def __init__(self):
  49. super(Deck,self).__init__()
  50. self.suit = ["c","d","h","s"]
  51. for suit in self.suit:
  52. for rank in range(1,14):
  53. card = Card(suit,rank) #Create a card
  54. self.add_card(card) #Add that card to the deck
  55. self.shuffle() #Shuffle the deck
  56.  
  57. #Part 2: Scoring Poker Hands
  58.  
  59. class Hand(Pile):
  60. def is_flush(self):
  61. first_suit = self.cards[0].suit #The first suit is the first card
  62. for card in self.cards: #For a card in the pile/deck
  63. if (card.suit != first_suit): #If the card doesn't match the suit
  64. return False #Nope
  65. return True #Yay!
  66.  
  67. def is_straight(self): #Cards next to each other have to differ by 1
  68. self.cards.sort() #Sort the cards in numeric order
  69. for i in range(0,4): #For a card in a hand of 5
  70. if (self.cards[i+1].rank-self.cards[i].rank!=1): #Check to see if the next one is only 1 greater
  71. return False #Lamesauce - it's not
  72. return True #w00t - it is!
  73.  
  74. def score(self):
  75. histogram = {}
  76. for card in self.cards:
  77. if card.rank not in histogram:
  78. histogram[card.rank] = 0
  79. histogram[card.rank]+=1 #Each entry, or key value pair, represents a rank:how many times it's appeared
  80. if len(histogram) == 2:
  81. for rank in histogram:
  82. if histogram[rank] == 3:
  83. return 10
  84. return "Full house!"
  85. if histogram[rank] == 4:
  86. return 16
  87. return "Four of a kind!"
  88. if len(histogram) == 3:
  89. for rank in histogram:
  90. if histogram[rank] == 3:
  91. return 6
  92. return "Three of a kind!"
  93. if histogram[rank] == 2:
  94. return 3
  95. return "Two pair!"
  96. if len(histogram) == 4:
  97. for rank in histogram:
  98. return 1
  99. return "One pair!"
  100. if len(histogram) == 5:
  101. if self.is_straight() == True and self.is_flush() == True and self.cards[0] == 1 and self.cards[1] == 10:
  102. return 50
  103. if self.is_flush() == True and self.is_straight() == True:
  104. return 30
  105. return "Straight flush!"
  106. return "Royal flush!"
  107. if self.is_flush() == True:
  108. return 5
  109. return "Flush!"
  110. if self.is_straight() == True:
  111. return 12
  112. return "Straight!"
  113. return 0
  114. return "No pair."
  115. if len(histogram) != 5:
  116. raise ValueError("You don't have five cards in your hand! Draw moar!")
  117.  
  118. #Part 3: Basic Interface Objects
  119.  
  120. class DeckButton(wx.BitmapButton): #creates a class that inherits from bitmap button
  121. def __init__(self,parent): #wx widgets always have a parent argument, where the wx widget lives
  122. super(DeckButton,self).__init__(parent, -1)
  123. self.deck = Deck() #Build the deck
  124. top = self.deck.top_card() #Build a card
  125. self.SetBitmapLabel(top.get_img()) #Set the image of the card to what gets displayed on the button
  126. def draw_card(self):
  127. result = self.deck.draw_card()
  128. top = self.deck.top_card()
  129. self.SetBitmapLabel(top.get_img())
  130. return result
  131. def restart(self):
  132. self.deck = Deck()
  133. top = self.deck.top_card()
  134. self.SetBitmapLabel(top.get_img())
  135.  
  136. class CellButton(wx.BitmapButton):
  137. def __init__(self,parent):
  138. super(CellButton,self).__init__(parent)
  139. back = wx.Image("cards_gif/b1fv.gif",wx.BITMAP_TYPE_ANY).ConvertToBitmap()
  140. self.SetBitmapLabel(back)
  141. self.occupied = False #Does the cell contain a card?
  142. self.card = None #What card occupies the cell?
  143. def restart(self):
  144. self.card = None
  145. self.occupied = False
  146. back = wx.Image("cards_gif/b1fv.gif",wx.BITMAP_TYPE_ANY).ConvertToBitmap()
  147. self.SetBitmapLabel(back)
  148. def add_card(self,card):
  149. self.card = card
  150. self.occupied = True
  151. self.SetBitmapLabel(card.get_img())
  152.  
  153. #After the deck and cell button work, we can get the __init__() function for the game going
  154.  
  155. #Part 4: Game Interface
  156.  
  157. class Game(wx.Frame):
  158. def __init__(self,parent,id):
  159. super(Game,self).__init__(parent,id,"Poker Solitare")
  160.  
  161. Deck_Panel = wx.Panel(self,-1)
  162. self.Deck = DeckButton(Deck_Panel)
  163. Deck_Sizer = wx.BoxSizer(wx.HORIZONTAL)
  164. Deck_Sizer.Add(self.Deck)
  165. Deck_Panel.SetSizer(Deck_Sizer)
  166. self.status=self.CreateStatusBar()
  167.  
  168. Cell_Panel = wx.Panel(self,-1)
  169. Grid_Sizer = wx.GridSizer(5,5,3,3)
  170. self.Cells = []
  171. for i in range(5):
  172. row_list = []
  173. for j in range(5):
  174. cell = CellButton(Cell_Panel)
  175. Grid_Sizer.Add(cell)
  176. self.Bind(wx.EVT_BUTTON,self.place_card,cell)
  177. row_list.append(cell)
  178. self.Cells.append(row_list)
  179. Cell_Panel.SetSizer(Grid_Sizer)
  180.  
  181. Sizer=wx.BoxSizer(wx.HORIZONTAL)
  182. Sizer.Add(Deck_Panel)
  183. Sizer.Add(Cell_Panel)
  184. self.SetSizer(Sizer)
  185. self.Fit()
  186.  
  187. #The code in here vvv interacts with self.Bind(wx.EVT_BUTTON...) which is why we reference it
  188.  
  189. def check_state(self):
  190. score = 0
  191. for i in range(5):
  192. score+=self.score_col(i)
  193. score+=self.score_row(i)
  194. self.status.SetStatusText("Score"+" "+ str(score))
  195. if self.check_end() == True:
  196. output="Game over, man. GAME OVER."
  197. message=wx.MessageDialog(self,output,"Message for you, sir!",wx.OK)
  198. message.ShowModal()
  199. message.Destroy()
  200. self.restart()
  201.  
  202.  
  203. def place_card(self,evt):
  204. for i in range(5):
  205. for j in range(5):
  206. cell = self.Cells[i][j] #Row i, column j
  207. if evt.GetId() == cell.GetId() and cell.occupied == False:
  208. card = self.Deck.draw_card()
  209. cell.add_card(card)
  210. self.check_state()
  211.  
  212. def score_col(self,col_num):
  213. score = 0
  214. hand = Hand()
  215. for i in range(5):
  216. card = self.Cells[i][col_num].card #This gives me the 5 cards for that column number
  217. hand.add_card(card) #This adds a card to my hand
  218. if self.Cells[i][col_num].occupied == False:
  219. return 0
  220. return hand.score()
  221.  
  222. def score_row(self,row_num):
  223. score = 0
  224. hand = Hand()
  225. for i in range(5):
  226. card = self.Cells[row_num][i].card #This gives me the 5 cards for that column number
  227. hand.add_card(card) #This adds a card to my hand
  228. if self.Cells[row_num][i].occupied == False:
  229. return 0
  230. return hand.score()
  231.  
  232. def check_end(self):
  233. for i in range(5):
  234. for j in range(5):
  235. cell = self.Cells[i][j]
  236. if cell.occupied == False:
  237. return False
  238. return True
  239.  
  240. def restart(self):
  241. self.Deck.restart()
  242. for i in range(5):
  243. for j in range(5):
  244. self.Cells[i][j].restart()
  245.  
  246. #Two panels will go inside our game class (which is a frame)
  247. #Gridsizer for cells panel, Boxsizer for the deck, and another Boxsizer for the whole thing
  248. #So three panels total
  249.  
  250. #Test the game with the code below
  251.  
  252. if __name__=="__main__":
  253. app=wx.App()
  254. game=Game(None,-1)
  255. game.Show()
  256. app.MainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement