Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bret's assignment #6

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
# Blackjack

* [Part 1](README_1.md)
* [Part 2](README_2.md)
Players can start the game in their console by typing:
python -m blackjack

Blackjack is a game played between a Dealer and a Player.

1. Cards are dealt from a deck that reshuffles after each hand.
These cards constitute the Hands of each player.
At the start of the Game, 2 cards are dealt to each player (The 1st and 3rd cards off the deck go to the player. The 2nd and 4th cards go to the dealer.)

2. The player's point total is the combined points of his hand. The dealer's point total is the combined points of his hand.

3. Points are ascribed as follows:
The ace is worth 11 points, unless the point total of the hand with an 11-point ace is worth more than 21. In that case, it is worth 1 point.
2-10 are worth the same points as their names
Face cards (Jack, Queen, King) are worth 10 points

4. The player acts first. The Player has the choice to Hit or Stand. Hitting means that another Card is drawn from the deck, the Card is added to the player's hand and the points from that card are added to the point total of the Player's Hand. Stand means that no more cards are drawn, and his point total remains at its current amount. Stay also means that he stays for all future rounds.

5. After the player acts, there should be a win-condition check to see if the player has busted. If the player hit and busted, the dealer wins.

6. If the player has not busted, the Dealer either hits or stays based on its built in hit-or-stay algorithm. If the total points in DealerHand are less than 17, the Dealer will Hit. If the total points in DealerHand are greater than 17, the Dealer will Stay. (If the dealer has an ace and TotalPoints == 17, then the Dealer hits.) If the dealer hits, another card is added to the player's Hand and the hand's point total are increased by the point value of the card card.

7. After Dealer plays, another win-condition check. If the dealer busted, the player wins. If neither player nor dealer busted, whoever's hand is higher wins. A tie is called a push.

8. If the player wins, he wins the amount of his bet. If he loses, he loses the amount of his bet. If it is a push, there is no change to the amount of the player's chips. If the players wins with blackjack, he wins 1.5x the amount of his bet.
Binary file added blackjack/.DS_Store
Binary file not shown.
Empty file added blackjack/__init__.py
Empty file.
185 changes: 185 additions & 0 deletions blackjack/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
from blackjack.card import Card
from blackjack.deck import Deck
from blackjack.hand import Hand
from blackjack.player import Player
from blackjack.dealer import Dealer
from blackjack.game import Game

import blackjack.functions as fun

if __name__ == "__main__":

player = Player()
dealer = Dealer()

print("\n \n")
player.name = input("What's your name? ")
print("")
print("Hi {}, let's play blackjack!".format(player.name))

play_game = True
while play_game:

p_win = False
d_win = False
tie = False
p_bust = False
d_bust = False
p_blackjack = False
d_blackjack = False

while not (p_win or d_win or tie or p_blackjack or d_blackjack):
game = Game()
deck = game.create_and_shuffle_deck()
player_hand = Hand()
dealer_hand = Hand()

print("\n \n")
print("New Hand\n")

while True:
print("You have {} chips.".format(player.chips))
print("What is your bet?")
new_bet = input("> ")
print("")
player.bet = int(new_bet)
if player.bet <= player.chips:
break

player.make_bet()

player_hand.get_card(deck)
dealer_hand.get_card(deck)
player_hand.get_card(deck)
dealer_hand.get_card(deck)

player_hand.value = player_hand.valuation()
dealer_hand.value = dealer_hand.valuation()

print("{}'s hand: {} {}".format(
player.name, player_hand.value, player_hand.cards))
print("Dealer shows: {} [{}]".format(
dealer.shown_value(dealer_hand), dealer.shown(dealer_hand)))
print("")
p_blackjack = game.blackjack_check(player_hand)
d_blackjack = game.blackjack_check(dealer_hand)
if p_blackjack is True:
break

hit_or_stand = 0
while not fun.hit_stand(hit_or_stand):
hit_or_stand = (input("Hit or Stand? ")).upper()

if hit_or_stand == "HIT":
player_hand.get_card(deck)
player_hand.value = player_hand.valuation()
print("")
print("{}".format(player_hand.new_card()))
print("")
print("{} has: {} {}".format(
player.name, player_hand.value, player_hand.cards))
print("Dealer shows: {} [{}]".format(
dealer.shown_value(dealer_hand), dealer.shown(
dealer_hand)))
print("")
p_bust = game.bust_check(player_hand)
if p_bust is True:
break
hit_or_stand = 0
if p_bust is True:
break

print("")
print("Dealer's turn.")
input("Press ENTER to continue. \n")
print("Dealer shows his hole card.")
print("Dealer has: {} {}".format(
dealer_hand.value, dealer_hand.cards))
input("Press ENTER to continue. \n")
print("")

d_blackjack = game.blackjack_check(dealer_hand)
if d_blackjack is True:
break

hit_or_stand = dealer.hit_test(dealer_hand)
while hit_or_stand == "HIT":
print("Dealer hits. \n")
dealer_hand.get_card(deck)
dealer_hand.value = dealer_hand.valuation()
print("{}".format(dealer_hand.new_card()))
print("")
print("{} has: {} {}".format(
player.name, player_hand.value, player_hand.cards))
print("Dealer has: {} {}".format(
dealer_hand.value, dealer_hand.cards))
input("Press ENTER to continue. \n")
d_bust = game.bust_check(dealer_hand)
if d_bust is True:
break
hit_or_stand = dealer.hit_test(dealer_hand)
if d_bust is True:
break

print("Dealer stands.")
input("Press ENTER to continue. \n")
print("Player has: {} {}".format(
player_hand.value, player_hand.cards))
print("Dealer has: {} {}".format(
dealer_hand.value, dealer_hand.cards))

higher_hand = game.higher_hand(player_hand, dealer_hand)
if higher_hand == 'p_hand':
p_win = True
if higher_hand == 'd_hand':
d_win = True
else:
tie = True

if p_blackjack and d_blackjack:
print("Wow. Crap. The dealer also had blackjack.")
print("That's a push.")
player.push()
print("You have {} chips remaining.".format(player.chips))
else:
if p_blackjack:
print("You've got blackjack! You win.")
player.win_blackjack()
print("You have {} chips remaining.".format(player.chips))
if d_blackjack:
print("Dealer has blackjack. Dealer wins.")
print("You have {} chips remaining.".format(player.chips))
elif p_bust:
print("Sorry, you busted! Dealer wins.")
print("You have {} chips remaining.".format(player.chips))
elif d_bust:
print("Dealer busts! You win.")
player.win_bet()
print("You have {} chips remaining.".format(player.chips))
elif p_win:
print("You have the higher hand! You win.")
player.win_bet()
print("You have {} chips remaining.".format(player.chips))
elif d_win:
print("Dealer has the higher hand! Dealer wins.")
print("You have {} chips remaining.".format(player.chips))
elif tie:
print("That's a push.")
player.push()
print("You have {} chips remaining.".format(player.chips))

if player.chips <= 0:
print("You're out of chips! You're done! Go home!")
print("I hope you learned a valuable lesson about gambling.")
exit()

print("")

checker = 1
while checker == 1:
answer = (input("Play again? Y/N: ")).upper()
if answer == "Y":
checker = 0
if answer == "N":
checker = 0
play_game = False
22 changes: 22 additions & 0 deletions blackjack/card.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Card:
"""A playing card.

Responsibilities:

* Has a rank and a suit.
* Cards should have readable representations

Collaborators:

* Collected into a Deck.
* Collected into a Hand for each player and a Hand for the dealer."""

def __init__(self, rank, suit):
self.rank = rank
self.suit = suit

def __str__(self):
return "{} of {}".format(self.rank, self.suit)

def __repr__(self):
return self.__str__()
76 changes: 76 additions & 0 deletions blackjack/dealer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from blackjack.card import Card
from blackjack.deck import Deck
from blackjack.hand import Hand
from blackjack.player import Player


class Dealer:
"""A computer player that uses an algorithm to determine whether to hit
or stay

Responsibilities:
* Stay on 17 (with no Ace in hand)
* Hit on 17 (with Ace in hand)
* Hit on 16 or under
* Stay on 18-21
* Display just the shown card when the dealer's hole card is
still concealed

Collaborators:
* uses the Hand class to determine the value of the hand, on which it
bases its actions"""

def __init__(self):
pass

def hit_test(self, a_hand):
"""algorithm to determine the automated actions of the dealer.
This includes the idea of the dealer hitting on a 'soft' 17"""

value_dict = {'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
'10': 10,
'J': 10,
'Q': 10,
'K': 10,
'A': 11}
hand_ranks = [card.rank for card in a_hand.cards]
values = [value_dict[x] for x in hand_ranks]
value = sum(values)

list_of_a = [card.rank for card in a_hand.cards if card.rank == 'A']
if len(list_of_a) > 0 and value < 21:
if a_hand.value < 18:
return "HIT"
else:
return "STAND"
else:
if a_hand.value < 17:
return "HIT"
else:
return "STAND"

def shown(self, a_hand):
"""displays just 1 card of the dealer's at the start of the hand"""
return a_hand.cards[1]

def shown_value(self, a_hand):
"""calculates the value of just the dealer's starting shown card"""
card = a_hand.cards[1]
new_hand = Hand()
new_hand.cards = [card]
value = new_hand.valuation()
return value

def __str__(self):
return "Dealer"

def __repr__(self):
return self.__str__()
46 changes: 46 additions & 0 deletions blackjack/deck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import random

from blackjack.card import Card


class Deck:
"""A set of 52 playing cards.

Responsibilities:
* hold a collection of cards
* self-generates a list of 52 cards of every variation
* Should be able to (re)shuffle itself
* should be able to report its current size

Collaborators:
* consists of cards"""

def __init__(self):
ranks = ['2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'10',
'J',
'Q',
'K',
'A']
suits = ['Spades', 'Hearts', 'Diamonds', 'Clubs']
self.cards = [Card(rank, suit) for rank in ranks for suit in suits]

def draw(self):
return self.cards.pop()

def shuffle(self):
return random.shuffle(self.cards)

def __str__(self):
card_list = [str(card) for card in self.cards]
return ', '.join(card_list)

def __repr__(self):
return self.__str__()
8 changes: 8 additions & 0 deletions blackjack/functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def hit_stand(x):
"checks to see if input was an acceptable option"
if x == 'HIT':
return True
if x == 'STAND':
return True
else:
return False
Loading