neleabels

Python version of 1973 BLAKJAK (101 Basic Computer Games)

May 31st, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.58 KB | None | 0 0
  1. ## Blakjak - rewritten from "BLAKJAK" in 101 Basic Computer Games
  2.  
  3. # This game was originally published in the seminal anthology '101 Computer Games
  4. # in Basic' in 1973. The original source code is a Gordian knot of jumbled
  5. # spaghetti code which proved to be mostly unsalvagable so that I chose to use the
  6. # basic game algorithm but preferred to completely rewrite the game from scratch,
  7. # following the sample game in the book. (May 2020, Nele Abels)
  8. # (Creative commons license: CC BY-SA)
  9.  
  10. import random
  11. import sys
  12.  
  13. # carddeck[] represents a deck of playing cards: Two to ten, Jack, Queen, King, Ace.
  14. # The deck is used for counting which cards have been drawn by pick_a_card() and
  15. # may contain the numbers 0 to 4. (e.g. all the four aces have been drawn or no 2)
  16.  
  17. cardnames = ["two","three","four","five","six","seven","eight","nine","ten",
  18.              "Jack","Queen","King","Ace"]
  19. carddeck = [0,0,0,0,0,0,0,0,0,0,0,0,0]
  20.  
  21. # money - the player starts with this hefty sum of hard earned dollars
  22.  
  23. money = 500
  24.  
  25. # ask_yes_or_no() - get Userinput, only "y" and "n" accepted,return 0 or 1
  26.  
  27. def ask_yes_or_no():
  28.     ein="."
  29.     while "yn".find(ein)<0:
  30.         ein=input("(y/n) ").lower()
  31.     if ein=="y":
  32.         return 1
  33.     else:
  34.         return 0
  35.  
  36. # pick_a_card() - draws a random card from the pack, checks before if all colours
  37. # of the card have been drawn, prints the card, returns its value.
  38. # The global values list counts the number of a specific type of cards (e.g. Queens).
  39. # The routine does not differentiate between colours.
  40.  
  41. def pick_a_card():
  42.     global carddeck
  43.     card_ok = 0
  44.     while not card_ok:
  45.         pick = random.randint(0,12)
  46.         if carddeck[pick] < 4:
  47.             card_ok = 1
  48.     print(cardnames[pick],end="")
  49.     carddeck[pick] = carddeck[pick] + 1
  50.     if pick < 9:
  51.         value = pick + 2            # numbers
  52.     elif pick == 12:
  53.         value = 11                  # Ace
  54.     else:
  55.         value = 10                  # Jack, Queen, King
  56.     return value
  57.  
  58. # enter_wager() - inputs wager from the keyboard, checks type and sum
  59.  
  60. def enter_wager():
  61.     fertig = 0
  62.     while fertig == 0:
  63.         correctinput = 0
  64.         while correctinput == 0:  
  65.             print("How much money to you want to bet? ",end="")
  66.             try:
  67.                 ein = int(input())
  68.                 correctinput = 1
  69.             except:
  70.                 print("Please enter a positive, rounded value (e.g. 200).")
  71.         if ein<1:
  72.             print("Please...")
  73.         elif ein>money:
  74.             print("Sorry, the bank does not give credit.")
  75.         else:
  76.             fertig = 1
  77.     return ein
  78.          
  79. # game_round() - play one round of blackjack until player or dealer are busted
  80. # or one of them wins the game. Return endresult: 0 - dealer has won, 1 - player has won
  81.  
  82. def game_round(playerpoints, dealerpoints):
  83.     round_over = 0
  84.     player_stopped_hitting = 0
  85.     endresult = 0
  86.    
  87.     while round_over == 0:
  88.         print("You have {} showing.".format(playerpoints))
  89.         print("The dealer has {} showing.".format(dealerpoints))
  90.        
  91.         # check if player or dealer have already a 21
  92.        
  93.         if playerpoints == 21:
  94.             print("You have a blackjack!")
  95.             round_over=1
  96.             endresult = 1
  97.         elif dealerpoints == 21:
  98.             print("The dealer has a blackjack!")
  99.             round_over = 1
  100.             endresult = 0
  101.         else:
  102.        
  103.             # If the player has chosen to stop, the routine does not ask again
  104.        
  105.             if round_over == 0 and player_stopped_hitting == 0:
  106.                 print("Do you want a hit? ",end="")
  107.                 if ask_yes_or_no() == 1:
  108.                     player_stopped_hitting = 0
  109.                 else:
  110.                     player_stopped_hitting = 1
  111.                 if player_stopped_hitting == 0:
  112.                     print("You draw a card: ",end="")
  113.                     playerpoints = playerpoints + pick_a_card()
  114.                     print("\n")
  115.                     if playerpoints > 21:
  116.                         print("You busted!")
  117.                         round_over = 1
  118.                         endresult = 0
  119.                     elif playerpoints == 21:
  120.                         print("Blackjack!")
  121.                         round_over = 1
  122.                         endresult = 1
  123.                        
  124.             # The dealer only hits if he has under 17 points and the game is not over, yet
  125.            
  126.            
  127.             if round_over == 0 and (dealerpoints<17 or playerpoints>dealerpoints):
  128.                 print("The dealer draws a card: ",end="")
  129.                 dealerpoints = dealerpoints + pick_a_card()
  130.                 print("\n")
  131.                 if dealerpoints > 21:
  132.                     print("The dealer busted!")
  133.                     round_over = 1
  134.                     endresult = 1
  135.                 elif dealerpoints == 21:
  136.                     print("Blackjack!")
  137.                     round_over = 1
  138.                     endresult = 0
  139.                    
  140.             # Both players have stopped hitting - compare results
  141.            
  142.             if round_over == 0 and dealerpoints > 16 and player_stopped_hitting == 1:
  143.                 if playerpoints > dealerpoints:
  144.                     print("You have won.")
  145.                     round_over = 1
  146.                     endresult = 1
  147.                 elif playerpoints < dealerpoints:
  148.                     print("The dealer has won.")
  149.                     round_over = 1
  150.                     endresult = 0
  151.                 else:
  152.                     print("It's a draw. Tough luck, the bank gets the money.")
  153.                     round_over = 1
  154.                     endresult = 0            
  155.     return endresult
  156.                    
  157. # main routine
  158.  
  159. print("\n\nWelcome to BLJACK EDUSYSTEM 30 - 2020 Python edition!\n")
  160. print("Welcome to Digital Edusystem Computer Blackjack.\n")
  161. print("Your dealer tonight is Petey P. Eight")
  162. print("Watch him closely... he has a reputation for dealing")
  163. print("off the bottom of his deck.\n")
  164. print("Questions requiring a yes or no answer must be answered with")
  165. print("'y' for yes and 'n' for no.\n")
  166. print("You start playing with {}$. Use it carefully!".format(money))
  167.  
  168.  
  169. another_game = 1
  170. while another_game:
  171.    
  172.     print("\nThe dealer shuffles a new deck.\n")
  173.     for x in range(0,13):
  174.         carddeck[x] = 0
  175.    
  176.     # How much money does the player want to invest?
  177.    
  178.     print("You have got {}$ in your wallet.".format(money))
  179.     wager = enter_wager()
  180.     print("You are betting {}$\n".format(wager))
  181.    
  182.     # Player draws first two cards
  183.    
  184.     player_points = 0
  185.     print("You draw a ",end="")
  186.     player_points = player_points + pick_a_card()
  187.     print(" and then you draw a ",end="")
  188.     player_points = player_points + pick_a_card()
  189.     print(".")
  190.    
  191.     # Dealer draws first two cards
  192.    
  193.     dealer_points = 0
  194.     print("The dealer draws a ",end="")
  195.     dealer_points = dealer_points + pick_a_card()
  196.     print(" and then he draws a ",end="")
  197.     dealer_points = dealer_points + pick_a_card()
  198.     print(".")
  199.    
  200.     # Play the round till the bitter end
  201.    
  202.     if game_round(player_points, dealer_points) == 1:
  203.         money = money + wager
  204.         print("\nYay, you have won. Now you own {}".format(money))
  205.     else:
  206.         money = money - wager
  207.         print("\nYou have lost your money...")
  208.     print("Now you own {}".format(money))
  209.  
  210.     # Another round?
  211.    
  212.     print("Would you like to play another round? ",end="")
  213.     another_game = ask_yes_or_no()
  214.    
  215. print("\nThanks for playing, goodbye!")
Add Comment
Please, Sign In to add comment