Advertisement
oanap123

Activity5

Jul 12th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.57 KB | None | 0 0
  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 $" + str(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 string_of_hand(hand):
  88.     hand_string = ''
  89.  
  90.     for i in range(1, len(hand)):
  91.         hand_string += string_of_card(hand[i])
  92.  
  93.     return hand_string
  94.  
  95.  
  96. def value_of_hand(hand):
  97.     hand_value = 0
  98.  
  99.     for i in range(1, len(hand)):
  100.         hand_value += value_of_hand(hand[i])
  101.  
  102.     return hand_value
  103.  
  104.  
  105.  
  106. def new_deck():
  107.     #Initialize variables
  108.     deck = []
  109.     card = ()
  110.  
  111.     #Create 13 cards in spades
  112.     for i in range(1, 14):
  113.         card = (i, '\u2660')
  114.         deck.append(card)
  115.     #Create 13 cards in hearts
  116.     for i in range(1, 14):
  117.         card = (i, '\u2661')
  118.         deck.append(card)
  119.     #Create 13 cards in diamonds
  120.     for i in range(1, 14):
  121.         card = (i, '\u2662')
  122.         deck.append(card)
  123.     #Create 13 cards in clubs
  124.     for i in range(1, 14):
  125.         card = (i, '\u2663')
  126.         deck.append(card)
  127.  
  128.     return deck
  129.  
  130.  
  131. def shuffle_deck(deck):
  132.     #Shuffle deck
  133.     random.shuffle(deck)
  134.  
  135.  
  136. def value_of_card(card):
  137.     if card[0] >= 2 and card[0] <= 10:
  138.         card_value = card[0]
  139.     elif card[0] == 1:
  140.         card_value = 11
  141.     else:
  142.         card_value = 10
  143.  
  144.     return card_value
  145.  
  146.  
  147. def string_of_card(card):
  148.     if card[0] >= 2 and card[0] <= 10:
  149.         card_string = str(card[0]) + card[1]
  150.     elif card[0] == 1:
  151.         card_string = 'A' + card[1]
  152.     elif card[0] == 11:
  153.         card_string = 'J' + card[1]
  154.     elif card[0] == 12:
  155.         card_string = 'Q' + card[1]
  156.     elif card[0] == 13:
  157.         card_string = 'K' + card[1]
  158.  
  159.     return card_string
  160.  
  161.  
  162. def save(name, money):
  163.     #Open function for writing blackjack.save
  164.     save_info = open('blackjack.save', 'w')
  165.  
  166.     #Write name and money into the file
  167.     save_info.write(str(name) + '\n')
  168.     save_info.write(str(money) + '\n')
  169.  
  170.     #Close function for writing blackjack.save
  171.     save_info.close()
  172.  
  173.  
  174. def restore():
  175.     #Initialize variable
  176.     restore_list = []
  177.  
  178.     #Check if blackjack.save exists
  179.     try:
  180.         #Open file for reading blackjack.save
  181.         read_info = open('blackjack.save', 'r')
  182.  
  183.         #Read the lines of text
  184.         name_in_file = read_info.readline()
  185.         money_in_file = read_info.readline()
  186.  
  187.         #Strip \n from the text lines
  188.         name = name_in_file.rstrip('\n')
  189.         money = int(money_in_file.rstrip('\n'))
  190.  
  191.         #Close function for reading blackjack.save
  192.         read_info.close()
  193.  
  194.     except FileNotFoundError:
  195.         name = ""
  196.         money = -1
  197.  
  198.     #Add name and money to restore_list so they can be returned
  199.     restore_list.append(name)
  200.     restore_list.append(money)
  201.  
  202.     return restore_list
  203.  
  204.  
  205. def input_bet(bet, money):
  206.     while True:
  207.         #Ask for bet
  208.         bet_check = input("Bet? (0 to quit, Enter to stay at $" + str(bet) + ") ")
  209.  
  210.         #Check if player hit Enter
  211.         if bet_check == "":
  212.             #Check whether or not player has enough money in account to continue
  213.             #with the same bet
  214.             if bet < money:
  215.                 break
  216.             elif bet > money:
  217.                 print("You cannot bet more than $" + str(money))
  218.  
  219.         else:
  220.             #Check if input was integer
  221.             try:
  222.                 bet_check = int(bet_check)
  223.                 #Check if they want to quit
  224.                 if bet_check == 0:
  225.                     return 0
  226.                 #Check that bet check is not negative
  227.                 elif bet_check < 0:
  228.                     print("You cannot bet a negative amount")
  229.                 #Check that bet is less than amount in account
  230.                 else:
  231.                     if bet_check > money:
  232.                         print("You cannot bet more than $" + str(money))
  233.                     else:
  234.                         bet = bet_check
  235.                         break
  236.             #If input was not an integer
  237.             except ValueError:
  238.                 #Output error if input was a float
  239.                 try:
  240.                     bet_check = float(bet_check)
  241.                     print("You must bet a whole number")
  242.                 #Output error if input was not a number
  243.                 except ValueError:
  244.                     print("You must type a number")
  245.  
  246.     return bet
  247.  
  248.  
  249. def play_hand(name, bet):
  250.  
  251.     player_hand = []
  252.     dealer_hand = []
  253.     dealer_hand_string = ''
  254.     player_hand_string = ''
  255.     dealer_hand_value = 0
  256.     player_hand_value = 0
  257.  
  258.     deck = new_deck()
  259.     shuffle_deck(deck)
  260.  
  261.     print("Bet: " + str(bet))
  262.  
  263.     #Deal intial cards for dealer and player
  264.     card_1 = deck.pop()
  265.     dealer_hand.append(card_1)
  266.     dealer_hand_string = string_of_hand(dealer_hand)
  267.     dealer_hand_value = value_of_hand(dealer_hand)
  268.     print("Dealer's hand: " + str(dealer_hand_string))
  269.     print("Value: " + str(dealer_hand_value))
  270.     card_2 = deck.pop()
  271.     card_3 = deck.pop()
  272.     player_hand.append(card_2)
  273.     player_hand.append(card_3)
  274.     player_hand_string = string_of_hand(player_hand)
  275.     player_hand_value = value_of_hand(player_hand)
  276.     print(name + "'s hand: " + str(player_hand_string)
  277.     print("Value: " + str(player_hand_value))
  278.  
  279.     #Initialize counter variables
  280.     dealer_counter = 0
  281.     player_counter = 0
  282.  
  283.     #Check if player total is 21
  284.     if player_hand_value == 21:
  285.         #Continue picking cards for dealer if player gets 21
  286.         while dealer_counter == 0:
  287.             dealer_card = deck.pop()
  288.             dealer_hand.append(dealer_card)
  289.             dealer_hand_string = string_of_hand(dealer_card)
  290.             dealer_hand_value = value_of_hand(dealer_card)
  291.             print("Bet: " + str(bet))
  292.             print("Dealer's hand: " + str(dealer_hand_string))
  293.             print("Value: " + str(dealer_hand_value))
  294.             print(name + "'s hand: " + str(player_hand_string)
  295.             print("Value: " + str(player_hand_value))
  296.             print()
  297.             if dealer_hand_value >= 17:
  298.                 dealer_counter = 1
  299.         #Output results for dealer ending less than 21
  300.         if dealer_hand_value < 21:
  301.             print(name + " wins")
  302.             winnings = bet
  303.             return winnings
  304.         #Output results for dealer tie with player
  305.         elif dealer_hand_value == 21:
  306.             print("Push")
  307.             winnings = 0
  308.             return winnings
  309.         #Output results for dealer bust
  310.         else:
  311.             print("Dealer bust")
  312.             winnings = bet
  313.             return winnings
  314.  
  315.     #Check if player total is less than 21
  316.     elif player_hand_value < 21:
  317.         #Ask player what move they want to make
  318.         while player_counter == 0:
  319.             move = input("Move? (h/s) ")
  320.             print()
  321.             #Continue dealing cards to player and outputting results for hit
  322.             if move == 'h':
  323.                 player_card = deck.pop()
  324.                 player_hand.append(player_card)
  325.                 player_hand_string = string_of_hand(player_hand)
  326.                 player_hand_value = value_of_hand(player_hand)
  327.                 print("Bet: " + str(bet))
  328.                 print("Dealer's hand: " + str(dealer_hand_string))
  329.                 print("Value: " + str(dealer_hand_value))
  330.                 print(name + "'s hand: " + str(player_hand_string)
  331.                 print("Value: " + str(player_hand_value))
  332.                 print()
  333.                 if player_hand_value >= 21:
  334.                     player_counter = 1
  335.             #Stop dealing cards to player for stay
  336.             elif move == 's':
  337.                 player_counter = 1
  338.  
  339.         #Check if player does not bust
  340.         if player_total <= 21:
  341.             #Continue picking cards for dealer if player stays
  342.             while dealer_counter == 0:
  343.                 dealer_card = deck.pop()
  344.                 dealer_hand.append(dealer_card)
  345.                 dealer_hand_string = string_of_hand(dealer_card)
  346.                 dealer_hand_value = value_of_hand(dealer_card)
  347.                 print("Bet: " + str(bet))
  348.                 print("Dealer's hand: " + str(dealer_hand_string))
  349.                 print("Value: " + str(dealer_hand_value))
  350.                 print(name + "'s hand: " + str(player_hand_string)
  351.                 print("Value: " + str(player_hand_value))
  352.                 print()
  353.  
  354.                 #Pick cards for dealer while his total is less than 17
  355.                 if dealer_hand_value >= 17:
  356.                     dealer_counter = 1
  357.  
  358.             #Check if dealer ends less than 21
  359.             if dealer_hand_value < 21:
  360.                 #Compare dealer total to player total and determine winner
  361.                 if dealer_hand_value < player_hand_value:
  362.                     print(name + " wins")
  363.                     winnings = bet
  364.                     return winnings
  365.                 elif dealer_hand_value > player_hand_value:
  366.                     print("Dealer wins")
  367.                     winnings = -bet
  368.                     return winnings
  369.                 else:
  370.                     print("Push")
  371.                     winnings = 0
  372.                     return winnings
  373.             #Check if dealer ends at 21
  374.             elif dealer_hand_value == 21:
  375.                 #Compare dealer total to player total and determine winner
  376.                 if dealer_hand_value == player_hand_value:
  377.                     print("Push")
  378.                     winnings = 0
  379.                     return winnings
  380.                 else:
  381.                     print("Dealer wins")
  382.                     winnings = -bet
  383.                     return winnings
  384.             #Check if dealer busts
  385.             else:
  386.                 print("Dealer bust")
  387.                 winnings = bet
  388.                 return winnings
  389.  
  390.         #Check if player busts after move
  391.         elif player_hand_value > 21:
  392.             print(name + " bust")
  393.             winnings = -bet
  394.             return winnings
  395.  
  396.     #Check if player busts after initial pick
  397.     else:
  398.         print(name + " bust")
  399.         winnings = -bet
  400.         return winnings
  401.  
  402. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement