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) discinput = lambda m: discordInput(interaction, m)
discoutput = lambda m: discordOutput(interaction, m) discoutput = lambda m: discordOutput(interaction, m)
await interaction.response.send_message("Let's play Black Jack!") 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): async def discordInput(interaction: discord.Interaction, message:str):
response = HitOrStand() response = HitOrStand()
@@ -107,7 +106,6 @@ async def bal(interaction: discord.Interaction):
""" """
#### Calculator #### #### Calculator ####
""" """
@tree.command(description="Do Meth") @tree.command(description="Do Meth")
async def calculator(interaction: discord.Interaction): async def calculator(interaction: discord.Interaction):
await interaction.response.defer() await interaction.response.defer()

View File

@@ -1,6 +1,12 @@
import random import random
from Ledger import Ledger 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 # Game States
INIT = 0 INIT = 0
PLAYERTURN = 1 PLAYERTURN = 1
@@ -112,8 +118,11 @@ class Hand():
def sortHand(self): def sortHand(self):
self.hand.sort() self.hand.sort()
def add_to_hand(self, card): def add_to_hand(self, card, index = None):
self.hand.append(card) if index is None:
self.hand.append(card)
else:
self.hand.insert(index, card)
def remove_from_hand(self, index): def remove_from_hand(self, index):
return self.hand.pop(index) return self.hand.pop(index)
@@ -121,7 +130,7 @@ class Hand():
def hide_card(self, index): def hide_card(self, index):
card = self.remove_from_hand(index) card = self.remove_from_hand(index)
card.turn_card() card.turn_card()
self.add_to_hand(card) self.add_to_hand(card, index)
def __len__(self): def __len__(self):
return len(self.hand) return len(self.hand)
@@ -146,14 +155,15 @@ class Hand():
value += card.value value += card.value
# Dynamically add ace value based on ideal rules # Dynamically add ace value based on ideal rules
card = 11 maxAceValue = aces * 11
for _ in range(aces): tempValue = value + maxAceValue
if tempValue < 21:
return str(tempValue)
while (tempValue > 21) and aces > 0:
tempValue = tempValue - 10
aces = aces - 1
if value <= 10: value = tempValue
card = 11
if value > 10:
card = 1
value += card
return str(value) return str(value)
@@ -198,12 +208,15 @@ class BlackJack:
await send(string) await send(string)
async def play_game(self, recv, send): async def play_game(self, ID, bet, recv, send):
gameState = INIT gameState = INIT
while gameState != OVER: while gameState != OVER:
if gameState == INIT: 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() self.game_setup()
gameState = PLAYERTURN gameState = PLAYERTURN
@@ -214,6 +227,7 @@ class BlackJack:
if playerHit: if playerHit:
self.deal_card(self.playerHand) self.deal_card(self.playerHand)
else: else:
#TODO Rename hide_card function
self.dealerHand.hide_card(1) self.dealerHand.hide_card(1)
gameState = DEALERTURN gameState = DEALERTURN
@@ -224,6 +238,9 @@ class BlackJack:
if gameState == DEALERTURN: if gameState == DEALERTURN:
if int(repr(self.dealerHand)) > int(repr(self.playerHand)):
gameState = FINISHED
continue
self.deal_card(self.dealerHand) self.deal_card(self.dealerHand)
if int(repr(self.dealerHand)) >= 17: if int(repr(self.dealerHand)) >= 17:
gameState = FINISHED gameState = FINISHED
@@ -263,31 +280,43 @@ class BlackJack:
await send("You tied") await send("You tied")
elif playerBlackJack: elif playerBlackJack:
await send("You reached BlackJack!") await send("You reached BlackJack!")
win = (2*bet, 1, 0, ID)
self.ledger.update(ID, win)
elif dealerBlackJack: elif dealerBlackJack:
await send("The dealer reached BlackJack!") await send("The dealer reached BlackJack!")
loss = (-bet, 0, 1, ID)
self.ledger.update(ID, loss)
elif playerBust: elif playerBust:
await send("You busted!") await send("You busted!")
loss = (-bet, 0, 1, ID)
self.ledger.update(ID, loss)
elif dealerBust: elif dealerBust:
await send("The dealer busted!") await send("The dealer busted!")
win = (2*bet, 1, 0, ID)
self.ledger.update(ID, win)
elif playerHigher: elif playerHigher:
await send("You won!") await send("You won!")
win = (2*bet, 1, 0, ID)
self.ledger.update(ID, win)
elif dealerHigher: elif dealerHigher:
await send("You lost!") await send("You lost!")
loss = (-bet, 0, 1, ID)
self.ledger.update(ID, loss)
else: else:
await send("Report this game to my creator!") await send("Report this game to my creator!")
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()
await send("Everyday I'm shuffling") await send("Everyday I'm shuffling")
gameState = OVER gameState = OVER
def main(): def main():
game = BlackJack() game = BlackJack()
trueFalseInput = lambda x: input(x) == "h" trueFalseInput = lambda x: input(x) == "h"
game.play_game(recv=trueFalseInput, send=print) game.play_game(recv=trueFalseInput, send=print)
pass
if __name__ == "__main__": if __name__ == "__main__":
main() main()

BIN
ledger.db

Binary file not shown.