python-dice-top-k-game-prob.../pydiceprob.py

89 lines
2.8 KiB
Python

import sys
import json
import pprint
from fastapi import FastAPI, Header
pp = pp.Printer = pprint.PrettyPrinter(indent=2, compact=True)
def print(stuff):
pp.pprint(stuff)
def usagequit():
print("""Usage: python pydiceprob.py [k] [mode]
k between 6 and 99 (higher numbers are supported, up to however much your memory can handle; given that win probabilities approach 50%, this isn't very useful past about 100.)
modes: single, single-table, multi, multi-table
single prints out the probability of a single throw with a k-sided dice yielding k.
single-table prints out a table between 6 and k with the probabilities of a single throw yielding k.
multi prints out the probability of winning (assuming we're going first) in a game where two players take turns and the person who throws k first wins.
multi-table prints out the probability of winning for a range between 6 and k if you have the first throw.""")
quit()
app = FastAPI()
@app.get("/")
async def api_get(k: int = Header(default=None)):
if not isinstance(k, int) and k is not None:
return {"message": "Header k must be an integer between 6 and 100 inclusive or not present."}
if k is None:
result = one_turn_table(100)
return {"Probabilities-dice": result}
elif k >= 6 and k <= 100:
result = one_turn_single(k)
return {f"Probability-dice-size-{k}": result}
else:
return {"message": "Usage: no request body, use header k to specify what probability you want, if no k, then a table is given between dice sizes 6 and 100 inclusive."}
def main():
global k
try:
if int(sys.argv[1]) >= 6:
k = int(sys.argv[1])
else:
usagequit()
except IndexError:
usagequit()
global mode
try:
mode = sys.argv[2]
except IndexError:
usagequit()
dispatch(mode, k)
def dispatch(mode, k):
modes = ["single", "multi", "single-table", "multi-table"]
if mode not in modes:
usagequit()
if mode == "single":
print(one_turn_single(k))
elif mode == "multi" :
print(multi_turn_single(k))
elif mode == "single-table":
print(one_turn_table(k))
elif mode == "multi-table":
print(multi_turn_table(k))
def one_turn_table(k):
result = {}
for k in range(6, int(k)+1):
result[k] = 1/k
return result
def one_turn_single(k):
return one_turn_table(k)[k]
def multi_turn_single(k):
p_win = 1 / k # Winning probability on any given throw
p_lose = (k-1) / k # Losing probability on any given throw
r = p_lose**2
probability_win = p_win / (1 - r)
return probability_win
def multi_turn_table(k):
result = {}
for i in range(6, int(k+1)):
result[i] = multi_turn_single(i)
return result
if __name__ == "__main__":
main()