Last active
August 14, 2020 06:43
-
-
Save lkam88/bd0ebdd4f71a53f4939ca96c1e996733 to your computer and use it in GitHub Desktop.
A simple Blackjack game written in Python.
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
import random | |
from time import sleep | |
class Game(object): | |
def __init__(self, deck, players): | |
self.deck = deck | |
self.players = players | |
self.dealer = Dealer() | |
self.players.append(self.dealer) | |
def play(self): | |
# deal everyone two cards | |
for player in players: | |
card = self.deck.draw() | |
player.deal(card) | |
for player in players: | |
card = self.deck.draw() | |
player.deal(card) | |
print(f"Dealer's revealed card is: {self.dealer.hand[0]}") | |
# each player plays | |
for player in players: | |
print(f"{player.name}'s turn") | |
print(f"{player.name}'s hand: {player.hand}") | |
print(f"{player.name}'s total: {player.total()}") | |
print("Do you wish to hit?") | |
while player.hits(): | |
card = self.deck.draw() | |
player.deal(card) | |
print(f"{player.name} draws a: {card}") | |
if player.total() > 21: | |
print(f"{player.name} busted!") | |
break | |
print(f"{player.name}'s Total: {player.total()}") | |
print("Do you wish to hit?") | |
print("============") | |
# score each player against dealer | |
print(f"Dealer's total is: {self.dealer.total()}") | |
for player in players: | |
if player == self.dealer: | |
continue | |
print(f"{player.name}: {player.total()}") | |
if player.total() > 21: | |
print(f"{player.name} loses") | |
elif self.dealer.total() > 21: | |
print(f"{player.name} wins") | |
elif player.total() == self.dealer.total(): | |
print(f"{player.name} pushes") | |
elif player.total() > self.dealer.total(): | |
print(f"{player.name} wins") | |
else: | |
print(f"{player.name} loses") | |
class Player(object): | |
def __init__(self, name): | |
self.name = name | |
self.hand = [] | |
def deal(self, card): | |
self.hand.append(card) | |
def total(self): | |
total = 0 | |
for card in self.hand: | |
total += card | |
if total > 21: | |
for card in self.hand: | |
if card == 11: | |
total -= 10 | |
if total <= 21: | |
break | |
return total | |
def hits(self): | |
player_input = input() | |
return player_input.upper() == "Y" | |
class Dealer(Player): | |
def __init__(self): | |
super().__init__("Dealer") | |
def hits(self): | |
sleep(2) | |
if self.total() < 17: | |
print("Dealer hits") | |
return True | |
else: | |
print("Dealer stands") | |
return False | |
class Deck(object): | |
def __init__(self): | |
cards = [ | |
2, 2, 2, 2, | |
3, 3, 3, 3, | |
4, 4, 4, 4, | |
5, 5, 5, 5, | |
6, 6, 6, 6, | |
7, 7, 7, 7, | |
8, 8, 8, 8, | |
9, 9, 9, 9, | |
10, 10, 10, 10, | |
10, 10, 10, 10, | |
10, 10, 10, 10, | |
10, 10, 10, 10, | |
11, 11, 11, 11, | |
] | |
random.shuffle(cards) | |
self.cards = cards | |
def draw(self): | |
return self.cards.pop() | |
if __name__ == '__main__': | |
deck = Deck() | |
players = [(Player("Player 1")), (Player("Player 2"))] | |
game = Game(deck, players) | |
game.play() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment