Implemented card counting. Refactored blackjack code a bit.

Removed gamepicker references
This commit is contained in:
2025-08-13 14:45:57 +10:00
parent 48f9389100
commit d743e5610a
3 changed files with 39 additions and 15 deletions

BIN
ledger.db

Binary file not shown.

View File

@@ -4,7 +4,6 @@ import sqlite3
from discord import app_commands
from BlackJack import BlackJack
from Calculator import Calculator
from Gamepicker import pick_game
botIntents = discord.Intents.all()

View File

@@ -30,6 +30,17 @@ class Card():
self.suit = suit
self.hidden = False
if self.value in range(2,7):
self.count_value = 1
elif self.value in range(7,10):
self.count_value = 0
else:
self.count_value = -1
def hide_card(self):
self.hidden = True
@@ -53,7 +64,7 @@ class Card():
strValue = str(self.value)
string = ""
string = f"{suits[self.suit]}{strValue}"
string = f"{suits[self.suit]}{strValue}: ({self.count_value})"
if self.hidden:
string = "??"
return string
@@ -146,12 +157,12 @@ class Hand():
print("Card not hidden")
def show_hand(self):
extra_count = 0
for card in self.hidden_cards:
card.show_card()
extra_count += card.count_value
self.hidden_cards.remove(card)
def __len__(self):
return len(self.hand)
return extra_count
def __str__(self) -> str:
string = ""
@@ -165,6 +176,8 @@ class Hand():
aces = 0
# Add static values
for card in self.hand:
if card.hidden:
continue
if card.value == 1:
aces += 1
continue
@@ -193,6 +206,9 @@ class Hand():
def __next__(self):
return next(self.iter)
def __len__(self):
return len(self.hand)
class BlackJack:
def __init__(self) -> None:
self.deck = Deck()
@@ -200,19 +216,28 @@ class BlackJack:
self.dealerHand = Hand()
self.ledger = Ledger()
self.playing = False
self.count = 0
def deal_card(self, hand):
card = self.deck.take_from_deck()
hand.add_to_hand(card)
self.count += card.count_value
def deal_hidden_card(self, hand):
hand.add_to_hand(self.deck.take_from_deck())
# Hide one of the dealers cards
self.dealerHand.hide_card(1)
def game_setup(self):
# Deal cards in alternate order
self.deck.shuffle()
for _ in range(2):
for i in range(2):
self.deal_card(self.playerHand)
if i == 0:
self.deal_card(self.dealerHand)
else:
self.deal_hidden_card(self.dealerHand)
# Hide one of the dealers cards
self.dealerHand.hide_card(1)
def discard_hand(self, hand):
hand.show_hand()
@@ -220,11 +245,9 @@ class BlackJack:
card = hand.remove_from_hand(0)
self.deck.addToDiscard(card)
async def show_cards(self, send, displayDealerScore=False):
async def show_cards(self, send):
# Show Cards to player
string = f"Player Hand = {repr(self.playerHand)}: {self.playerHand}\nDealer Hand = ??: {self.dealerHand}"
if displayDealerScore:
string = f"Player Hand = {repr(self.playerHand)}: {self.playerHand}\nDealer Hand = {repr(self.dealerHand)}: {self.dealerHand}"
string = f"Player Hand = {repr(self.playerHand)}: {self.playerHand}\nDealer Hand = {repr(self.dealerHand)}: {self.dealerHand}\nCount = {self.count}"
await send(string)
@@ -263,6 +286,8 @@ class BlackJack:
if gameState == DEALERTURN:
extra_count = self.dealerHand.show_hand()
self.count += extra_count
if int(repr(self.dealerHand)) > int(repr(self.playerHand)):
gameState = FINISHED
continue
@@ -272,7 +297,6 @@ class BlackJack:
if gameState == FINISHED:
self.dealerHand.show_hand()
playerBlackJack = False
dealerBlackJack = False
playerBust = False
@@ -282,7 +306,7 @@ class BlackJack:
tie = False
playerScore = int(repr(self.playerHand))
dealerScore = int(repr(self.dealerHand))
await self.show_cards(send, True)
await self.show_cards(send)
if playerScore == 21:
playerBlackJack = True
if dealerScore == 21:
@@ -334,6 +358,7 @@ class BlackJack:
if len(self.deck) < 0.25 * 51:
self.deck.return_all_from_discard()
self.deck.shuffle()
self.count = 0
await send("Everyday I'm shuffling")
gameState = OVER
self.playing = False