Last active
March 22, 2017 01:08
-
-
Save qianguigui1104/c10d90cd8eb2fc4439664a47b9c9f0db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Pam Qian Project Blackjack | |
# Use of class in Python | |
# Played by two players | |
import random | |
class Card: # Create the class | |
def __init__(self, card_num): | |
self.card_num = card_num | |
self.face_value = self.card_num % 13 + 1 # Calculate the face value | |
self.suit = self.card_num // 13 # Calculate the suit | |
if self.suit == 0: | |
self.suit = "♣" | |
elif self.suit == 1: | |
self.suit = "♦" | |
elif self.suit == 2: | |
self.suit = "♥" | |
else: | |
self.suit = "♠" | |
self.bj_value = self.face_value # Calculate the Blackjack value | |
if self.face_value > 10: | |
self.bj_value = 10 | |
elif self.face_value == 1: | |
value_of_ace = ask_ace() | |
self.bj_value = value_of_ace | |
else: | |
self.bj_value = self.face_value | |
def __str__(self): # Print out the card | |
return("You draw a " + str(self.face_value) + self.suit) | |
class Player: # Player class | |
def __init__(self, name): | |
self.name = name | |
self.score = 0 | |
def main(): | |
instruction() # Function gives instruction | |
players = get_players() # Ask for players for their name | |
deck = make_a_deck() # Function creates a deck of card | |
for i in range (2): # Use a for loop to make players take turns | |
player_num = i % 2 | |
print ("\n") | |
print ("===[Currenet player: " + players[player_num].name + "]===") | |
cards,deck = deal_a_hand(deck) | |
display_hand(cards) | |
total = 0 | |
keepGoing = "hit" | |
while (keepGoing == "hit" and total < 21): | |
total = hand_value(cards) # Calculate the hand | |
players[player_num].score = total | |
bust = adjust_for_bust(total, keepGoing) # if busts | |
if bust != "stay": | |
keepGoing = input("hit or stay? ") # If wnats more cards | |
if keepGoing == "hit": | |
cards, deck = turn(cards, deck) | |
display_hand(cards) | |
winner = is_winner(players) | |
def instruction(): | |
print("Hello guys! Welcome to Blackjack!") | |
print("\n") | |
print("===[Instructions]===") | |
print("Blackjack, also known as twenty-one, is a card game" | |
"played between a player and a dealer.") # From Wikipedia | |
print("The objective of the game is to get 21 points or to get a number" | |
"that is as close to 21 as you can.") | |
print("After drawing the first card, you can choose to hit or stay to see" | |
"if you want more cards. ") | |
input("Press enter when you are ready. ") | |
print("\n") | |
def get_players(): | |
name = input ("Player 1, name: ") | |
player1 = Player(name) # Send player's name to the Player class | |
name = input("Player 2, name: ") | |
player2 = Player(name) | |
players = [player1, player2] # Create a list of the players | |
return (players) | |
def make_a_deck(): # Code from Professor Nanette | |
NUM_OF_CARDS_IN_DECK=52 | |
deck = [] # Create the list of deck | |
while (len(deck)<NUM_OF_CARDS_IN_DECK): | |
num = random.randrange(NUM_OF_CARDS_IN_DECK) | |
if(num not in deck): | |
deck.append(num) | |
return (deck) | |
def deal_a_hand(deck): | |
cards = [] # Create a list of hand | |
cards.append(Card(deck.pop())) # Add the drawn to the hand list, | |
cards.append(Card(deck.pop())) # and reomove it from the deck list | |
return (cards, deck) | |
def display_hand(cards): | |
i = 0 | |
while (i < len(cards)): | |
print(cards[i]) # Print the cards in hand | |
i += 1 | |
def ask_ace(): # Ask for low or hight for Ace | |
value_of_ace = int(input("Do you want the Ace to be 1 or 11? ")) | |
return(value_of_ace) | |
def turn(cards, deck): | |
cards.append(Card(deck.pop())) # If hits, add the new card in hand list, | |
return(cards, deck) # and reomove it from the deck list. | |
def hand_value(cards): # Calculate the value of all cards in hand | |
total = 0 | |
for card in cards: | |
total += card.bj_value | |
print("Value of all your cards: " + str(total)) | |
return (total) | |
def adjust_for_bust(total, keepGoing): # See if hand is more than 21 | |
if total > 21: | |
print ("Bust!") | |
keepGoing = "stay" | |
return (keepGoing) | |
def is_winner(players): # Determine the winner | |
if (players[0].score <= 21 and players[1].score <= 21): | |
if (players[0].score < players[1].score): | |
print(players[1].name + ", you won.") | |
elif (players[0].score > players[1].score): | |
print(players[0].name + ", you won.") | |
else: | |
print("You tie!") | |
elif (players[0].score >= 21 and players[1].score >= 21): | |
print("You both lost!") | |
elif(players[0].score >= 21 and players[1].score <= 21): | |
print(players[1].name + ", you won!") | |
elif(players[0].score <= 21 and players[1].score >= 21): | |
print(players[0].name + ", you won!") | |
# Call Main | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment