Improving blackjack

This commit is contained in:
Nikolaj Danger
2020-08-05 11:19:24 +02:00
parent 089791c3ba
commit b241dc13a8
3 changed files with 156 additions and 213 deletions

View File

@ -197,127 +197,19 @@ def blackjackHit(channel,user,handNumber = 0):
data = json.load(f)
if user in data["blackjack games"][channel]["user hands"]:
if data["blackjack games"][channel]["user hands"][user]["split"] == 0:
hand = data["blackjack games"][channel]["user hands"][user]
handNumber = 0
else:
if handNumber != 0:
if handNumber == 1:
hand = data["blackjack games"][channel]["user hands"][user]
elif handNumber == 2:
hand = data["blackjack games"][channel]["user hands"][user]["other hand"]
elif handNumber == 3:
hand = data["blackjack games"][channel]["user hands"][user]["third hand"]
elif handNumber == 4:
hand = data["blackjack games"][channel]["user hands"][user]["fourth hand"]
else:
logThis(user+" tried to hit without specifying which hand")
return "You have to specify the hand you're hitting with."
else:
logThis(user+" tried to hit without specifying which hand")
return "You have to specify the hand you're hitting with."
if data["blackjack games"][channel]["round"] > 0:
if hand["hit"] == False:
if hand["standing"] == False:
hand["hand"].append(drawCard(channel))
hand["hit"] = True
handValue = calcHandValue(hand["hand"])
if handValue > 21:
hand["busted"] = True
if handNumber == 2:
data["blackjack games"][channel]["user hands"][user]["other hand"] = hand
elif handNumber == 3:
data["blackjack games"][channel]["user hands"][user]["third hand"] = hand
elif handNumber == 4:
data["blackjack games"][channel]["user hands"][user]["fourth hand"] = hand
else:
data["blackjack games"][channel]["user hands"][user] = hand
with open("resources/games/games.json", "w") as f:
json.dump(data,f,indent=4)
response = "accept"
roundDone = True
for person in data["blackjack games"][channel]["user hands"].values():
if person["hit"] == False and person["standing"] == False:
roundDone = False
if person["split"] > 0:
if person["other hand"]["hit"] == False and person["other hand"]["standing"] == False:
roundDone = False
if person["split"] > 1:
if person["third hand"]["hit"] == False and person["third hand"]["standing"] == False:
roundDone = False
if person["split"] > 2:
if person["fourth hand"]["hit"] == False and person["fourth hand"]["standing"] == False:
roundDone = False
return response + str(roundDone)[0] + str(data["blackjack games"][channel]["round"])
else:
logThis(user+" is already standing")
return "You can't hit when you're standing"
else:
logThis(user+" has already hit this round")
return "You've already hit this round"
else:
logThis(user+" tried to hit on the 0th round")
return "You can't hit before you see your cards"
else:
logThis(user+" tried to hit without being in the game")
return "You have to enter the game before you can hit"
# When players try to double down
def blackjackDouble(channel,user,handNumber = 0):
with open("resources/games/games.json", "r") as f:
data = json.load(f)
if data["blackjack games"][channel]["user hands"][user]["split"] == 0:
hand = data["blackjack games"][channel]["user hands"][user]
handNumber = 0
else:
if handNumber != 0:
if handNumber == 1:
hand = data["blackjack games"][channel]["user hands"][user]
elif handNumber == 2:
hand = data["blackjack games"][channel]["user hands"][user]["other hand"]
elif handNumber == 3:
hand = data["blackjack games"][channel]["user hands"][user]["third hand"]
elif handNumber == 4:
hand = data["blackjack games"][channel]["user hands"][user]["fourth hand"]
else:
logThis(user+" tried to double without specifying which hand")
return "You have to specify the hand you're doubling down.",""
else:
logThis(user+" tried to double without specifying which hand")
return "You have to specify the hand you're doubling down.",""
if data["blackjack games"][channel]["round"] > 0:
if hand["hit"] == False:
if hand["standing"] == False:
if len(hand["hand"]) == 2:
bet = hand["bet"]
if money.checkBalance(user) >= bet:
money.addMoney(user,-1 * bet)
with open("resources/games/games.json", "r") as f:
data = json.load(f)
hand, handNumber = getHandNumber(data["blackjack games"][channel]["user hands"][user],handNumber)
print(hand)
if hand != None:
if data["blackjack games"][channel]["round"] > 0:
if hand["hit"] == False:
if hand["standing"] == False:
hand["hand"].append(drawCard(channel))
hand["hit"] = True
hand["doubled"] = True
hand["bet"] += bet
handValue = calcHandValue(hand["hand"])
if handValue > 21:
hand["busted"] = True
@ -333,41 +225,93 @@ def blackjackDouble(channel,user,handNumber = 0):
with open("resources/games/games.json", "w") as f:
json.dump(data,f,indent=4)
roundDone = True
response = "accept"
roundDone = isRoundDone(data["blackjack games"][channel])
for person in data["blackjack games"][channel]["user hands"].values():
if person["hit"] == False and person["standing"] == False:
roundDone = False
if person["split"] > 0:
if person["other hand"]["hit"] == False and person["other hand"]["standing"] == False:
roundDone = False
if person["split"] > 1:
if person["third hand"]["hit"] == False and person["third hand"]["standing"] == False:
roundDone = False
if person["split"] > 2:
if person["fourth hand"]["hit"] == False and person["fourth hand"]["standing"] == False:
roundDone = False
return "Adding another "+str(bet)+" GwendoBucks to "+getName(user)+"'s bet and drawing another card.",str(roundDone)[0] + str(data["blackjack games"][channel]["round"])
return response + str(roundDone)[0] + str(data["blackjack games"][channel]["round"])
else:
logThis(user+" doesn't have enough GwendoBucks")
return "You don't have enough GwendoBucks",""
logThis(user+" is already standing")
return "You can't hit when you're standing"
else:
logThis(user+" tried to double on round "+str(data["blackjack games"][channel]["round"]))
return "You can only double down on the first round",""
logThis(user+" has already hit this round")
return "You've already hit this round"
else:
logThis(user+" is already standing")
return "You can't double when you're standing",""
logThis(user+" tried to hit on the 0th round")
return "You can't hit before you see your cards"
else:
logThis(user+" has already hit this round")
return "You've already hit this round",""
logThis(user+" didn't specify a hand")
return "You need to specify a hand"
else:
logThis(user+" tried to double on the 0th round")
return "You can't double down before you see your cards",""
logThis(user+" tried to hit without being in the game")
return "You have to enter the game before you can hit"
# When players try to double down
def blackjackDouble(channel,user,handNumber = 0):
with open("resources/games/games.json", "r") as f:
data = json.load(f)
if user in data["blackjack games"][channel]["user hands"]:
hand, handNumber = getHandNumber(data["blackjack games"][channel]["user hands"][user],handNumber)
if hand != None:
if data["blackjack games"][channel]["round"] > 0:
if hand["hit"] == False:
if hand["standing"] == False:
if len(hand["hand"]) == 2:
bet = hand["bet"]
if money.checkBalance(user) >= bet:
money.addMoney(user,-1 * bet)
with open("resources/games/games.json", "r") as f:
data = json.load(f)
hand["hand"].append(drawCard(channel))
hand["hit"] = True
hand["doubled"] = True
hand["bet"] += bet
handValue = calcHandValue(hand["hand"])
if handValue > 21:
hand["busted"] = True
if handNumber == 2:
data["blackjack games"][channel]["user hands"][user]["other hand"] = hand
elif handNumber == 3:
data["blackjack games"][channel]["user hands"][user]["third hand"] = hand
elif handNumber == 4:
data["blackjack games"][channel]["user hands"][user]["fourth hand"] = hand
else:
data["blackjack games"][channel]["user hands"][user] = hand
with open("resources/games/games.json", "w") as f:
json.dump(data,f,indent=4)
roundDone = isRoundDone(data["blackjack games"][channel])
return "Adding another "+str(bet)+" GwendoBucks to "+getName(user)+"'s bet and drawing another card.",str(roundDone)[0] + str(data["blackjack games"][channel]["round"])
else:
logThis(user+" doesn't have enough GwendoBucks")
return "You don't have enough GwendoBucks",""
else:
logThis(user+" tried to double on round "+str(data["blackjack games"][channel]["round"]))
return "You can only double down on the first round",""
else:
logThis(user+" is already standing")
return "You can't double when you're standing",""
else:
logThis(user+" has already hit this round")
return "You've already hit this round",""
else:
logThis(user+" tried to double on the 0th round")
return "You can't double down before you see your cards",""
else:
logThis(user+" didn't specify a hand")
return "You need to specify which hand"
else:
logThis(user+" tried to double without being in the game")
return "You can't double when you're not in the game",""
# When players try to stand
def blackjackStand(channel,user,handNumber = 0):
@ -375,62 +319,33 @@ def blackjackStand(channel,user,handNumber = 0):
data = json.load(f)
if user in data["blackjack games"][channel]["user hands"]:
if data["blackjack games"][channel]["user hands"][user]["split"] == 0:
hand = data["blackjack games"][channel]["user hands"][user]
handNumber = 0
else:
if handNumber != 0:
if handNumber == 1:
hand = data["blackjack games"][channel]["user hands"][user]
elif handNumber == 2:
hand = data["blackjack games"][channel]["user hands"][user]["other hand"]
elif handNumber == 3:
hand = data["blackjack games"][channel]["user hands"][user]["third hand"]
elif handNumber == 4:
hand = data["blackjack games"][channel]["user hands"][user]["fourth hand"]
hand, handNumber = getHandNumber(data["blackjack games"][channel]["user hands"][user],handNumber)
if hand != None:
if data["blackjack games"][channel]["round"] > 0:
if hand["hit"] == False:
if hand["standing"] == False:
hand["standing"] = True
with open("resources/games/games.json", "w") as f:
json.dump(data,f,indent=4)
response = "accept"
roundDone = isRoundDone(data["blackjack games"][channel])
return response + str(roundDone)[0] + str(data["blackjack games"][channel]["round"])
else:
logThis(user+" is already standing")
return "You're already standing"
else:
logThis(user+" tried to hit without specifying which hand")
return "You have to specify the hand you're hitting with."
logThis(user+" has already hit this round")
return "You've already hit this round"
else:
logThis(user+" tried to hit without specifying which hand")
return "You have to specify the hand you're hitting with."
if data["blackjack games"][channel]["round"] > 0:
if hand["hit"] == False:
if hand["standing"] == False:
hand["standing"] = True
with open("resources/games/games.json", "w") as f:
json.dump(data,f,indent=4)
response = "accept"
roundDone = True
for person in data["blackjack games"][channel]["user hands"].values():
if person["hit"] == False and person["standing"] == False:
roundDone = False
if person["split"] > 0:
if person["other hand"]["hit"] == False and person["other hand"]["standing"] == False:
roundDone = False
if person["split"] > 1:
if person["third hand"]["hit"] == False and person["third hand"]["standing"] == False:
roundDone = False
if person["split"] > 2:
if person["fourth hand"]["hit"] == False and person["fourth hand"]["standing"] == False:
roundDone = False
return response + str(roundDone)[0] + str(data["blackjack games"][channel]["round"])
else:
logThis(user+" is already standing")
return "You're already standing"
else:
logThis(user+" has already hit this round")
return "You've already hit this round"
logThis(user+" tried to stand on the first round")
return "You can't stand before you see your cards"
else:
logThis(user+" tried to stand on the first round")
return "You can't stand before you see your cards"
logThis(user+" didn't specify a hand")
return "You need to specify which hand"
else:
logThis(user+" tried to stand without being in the game")
return "You have to enter the game before you can stand"
@ -526,23 +441,7 @@ def blackjackSplit(channel,user,handNumber = 0):
with open("resources/games/games.json", "w") as f:
json.dump(data,f,indent=4)
roundDone = True
for person in data["blackjack games"][channel]["user hands"].values():
if person["hit"] == False and person["standing"] == False:
roundDone = False
if person["split"] > 0:
if person["other hand"]["hit"] == False and person["other hand"]["standing"] == False:
roundDone = False
if person["split"] > 1:
if person["third hand"]["hit"] == False and person["third hand"]["standing"] == False:
roundDone = False
if person["split"] > 2:
if person["fourth hand"]["hit"] == False and person["fourth hand"]["standing"] == False:
roundDone = False
roundDone = isRoundDone(data["blackjack games"][channel])
return "Splitting "+getName(user)+"'s hand into 2. Adding their original bet to the second hand. You can use \"!Blackjack hit/stand/double 1\" and \"!Blackjack hit/stand/double 2\" to play the different hands.",str(roundDone)[0] + str(data["blackjack games"][channel]["round"])
else:
@ -745,3 +644,46 @@ def calcWinnings(hand, dealerValue, topLevel, dealerBlackjack, dealerBusted):
reason += reasonTemp
return winnings, netWinnings, reason
def getHandNumber(user,handNumber):
try:
hand = None
if user["split"] == 0:
hand = user
handNumber = 0
else:
if handNumber != 0:
if handNumber == 1:
hand = user
elif handNumber == 2:
hand = user["other hand"]
elif handNumber == 3:
hand = user["third hand"]
elif handNumber == 4:
hand = user["fourth hand"]
return hand, handNumber
except:
logThis("Problem with getHandNumber() (error code 1322)")
def isRoundDone(game):
roundDone = True
for person in game["user hands"].values():
if person["hit"] == False and person["standing"] == False:
roundDone = False
if person["split"] > 0:
if person["other hand"]["hit"] == False and person["other hand"]["standing"] == False:
roundDone = False
if person["split"] > 1:
if person["third hand"]["hit"] == False and person["third hand"]["standing"] == False:
roundDone = False
if person["split"] > 2:
if person["fourth hand"]["hit"] == False and person["fourth hand"]["standing"] == False:
roundDone = False
return roundDone

View File

@ -36,7 +36,7 @@ def drawImage(channel):
for x in range(len(hands)):
key, value = list(hands.items())[x]
key = getName(key)
logThis("drawing "+key+"'s hand")
#logThis("Drawing "+key+"'s hand")
userHand = drawHand(value["hand"],False,value["busted"],value["blackjack"])
try:
if value["split"] == 3:

View File

@ -93,6 +93,7 @@
1312 - Error in calcWinnings function
1320 - Unspecified loop error
1321 - Loop interrupted while waiting
1322 - Error with getHandNumber()
1330 - Unspecified continue error
1331 - Error in testIfStanding()
1340 - Error in drawing blackjack table