Blackjack is a state machine
This commit is contained in:
@@ -85,7 +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(interaction.user.id, interaction.message, discinput, discoutput)
|
||||
await blackJack.play_game(interaction.user.id, 100, discinput, discoutput)
|
||||
|
||||
async def discordInput(interaction: discord.Interaction, message:str):
|
||||
response = HitOrStand()
|
||||
|
||||
93
BlackJack.py
93
BlackJack.py
@@ -1,6 +1,16 @@
|
||||
import random
|
||||
import Ledger
|
||||
|
||||
# Game States
|
||||
INIT = 0
|
||||
PLAYING = 1
|
||||
ENDING = 2
|
||||
FINISHED = 3
|
||||
OVER = 5
|
||||
CHECKING = 6
|
||||
|
||||
|
||||
|
||||
def convertNumberToCard(cardNumber):
|
||||
# Cards index from 0 i.e ace is 0, and the highest card value is 51
|
||||
if cardNumber <= 12:
|
||||
@@ -49,13 +59,21 @@ def handNumbersToCards(hand):
|
||||
def getHandTotal(hand):
|
||||
cardValue = lambda c: min(c % 13 + 1, 10)
|
||||
i = 0
|
||||
aces = 0
|
||||
for card in hand:
|
||||
card = cardValue(card)
|
||||
if (card == 1) and (i < 20):
|
||||
if card == 1:
|
||||
aces += 1
|
||||
else:
|
||||
i += card
|
||||
|
||||
while aces > 0:
|
||||
if i < 20:
|
||||
card = 11
|
||||
if (i + card) > 21:
|
||||
card = 1
|
||||
i += card
|
||||
aces -= 1
|
||||
return i
|
||||
|
||||
def showDeck(deck):
|
||||
@@ -66,6 +84,7 @@ def showDeck(deck):
|
||||
|
||||
class BlackJack:
|
||||
def __init__(self):
|
||||
self.gameState = INIT
|
||||
self.playerTurn = True
|
||||
self.deck = generateDeck()
|
||||
random.shuffle(self.deck)
|
||||
@@ -118,11 +137,12 @@ class BlackJack:
|
||||
else:
|
||||
return "c"
|
||||
|
||||
def dealerHitLogic(self, hand):
|
||||
total = getHandTotal()
|
||||
def dealerHitLogic(self):
|
||||
total = getHandTotal(self.dealerHand)
|
||||
if total > 17:
|
||||
self.stand()
|
||||
elif total <= 16:
|
||||
return True
|
||||
else:
|
||||
self.hit()
|
||||
|
||||
|
||||
@@ -134,24 +154,34 @@ class BlackJack:
|
||||
gameOver = True
|
||||
return gameOver
|
||||
|
||||
def compareHands(self):
|
||||
dealerTotal = getHandTotal(self.dealerHand)
|
||||
playerTotal = getHandTotal(self.playerHand)
|
||||
if dealerTotal < playerTotal:
|
||||
return "l"
|
||||
else:
|
||||
return "w"
|
||||
|
||||
async def play_game(self, ID, bet, recv, send):
|
||||
|
||||
while self.gameState != OVER:
|
||||
|
||||
if self.gameState == INIT:
|
||||
|
||||
gameOver = False
|
||||
playerStats = self.ledger.read(ID)
|
||||
if playerStats is None:
|
||||
self.ledger.write(ID)
|
||||
playerStats = self.ledger.read(ID)
|
||||
|
||||
self.deal()
|
||||
while not gameOver:
|
||||
|
||||
play = None
|
||||
playerStood = False
|
||||
dealerStood = False
|
||||
validInput = False
|
||||
self.gameState = PLAYING
|
||||
|
||||
playerWinState = self.checkHandState(self.playerHand)
|
||||
dealerWinState = self.checkHandState(self.dealerHand)
|
||||
gameOver = self.checkGameOver(playerWinState) or self.checkGameOver(dealerWinState)
|
||||
if gameOver:
|
||||
continue
|
||||
elif self.gameState == PLAYING:
|
||||
|
||||
await send("Players hand = " + str(getHandTotal(self.playerHand)) + ": " + handNumbersToCards(
|
||||
self.playerHand) + "\n" + "Dealers hand = ??: " + convertNumberToCard(self.dealerHand[0]) + "??")
|
||||
@@ -162,28 +192,39 @@ class BlackJack:
|
||||
validInput = True
|
||||
elif play == "s":
|
||||
self.stand()
|
||||
validInput = True
|
||||
playerStood = True
|
||||
validInput = True
|
||||
|
||||
self.dealerHitLogic()
|
||||
playerWinState = self.checkHandState(self.playerHand)
|
||||
if self.checkGameOver(playerWinState):
|
||||
continue
|
||||
dealerWinState = self.checkHandState(self.dealerHand)
|
||||
self.gameState = CHECKING
|
||||
|
||||
# Dealers turn
|
||||
if playerStood:
|
||||
while dealerWinState == 'c':
|
||||
# Dealer hits
|
||||
self.hit()
|
||||
# Player stands
|
||||
elif self.gameState == ENDING:
|
||||
|
||||
# Players turn
|
||||
self.stand()
|
||||
# Dealers turn
|
||||
dealerStood = self.dealerHitLogic()
|
||||
# Loop ends when game is over
|
||||
dealerWinState = self.checkHandState(self.dealerHand)
|
||||
else:
|
||||
self.hit()
|
||||
dealerWinState = self.checkHandState(self.dealerHand)
|
||||
if dealerStood:
|
||||
|
||||
if self.checkGameOver(dealerWinState):
|
||||
continue
|
||||
if dealerWinState == "c":
|
||||
|
||||
dealerWinState = self.compareHands()
|
||||
self.gameState = CHECKING
|
||||
|
||||
elif self.gameState == CHECKING:
|
||||
|
||||
if self.checkGameOver(playerWinState) or self.checkGameOver(dealerWinState):
|
||||
self.gameState = FINISHED
|
||||
elif playerStood:
|
||||
self.gameState = ENDING
|
||||
else:
|
||||
self.gameState = PLAYING
|
||||
|
||||
elif self.gameState == FINISHED:
|
||||
|
||||
await send("Players hand = " + str(getHandTotal(self.playerHand)) + ": " + handNumbersToCards(self.playerHand) +
|
||||
"\n" + "Dealers hand = " + str(getHandTotal(self.dealerHand)) + ": " + handNumbersToCards(self.dealerHand))
|
||||
@@ -208,3 +249,5 @@ class BlackJack:
|
||||
if len(self.deck) < 0.25 * 52:
|
||||
await send("Shuffling Deck")
|
||||
self.shuffle()
|
||||
|
||||
self.gameState = OVER
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user