Created
October 21, 2024 09:14
-
-
Save Auax/69ba20e85d74b02bcbf99db597eb60f6 to your computer and use it in GitHub Desktop.
Python european roulette script to test strategies. Change the simulate_strategy() method for creating your own strategies. Note this code is not really finished, it's just a side project I made that can be used as for inspiration for another finished project.
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
import random | |
import sys | |
from typing import List, Dict, Union | |
class InsufficientFunds(Exception): | |
pass | |
class Options: | |
def __init__(self, boxes: List[int], payout: int): | |
self.boxes = boxes | |
self.payout = payout | |
class Bet: | |
types: Dict[str, Options] = { | |
"Number": Options(list(range(0, 37)), 35), | |
"Even": Options(list(range(2, 37, 2)), 1), | |
"Odd": Options(list(range(1, 36, 2)), 1), | |
"Red": Options([1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36], 1), | |
"Black": Options([2, 4, 6, 8, 10, 11, 13, 15, 17, 20, 22, 24, 26, 28, 29, 31, 33, 35], 1), | |
"First_Third": Options(list(range(1, 13)), 2), | |
"Second_Third": Options(list(range(13, 25)), 2), | |
"Third_Third": Options(list(range(25, 37)), 2), | |
"Row_1": Options([1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34], 2), | |
"Row_2": Options([2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35], 2), | |
"Row_3": Options([3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36], 2), | |
"Low": Options(list(range(1, 19)), 1), | |
"High": Options(list(range(19, 37)), 1), | |
"Two_Numbers": Options([], 17), | |
"Three_Numbers": Options([], 11), | |
"Four_Numbers": Options([], 8), | |
"Six_Numbers": Options([], 5) | |
} | |
def __init__(self, amount: int, boxes: Union[List[int], None] = None, bet_type: str = "Number"): | |
self.amount = amount | |
self.boxes = boxes if boxes is not None else [] | |
self.type = bet_type | |
self.valid_boxes = [] | |
if bet_type in ["Number", "Two_Numbers", "Three_Numbers", "Four_Numbers", "Six_Numbers"]: | |
self.validate_and_set_boxes() | |
elif bet_type in self.types: | |
self.valid_boxes = self.types[bet_type].boxes | |
else: | |
raise ValueError(f"Invalid bet type: {bet_type}") | |
def validate_and_set_boxes(self): | |
if self.type == "Number" and len(self.boxes) == 1: | |
if 0 <= self.boxes[0] <= 36: | |
self.valid_boxes = self.boxes | |
else: | |
raise ValueError("Invalid number bet. Number must be between 0 and 36.") | |
elif self.type == "Two_Numbers" and len(self.boxes) == 2: | |
if all(0 <= num <= 36 for num in self.boxes) and abs(self.boxes[0] - self.boxes[1]) in [1, 3]: | |
self.valid_boxes = self.boxes | |
else: | |
raise ValueError("Invalid two numbers bet. Numbers must be adjacent on the roulette table.") | |
elif self.type == "Three_Numbers" and len(self.boxes) == 3: | |
if all(0 <= num <= 36 for num in self.boxes) and max(self.boxes) - min(self.boxes) == 2: | |
self.valid_boxes = self.boxes | |
else: | |
raise ValueError("Invalid three numbers bet. Numbers must be in a row on the roulette table.") | |
elif self.type == "Four_Numbers" and len(self.boxes) == 4: | |
if all(0 <= num <= 36 for num in self.boxes) and max(self.boxes) - min(self.boxes) in [3, 4]: | |
self.valid_boxes = self.boxes | |
else: | |
raise ValueError("Invalid four numbers bet. Numbers must form a square on the roulette table.") | |
elif self.type == "Six_Numbers" and len(self.boxes) == 6: | |
if all(0 <= num <= 36 for num in self.boxes) and max(self.boxes) - min(self.boxes) == 5: | |
self.valid_boxes = self.boxes | |
else: | |
raise ValueError("Invalid six numbers bet. Numbers must be two adjacent rows on the roulette table.") | |
else: | |
raise ValueError(f"Invalid bet type or number of boxes for {self.type}") | |
class EuropeanRoulette: | |
def __init__(self, balance: int = 100, verbose: bool = True, show_results: bool = True): | |
self.balance = balance | |
self.player_bets: List[Bet] = [] | |
self.wins = 0 | |
self.losses = 0 | |
self.verbose = verbose | |
self.show_results = show_results | |
def check_balance(self): | |
print(f"Balance: {self.balance}") | |
def spin(self) -> int: | |
return random.randint(0, 36) | |
def place_bet(self, bet: Bet): | |
if bet.amount > self.balance: | |
raise InsufficientFunds("Insufficient balance to place this bet.") | |
self.balance -= bet.amount # Deduct the bet amount from the balance immediately | |
self.player_bets.append(bet) | |
def clear_bets(self): | |
self.player_bets.clear() | |
def play_round(self): | |
if not self.player_bets: | |
if self.verbose: | |
print("No bets placed. Please place a bet before playing a round.") | |
return | |
winning_number = self.spin() | |
if self.verbose: | |
print(f"The winning number is: {winning_number}") | |
total_winnings = 0 | |
for bet in self.player_bets: | |
bet_type = Bet.types[bet.type] | |
winning_boxes = bet.valid_boxes if bet.valid_boxes else bet_type.boxes | |
if winning_number in winning_boxes: | |
winnings = bet.amount + (bet.amount * bet_type.payout) | |
total_winnings += winnings | |
if self.verbose: | |
print(f"Won {winnings} on {bet.type} bet") | |
elif self.verbose: | |
print(f"Lost {bet.amount} on {bet.type} bet") | |
self.balance += total_winnings | |
if total_winnings > 0: | |
self.wins += 1 | |
elif total_winnings == 0: | |
self.losses += 1 | |
if self.verbose: | |
print(f"Round result: {'Win' if total_winnings > 0 else 'Loss' if total_winnings == 0 else 'Push'}") | |
self.check_balance() | |
self.clear_bets() | |
def results(self, rounds: int, initial_balance: int): | |
print(f"Final Results after {rounds} rounds:") | |
print(f"Wins: {self.wins}") | |
print(f"Losses: {self.losses}") | |
print(f"Pushes: {rounds - self.wins - self.losses}") | |
print(f"Net Profit/Loss: {self.balance - initial_balance}") | |
def play_interactive(self, rounds: int = 1): | |
initial_balance = self.balance | |
for round in range(1, rounds + 1): | |
print(f"\n--- Round {round} ---") | |
# Allow the player to place bets before each round | |
while True: | |
bet_type = input("Enter bet type (or 'done' to finish betting): ") | |
if bet_type.lower() == 'done': | |
break | |
amount = int(input("Enter bet amount: ")) | |
boxes = input("Enter box numbers (comma-separated, or leave empty): ") | |
boxes = [int(box.strip()) for box in boxes.split(',')] if boxes else None | |
try: | |
bet = Bet(amount, boxes, bet_type) | |
self.place_bet(bet) | |
print(f"Bet placed: {bet_type} for ${amount}") | |
except ValueError as e: | |
print(f"Error placing bet: {e}") | |
self.play_round() | |
self.results(rounds, initial_balance) | |
def simulate_strategy(self, rounds: int = 1): | |
initial_balance = self.balance | |
bet_type = "Red" | |
amount = 1 | |
boxes = [] | |
bet = Bet(amount, boxes, bet_type) | |
for _ in range(1, rounds + 1): | |
try: | |
self.place_bet(bet) | |
except InsufficientFunds: | |
# print(f"Insufficient funds...") | |
break | |
self.play_round() | |
if self.show_results: | |
self.results(rounds, initial_balance) | |
return self.balance - initial_balance | |
# Example usage | |
# roulette.play_interactive(rounds=5) | |
strategy_total = 1000 | |
strategy_wins = 0 | |
for i in range(strategy_total): | |
roulette = EuropeanRoulette(balance=200, verbose=False, show_results=False) | |
net_profit = roulette.simulate_strategy(1000) | |
if net_profit > 0: | |
strategy_wins += 1 | |
print(f"\nStrategy wins: {strategy_wins} / {strategy_total}") | |
print(f"\nWin rate: {round(strategy_wins / strategy_total * 100)}%") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment