284 lines
8.6 KiB
Python
284 lines
8.6 KiB
Python
from typing import List, Dict, Optional
|
|
from functools import reduce
|
|
from dataclasses import dataclass, field
|
|
from random import choice, randint
|
|
|
|
|
|
Bet = Dict[int, float]
|
|
|
|
SINGLE_BETS = {str(i) for i in range(-1, 37)}
|
|
|
|
FEASIBLE_MOVES = sorted(
|
|
{
|
|
*[f"street-{i}" for i in range(1, 14)],
|
|
*[f"col-{i}" for i in range(1, 4)],
|
|
*[f"corner-{i}-{i+1}-{i+3}-{i+4}" for i in range(1, 33) if (i - 1) % 3 < 2],
|
|
*["1-12", "13-24", "25-36", "1-18", "19-36", "even", "odd", "red", "black"],
|
|
*["triple-0", "triple-00"],
|
|
*SINGLE_BETS,
|
|
}
|
|
)
|
|
|
|
|
|
ALIASES = {
|
|
"reds",
|
|
"blacks",
|
|
"evens",
|
|
"odds",
|
|
"first-half",
|
|
"last-half",
|
|
"second-half",
|
|
"first-18",
|
|
"last-18",
|
|
"second-18",
|
|
}
|
|
|
|
CHIP_VALUES = {0.25, 0.5, 1, 5, 10, 25, 50, 100}
|
|
|
|
|
|
def expectation(bet):
|
|
odds = 0
|
|
pmnt = 0
|
|
return odds * pmnt
|
|
|
|
|
|
# 38 numbers, 6 street bets, 2 half-bets,
|
|
|
|
|
|
# payout grid based on bets placed.
|
|
# a street bet is the same as splitting the bet across all the numbers in the group.
|
|
# will use a function to distribute / interpret the bets, but it seems like we only need to track the numbers on the wheel.
|
|
|
|
|
|
def init_bet() -> Bet:
|
|
D = {i: 0 for i in range(-1, 37)}
|
|
return D
|
|
|
|
|
|
def place_bet(bet: Bet, on: int, amount: float):
|
|
bet = bet.copy()
|
|
bet[on] += amount
|
|
return bet
|
|
|
|
|
|
def interpret_bet(on="red", amount=0, bet=Optional[Bet]):
|
|
assert (on in FEASIBLE_MOVES) or (
|
|
on in ALIASES
|
|
), f"Bet `{on}` not understood. Choose from feasible moves:\n {FEASIBLE_MOVES}"
|
|
if bet is None:
|
|
bet = init_bet()
|
|
else:
|
|
bet = bet.copy()
|
|
REDS = {1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36}
|
|
BLACKS = set(range(37)) - REDS
|
|
NUMS = {}
|
|
on = on.strip().replace(" ", "-")
|
|
div = 18
|
|
if on in ("red", "reds"):
|
|
NUMS = REDS
|
|
if on in ("black", "blacks"):
|
|
NUMS = BLACKS
|
|
if on in ("odd", "odds"):
|
|
NUMS = {i for i in range(1, 37) if i % 2 == 0}
|
|
if on in ("even", "evens"):
|
|
NUMS = {i for i in range(1, 37) if i % 2}
|
|
if on in ("1-18", "first-18", "first-half"):
|
|
NUMS = set(range(1, 19))
|
|
if on in ("19-36", "last-18", "last-half", "second-half", "second-18"):
|
|
NUMS = set(range(19, 37))
|
|
if on in ("1-12", "13-24", "25-36"):
|
|
low, high = on.split("-")
|
|
NUMS = set(range(int(low), int(high) + 1))
|
|
div = 12
|
|
if on in ["triple-0", "triple-00"]:
|
|
NUMS = {0, 1, 2} if on == "triple-0" else {-1, 2, 3}
|
|
div = 3
|
|
if not NUMS:
|
|
other_bet = on.split("-")
|
|
if other_bet[0] == "street":
|
|
street = int(other_bet[1]) - 1
|
|
assert street in list(range(13))
|
|
NUMS = {i for i in range(street + 1, street + 4)}
|
|
div = 3
|
|
elif other_bet[0] == "col":
|
|
col = int(other_bet[1]) - 1
|
|
assert col in list(range(0, 3))
|
|
NUMS = {i for i in range(1, 37) if (i - 1) % 3 == col}
|
|
div = 12
|
|
elif (
|
|
other_bet[0] == "split"
|
|
): # TODO: validate choices, for now we disallow these.
|
|
num_1, num_2 = int(other_bet[1]), int(other_bet[2])
|
|
NUMS = {num_1, num_2}
|
|
div = 2
|
|
elif other_bet[0] == "corner":
|
|
num_1, num_2 = int(other_bet[1]), int(other_bet[2])
|
|
num_3, num_4 = int(other_bet[3]), int(other_bet[4])
|
|
NUMS = {num_1, num_2, num_3, num_4}
|
|
div = 4
|
|
else:
|
|
try:
|
|
NUMS = {int(on)}
|
|
div = 1
|
|
except ValueError as e:
|
|
raise e(
|
|
f"Bet `{on}` not understood. Choose from feasible moves:\n {set(range(-1, 37))}"
|
|
)
|
|
|
|
bet = reduce(lambda bet, num: place_bet(bet, num, amount / div), NUMS, bet)
|
|
|
|
return bet
|
|
|
|
|
|
@dataclass
|
|
class Placement:
|
|
"""
|
|
Defines a bet based on the number of chips and value of each chip.
|
|
|
|
Args:
|
|
num (int): number of chips
|
|
amt (float): value of each chip
|
|
on (str): bet type
|
|
|
|
Returns:
|
|
Placement: an object representing the placement of a stack of chips on a particular bet type.
|
|
"""
|
|
|
|
num: int
|
|
amt: float
|
|
on: str
|
|
|
|
def __post_init__(self):
|
|
assert (self.on in FEASIBLE_MOVES) or (
|
|
self.on in ALIASES
|
|
), f"Bet `{self.on}` not understood. Choose from feasible moves:\n {FEASIBLE_MOVES}"
|
|
|
|
@property
|
|
def value(self):
|
|
"""
|
|
Returns the value of the bet.
|
|
"""
|
|
return self.num * self.amt
|
|
|
|
def place_bet(self, bet=None):
|
|
"""
|
|
Places a bet on the wheel based on the bet type.
|
|
"""
|
|
return interpret_bet(self.on, self.num * self.amt, bet)
|
|
|
|
|
|
# for two bets of structure Dict[int, float], iterate through all the keys and add up the values, returning a new dict.
|
|
def combine_bets(bet_1, bet_2):
|
|
return {k: bet_1.get(k, 0) + bet_2.get(k, 0) for k in set(bet_1) | set(bet_2)}
|
|
|
|
|
|
# for a list of Placements, call the place_bet method on each one and combine the results using reduce and combine_bets, starting with an empty dictionary as the initial argument
|
|
def place_bets(placements):
|
|
return reduce(
|
|
lambda bet, placement: combine_bets(bet, placement.place_bet()), placements, {}
|
|
)
|
|
|
|
|
|
@dataclass
|
|
class Strategy:
|
|
budget: float = 200
|
|
placements: List[Placement] = field(default_factory=list)
|
|
|
|
def __repr__(self) -> str:
|
|
return f"Strategy(budget={self.budget}, value={self.value}, placements={self.placements})"
|
|
|
|
@property
|
|
def value(self):
|
|
return sum([p.value for p in self.placements])
|
|
|
|
@classmethod
|
|
def generate_random(cls, budget) -> "Strategy":
|
|
placements = []
|
|
initial_budget = budget
|
|
while budget > 0:
|
|
amt = choice([v for v in CHIP_VALUES if v <= budget])
|
|
# guarantees the max bet cannot exceed budget:
|
|
num = randint(1, budget // amt)
|
|
# select random bet type
|
|
# todo: consider if this is the logic you want...
|
|
if randint(0, 1) == 0:
|
|
on = choice(list(FEASIBLE_MOVES))
|
|
else:
|
|
on = choice(list(SINGLE_BETS))
|
|
on = choice(
|
|
list(FEASIBLE_MOVES)
|
|
) # todo: make a parameter, allow for just single bets.
|
|
placement = Placement(num, amt, on)
|
|
placements.append(placement)
|
|
budget -= placement.value
|
|
return Strategy(budget=initial_budget, placements=placements)
|
|
|
|
def print_all(self) -> None:
|
|
for p in self.placements:
|
|
print(p)
|
|
|
|
def get_bet(self):
|
|
return place_bets(self.placements)
|
|
|
|
|
|
@dataclass
|
|
class Player:
|
|
budget: float
|
|
strategy: Strategy
|
|
|
|
|
|
def simulate_random_strategy(min_num_games=1, total_budget=200):
|
|
strategy_budget = total_budget // min_num_games
|
|
return Strategy.generate_random(strategy_budget)
|
|
|
|
|
|
# given BUDGET, generate a bunch of random players, each with a random strategy, and return a list of players
|
|
def generate_players(num_players=10, min_num_games=1, total_budget=200):
|
|
return [
|
|
Player(
|
|
total_budget,
|
|
simulate_random_strategy(
|
|
min_num_games=min_num_games, total_budget=total_budget
|
|
),
|
|
)
|
|
for _ in range(num_players)
|
|
]
|
|
|
|
|
|
# simulate a game of roulette, picking a random integer from -1 to 37, taking the players as inputs and returning their expected winnings
|
|
def simulate_game(players):
|
|
# pick a random number
|
|
num = randint(-1, 36)
|
|
# print("WINNER:", num)
|
|
# for each player, place their bets on the wheel
|
|
bets = [p.strategy.get_bet() for p in players]
|
|
# for each player, calculate their winnings
|
|
winnings = [36 * bet.get(num, 0) for bet in bets]
|
|
# for each player, calculate their expected winnings
|
|
return winnings
|
|
|
|
|
|
# simulate multiple games, reducing each player's budget by the amount of their bet and adding the amount of their winnings
|
|
def simulate_games(players, num_games=10):
|
|
losers = []
|
|
for g in range(num_games):
|
|
if not players:
|
|
break
|
|
print(f"GAME {g}")
|
|
winnings = simulate_game(players)
|
|
new_losers = []
|
|
for i, p in enumerate(players):
|
|
p.budget -= p.strategy.value
|
|
p.budget += winnings[
|
|
i
|
|
] # TODO: reinvestment logic goes here. maybe add "reinvest" as a player attribute?
|
|
# if a player runs out of money to keep using their strategy,
|
|
# remove them from the list of players and add them to the list of losers
|
|
if p.budget < p.strategy.value:
|
|
new_losers.append(p)
|
|
for l in new_losers:
|
|
players.remove(l)
|
|
losers.extend(new_losers)
|
|
|
|
return players + losers
|