Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.93 KB | None | 0 0
  1. from tkinter import *
  2. from tkinter import messagebox
  3. import random
  4. import time
  5.  
  6.  
  7. class Card:
  8. def __init__(self, suit, value):
  9. self.suit = suit
  10. self.value = value
  11. self.name = self.get_name(suit, value)
  12.  
  13. def get_name(self, suit, value):
  14. if 1 < value < 11:
  15. return str(value) + " of " + suit
  16. elif value == 11:
  17. return "Jack" + " of " + suit
  18. elif value == 12:
  19. return "Queen" + " of " + suit
  20. elif value == 13:
  21. return "King" + " of " + suit
  22. elif value == 14:
  23. return "Ace" + " of " + suit
  24.  
  25.  
  26. class Player:
  27. def __init__(self, name):
  28. self.name = name
  29. self.deck = []
  30.  
  31.  
  32. def setup_deck():
  33. # Creates and shuffles an array of Card objects to make a complete, shuffled deck.
  34. setup_deck = []
  35. suits = ["Spades", "Hearts", "Diamonds", "Clubs"]
  36. for j in range(0, 4):
  37. for i in range(2, 15):
  38. setup_deck.append(Card(suits[j], i))
  39. random.shuffle(setup_deck)
  40. return setup_deck
  41.  
  42.  
  43. root = Tk()
  44. root.geometry("300x200")
  45.  
  46. # set up the shuffled deck, and split the array down the middle, giving half to each player
  47. deck = []
  48. deck = setup_deck()
  49. human = Player("human")
  50. compy = Player("compy")
  51. human.deck = deck[:len(deck) // 2]
  52. compy.deck = deck[len(deck) // 2:]
  53.  
  54.  
  55. varCard = IntVar() # varCard is the text of the "pile" button, which gets clicked to generate a slap.
  56. varCard.set(" ")
  57. varTurn = IntVar() # varTurn keeps track of how many turns there have been
  58. varTurn.set(0)
  59.  
  60. Button(root, textvariable=varCard, command=lambda: slap(human)).pack()
  61. btn_deal = Button(root, text="deal", command=lambda: deal())
  62. btn_deal.pack()
  63.  
  64. slapped = False
  65. pile = []
  66.  
  67.  
  68. def deal():
  69. global slapped
  70. slapped = False # reset the state of "slapped" every time a new card is drawn.
  71. #Check for win conditions
  72. if len(human.deck) == 0 or len(compy.deck) == 0:
  73. if len(human.deck) == 0:
  74. winner = "Compy wins!"
  75. else:
  76. winner = "You win!"
  77. messagebox.showinfo("Game Over", winner)
  78. #Human's turn
  79. if varTurn.get() % 2 == 0:
  80. pile.append(human.deck[0])
  81. human.deck.remove(human.deck[0])
  82. update_label() #varTurn is incremented in this method.
  83. root.update()
  84. check_for_slap() #tells the AI to check for a valid slap condition, and will resolve here if necessary
  85. print("Human: " + str(len(human.deck)))
  86. deal() #recursively calls deal() for the AI to take its turn.
  87. else:
  88. #AI's turn. This block gets entered on an odd turn number
  89. timex = time.time()
  90. global btn_deal
  91. btn_deal.config(state=DISABLED) #prevent player from clicking "deal" in the middle of AI's turn
  92. while time.time() - timex < 2: #two second delay between player's turn and AI's.
  93. root.update()
  94. if not slapped: #either player may have slapped in the 2 sec. delay. this If statement aborts the turn if that's the case.
  95. pile.append(compy.deck[0])
  96. compy.deck.remove(compy.deck[0])
  97. update_label()
  98. root.update()
  99. check_for_slap()
  100. print("Compy: " + str(len(compy.deck)))
  101. btn_deal.config(state=NORMAL)
  102. root.update()
  103.  
  104.  
  105. def check_for_slap():
  106. #AI runs this method after each card is played. Result is a possible slap action.
  107. make_bad_slap_int = random.randint(1, 50) #allows the AI to make a "mistake" and make an invalid slap.
  108. if make_bad_slap_int == 1:
  109. make_bad_slap = True
  110. else:
  111. make_bad_slap = False
  112. #valid slap conditions are "jack" or "top two match"
  113. if pile[len(pile) - 1].value == 11 or len(pile) > 1 and (pile[len(pile) - 1].value == pile[len(pile) - 2].value):
  114. correct_to_slap = True
  115. else:
  116. correct_to_slap = False
  117.  
  118. if (correct_to_slap and not make_bad_slap) or (not correct_to_slap and make_bad_slap):
  119. delay = .25 + random.triangular(.1, 1) #Randomized delay before AI slaps.
  120. timex = time.time()
  121. while time.time() - timex < delay:
  122. root.update()
  123. slap(compy)
  124.  
  125.  
  126. def update_label():
  127. #updates the label on the "slap pile"
  128. #increments the turn.
  129. print("pile: " + str(len(pile)))
  130. varCard.set(pile[len(pile) - 1].name)
  131. varTurn.set(varTurn.get() + 1)
  132. if varTurn.get() % 2 == 0:
  133. root.configure(background='green')
  134. else:
  135. root.configure(background='red')
  136.  
  137.  
  138. def slap(player):
  139. print(player.name + " slapped.")
  140. global slapped
  141. if not slapped: #skips over logic if another slap has already arrived.
  142. slapped = True
  143. root.update()
  144. global pile
  145. #Check for valid slap conditions.
  146. if pile[len(pile) - 1].value == 11 or len(pile) > 1 and (
  147. pile[len(pile) - 1].value == pile[len(pile) - 2].value):
  148. if player.name == "human":
  149. messagebox.showinfo("Slap!", "You WON the slap!")
  150. print("human takes pile")
  151. #transfer the cards in pile to the pile of the plaer
  152. for card in pile:
  153. human.deck.append(card)
  154. pile = []
  155. else:
  156. messagebox.showinfo("Slap!", "You LOST the slap!")
  157. print("compy takes pile")
  158. for card in pile:
  159. compy.deck.append(card)
  160. pile = []
  161. else:
  162. #handles the one card penalty for slapping at the wrong time.
  163. print("bad slap")
  164. if player.name == "human":
  165. messagebox.showinfo("Slap!", "You slapped at the wrong time.")
  166. pile.append(human.deck[0])
  167. human.deck.remove(human.deck[0])
  168. update_label()
  169. root.update()
  170. else:
  171. messagebox.showinfo("Slap!", "Computer slapped at the wrong time.")
  172. pile.append(compy.deck[0])
  173. compy.deck.remove(compy.deck[0])
  174. update_label()
  175. root.update()
  176.  
  177.  
  178. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement