daily pastebin goal
77%
SHARE
TWEET

Activity4

oanap123 Jul 11th, 2018 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2.  
  3. def main():
  4.     #Initialize variables
  5.     play_again_counter = 0
  6.     winnings = 0
  7.     bet = 25
  8.     money = 1000
  9.     name = ""
  10.     restore_list = []
  11.  
  12.     #Get name and money from restore() function
  13.     restore_list = restore()
  14.     name = restore_list[0]
  15.     money = restore_list[1]
  16.  
  17.     #Run if a blackjack.save file exists
  18.     if name != "" and money != -1:
  19.         #Ask user if they want to resume saved game
  20.         resume = input("Resume saved game '" + name + "'? (y/n) ")
  21.         #Check if user resumes saved game
  22.         if resume == 'y':
  23.             #Print out values from saved game
  24.             print(name + " has $" + money)
  25.             print()
  26.  
  27.             #Run game and loop if the user wants to keep playing
  28.             while play_again_counter == 0:
  29.                 bet = input_bet(bet, money)
  30.                 if bet == 0:
  31.                     print("Player has quit")
  32.                     save(name, money)
  33.                     exit()
  34.                 else:
  35.                     play_again_counter = 0
  36.                 winnings = play_hand(name, bet)
  37.                 money += winnings
  38.                 print()
  39.                 print(name + " has $" + str(money))
  40.                 print()
  41.         #Make new game if the user does not want to resume saved game
  42.         else:
  43.             #Get user's name and print out starting value
  44.             name = input("Name? ")
  45.             money = 1000
  46.             print(name + " has $1,000")
  47.             print()
  48.  
  49.             #Run game and loop if the user wants to keep playing
  50.             while play_again_counter == 0:
  51.                 bet = input_bet(bet, money)
  52.                 if bet == 0:
  53.                     print("Player has quit")
  54.                     save(name, money)
  55.                     exit()
  56.                 else:
  57.                     play_again_counter = 0
  58.                 winnings = play_hand(name, bet)
  59.                 money += winnings
  60.                 print()
  61.                 print(name + " has $" + str(money))
  62.                 print()
  63.     #Run if there is no blackjack.save file from a previous game
  64.     else:
  65.         #Get user's name and print out starting value
  66.         name = input("Name? ")
  67.         money = 1000
  68.         print(name + " has $1,000")
  69.         print()
  70.  
  71.         #Run game and loop if the user wants to keep playing
  72.         while play_again_counter == 0:
  73.             bet = input_bet(bet, money)
  74.             if bet == 0:
  75.                 print("Player has quit")
  76.                 save(name, money)
  77.                 exit()
  78.             else:
  79.                 play_again_counter = 0
  80.             winnings = play_hand(name, bet)
  81.             money += winnings
  82.             print()
  83.             print(name + " has $" + str(money))
  84.             print()
  85.  
  86.  
  87. def new_deck():
  88.     #Initialize variables
  89.     deck = []
  90.     card = ()
  91.  
  92.     #Create 13 cards in spades
  93.     for i in range(1, 14):
  94.         card = (i, '\u2660')
  95.         deck.append(card)
  96.     #Create 13 cards in hearts
  97.     for i in range(1, 14):
  98.         card = (i, '\u2661')
  99.         deck.append(card)
  100.     #Create 13 cards in diamonds
  101.     for i in range(1, 14):
  102.         card = (i, '\u2662')
  103.         deck.append(card)
  104.     #Create 13 cards in clubs
  105.     for i in range(1, 14):
  106.         card = (i, '\u2663')
  107.         deck.append(card)
  108.  
  109.     return deck
  110.  
  111.  
  112. def shuffle_deck(deck):
  113.     #Shuffle deck
  114.     random.shuffle(deck)
  115.  
  116.  
  117. def value_of_card(card):
  118.     for card[0] in range(2, 11):
  119.         card_value = card[0]
  120.     if card[0] == 1:
  121.         card_value = 11
  122.     else:
  123.         card_value = 10
  124.  
  125.     return card_value
  126.  
  127.  
  128. def string_of_card(card):
  129.     for card[0] in range(2, 11):
  130.         card_string = card[0] + card[1]
  131.     if card[0] == 1:
  132.         card_string = 'A' + card[1]
  133.     elif card[0] == 11:
  134.         card_string = 'J' + card[1]
  135.     elif card[0] == 12:
  136.         card_string = 'Q' + card[1]
  137.     elif card[0] == 13:
  138.         card_string = 'K' + card[1]
  139.  
  140.     return card_string
  141.  
  142.  
  143. def save(name, money):
  144.     #Open function for writing blackjack.save
  145.     save_info = open('blackjack.save', 'w')
  146.  
  147.     #Write name and money into the file
  148.     save_info.write(str(name) + '\n')
  149.     save_info.write(str(money) + '\n')
  150.  
  151.     #Close function for writing blackjack.save
  152.     save_info.close()
  153.  
  154.  
  155. def restore():
  156.     #Initialize variable
  157.     restore_list = []
  158.  
  159.     #Check if blackjack.save exists
  160.     try:
  161.         #Open file for reading blackjack.save
  162.         read_info = open('blackjack.save', 'r')
  163.  
  164.         #Read the lines of text
  165.         name_in_file = read_info.readline()
  166.         money_in_file = read_info.readline()
  167.  
  168.         #Strip \n from the text lines
  169.         name = name_in_file.rstrip('\n')
  170.         money = money_in_file.rstrip('\n')
  171.  
  172.         #Close function for reading blackjack.save
  173.         read_info.close()
  174.  
  175.     except FileNotFoundError:
  176.         name = ""
  177.         money = -1
  178.  
  179.     #Add name and money to restore_list so they can be returned
  180.     restore_list.append(name)
  181.     restore_list.append(money)
  182.  
  183.     return restore_list
  184.  
  185.  
  186. def input_bet(bet, money):
  187.     while True:
  188.         #Ask for bet
  189.         bet_check = input("Bet? (0 to quit, Enter to stay at $" + str(bet) + ") ")
  190.  
  191.         #Check if player hit Enter
  192.         if bet_check == "":
  193.             #Check whether or not player has enough money in account to continue
  194.             #with the same bet
  195.             if bet < money:
  196.                 break
  197.             elif bet > money:
  198.                 print("You cannot bet more than $" + str(money))
  199.  
  200.         else:
  201.             #Check if input was integer
  202.             try:
  203.                 bet_check = int(bet_check)
  204.                 #Check if they want to quit
  205.                 if bet_check == 0:
  206.                     return 0
  207.                 #Check that bet check is not negative
  208.                 elif bet_check < 0:
  209.                     print("You cannot bet a negative amount")
  210.                 #Check that bet is less than amount in account
  211.                 else:
  212.                     if bet_check > money:
  213.                         print("You cannot bet more than $" + str(money))
  214.                     else:
  215.                         bet = bet_check
  216.                         break
  217.             #If input was not an integer
  218.             except ValueError:
  219.                 #Output error if input was a float
  220.                 try:
  221.                     bet_check = float(bet_check)
  222.                     print("You must bet a whole number")
  223.                 #Output error if input was not a number
  224.                 except ValueError:
  225.                     print("You must type a number")
  226.  
  227.     return bet
  228.  
  229.  
  230. def play_hand(name, bet):
  231.  
  232.     deck = new_deck()
  233.     shuffle_deck(deck)
  234.     card_value = value_of_card(card)
  235.     card_string = string_of_card(card)
  236.  
  237.     #Deal intial cards for dealer and player
  238.     card_1 = deck.pop[0]
  239.     print("Dealer received " + str(string_of_card(card_1)))
  240.     card_2 = deck.pop[0]
  241.     print(name + " received " + str(string_of_card(card_2)))
  242.     card_3 = deck.pop[0]
  243.     print(name + " received " + str(string_of_card(card_3)))
  244.  
  245.     #Calculate totals for dealer and player
  246.     dealer_total = value_of_card(card_1)
  247.     print("Dealer total: " + str(dealer_total))
  248.     player_total = value_of_card(card_2) + value_of_card(card_3)
  249.     print(name + " total: " + str(player_total))
  250.  
  251.     #Initialize counter variables
  252.     dealer_counter = 0
  253.     player_counter = 0
  254.  
  255.     #Check if player total is 21
  256.     if player_total == 21:
  257.         #Continue picking cards for dealer if player gets 21
  258.         while dealer_counter == 0:
  259.             dealer_card = deck.pop[0]
  260.             dealer_total += value_of_card(dealer_card)
  261.             print("Dealer received " + str(string_of_card(dealer_card)))
  262.             print("Dealer total: " + str(dealer_total))
  263.             print(name + " total: " + str(player_total))
  264.             print()
  265.             if dealer_total >= 17:
  266.                 dealer_counter = 1
  267.         #Output results for dealer ending less than 21
  268.         if dealer_total < 21:
  269.             print(name + " wins")
  270.             winnings = bet
  271.             return winnings
  272.         #Output results for dealer tie with player
  273.         elif dealer_total == 21:
  274.             print("Push")
  275.             winnings = 0
  276.             return winnings
  277.         #Output results for dealer bust
  278.         else:
  279.             print("Dealer bust")
  280.             winnings = bet
  281.             return winnings
  282.  
  283.     #Check if player total is less than 21
  284.     elif player_total < 21:
  285.         #Ask player what move they want to make
  286.         while player_counter == 0:
  287.             move = input("Move? (h/s) ")
  288.             print()
  289.             #Continue dealing cards to player and outputting results for hit
  290.             if move == 'h':
  291.                 player_card = deck.pop(0)
  292.                 player_total += value_of_card(player_card)
  293.                 print(name + " received " + str(string_of_card(player_card)))
  294.                 print("Dealer total: " + str(dealer_total))
  295.                 print(name + " total: " + str(player_total))
  296.                 print()
  297.                 if player_total >= 21:
  298.                     player_counter = 1
  299.             #Stop dealing cards to player for stay
  300.             elif move == 's':
  301.                 player_counter = 1
  302.  
  303.         #Check if player does not bust
  304.         if player_total <= 21:
  305.             #Continue picking cards for dealer if player stays
  306.             while dealer_counter == 0:
  307.                 dealer_card = deck.pop[0]
  308.                 dealer_total += value_of_card(dealer_card)
  309.                 print("Dealer received " + str(string_of_card(dealer_card)))
  310.                 print("Dealer total: " + str(dealer_total))
  311.                 print(name + " total: " + str(player_total))
  312.                 print()
  313.  
  314.                 #Pick cards for dealer while his total is less than 17
  315.                 if dealer_total >= 17:
  316.                     dealer_counter = 1
  317.  
  318.             #Check if dealer ends less than 21
  319.             if dealer_total < 21:
  320.                 #Compare dealer total to player total and determine winner
  321.                 if dealer_total < player_total:
  322.                     print(name + " wins")
  323.                     winnings = bet
  324.                     return winnings
  325.                 elif dealer_total > player_total:
  326.                     print("Dealer wins")
  327.                     winnings = -bet
  328.                     return winnings
  329.                 else:
  330.                     print("Push")
  331.                     winnings = 0
  332.                     return winnings
  333.             #Check if dealer ends at 21
  334.             elif dealer_total == 21:
  335.                 #Compare dealer total to player total and determine winner
  336.                 if dealer_total == player_total:
  337.                     print("Push")
  338.                     winnings = 0
  339.                     return winnings
  340.                 else:
  341.                     print("Dealer wins")
  342.                     winnings = -bet
  343.                     return winnings
  344.             #Check if dealer busts
  345.             else:
  346.                 print("Dealer bust")
  347.                 winnings = bet
  348.                 return winnings
  349.  
  350.         #Check if player busts after move
  351.         elif player_total > 21:
  352.             print(name + " bust")
  353.             winnings = -bet
  354.             return winnings
  355.  
  356.     #Check if player busts after initial pick
  357.     else:
  358.         print(name + " bust")
  359.         winnings = -bet
  360.         return winnings
  361.  
  362. main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top