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