From b241dc13a8ac1c208460f0bd92b890f954310079 Mon Sep 17 00:00:00 2001 From: Nikolaj Danger Date: Wed, 5 Aug 2020 11:19:24 +0200 Subject: [PATCH] :zap: Improving blackjack --- funcs/games/blackjack.py | 364 +++++++++++++++-------------------- funcs/games/blackjackDraw.py | 2 +- resources/errorCodes.txt | 3 +- 3 files changed, 156 insertions(+), 213 deletions(-) diff --git a/funcs/games/blackjack.py b/funcs/games/blackjack.py index 719bbd8..11e5647 100644 --- a/funcs/games/blackjack.py +++ b/funcs/games/blackjack.py @@ -197,126 +197,18 @@ 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"] - 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["standing"] = True - with open("resources/games/games.json", "w") as f: - json.dump(data,f,indent=4) + hand, handNumber = getHandNumber(data["blackjack games"][channel]["user hands"][user],handNumber) - response = "accept" - roundDone = True + 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) - 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 + response = "accept" + roundDone = isRoundDone(data["blackjack games"][channel]) - 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"]) + 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+" is already standing") - return "You're already standing" + logThis(user+" has already hit this round") + return "You've already hit this round" 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: @@ -744,4 +643,47 @@ def calcWinnings(hand, dealerValue, topLevel, dealerBlackjack, dealerBusted): netWinnings += netWinningsTemp reason += reasonTemp - return winnings, netWinnings, reason \ No newline at end of file + 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 diff --git a/funcs/games/blackjackDraw.py b/funcs/games/blackjackDraw.py index 12e0a47..46c1eb4 100644 --- a/funcs/games/blackjackDraw.py +++ b/funcs/games/blackjackDraw.py @@ -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: diff --git a/resources/errorCodes.txt b/resources/errorCodes.txt index 4876893..adde8e4 100644 --- a/resources/errorCodes.txt +++ b/resources/errorCodes.txt @@ -92,7 +92,8 @@ 1311 - Error calculating winnings 1312 - Error in calcWinnings function 1320 - Unspecified loop error -1321 - Loop interrupted while waiting +1321 - Loop interrupted while waiting +1322 - Error with getHandNumber() 1330 - Unspecified continue error 1331 - Error in testIfStanding() 1340 - Error in drawing blackjack table