Last active
May 20, 2024 21:51
-
-
Save markhc/49a71b135b866b6d015c0bdf8f8d94e7 to your computer and use it in GitHub Desktop.
poe div card gamba simulator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# python3 main.py startingStack=4 maxStack=9 strat=one winningAmount=300 maxRuns=10000 | |
# | |
import sys | |
import random | |
import time | |
debug = False | |
def runSimulation(startingStack, maxStack, strat, maxRuns=1000, winningAmount=-1): | |
currentStack = startingStack | |
amountToGamble = 0 | |
if strat == "one": | |
amountToGamble = 1 | |
elif strat == "two": | |
amountToGamble = 2 | |
totalRuns = 0 | |
peakStack = -1 | |
if maxRuns > 0 and winningAmount < 0: | |
for i in range(maxRuns): | |
if strat == "half": | |
amountToGamble = min(currentStack // 2, maxStack // 2) | |
elif strat == "max": | |
amountToGamble = min(currentStack, maxStack // 2) | |
if currentStack < amountToGamble: | |
if debug: | |
print("\nSimulation stopped. Not enough cards to gamble.") | |
return {"remainingStack": currentStack, "totalRuns": i, "peakStack": peakStack} | |
if debug: | |
print("Current Stack: {}. Wagering {}".format(currentStack, amountToGamble), end="") | |
# Gamble | |
currentStack -= amountToGamble | |
possibleResults = list(range(0, amountToGamble * 2 + 1)) | |
result = random.choice(possibleResults) | |
if debug: | |
print(" | Result {}".format(result)) | |
currentStack += result | |
if currentStack > peakStack: | |
peakStack = currentStack | |
totalRuns += 1 | |
elif winningAmount > 0: | |
while currentStack < winningAmount: | |
if strat == "half": | |
amountToGamble = min(currentStack // 2, maxStack // 2) | |
elif strat == "max": | |
amountToGamble = min(currentStack, maxStack // 2) | |
if currentStack < amountToGamble: | |
if debug: | |
print("\nSimulation stopped. Not enough cards to gamble.") | |
return {"remainingStack": currentStack, "totalRuns": totalRuns, "peakStack": peakStack} | |
if debug: | |
print("Current Stack: {}. Wagering {}".format(currentStack, amountToGamble), end="") | |
# Gamble | |
currentStack -= amountToGamble | |
possibleResults = list(range(0, amountToGamble * 2 + 1)) | |
result = random.choice(possibleResults) | |
if debug: | |
print(" | Result {}".format(result)) | |
currentStack += result | |
if currentStack > peakStack: | |
peakStack = currentStack | |
totalRuns += 1 | |
return {"remainingStack": currentStack, "totalRuns": totalRuns, "peakStack": peakStack} | |
def printUsage(): | |
print("Usage: python main.py [options]") | |
print("Options:") | |
print(" startingStack: Starting stack of cards") | |
print(" maxStack: Maximum stack of cards allowed by the game (e.g 9 for House of Mirrors)") | |
print(" strat: Strategy to use. Can be 'one', 'two', 'half', 'max' or 'computeBest'") | |
print(" 'one' is doing single cards bets") | |
print(" 'two' is doing double cards bets") | |
print(" 'half' is betting half of the current stack (up to half of maxStack)") | |
print(" 'max' is betting as much as possible every time (up to half of maxStack)") | |
print(" maxRuns: Maximum number of runs to do (optional)") | |
print(" winningAmount: The amount of cards to reach to consider the simulation a success (optional)") | |
# Usage: | |
# python main.py [options] | |
if __name__ == "__main__": | |
if len(sys.argv) < 3: | |
printUsage() | |
sys.exit(1) | |
startingStack = 0 | |
maxStack = 0 | |
strat = "" | |
maxRuns = 1000 | |
winningAmount = -1 | |
for arg in sys.argv: | |
if arg == "-d": | |
debug = True | |
if arg == "-h": | |
printUsage() | |
sys.exit(0) | |
if arg.startswith("startingStack="): | |
startingStack = int(arg.split("=")[1]) | |
if arg.startswith("maxStack="): | |
maxStack = int(arg.split("=")[1]) | |
if arg.startswith("strat="): | |
strat = arg.split("=")[1] | |
if arg.startswith("maxRuns="): | |
maxRuns = int(arg.split("=")[1]) | |
if arg.startswith("winningAmount="): | |
winningAmount = int(arg.split("=")[1]) | |
if startingStack == 0 or maxStack == 0 or strat == "": | |
printUsage() | |
sys.exit(1) | |
print("Starting Stack: {}".format(startingStack)) | |
print("Max Stack: {}".format(maxStack)) | |
print("Strategy: {}".format(strat)) | |
print("Max Runs: {}".format(maxRuns)) | |
print("Winning Amount: {}".format(winningAmount)) | |
#sys.exit(runSimulation(startingStack, maxStack, strat)) | |
winning = 0 | |
total = 0 | |
while winning < 100000: | |
stats = runSimulation(startingStack, maxStack, strat, maxRuns, winningAmount) | |
if stats["remainingStack"] >= winningAmount: | |
winning += 1 | |
total += 1 | |
print("\r", end="") | |
print("Win percentage: {:.2f}%".format(winning / total * 100), end="") | |
print("") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment