Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import sys
- import time
- username = 'zz'
- password = 'z'
- def main():
- login()
- #TODO:Add user and password database.
- def login():
- print("Enter username : ")
- answer1 = raw_input()
- print("Enter password : ")
- answer2 = raw_input()
- if str(answer1) == username and str(answer2) == password:
- print("Welcome back '"+username+"' - ACCESS GRANTED.")
- menu()
- else:
- print("Wrong username and or password - ACCESS DENIED.")
- login()
- #The lobby itself with different choice options and an interactive menu.
- #TODO:Add personal stats, database included. Possibly more choices and a better looking menu.
- def menu():
- print("************LOBBY**************")
- choice = raw_input("""
- A: JOIN A TABLE
- B: VIEW MOST WINS
- C: VIEW MOST LOSES
- D: VIEW MOST CASH
- Q: LOG OUT
- Please enter your choice: """)
- if choice == "A" or choice == "a":
- startplay()
- print('')
- elif choice == "B" or choice == "b":
- mostwin()
- elif choice == "C" or choice == "c":
- mostloss()
- elif choice == "D" or choice == "d":
- mostcash()
- elif choice == "Q" or choice == "q":
- print("Logging out of user '"+username+"'...")
- time.sleep(2)
- login()
- else:
- print("\nYou must only select either A, B, C, D or Q.")
- print("Please try again\n")
- menu()
- def startplay():
- startgame()
- def mostwin():
- pass
- #Most win scoreboard goes here along with the script.
- def mostloss():
- pass
- #Most loss scoreboard goes here along with the script.
- def mostcash():
- pass
- #Most cash scoreboard goes here along with the script.
- #-----------------------------------------------------
- #Deck creating and shuffling.
- class StackOfCards(object):
- def __init__(self):
- self.contents = []
- def shuffle(self):
- random.shuffle(self.contents)
- class BlackjackHand(StackOfCards):
- def __init__(self):
- self.contents = []
- self.ace_value = 11
- self.bust = False
- def draw_card(self, draw_from):
- transfer_card(draw_from, self, 0, 1)
- def point_value(self):
- self.points = 0
- for card in range(0, len(self.contents)):
- if self.contents[card] in ["K", "Q", "J"]:
- self.points += 10
- elif self.contents[card] == 'A':
- self.points += self.ace_value
- else:
- self.points += self.contents[card]
- if "A" in self.contents and self.points > 21 and self.ace_value == 11:
- self.ace_value = 1
- return self.point_value()
- return self.points
- def show_hand(self):
- pass
- def transfer_card(fromStack, toStack, cardKey, transfers):
- for i in range(0, transfers):
- toStack.contents.insert(0, fromStack.contents.pop(cardKey))
- def draw_hands():
- deck.contents = []
- player_hand.contents = []
- dealer_hand.contents = []
- for x in range(2, 11):
- for i in range(0, 4):
- deck.contents.append(x)
- for card in ["J", "Q", "K", "A"]:
- for i in range(0, 4):
- deck.contents.append(card)
- deck.shuffle()
- transfer_card(deck, dealer_hand, 0, 2)
- transfer_card(deck, player_hand, 0, 2)
- def display_game_state():
- print "\nThe dealer is showing " + str(dealer_hand.contents[0])
- print "You're holding " + str(player_hand.point_value()) + ": " + ", ".join(str(x) for x in player_hand.contents)
- def request_input():
- while True:
- display_game_state()
- print "\nYou may enter:"
- print "Hit"
- print "Stick"
- print "Leave"
- player_input = raw_input("\nEnter an action: ")
- player_input = player_input.lower()
- if player_input in ["hit", "stick", "leave"]:
- player_action = player_input
- return player_action
- else:
- print "\n I'm sorry, I didn't understand that"
- def take_dealers_turn():
- while dealer_hand.point_value() < 17:
- dealer_hand.draw_card(deck)
- if dealer_hand.point_value() > 21:
- dealer_hand.bust = True
- def display_game_outcome(player_outcome):
- #TODO:Improve visuals.
- if player_outcome == "win":
- print "\n******YOU WIN******"
- print "\nYou held " + str(player_hand.point_value()) + " while the dealer held " + str(dealer_hand.point_value())
- print "\n******YOU WIN******"
- if player_outcome == "lose":
- print "\n******YOU LOSE******"
- print "\nThe dealer held " + str(dealer_hand.point_value()) + " while you held " + str(player_hand.point_value())
- print "\n******YOU LOSE******"
- dealer_hand = BlackjackHand()
- player_hand = BlackjackHand()
- deck = StackOfCards()
- def startgame():
- losscount = 0
- wincount = 0
- while 1:
- draw_hands()
- dealer_hand.bust = False
- player_hand.bust = False
- player_action = request_input()
- if player_action == "leave":
- menu()
- while player_action == "hit":
- player_hand.draw_card(deck)
- print "\nYou drew: " + str(player_hand.contents[0])
- if player_hand.point_value() > 21:
- player_hand.bust = True
- display_game_outcome("lose")
- losscount += 1
- break
- player_action = request_input()
- if player_action == "leave":
- menu()
- if player_action == "stick":
- take_dealers_turn()
- if dealer_hand.bust == True or dealer_hand.point_value() < player_hand.point_value():
- display_game_outcome("win")
- wincount += 1
- else:
- display_game_outcome("lose")
- losscount += 1
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement