Blackjack is done :)

This commit is contained in:
2023-07-25 12:55:48 +10:00
parent 464c23b502
commit ba808a5bf6
4 changed files with 46 additions and 19 deletions

View File

@@ -85,8 +85,7 @@ async def bj(interaction: discord.Interaction):
discinput = lambda m: discordInput(interaction, m)
discoutput = lambda m: discordOutput(interaction, m)
await interaction.response.send_message("Let's play Black Jack!")
await blackJack.play_game(discinput, discoutput)
# await blackJack.play_game(interaction.user.id, 100, discinput, discoutput)
await blackJack.play_game(interaction.user.id, 100, discinput, discoutput)
async def discordInput(interaction: discord.Interaction, message:str):
response = HitOrStand()
@@ -107,7 +106,6 @@ async def bal(interaction: discord.Interaction):
"""
#### Calculator ####
"""
@tree.command(description="Do Meth")
async def calculator(interaction: discord.Interaction):
await interaction.response.defer()

View File

@@ -1,6 +1,12 @@
import random
from Ledger import Ledger
#TODO Kill Benson
#TODO Starting a game with 21 fucks with hidden cards
#TODO Look into 5card jack thingo that Tim mentioned
#TODO Insurance - If dealer starts with 21, you can bet against it and win your money back
#TODO Make each game session more distinct (Embeds?)
# Game States
INIT = 0
PLAYERTURN = 1
@@ -112,8 +118,11 @@ class Hand():
def sortHand(self):
self.hand.sort()
def add_to_hand(self, card):
def add_to_hand(self, card, index = None):
if index is None:
self.hand.append(card)
else:
self.hand.insert(index, card)
def remove_from_hand(self, index):
return self.hand.pop(index)
@@ -121,7 +130,7 @@ class Hand():
def hide_card(self, index):
card = self.remove_from_hand(index)
card.turn_card()
self.add_to_hand(card)
self.add_to_hand(card, index)
def __len__(self):
return len(self.hand)
@@ -146,14 +155,15 @@ class Hand():
value += card.value
# Dynamically add ace value based on ideal rules
card = 11
for _ in range(aces):
maxAceValue = aces * 11
tempValue = value + maxAceValue
if tempValue < 21:
return str(tempValue)
while (tempValue > 21) and aces > 0:
tempValue = tempValue - 10
aces = aces - 1
if value <= 10:
card = 11
if value > 10:
card = 1
value += card
value = tempValue
return str(value)
@@ -198,12 +208,15 @@ class BlackJack:
await send(string)
async def play_game(self, recv, send):
async def play_game(self, ID, bet, recv, send):
gameState = INIT
while gameState != OVER:
if gameState == INIT:
# TODO: Load player statistics
playerStats = self.ledger.read(ID)
if playerStats is None:
self.ledger.write(ID)
playerStats = self.ledger.read(ID)
self.game_setup()
gameState = PLAYERTURN
@@ -214,6 +227,7 @@ class BlackJack:
if playerHit:
self.deal_card(self.playerHand)
else:
#TODO Rename hide_card function
self.dealerHand.hide_card(1)
gameState = DEALERTURN
@@ -224,6 +238,9 @@ class BlackJack:
if gameState == DEALERTURN:
if int(repr(self.dealerHand)) > int(repr(self.playerHand)):
gameState = FINISHED
continue
self.deal_card(self.dealerHand)
if int(repr(self.dealerHand)) >= 17:
gameState = FINISHED
@@ -263,31 +280,43 @@ class BlackJack:
await send("You tied")
elif playerBlackJack:
await send("You reached BlackJack!")
win = (2*bet, 1, 0, ID)
self.ledger.update(ID, win)
elif dealerBlackJack:
await send("The dealer reached BlackJack!")
loss = (-bet, 0, 1, ID)
self.ledger.update(ID, loss)
elif playerBust:
await send("You busted!")
loss = (-bet, 0, 1, ID)
self.ledger.update(ID, loss)
elif dealerBust:
await send("The dealer busted!")
win = (2*bet, 1, 0, ID)
self.ledger.update(ID, win)
elif playerHigher:
await send("You won!")
win = (2*bet, 1, 0, ID)
self.ledger.update(ID, win)
elif dealerHigher:
await send("You lost!")
loss = (-bet, 0, 1, ID)
self.ledger.update(ID, loss)
else:
await send("Report this game to my creator!")
if len(self.deck) < 0.25 * 51:
self.deck.return_all_from_discard()
self.deck.shuffle()
await send("Everyday I'm shuffling")
gameState = OVER
def main():
game = BlackJack()
trueFalseInput = lambda x: input(x) == "h"
game.play_game(recv=trueFalseInput, send=print)
pass
if __name__ == "__main__":
main()

BIN
ledger.db

Binary file not shown.