Advertisement
Guest User

Untitled

a guest
May 30th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.71 KB | None | 0 0
  1. import wx
  2. import winsound
  3. import wx.grid as gridlib
  4. from random import randint
  5.  
  6. OPTIONS = [1, 2, 3, 4, 5, 6, 7, 8, 9, "DEL", 0, "SEND"]
  7. # these are the events' IDs sent to a function when you click a button.
  8. # the OPTIONS_ID is in the same order of OPTIONS.
  9.  
  10. OPTIONS_ID = [-31984,-31983,-31982,-31981,-31980,-31979, -31978, -31977, -31976, -31975, -31974, -31973, -31985] # the built in wxpython IDs for the buttons
  11.  
  12.  
  13. GAME_POSITION = (400, 100)
  14. GAME_SIZE = [900, 600]
  15.  
  16. def RandomNum():
  17. count = 5
  18. while count > 4:
  19. num = randint(1000, 9999)
  20. digits = str(num)
  21. count = 0
  22. for digit in digits:
  23. for digit2 in digits:
  24. if digit == digit2:
  25. count = count + 1
  26. return digits
  27.  
  28. class Frame(wx.Frame): # class for all the frames in our game.
  29. def __init__(self, parent, id, title, pos, size):
  30. wx.Frame.__init__(self, parent, id, title, pos, size)
  31. self.panel = wx.Panel(self)
  32. self.fdf = wx.TextCtrl(self.panel, size=(275, 75), pos=(520, 20))
  33. self.count = 0
  34. self.turnsCounter = 0
  35. self.numbers = RandomNum()
  36. self.bulls = 0
  37. self.cows = 0
  38. self.counter_of_turns = 0
  39. self.check = False
  40.  
  41. self.grid = gridlib.Grid(self.panel, pos = (85, 150), size=(323, 212))
  42. self.grid.CreateGrid(10, 3)
  43. sizer = wx.BoxSizer(wx.VERTICAL)
  44. sizer.Add(self.panel, 1, wx.EXPAND)
  45. sizer.Add(self.grid, 1, wx.EXPAND)
  46. self.panel.SetSizer(sizer)
  47. for i in range(10):
  48. for j in range(4):
  49. self.grid.SetReadOnly(i, j)
  50. self.grid.SetColLabelValue(0, "guess")
  51. self.grid.SetColLabelValue(1, "cows")
  52. self.grid.SetColLabelValue(2, "bulls")
  53.  
  54.  
  55. def message_dialog(self, message, caption, style=wx.YES_NO, position=GAME_POSITION):
  56. if message != "": # making sure not to execute a message if its empety
  57. message = wx.MessageDialog(None, message, caption, style, position)
  58. answer = message.ShowModal()
  59. if answer == wx.ID_YES:
  60. self.reset()
  61. else:
  62. self.Destroy()
  63. else:
  64. return -1
  65. # this function creates a textbox at a specific position with a specific size.
  66. def write(self, panel, txt, pos, size=20, font_family=wx.SWISS, font_style = wx.NORMAL,font_weight = wx.BOLD, underline = False, color=wx.WHITE):
  67. # create a textbox at a specific position with a specific size.
  68. your_txt = wx.StaticText(panel, -1, txt, pos)
  69. your_txt.SetFont(wx.Font(size,font_family,font_style,font_weight,underline))
  70. your_txt.SetForegroundColour(color)
  71. # same as above, just for a button.
  72. def create_button(self, panel, txt, position, width, height, color, disable):
  73. Size = wx.Size(width, height)
  74. self.button = wx.Button(panel, -1, txt, position, Size)
  75. self.button.SetBackgroundColour(color)
  76. self.border = wx.BoxSizer(wx.VERTICAL)
  77. self.border.Add(self.button)
  78. self.Bind(wx.EVT_BUTTON, lambda evt: self.OnButton(evt), self.button)
  79. if disable == True:
  80. self.button.Disable()
  81.  
  82. def count_bulls(self, txtctrl, seria):
  83. for i in range(4):
  84. if seria[i] == txtctrl[i]:
  85. self.bulls += 1
  86. replacement = self.bulls
  87. self.bulls = 0
  88. return replacement
  89.  
  90. def count_cows(self, txtctrl, seria):
  91. for i in range(4):
  92. if seria[i] != txtctrl[i] and seria[i] in txtctrl:
  93. self.cows += 1
  94. replacement = self.cows
  95. self.cows = 0
  96. return replacement
  97.  
  98. def reset(self):
  99. self.fdf.Clear()
  100. self.grid.ClearGrid()
  101. self.count = 0
  102. self.turnsCounter = 0
  103. self.numbers = RandomNum()
  104. self.bulls = 0
  105. self.cows = 0
  106. self.counter_of_turns = 0
  107. self.check = False
  108. for child in self.panel.GetChildren():
  109. if child.GetLabel() != "SEND":
  110. child.Enable()
  111. else:
  112. child.Disable()
  113. if self.count == 0:
  114. if child.GetLabel() == "DEL" or child.GetLabel() == "0":
  115. child.Disable()
  116. def OnButton(self, event):
  117. print repr(event.Id) + ","
  118. print self.numbers
  119. if event.Id in OPTIONS_ID: # if indeed an option button was pressed
  120. exited = -1 # exited is 5100 if the user exited his dialog box
  121. # assigning the events to the button.
  122. for i in range(13):
  123. if event.Id != -31985 and event.Id != -31975 and event.Id != -31974 and event.Id != -31973 and event.Id == OPTIONS_ID[i]:
  124. self.fdf.AppendText(str(OPTIONS[i]))
  125. self.count += 1
  126. if event.Id == -31974:
  127. self.fdf.AppendText(str(OPTIONS[10]))
  128. self.count += 1
  129. if event.Id == -31985:
  130. self.reset()
  131. if event.Id == -31973:
  132. self.counter_of_turns += 1
  133. print self.numbers
  134. print self.fdf.GetValue()
  135. cows = self.count_cows(self.fdf.GetValue(), self.numbers)
  136. bulls = self.count_bulls(self.fdf.GetValue(), self.numbers)
  137. self.grid.SetCellValue(self.turnsCounter,0, self.fdf.GetValue())
  138. self.grid.SetCellValue(self.turnsCounter, 1, str(cows))
  139. self.grid.SetCellValue(self.turnsCounter, 2, str(bulls))
  140. self.fdf.Clear()
  141. self.count = 0
  142. if self.turnsCounter < 9:
  143. self.turnsCounter += 1
  144. if bulls == 4:
  145. self.check = True
  146. winsound.PlaySound('The_Power_-_Snap_1_.wav', winsound.SND_ASYNC | winsound.SND_LOOP)
  147. self.message_dialog("Well done! you won this game..n You won the game in %s turns .. n Play again ? " % self.counter_of_turns , "You won!")
  148. winsound.PlaySound(None, 0)
  149. if event.Id == -31975:
  150. if self.count > 0:
  151. self.count -= 1
  152. self.fdf.Remove(self.fdf.GetLastPosition()-1, self.fdf.GetLastPosition())
  153. if self.count == 4:
  154. for child in self.panel.GetChildren():
  155. if isinstance(child, wx.Button):
  156. try:
  157. int(child.GetLabel())
  158. except ValueError:
  159. if child.GetLabel() == "SEND":
  160. child.Enable()
  161. else:
  162. child.Disable()
  163. elif self.check == False:
  164. for child in self.panel.GetChildren():
  165. if child.GetLabel() != "SEND":
  166. child.Enable()
  167. else:
  168. child.Disable()
  169. if self.count == 0:
  170. if child.GetLabel() == "DEL" or child.GetLabel() == "0":
  171. child.Disable()
  172. #for child in self.panel.GetChildren():
  173. #if isinstance(child, wx.Button):
  174. #if child.GetLabel() in self.fdf.GetValue():
  175. #child.Disable()
  176. if self.counter_of_turns == 10 and self.check == False:
  177. self.message_dialog("YOU LOST :( n THE NUMBERS WERE %s n PLAY AGAIN ?" % self.numbers,"Bad news ..")
  178.  
  179. class Game(wx.App):
  180. def OnInit(self): # upon game opening
  181. # I would like the options window to be the first window's parent
  182. # so I will first set up our options window:
  183. window = Frame(None, -1, "Good Luck!", GAME_POSITION, GAME_SIZE)
  184. first_panel = window.panel
  185. window.SetBackgroundColour(wx.BLACK)
  186. window.write(first_panel, "BULLS AND COWS!", (50, 50), size=(35))
  187. countX = 500
  188. countY = 100
  189. window.create_button(first_panel,"restart!", (50, 400), 100, 100, wx.WHITE, False)
  190. for option in OPTIONS:
  191. if str(option) == "SEND" or str(option) == "DEL":
  192. window.create_button(first_panel,str(option), (countX, countY), 100, 100, wx.GREEN, True)
  193. elif str(option) == "0":
  194. window.create_button(first_panel,str(option), (countX, countY), 100, 100, wx.WHITE, True)
  195. else:
  196. window.create_button(first_panel,str(option), (countX, countY), 100, 100, wx.WHITE, False)
  197. countX += 110
  198. if str(option) == "3" or str(option) == "6" or str(option) == "9":
  199. countY += 110
  200. countX = 500
  201.  
  202.  
  203.  
  204. window.Show(True)
  205. return True
  206.  
  207.  
  208. def main():
  209. MasterMind = Game()
  210. MasterMind.MainLoop()
  211.  
  212.  
  213. if __name__ == '__main__':
  214. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement