BlackJack 2 Progress
This commit is contained in:
@@ -133,7 +133,7 @@ class BlackJack:
|
||||
|
||||
def dealerHitLogic(self):
|
||||
total = getHandTotal(self.dealerHand)
|
||||
if total > 17:
|
||||
if total >= 17:
|
||||
self.stand()
|
||||
return True
|
||||
else:
|
||||
|
||||
112
BlackJack2.py
112
BlackJack2.py
@@ -1,4 +1,5 @@
|
||||
import random
|
||||
from Ledger import Ledger
|
||||
|
||||
"""
|
||||
Class which describes playing cards
|
||||
@@ -15,6 +16,17 @@ class Card():
|
||||
def __init__(self, value, suit) -> None:
|
||||
self.value = value
|
||||
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:
|
||||
suits = ["J", "♥", "♦", "♠", "♣"]
|
||||
@@ -27,27 +39,35 @@ class Card():
|
||||
strValue = str(self.value)
|
||||
|
||||
string = ""
|
||||
string.format("%s %s", suits[self.suit], strValue)
|
||||
string = f"{suits[self.suit]}{strValue}"
|
||||
if self.hidden:
|
||||
string = "??"
|
||||
return string
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return str(self)
|
||||
"""
|
||||
Class for interacting with a deck of cards
|
||||
"""
|
||||
class Deck():
|
||||
def __init__(self) -> None:
|
||||
def __init__(self, joker = False) -> None:
|
||||
self.deck = []
|
||||
self.discard = []
|
||||
self.joker = False
|
||||
if self.joker:
|
||||
self.deck = [x for x in range(0, 52)]
|
||||
else:
|
||||
self.deck = [x for x in range(0, 51)]
|
||||
self.joker = joker
|
||||
|
||||
self.deck.extend([Card(x, Card.HEARTS) for x in range(1, 13)])
|
||||
self.deck.extend([Card(x, Card.DIAMONDS) for x in range(1, 13)])
|
||||
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):
|
||||
random.shuffle(self.deck)
|
||||
|
||||
def sort(self):
|
||||
self.deck.sort()
|
||||
|
||||
def take_from_deck(self):
|
||||
card = self.deck.pop()
|
||||
@@ -68,7 +88,7 @@ class Deck():
|
||||
def __str__(self) -> str:
|
||||
string = ""
|
||||
for card in self.deck:
|
||||
string += convertNumberToCard(card)
|
||||
string += str(card)
|
||||
return string
|
||||
|
||||
class Hand():
|
||||
@@ -78,12 +98,70 @@ class Hand():
|
||||
def sortHand(self):
|
||||
self.hand.sort()
|
||||
|
||||
def addToHand(self, card):
|
||||
def add_to_hand(self, card):
|
||||
self.hand.append(card)
|
||||
|
||||
def removeFromHand(self, index):
|
||||
card = self.hand.remove(index)
|
||||
return card
|
||||
def remove_from_hand(self, index):
|
||||
return self.hand.pop(index)
|
||||
|
||||
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()
|
||||
|
||||
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user