Merge branch 'BlackJackIntegration'
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import discord
|
import discord
|
||||||
from discord import app_commands
|
from discord import app_commands
|
||||||
|
from BlackJack import BlackJack
|
||||||
|
|
||||||
botIntents = discord.Intents.all()
|
botIntents = discord.Intents.all()
|
||||||
|
|
||||||
@@ -40,5 +41,23 @@ class Buttons(discord.ui.View):
|
|||||||
for child in self.children:
|
for child in self.children:
|
||||||
child.disabled = True
|
child.disabled = True
|
||||||
await interaction.response.edit_message(content=f"Ouch!", view=self)
|
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)
|
||||||
|
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.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: discord.Interaction, message):
|
||||||
|
await interaction.followup.send(message)
|
||||||
|
|
||||||
client.run('NzgwNzg4NDIwMjkzMDM0MDA0.GEKkUB.Bbl09D3lWMGea_mcIESPMLUyGlkW-6N53BPFjI')
|
client.run('NzgwNzg4NDIwMjkzMDM0MDA0.GEKkUB.Bbl09D3lWMGea_mcIESPMLUyGlkW-6N53BPFjI')
|
||||||
265
BlackJack.py
Normal file
265
BlackJack.py
Normal file
@@ -0,0 +1,265 @@
|
|||||||
|
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, recv, send):
|
||||||
|
self.playerTurn = True
|
||||||
|
|
||||||
|
self.deck = generateDeck()
|
||||||
|
self.playerHand = []
|
||||||
|
generateHand(self.playerHand, self.deck)
|
||||||
|
self.dealerHand = []
|
||||||
|
generateHand(self.dealerHand, self.deck)
|
||||||
|
|
||||||
|
self.recv = recv
|
||||||
|
self.send = send
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
async def play_game(self, bet):
|
||||||
|
validInput = False
|
||||||
|
gameOver = False
|
||||||
|
playerStood = False
|
||||||
|
# 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)
|
||||||
|
dealerWinState = self.checkHandState(self.dealerHand)
|
||||||
|
gameOver = self.checkGameOver(playerWinState)
|
||||||
|
if gameOver:
|
||||||
|
continue
|
||||||
|
gameOver = self.checkGameOver(dealerWinState)
|
||||||
|
if gameOver:
|
||||||
|
continue
|
||||||
|
|
||||||
|
await self.send("Players hand = " + str(getHandTotal(self.playerHand)) + ": " + handNumbersToCards(
|
||||||
|
self.playerHand) + "\n" + "Dealers hand = ??: " + convertNumberToCard(self.dealerHand[0]) + "??")
|
||||||
|
play = await self.recv("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
|
||||||
|
|
||||||
|
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":
|
||||||
|
await self.send("You won!")
|
||||||
|
# self.writeLedger(ID, 2*bet, True)
|
||||||
|
elif playerWinState == "l":
|
||||||
|
await self.send("You busted!")
|
||||||
|
# self.writeLedger(ID, -bet, False)
|
||||||
|
elif dealerWinState == "w":
|
||||||
|
await self.send("The Dealer reached 21 before you!")
|
||||||
|
# self.writeLedger(ID, -bet, False)
|
||||||
|
elif dealerWinState == "l":
|
||||||
|
await self.send("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))
|
||||||
|
|
||||||
95
BlackJackBot/BJBot.py
Normal file
95
BlackJackBot/BJBot.py
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
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():
|
||||||
|
print("Let's pwn some scrubs")
|
||||||
|
|
||||||
|
@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
|
||||||
|
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("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')
|
||||||
BIN
BlackJackBot/__pycache__/BlackJack.cpython-310.pyc
Normal file
BIN
BlackJackBot/__pycache__/BlackJack.cpython-310.pyc
Normal file
Binary file not shown.
BIN
BlackJackBot/__pycache__/BlackJack.cpython-39.pyc
Normal file
BIN
BlackJackBot/__pycache__/BlackJack.cpython-39.pyc
Normal file
Binary file not shown.
0
BlackJackBot/ledger.txt
Normal file
0
BlackJackBot/ledger.txt
Normal file
BIN
__pycache__/BlackJack.cpython-310.pyc
Normal file
BIN
__pycache__/BlackJack.cpython-310.pyc
Normal file
Binary file not shown.
Reference in New Issue
Block a user