BlackJack 2 Progress

This commit is contained in:
2023-07-19 09:55:54 +10:00
parent 50251c3f1d
commit 72aa847663
3 changed files with 96 additions and 18 deletions

View File

@@ -133,7 +133,7 @@ class BlackJack:
def dealerHitLogic(self): def dealerHitLogic(self):
total = getHandTotal(self.dealerHand) total = getHandTotal(self.dealerHand)
if total > 17: if total >= 17:
self.stand() self.stand()
return True return True
else: else:

View File

@@ -1,4 +1,5 @@
import random import random
from Ledger import Ledger
""" """
Class which describes playing cards Class which describes playing cards
@@ -15,6 +16,17 @@ class Card():
def __init__(self, value, suit) -> None: def __init__(self, value, suit) -> None:
self.value = value self.value = value
self.suit = suit self.suit = suit
self.hidden = False
def turn_card(self):
# XOR to flip hidden element
self.hidden = self.hidden != True
def get_value(self):
return self.value
def get_suit(self):
return self.suit
def __str__(self) -> str: def __str__(self) -> str:
suits = ["J", "", "", "", ""] suits = ["J", "", "", "", ""]
@@ -27,28 +39,36 @@ class Card():
strValue = str(self.value) strValue = str(self.value)
string = "" string = ""
string.format("%s %s", suits[self.suit], strValue) string = f"{suits[self.suit]}{strValue}"
if self.hidden:
string = "??"
return string return string
def __repr__(self) -> str:
return str(self)
""" """
Class for interacting with a deck of cards Class for interacting with a deck of cards
""" """
class Deck(): class Deck():
def __init__(self) -> None: def __init__(self, joker = False) -> None:
self.deck = [] self.deck = []
self.discard = [] self.discard = []
self.joker = False self.joker = joker
if self.joker:
self.deck = [x for x in range(0, 52)] self.deck.extend([Card(x, Card.HEARTS) for x in range(1, 13)])
else: self.deck.extend([Card(x, Card.DIAMONDS) for x in range(1, 13)])
self.deck = [x for x in range(0, 51)] self.deck.extend([Card(x, Card.SPADES) for x in range(1, 13)])
self.deck.extend([Card(x, Card.CLUBS) for x in range(1, 13)])
if joker:
self.deck.append(Card(0, 0))
def shuffle(self): def shuffle(self):
random.shuffle(self.deck) random.shuffle(self.deck)
def sort(self):
self.deck.sort()
def take_from_deck(self): def take_from_deck(self):
card = self.deck.pop() card = self.deck.pop()
return card return card
@@ -68,7 +88,7 @@ class Deck():
def __str__(self) -> str: def __str__(self) -> str:
string = "" string = ""
for card in self.deck: for card in self.deck:
string += convertNumberToCard(card) string += str(card)
return string return string
class Hand(): class Hand():
@@ -78,12 +98,70 @@ class Hand():
def sortHand(self): def sortHand(self):
self.hand.sort() self.hand.sort()
def addToHand(self, card): def add_to_hand(self, card):
self.hand.append(card) self.hand.append(card)
def removeFromHand(self, index): def remove_from_hand(self, index):
card = self.hand.remove(index) return self.hand.pop(index)
return card
def hide_card(self, index):
card = self.remove_from_hand(0)
card.turn_card()
self.add_to_hand(card)
def __str__(self) -> str:
string = ""
for card in self.hand:
string += str(card)
string += " "
return string
def __iter__(self):
self.iter = iter(self.hand)
return self.iter
def __next__(self):
return next(self.iter)
class BlackJack:
def __init__(self) -> None:
self.deck = Deck()
self.playerHand = Hand()
self.dealerHand = Hand()
self.ledger = Ledger()
def deal_card(self, hand):
hand.add_to_hand(self.deck.take_from_deck())
def game_setup(self):
# Deal cards in alternate order
self.deck.shuffle()
for _ in range(2):
self.deal_card(self.playerHand)
self.deal_card(self.dealerHand)
# Hide one of the dealers cards
self.dealerHand.hide_card(1)
def play_game(self):
# Load player statistics
self.game_setup()
# Show Cards to player
print("Player Hand:", end=" ")
print(self.playerHand)
print("Dealer Hand:", end=" ")
print(self.dealerHand)
def main():
game = BlackJack()
game.play_game()
if __name__ == "__main__":
main()