From fd204588b7756898f2ad9a269cc2006ed430e3d6 Mon Sep 17 00:00:00 2001 From: BigGamerGary Date: Tue, 22 Nov 2022 11:19:32 +1000 Subject: [PATCH 1/4] Importing Blackjact Bot --- BlackJackBot/BJBot.py | 87 ++++++ BlackJackBot/BlackJack.py | 270 ++++++++++++++++++ .../__pycache__/BlackJack.cpython-39.pyc | Bin 0 -> 6527 bytes BlackJackBot/ledger.txt | 4 + 4 files changed, 361 insertions(+) create mode 100644 BlackJackBot/BJBot.py create mode 100644 BlackJackBot/BlackJack.py create mode 100644 BlackJackBot/__pycache__/BlackJack.cpython-39.pyc create mode 100644 BlackJackBot/ledger.txt diff --git a/BlackJackBot/BJBot.py b/BlackJackBot/BJBot.py new file mode 100644 index 0000000..dc94e74 --- /dev/null +++ b/BlackJackBot/BJBot.py @@ -0,0 +1,87 @@ +from discord import * +from discord.ext import commands + +from BlackJack import * + +client = commands.Bot(command_prefix='~~') + + +@client.event +async def on_ready(): + print("Let's pwn some scrubs") + +@client.command() +async def play_game(ctx, arg): + bj = BlackJack() + gameOver = False + playerStood = False + ID = ctx.message.author.id + IDFound = bj.findID(ID)[0] + if IDFound: + playerStats = bj.readLedger(ID)[1:] + playerStats[0] = ID + else: + bj.addToLedger(ID) + playerStats = (ID, 100, 0, 0) + while not gameOver: + playerWinState = bj.checkHandState(bj.playerHand) + dealerWinState = bj.checkHandState(bj.dealerHand) + gameOver = bj.checkGameOver(playerWinState) + if gameOver: + continue + gameOver = bj.checkGameOver(dealerWinState) + if gameOver: + continue + await ctx.send("Players hand = " + str(getHandTotal(bj.playerHand)) + ": " + handNumbersToCards( + bj.playerHand) + "\n" + "Dealers hand = ??: " + convertNumberToCard(bj.dealerHand[0]) + "??") + + await ctx.send("Hit or Stand? (h/s)") + + def check(m): + return m.content in ["h", "s"] and m.channel == ctx.channel + + msg = await client.wait_for('message', check=check) + if msg == "h": + bj.hit() + elif msg == "s": + bj.stand() + playerStood = True + + + playerWinState = bj.checkHandState(bj.playerHand) + gameOver = bj.checkGameOver(playerWinState) + if gameOver: + continue + # Dealers turn + if playerStood: + while dealerWinState == 'c': + # Dealer hits + bj.hit() + # Player stands + bj.stand() + # Loop ends when game is over + dealerWinState = bj.checkHandState(bj.dealerHand) + else: + bj.hit() + dealerWinState = bj.checkHandState(bj.dealerHand) + + gameOver = bj.checkGameOver(dealerWinState) + if gameOver: + continue + await ctx.send("Players hand = " + str(getHandTotal(bj.playerHand)) + ": " + handNumbersToCards(bj.playerHand) + + "\n" + "Dealers hand = " + str(getHandTotal(bj.dealerHand)) + ": " + handNumbersToCards(bj.dealerHand)) + bet = int(arg) + if playerWinState == "w": + await ctx.send("You won!") + bj.writeLedger(ID, 2*bet, True) + elif playerWinState == "l": + await ctx.send("You busted!") + bj.writeLedger(ID, -bet, False) + elif dealerWinState == "w": + await ctx.send("The Dealer reached 21 before you!") + bj.writeLedger(ID, -bet, False) + elif dealerWinState == "l": + await ctx.send("The Dealer busted before you!") + bj.writeLedger(ID, 2*bet, True) + +client.run('ODU0MjE5MDMzNzc0NTIyMzY4.YMgveA.laeIhhccTDvEbU81Y-RjSEMkaEY') diff --git a/BlackJackBot/BlackJack.py b/BlackJackBot/BlackJack.py new file mode 100644 index 0000000..9b8a2df --- /dev/null +++ b/BlackJackBot/BlackJack.py @@ -0,0 +1,270 @@ +import random + + +def convertNumberToCard(cardNumber): + # Cards index from 0 i.e ace is 0, and the highest card value is 51 + if cardNumber <= 12: + cardSuit = "♣" + elif cardNumber <= 25: + cardSuit = "♦" + elif cardNumber <= 38: + cardSuit = "♥" + elif cardNumber <= 51: + cardSuit = "♠" + + cardNumber = cardNumber % 13 + + if cardNumber == 0: + cardNumber = "A" + elif cardNumber == 10: + cardNumber = "J" + elif cardNumber == 11: + cardNumber = "Q" + elif cardNumber == 12: + cardNumber = "K" + else: + cardNumber += 1 + + return "{value}{suit} ".format(value=str(cardNumber), suit=cardSuit) + + +def generateDeck(): + deck = [] + while len(deck) < 52: + newCard = random.randint(0, 51) + if deck.count(newCard) == 0: + deck.append(newCard) + return deck + + +def addCardToHand(hand, deck): + card = deck.pop(0) + hand.append(card) + + +def generateHand(hand, deck): + i = 0 + while i < 2: + addCardToHand(hand, deck) + i += 1 + + +def handNumbersToCards(hand): + cards = "" + for a in hand: + cards += convertNumberToCard(a) + return cards + + +def getHandTotal(hand): + i = 0 + for card in hand: + card = card % 13 + 1 + if card > 10: + card = 10 + if (card == 1) and (i < 20): + card = 11 + if (i + card) > 21: + card = 1 + i += card + return i + + +class BlackJack: + def __init__(self): + self.playerTurn = True + + self.deck = generateDeck() + self.playerHand = [] + generateHand(self.playerHand, self.deck) + self.dealerHand = [] + generateHand(self.dealerHand, self.deck) + + def getPH(self): + return self.playerHand + + def returnCards(self): + self.playerHand.clear() + self.dealerHand.clear() + + def shuffle(self): + self.returnCards() + self.deck = generateDeck() + generateHand(self.playerHand, self.deck) + generateHand(self.dealerHand, self.deck) + + def getDH(self): + return self.dealerHand + + def stand(self): + if self.playerTurn: + self.playerTurn = False + + def hit(self): + if self.playerTurn: + addCardToHand(self.playerHand, self.deck) + self.playerTurn = False + else: + addCardToHand(self.dealerHand, self.deck) + self.playerTurn = True + + def checkHandState(self, hand): + # Check the hand state and return w if game is won, l if lost and c for continue + total = getHandTotal(hand) + + if total == 21: + return "w" + elif total > 21: + return "l" + else: + return "c" + + def dealerHitLogic(self, hand): + total = getHandTotal() + if total > 17: + self.stand() + elif total <= 16: + self.hit() + + + def checkGameOver(self, state): + gameOver = False + if state == "w": + gameOver = True + elif state == "l": + gameOver = True + return gameOver + + def play_game(self, bet): + validInput = False + gameOver = False + playerStood = False + ID = input("ID: (Unique)") + IDFound = self.findID(ID)[0] + if IDFound: + playerStats = self.readLedger(ID)[1:] + playerStats[0] = ID + else: + self.addToLedger(ID) + playerStats = (ID, 100, 0, 0) + + while not gameOver: + playerWinState = self.checkHandState(self.playerHand) + dealerWinState = self.checkHandState(self.dealerHand) + gameOver = self.checkGameOver(playerWinState) + if gameOver: + continue + gameOver = self.checkGameOver(dealerWinState) + if gameOver: + continue + + print("Players hand = " + str(getHandTotal(self.playerHand)) + ": " + handNumbersToCards( + self.playerHand) + "\n" + "Dealers hand = ??: " + convertNumberToCard(self.dealerHand[0]) + "??") + play = input("Hit or Stand? (h/s)") + while not validInput: + if play == "h": + self.hit() + validInput = True + elif play == "s": + self.stand() + validInput = True + playerStood = True + + playerWinState = self.checkHandState(self.playerHand) + gameOver = self.checkGameOver(playerWinState) + if gameOver: + continue + + # Dealers turn + if playerStood: + while dealerWinState == 'c': + # Dealer hits + self.hit() + # Player stands + self.stand() + # Loop ends when game is over + dealerWinState = self.checkHandState(self.dealerHand) + else: + self.hit() + dealerWinState = self.checkHandState(self.dealerHand) + + gameOver = self.checkGameOver(dealerWinState) + if gameOver: + continue + + print("Players hand = " + str(getHandTotal(self.playerHand)) + ": " + handNumbersToCards(self.playerHand) + + "\n" + "Dealers hand = " + str(getHandTotal(self.dealerHand)) + ": " + handNumbersToCards(self.dealerHand)) + if playerWinState == "w": + print("You won!") + self.writeLedger(ID, 2*bet, True) + elif playerWinState == "l": + print("You busted!") + self.writeLedger(ID, -bet, False) + elif dealerWinState == "w": + print("The Dealer reached 21 before you!") + self.writeLedger(ID, -bet, False) + elif dealerWinState == "l": + print("The Dealer busted before you!") + self.writeLedger(ID, 2*bet, True) + + def findID(self, ID): + with open("ledger.txt", "r") as ledger: + IDFound = False + lineNo = 0 + print(ID) + for line in ledger: + print(lineNo) + splitline = line.split(":") + if splitline[0] == ID: + IDFound = True + break + lineNo += 1 + return IDFound, lineNo + + def readLedger(self, ID): + lineNo = self.findID(ID)[1] + with open("ledger.txt", "r") as ledger: + lines = ledger.readlines() + + line = lines[lineNo] + splitLine = line.split(":") + money, wins, losses = int(splitLine[1]), int( + splitLine[2]), int(splitLine[3]) + return [lines, lineNo, money, wins, losses] + + def writeLedger(self, ID, moneyWon, wonAGame): + lines, lineNo, currentMoney, currentWins, currentLosses = self.readLedger( + ID) + updatedMoney = currentMoney + moneyWon + if wonAGame: + updatedWins = currentWins + 1 + updatedLosses = currentLosses + else: + updatedLosses = currentLosses + 1 + updatedWins = currentWins + + with open("ledger.txt", "w") as ledger: + i = 0 + for line in lines: + if i == lineNo: + ledger.write("{ID}:{Money}:{Wins}:{Losses}\n".format( + ID=ID, + Money=updatedMoney, + Wins=updatedWins, + Losses=updatedLosses)) + else: + ledger.write(line) + i += 1 + + def addToLedger(self, ID, starterCash=100): + with open("ledger.txt", "a") as ledger: + ledger.write("{ID}:{Money}:0:0\n".format(ID=ID, Money=starterCash)) + + +def main(): + bj = BlackJack() + bj.play_game(100) + + +if __name__ == "__main__": + main() diff --git a/BlackJackBot/__pycache__/BlackJack.cpython-39.pyc b/BlackJackBot/__pycache__/BlackJack.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..10a9810878af0c4ef8605a353460554628be7a5f GIT binary patch literal 6527 zcma)A+mBmE89#GwK0dzeHM^Vi=B+}B3#4f&X<6DPO;*V!O-)J`lA0~9*K>B)8}B*0 zbB>e7ITjKwr9xEA69S2cjSxuTFX5TTqdp*bvUo?RLJGfc#`jGmY|Z7H`DW&u?>?uR z%efkUzj^DgAOGqFP5UPSqdx<{d6e*DR9xe%p_MhRbK_I3%(%%dv^uwW3a!B%orWdWrCo#jrOp`TvR+O*omsn>X> zt@*D3$^t6$9H5hc@_=^u6rcq_4A31u4afmB^9I)Q*5Qc5?s(-85nEc1a2eyV}jj_Jf4I*QKde{{Ee6qv^Hp zhs}D_E)?~|k^^Ov1_0Vyf!M1?i4jJksF&SZRq#@CZ_^WHhx#{w_DB;m*zD_b@2+fA z8{Q|?jn$ym-1Gb>+_+qicAA?TtM#xJ2!0{J(1k{|c6%3PGl&#$6~O8Jd&yia@b7pc zQU|OD7jXc|CZtzZ2yPWd9yL2nbFjdWBn3>4I>8BTt6@+VWuLiOV<*l zutl|X(GbtkkR*=PCn+0niwOG;5JLwQamyj%UPOP)#)(Try5!XJ=zutgdR6;q8bmUX zr*|00Vh2u4enKxL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KIh8MsOs~9Og~Q17?CL#3Xgh2F%52=IBzL#ze6{WZVeZH2A8*- O6#n`A&$aXDv;P5~S2z&> literal 0 HcmV?d00001 diff --git a/BlackJackBot/ledger.txt b/BlackJackBot/ledger.txt new file mode 100644 index 0000000..631502d --- /dev/null +++ b/BlackJackBot/ledger.txt @@ -0,0 +1,4 @@ +123672601259147265:100:0:0 +123672601259147265:100:0:0 +123672601259147265:100:0:0 +123672601259147265:100:0:0 From 0d145f03a3bc238f4fab1fe5ae56a2efdb426c43 Mon Sep 17 00:00:00 2001 From: BigGamerGary Date: Tue, 22 Nov 2022 11:30:06 +1000 Subject: [PATCH 2/4] The play_game function takes functions as input --- BlackJackBot/BlackJack.py | 48 +++++++++++++++++++++------------------ BlackJackBot/ledger.txt | 4 ---- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/BlackJackBot/BlackJack.py b/BlackJackBot/BlackJack.py index 9b8a2df..500e066 100644 --- a/BlackJackBot/BlackJack.py +++ b/BlackJackBot/BlackJack.py @@ -71,7 +71,7 @@ def getHandTotal(hand): class BlackJack: - def __init__(self): + def __init__(self, recv, send): self.playerTurn = True self.deck = generateDeck() @@ -80,6 +80,9 @@ class BlackJack: self.dealerHand = [] generateHand(self.dealerHand, self.deck) + self.recv = recv + self.send = send + def getPH(self): return self.playerHand @@ -139,14 +142,14 @@ class BlackJack: validInput = False gameOver = False playerStood = False - ID = input("ID: (Unique)") - IDFound = self.findID(ID)[0] - if IDFound: - playerStats = self.readLedger(ID)[1:] - playerStats[0] = ID - else: - self.addToLedger(ID) - playerStats = (ID, 100, 0, 0) + # ID = self.recv("ID: (Unique)") + # IDFound = self.findID(ID)[0] + # if IDFound: + # playerStats = self.readLedger(ID)[1:] + # playerStats[0] = ID + # else: + # self.addToLedger(ID) + # playerStats = (ID, 100, 0, 0) while not gameOver: playerWinState = self.checkHandState(self.playerHand) @@ -158,9 +161,9 @@ class BlackJack: if gameOver: continue - print("Players hand = " + str(getHandTotal(self.playerHand)) + ": " + handNumbersToCards( + self.send("Players hand = " + str(getHandTotal(self.playerHand)) + ": " + handNumbersToCards( self.playerHand) + "\n" + "Dealers hand = ??: " + convertNumberToCard(self.dealerHand[0]) + "??") - play = input("Hit or Stand? (h/s)") + play = self.recv("Hit or Stand? (h/s)") while not validInput: if play == "h": self.hit() @@ -192,20 +195,20 @@ class BlackJack: if gameOver: continue - print("Players hand = " + str(getHandTotal(self.playerHand)) + ": " + handNumbersToCards(self.playerHand) + + self.send("Players hand = " + str(getHandTotal(self.playerHand)) + ": " + handNumbersToCards(self.playerHand) + "\n" + "Dealers hand = " + str(getHandTotal(self.dealerHand)) + ": " + handNumbersToCards(self.dealerHand)) if playerWinState == "w": - print("You won!") - self.writeLedger(ID, 2*bet, True) + self.send("You won!") + # self.writeLedger(ID, 2*bet, True) elif playerWinState == "l": - print("You busted!") - self.writeLedger(ID, -bet, False) + self.send("You busted!") + # self.writeLedger(ID, -bet, False) elif dealerWinState == "w": - print("The Dealer reached 21 before you!") - self.writeLedger(ID, -bet, False) + self.send("The Dealer reached 21 before you!") + # self.writeLedger(ID, -bet, False) elif dealerWinState == "l": - print("The Dealer busted before you!") - self.writeLedger(ID, 2*bet, True) + self.send("The Dealer busted before you!") + # self.writeLedger(ID, 2*bet, True) def findID(self, ID): with open("ledger.txt", "r") as ledger: @@ -260,9 +263,10 @@ class BlackJack: with open("ledger.txt", "a") as ledger: ledger.write("{ID}:{Money}:0:0\n".format(ID=ID, Money=starterCash)) - def main(): - bj = BlackJack() + terminput = lambda message: input(message) + termoutput = lambda message: print(message) + bj = BlackJack(terminput, termoutput) bj.play_game(100) diff --git a/BlackJackBot/ledger.txt b/BlackJackBot/ledger.txt index 631502d..e69de29 100644 --- a/BlackJackBot/ledger.txt +++ b/BlackJackBot/ledger.txt @@ -1,4 +0,0 @@ -123672601259147265:100:0:0 -123672601259147265:100:0:0 -123672601259147265:100:0:0 -123672601259147265:100:0:0 From 501f73e8c20419634e427eb0397fd556773fb102 Mon Sep 17 00:00:00 2001 From: BigGamerGary Date: Tue, 22 Nov 2022 11:55:25 +1000 Subject: [PATCH 3/4] Gutted BJBot - /bj doesn't work --- Acronymbot.py | 18 ++++++++++++++++-- BlackJackBot/BlackJack.py => BlackJack.py | 1 + BlackJackBot/BJBot.py | 16 ++++++++++++---- .../__pycache__/BlackJack.cpython-310.pyc | Bin 0 -> 6714 bytes __pycache__/BlackJack.cpython-310.pyc | Bin 0 -> 6701 bytes 5 files changed, 29 insertions(+), 6 deletions(-) rename BlackJackBot/BlackJack.py => BlackJack.py (99%) create mode 100644 BlackJackBot/__pycache__/BlackJack.cpython-310.pyc create mode 100644 __pycache__/BlackJack.cpython-310.pyc diff --git a/Acronymbot.py b/Acronymbot.py index 069d021..966a956 100644 --- a/Acronymbot.py +++ b/Acronymbot.py @@ -1,9 +1,9 @@ import discord from discord import app_commands +from BlackJack import BlackJack botIntents = discord.Intents.all() - client = discord.Client(intents=botIntents) tree = app_commands.CommandTree(client) @@ -41,7 +41,21 @@ class Buttons(discord.ui.View): for child in self.children: child.disabled = True await interaction.response.edit_message(content=f"Ouch!", view=self) - +@tree.command(description="Play Black Jack!") +async def bj(interaction: discord.Interaction): + discinput = lambda m: discordInput(interaction, m) + discoutput = lambda m: discordOutput(interaction, m) + blackJack = BlackJack(discinput, discoutput) + blackJack.play_game(100) + +async def discordInput(interaction: discord.Interaction, message:str): + await interaction.response.send_message(message) + def check(m): + return m.content in ["h", "s"] and m.channel == interaction.channel + msg = await client.wait_for('message', check=check) + +async def discordOutput(interaction, message): + await interaction.response.send_message(message) client.run('NzgwNzg4NDIwMjkzMDM0MDA0.GEKkUB.Bbl09D3lWMGea_mcIESPMLUyGlkW-6N53BPFjI') \ No newline at end of file diff --git a/BlackJackBot/BlackJack.py b/BlackJack.py similarity index 99% rename from BlackJackBot/BlackJack.py rename to BlackJack.py index 500e066..a3e5a59 100644 --- a/BlackJackBot/BlackJack.py +++ b/BlackJack.py @@ -263,6 +263,7 @@ class BlackJack: with open("ledger.txt", "a") as ledger: ledger.write("{ID}:{Money}:0:0\n".format(ID=ID, Money=starterCash)) + def main(): terminput = lambda message: input(message) termoutput = lambda message: print(message) diff --git a/BlackJackBot/BJBot.py b/BlackJackBot/BJBot.py index dc94e74..3f00a85 100644 --- a/BlackJackBot/BJBot.py +++ b/BlackJackBot/BJBot.py @@ -1,10 +1,17 @@ -from discord import * -from discord.ext import commands +import discord from BlackJack import * client = commands.Bot(command_prefix='~~') +async def discordInput(interaction: discord.Interaction, message:str): + await interaction.response.send_message(message) + def check(m): + return m.content in ["h", "s"] and m.channel == interaction.channel + msg = await client.wait_for('message', check=check) + +async def discordOutput(ctx, message): + await ctx.send(message) @client.event async def on_ready(): @@ -12,6 +19,8 @@ async def on_ready(): @client.command() async def play_game(ctx, arg): + await ctx.send("Players hand = " + str(getHandTotal(bj.playerHand)) + ": " + handNumbersToCards( + bj.playerHand) + "\n" + "Dealers hand = ??: " + convertNumberToCard(bj.dealerHand[0]) + "??") bj = BlackJack() gameOver = False playerStood = False @@ -32,8 +41,7 @@ async def play_game(ctx, arg): gameOver = bj.checkGameOver(dealerWinState) if gameOver: continue - await ctx.send("Players hand = " + str(getHandTotal(bj.playerHand)) + ": " + handNumbersToCards( - bj.playerHand) + "\n" + "Dealers hand = ??: " + convertNumberToCard(bj.dealerHand[0]) + "??") + await ctx.send("Hit or Stand? (h/s)") diff --git a/BlackJackBot/__pycache__/BlackJack.cpython-310.pyc b/BlackJackBot/__pycache__/BlackJack.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..13c9feb8a4a7e40b5a777fe5c7c091891b029aed GIT binary patch literal 6714 zcma)A+mBPn8TXu)IOZYrhV{BRS8v5QOfrBo8vn{s;#+v-^`i$=DW@J zO)H=GQuv)IMYZe2RO)jA<3AI?TPT4J$(c=~oR+h44p`UE$oZ>=I_$ffDS?&G>rz_iVW-qmO|a?z z473)?36$Vhs3Oe0DpXn;UD4bI$OKf77N9&J8_;&?0CE7O0qvD;SD>HSk?Qh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onrMKEA2+K z>iv-53ab(6y2tkh^*pN6C_xdGn}RfRuIXB?qifgAy8NAT3vRZsOV6%h;_yeO#iGsq zIdqa&A-tfQd>!iYiH_Nn!N~m%;&>WmUqp}Oj0V1^r@K#u;D?)d)9a_iv0Z0maZkkQ zbw3EIxU5FU;YnimjYf5SMOKgIAO`({;ift})~ogAp*I?>TD1`zMJJi^*sT>4-9Z-q zA^U5rCbv>dF!R_&UFDz_rlEPGd0#Ti8K}&Q-jiOZiP}NwCXdX%%JW90eC^k$5%%mZ5 X)7|T4T-!~%=|Um(Blm=xW{&?qXt8AJ literal 0 HcmV?d00001 diff --git a/__pycache__/BlackJack.cpython-310.pyc b/__pycache__/BlackJack.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..2351a17e4063f15699a044042cfa9a64f1dd75c8 GIT binary patch literal 6701 zcma)A+mBPn8TXu~e=BhLAK)6GGDTl1&03YzYvuAz>RD3+j432iA+%UgjJJ zFvn`8O{=OcN+0^thpM2em5}EPHf>c{``M#Mm^UZgg z@0(UW@1^iNQ;KTWi>cJ-1jc_RfVWTriAtuVXr#(1X-MwPjA`(K_#>WI@h+nvye{Mma5K>(%J0KHKL0W+FfNVfJqyxwSlm@gOW@oC)uRZMi ztju;(%{_o}fL@e&Ky!c!fL@g|fUU!AO@96DH^1-g83BLnJv##a(EG*+xYNr6P&{cYj`t{uSe)r)b^Adq-tBRyQ@Hg> zwbAyww}N&(>=sK#Y->c>A_Aauxuw>tVQdDWDj8+3R#kGTy}shBvdjK?OuLs-+pyS! z_o@pQs}27~_2Ocy)?W9UVQ}%BYF$y)b#!iqtF2~n2*2G5FCMR{R`cfiN-HdW=*#wv zi*GlowX0`P4sYCy=WDIzC%y`k?Uq|7upK28@XW5^2v-b>C0t_?gArrSQ1h5tLmB=i z#Jvdg6)-)7*fs^kky6D_+%c$xcOm{Lb;pdvT_Ft!YA+y*kQkdExKRosvX*+UVlB;X zWusTJW3%BmV@Fk+vb7$k+18t3Y}Ztm*x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@sn{ZcS*$gDIM7eF z>sXlT!O= Date: Wed, 23 Nov 2022 21:07:27 +1000 Subject: [PATCH 4/4] Black Jack works --- Acronymbot.py | 10 ++++++---- BlackJack.py | 26 ++++++++------------------ __pycache__/BlackJack.cpython-310.pyc | Bin 6701 -> 6759 bytes 3 files changed, 14 insertions(+), 22 deletions(-) diff --git a/Acronymbot.py b/Acronymbot.py index 966a956..1caa3ca 100644 --- a/Acronymbot.py +++ b/Acronymbot.py @@ -47,15 +47,17 @@ async def bj(interaction: discord.Interaction): discinput = lambda m: discordInput(interaction, m) discoutput = lambda m: discordOutput(interaction, m) blackJack = BlackJack(discinput, discoutput) - blackJack.play_game(100) + await interaction.response.send_message("Let's play Black Jack!") + await blackJack.play_game(100) async def discordInput(interaction: discord.Interaction, message:str): - await interaction.response.send_message(message) + await interaction.followup.send(message) def check(m): return m.content in ["h", "s"] and m.channel == interaction.channel msg = await client.wait_for('message', check=check) + return msg.content -async def discordOutput(interaction, message): - await interaction.response.send_message(message) +async def discordOutput(interaction: discord.Interaction, message): + await interaction.followup.send(message) client.run('NzgwNzg4NDIwMjkzMDM0MDA0.GEKkUB.Bbl09D3lWMGea_mcIESPMLUyGlkW-6N53BPFjI') \ No newline at end of file diff --git a/BlackJack.py b/BlackJack.py index a3e5a59..770fdb1 100644 --- a/BlackJack.py +++ b/BlackJack.py @@ -138,7 +138,7 @@ class BlackJack: gameOver = True return gameOver - def play_game(self, bet): + async def play_game(self, bet): validInput = False gameOver = False playerStood = False @@ -161,9 +161,9 @@ class BlackJack: if gameOver: continue - self.send("Players hand = " + str(getHandTotal(self.playerHand)) + ": " + handNumbersToCards( + await self.send("Players hand = " + str(getHandTotal(self.playerHand)) + ": " + handNumbersToCards( self.playerHand) + "\n" + "Dealers hand = ??: " + convertNumberToCard(self.dealerHand[0]) + "??") - play = self.recv("Hit or Stand? (h/s)") + play = await self.recv("Hit or Stand? (h/s)") while not validInput: if play == "h": self.hit() @@ -195,19 +195,19 @@ class BlackJack: if gameOver: continue - self.send("Players hand = " + str(getHandTotal(self.playerHand)) + ": " + handNumbersToCards(self.playerHand) + + await self.send("Players hand = " + str(getHandTotal(self.playerHand)) + ": " + handNumbersToCards(self.playerHand) + "\n" + "Dealers hand = " + str(getHandTotal(self.dealerHand)) + ": " + handNumbersToCards(self.dealerHand)) if playerWinState == "w": - self.send("You won!") + await self.send("You won!") # self.writeLedger(ID, 2*bet, True) elif playerWinState == "l": - self.send("You busted!") + await self.send("You busted!") # self.writeLedger(ID, -bet, False) elif dealerWinState == "w": - self.send("The Dealer reached 21 before you!") + await self.send("The Dealer reached 21 before you!") # self.writeLedger(ID, -bet, False) elif dealerWinState == "l": - self.send("The Dealer busted before you!") + await self.send("The Dealer busted before you!") # self.writeLedger(ID, 2*bet, True) def findID(self, ID): @@ -263,13 +263,3 @@ class BlackJack: with open("ledger.txt", "a") as ledger: ledger.write("{ID}:{Money}:0:0\n".format(ID=ID, Money=starterCash)) - -def main(): - terminput = lambda message: input(message) - termoutput = lambda message: print(message) - bj = BlackJack(terminput, termoutput) - bj.play_game(100) - - -if __name__ == "__main__": - main() diff --git a/__pycache__/BlackJack.cpython-310.pyc b/__pycache__/BlackJack.cpython-310.pyc index 2351a17e4063f15699a044042cfa9a64f1dd75c8..85398a4291f34c1a1469ba9a86d290b194368ca1 100644 GIT binary patch delta 465 zcmZXRJ4*vW6ou!puWWWUn}-+`8zqQfVUZtTVUY!~(8k6p(`dku-AbG#NntDM)Pg|~ z8w&~kg7p3Zd%@C5w1_9Gi>M11cFuS1ox`2G`g2{hg>VVJhmWK=yceOE8{rg}7$JQs zX~Iw(Whfrs=(ebp=R39{mt0u`5XGZf$JQ7Al71A^-H=GqAe6w6lg@yCofBKJxTS2F zYq4k-%pJfZ-Gm)@Q1ejtpyo3b#852Sf`&=^6c?HJ=gR!P@(>Y>=v-OSL+@EOQRy9j zmUeaQFWT3z@F<65jh|Ge^x=Pk-;jkY9g>i3qD&cpy`-%8hgoUgTgpoR82V0WG+Bxv z%fIPl^73%Ka73*Wtd;fjC|Kd%IMA;_SoFpYtI(`mRvB&-eLJ5%z$)y0s+-k?LblX& an7OIfoGE=(eNW$w^&IL@msa%c%<(63#cwzO delta 359 zcmYk2y-LJD6ou#JXOhkC%qFHV3pVa5x*%5j1~#siik*!P2ntG)LW|kLLJQY!uoO)# z7LwX$(ArWITfqldPlCt}oZ^1x+HCHX9ugtFO(V01yJ?|Edo%yI;-$tdYBmys3 zc2v6{)Ud6JhM;dXjs7XGv!6AcnW9zpyQXnLPUJfbX;e-%HAa1@;OPU>0|QAr+}n+?_B%>J@Hus