Advertisement
Guest User

Untitled

a guest
Dec 20th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 KB | None | 0 0
  1. import random
  2. import sys
  3. import time
  4.  
  5. username = 'zz'
  6. password = 'z'
  7.  
  8.  
  9.  
  10. def main():
  11. login()
  12.  
  13. #TODO:Add user and password database.
  14.  
  15. def login():
  16. print("Enter username : ")
  17. answer1 = raw_input()
  18. print("Enter password : ")
  19. answer2 = raw_input()
  20. if str(answer1) == username and str(answer2) == password:
  21. print("Welcome back '"+username+"' - ACCESS GRANTED.")
  22. menu()
  23. else:
  24. print("Wrong username and or password - ACCESS DENIED.")
  25. login()
  26.  
  27. #The lobby itself with different choice options and an interactive menu.
  28.  
  29. #TODO:Add personal stats, database included. Possibly more choices and a better looking menu.
  30.  
  31. def menu():
  32. print("************LOBBY**************")
  33.  
  34. choice = raw_input("""
  35. A: JOIN A TABLE
  36. B: VIEW MOST WINS
  37. C: VIEW MOST LOSES
  38. D: VIEW MOST CASH
  39. Q: LOG OUT
  40.  
  41. Please enter your choice: """)
  42.  
  43. if choice == "A" or choice == "a":
  44. startplay()
  45. print('')
  46. elif choice == "B" or choice == "b":
  47. mostwin()
  48. elif choice == "C" or choice == "c":
  49. mostloss()
  50. elif choice == "D" or choice == "d":
  51. mostcash()
  52. elif choice == "Q" or choice == "q":
  53. print("Logging out of user '"+username+"'...")
  54. time.sleep(2)
  55. login()
  56. else:
  57. print("\nYou must only select either A, B, C, D or Q.")
  58. print("Please try again\n")
  59. menu()
  60.  
  61.  
  62. def startplay():
  63. startgame()
  64.  
  65.  
  66. def mostwin():
  67. pass
  68. #Most win scoreboard goes here along with the script.
  69.  
  70. def mostloss():
  71. pass
  72. #Most loss scoreboard goes here along with the script.
  73.  
  74. def mostcash():
  75. pass
  76. #Most cash scoreboard goes here along with the script.
  77.  
  78.  
  79. #-----------------------------------------------------
  80.  
  81.  
  82.  
  83. #Deck creating and shuffling.
  84.  
  85. class StackOfCards(object):
  86. def __init__(self):
  87. self.contents = []
  88.  
  89. def shuffle(self):
  90. random.shuffle(self.contents)
  91.  
  92.  
  93. class BlackjackHand(StackOfCards):
  94. def __init__(self):
  95. self.contents = []
  96. self.ace_value = 11
  97. self.bust = False
  98.  
  99.  
  100. def draw_card(self, draw_from):
  101. transfer_card(draw_from, self, 0, 1)
  102.  
  103. def point_value(self):
  104. self.points = 0
  105. for card in range(0, len(self.contents)):
  106. if self.contents[card] in ["K", "Q", "J"]:
  107. self.points += 10
  108. elif self.contents[card] == 'A':
  109. self.points += self.ace_value
  110. else:
  111. self.points += self.contents[card]
  112.  
  113. if "A" in self.contents and self.points > 21 and self.ace_value == 11:
  114. self.ace_value = 1
  115. return self.point_value()
  116. return self.points
  117.  
  118. def show_hand(self):
  119. pass
  120.  
  121.  
  122. def transfer_card(fromStack, toStack, cardKey, transfers):
  123. for i in range(0, transfers):
  124. toStack.contents.insert(0, fromStack.contents.pop(cardKey))
  125.  
  126. def draw_hands():
  127. deck.contents = []
  128. player_hand.contents = []
  129. dealer_hand.contents = []
  130. for x in range(2, 11):
  131. for i in range(0, 4):
  132. deck.contents.append(x)
  133.  
  134. for card in ["J", "Q", "K", "A"]:
  135. for i in range(0, 4):
  136. deck.contents.append(card)
  137.  
  138. deck.shuffle()
  139. transfer_card(deck, dealer_hand, 0, 2)
  140. transfer_card(deck, player_hand, 0, 2)
  141.  
  142. def display_game_state():
  143. print "\nThe dealer is showing " + str(dealer_hand.contents[0])
  144. print "You're holding " + str(player_hand.point_value()) + ": " + ", ".join(str(x) for x in player_hand.contents)
  145.  
  146. def request_input():
  147. while True:
  148. display_game_state()
  149. print "\nYou may enter:"
  150. print "Hit"
  151. print "Stick"
  152. print "Leave"
  153. player_input = raw_input("\nEnter an action: ")
  154. player_input = player_input.lower()
  155.  
  156. if player_input in ["hit", "stick", "leave"]:
  157. player_action = player_input
  158. return player_action
  159. else:
  160. print "\n I'm sorry, I didn't understand that"
  161.  
  162. def take_dealers_turn():
  163. while dealer_hand.point_value() < 17:
  164. dealer_hand.draw_card(deck)
  165. if dealer_hand.point_value() > 21:
  166. dealer_hand.bust = True
  167.  
  168. def display_game_outcome(player_outcome):
  169.  
  170. #TODO:Improve visuals.
  171. if player_outcome == "win":
  172. print "\n******YOU WIN******"
  173. print "\nYou held " + str(player_hand.point_value()) + " while the dealer held " + str(dealer_hand.point_value())
  174. print "\n******YOU WIN******"
  175. if player_outcome == "lose":
  176. print "\n******YOU LOSE******"
  177. print "\nThe dealer held " + str(dealer_hand.point_value()) + " while you held " + str(player_hand.point_value())
  178. print "\n******YOU LOSE******"
  179.  
  180.  
  181. dealer_hand = BlackjackHand()
  182. player_hand = BlackjackHand()
  183. deck = StackOfCards()
  184.  
  185.  
  186. def startgame():
  187.  
  188. losscount = 0
  189. wincount = 0
  190.  
  191. while 1:
  192. draw_hands()
  193. dealer_hand.bust = False
  194. player_hand.bust = False
  195.  
  196. player_action = request_input()
  197. if player_action == "leave":
  198. menu()
  199.  
  200. while player_action == "hit":
  201. player_hand.draw_card(deck)
  202. print "\nYou drew: " + str(player_hand.contents[0])
  203. if player_hand.point_value() > 21:
  204. player_hand.bust = True
  205. display_game_outcome("lose")
  206. losscount += 1
  207. break
  208. player_action = request_input()
  209. if player_action == "leave":
  210. menu()
  211.  
  212. if player_action == "stick":
  213. take_dealers_turn()
  214. if dealer_hand.bust == True or dealer_hand.point_value() < player_hand.point_value():
  215. display_game_outcome("win")
  216. wincount += 1
  217. else:
  218. display_game_outcome("lose")
  219. losscount += 1
  220.  
  221.  
  222.  
  223. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement