Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Blakjak - rewritten from "BLAKJAK" in 101 Basic Computer Games
- # This game was originally published in the seminal anthology '101 Computer Games
- # in Basic' in 1973. The original source code is a Gordian knot of jumbled
- # spaghetti code which proved to be mostly unsalvagable so that I chose to use the
- # basic game algorithm but preferred to completely rewrite the game from scratch,
- # following the sample game in the book. (May 2020, Nele Abels)
- # (Creative commons license: CC BY-SA)
- import random
- import sys
- # carddeck[] represents a deck of playing cards: Two to ten, Jack, Queen, King, Ace.
- # The deck is used for counting which cards have been drawn by pick_a_card() and
- # may contain the numbers 0 to 4. (e.g. all the four aces have been drawn or no 2)
- cardnames = ["two","three","four","five","six","seven","eight","nine","ten",
- "Jack","Queen","King","Ace"]
- carddeck = [0,0,0,0,0,0,0,0,0,0,0,0,0]
- # money - the player starts with this hefty sum of hard earned dollars
- money = 500
- # ask_yes_or_no() - get Userinput, only "y" and "n" accepted,return 0 or 1
- def ask_yes_or_no():
- ein="."
- while "yn".find(ein)<0:
- ein=input("(y/n) ").lower()
- if ein=="y":
- return 1
- else:
- return 0
- # pick_a_card() - draws a random card from the pack, checks before if all colours
- # of the card have been drawn, prints the card, returns its value.
- # The global values list counts the number of a specific type of cards (e.g. Queens).
- # The routine does not differentiate between colours.
- def pick_a_card():
- global carddeck
- card_ok = 0
- while not card_ok:
- pick = random.randint(0,12)
- if carddeck[pick] < 4:
- card_ok = 1
- print(cardnames[pick],end="")
- carddeck[pick] = carddeck[pick] + 1
- if pick < 9:
- value = pick + 2 # numbers
- elif pick == 12:
- value = 11 # Ace
- else:
- value = 10 # Jack, Queen, King
- return value
- # enter_wager() - inputs wager from the keyboard, checks type and sum
- def enter_wager():
- fertig = 0
- while fertig == 0:
- correctinput = 0
- while correctinput == 0:
- print("How much money to you want to bet? ",end="")
- try:
- ein = int(input())
- correctinput = 1
- except:
- print("Please enter a positive, rounded value (e.g. 200).")
- if ein<1:
- print("Please...")
- elif ein>money:
- print("Sorry, the bank does not give credit.")
- else:
- fertig = 1
- return ein
- # game_round() - play one round of blackjack until player or dealer are busted
- # or one of them wins the game. Return endresult: 0 - dealer has won, 1 - player has won
- def game_round(playerpoints, dealerpoints):
- round_over = 0
- player_stopped_hitting = 0
- endresult = 0
- while round_over == 0:
- print("You have {} showing.".format(playerpoints))
- print("The dealer has {} showing.".format(dealerpoints))
- # check if player or dealer have already a 21
- if playerpoints == 21:
- print("You have a blackjack!")
- round_over=1
- endresult = 1
- elif dealerpoints == 21:
- print("The dealer has a blackjack!")
- round_over = 1
- endresult = 0
- else:
- # If the player has chosen to stop, the routine does not ask again
- if round_over == 0 and player_stopped_hitting == 0:
- print("Do you want a hit? ",end="")
- if ask_yes_or_no() == 1:
- player_stopped_hitting = 0
- else:
- player_stopped_hitting = 1
- if player_stopped_hitting == 0:
- print("You draw a card: ",end="")
- playerpoints = playerpoints + pick_a_card()
- print("\n")
- if playerpoints > 21:
- print("You busted!")
- round_over = 1
- endresult = 0
- elif playerpoints == 21:
- print("Blackjack!")
- round_over = 1
- endresult = 1
- # The dealer only hits if he has under 17 points and the game is not over, yet
- if round_over == 0 and (dealerpoints<17 or playerpoints>dealerpoints):
- print("The dealer draws a card: ",end="")
- dealerpoints = dealerpoints + pick_a_card()
- print("\n")
- if dealerpoints > 21:
- print("The dealer busted!")
- round_over = 1
- endresult = 1
- elif dealerpoints == 21:
- print("Blackjack!")
- round_over = 1
- endresult = 0
- # Both players have stopped hitting - compare results
- if round_over == 0 and dealerpoints > 16 and player_stopped_hitting == 1:
- if playerpoints > dealerpoints:
- print("You have won.")
- round_over = 1
- endresult = 1
- elif playerpoints < dealerpoints:
- print("The dealer has won.")
- round_over = 1
- endresult = 0
- else:
- print("It's a draw. Tough luck, the bank gets the money.")
- round_over = 1
- endresult = 0
- return endresult
- # main routine
- print("\n\nWelcome to BLJACK EDUSYSTEM 30 - 2020 Python edition!\n")
- print("Welcome to Digital Edusystem Computer Blackjack.\n")
- print("Your dealer tonight is Petey P. Eight")
- print("Watch him closely... he has a reputation for dealing")
- print("off the bottom of his deck.\n")
- print("Questions requiring a yes or no answer must be answered with")
- print("'y' for yes and 'n' for no.\n")
- print("You start playing with {}$. Use it carefully!".format(money))
- another_game = 1
- while another_game:
- print("\nThe dealer shuffles a new deck.\n")
- for x in range(0,13):
- carddeck[x] = 0
- # How much money does the player want to invest?
- print("You have got {}$ in your wallet.".format(money))
- wager = enter_wager()
- print("You are betting {}$\n".format(wager))
- # Player draws first two cards
- player_points = 0
- print("You draw a ",end="")
- player_points = player_points + pick_a_card()
- print(" and then you draw a ",end="")
- player_points = player_points + pick_a_card()
- print(".")
- # Dealer draws first two cards
- dealer_points = 0
- print("The dealer draws a ",end="")
- dealer_points = dealer_points + pick_a_card()
- print(" and then he draws a ",end="")
- dealer_points = dealer_points + pick_a_card()
- print(".")
- # Play the round till the bitter end
- if game_round(player_points, dealer_points) == 1:
- money = money + wager
- print("\nYay, you have won. Now you own {}".format(money))
- else:
- money = money - wager
- print("\nYou have lost your money...")
- print("Now you own {}".format(money))
- # Another round?
- print("Would you like to play another round? ",end="")
- another_game = ask_yes_or_no()
- print("\nThanks for playing, goodbye!")
Add Comment
Please, Sign In to add comment