European Roulette Game (final)

This commit is contained in:
Donald Calloway 2025-10-27 10:11:11 -07:00
parent 1086acdbe9
commit 7c06e4409f

View File

@ -4,8 +4,48 @@ import random
import time
import re
# Define the roulette wheel and board components as immutable dictionary values ----
wheel = tuple(list(range(37)))
wagers = []
playing = True
DEBUG = False
# Define the roulette wheel and board components as dictionary values ----
Z0 = {'Z0': (0)}
N1 = {'N1': (1)}
N2 = {'N2': (2)}
N3 = {'N3': (3)}
N4 = {'N4': (4)}
N5 = {'N5': (5)}
N6 = {'N6': (6)}
N7 = {'N7': (7)}
N8 = {'N8': (8)}
N9 = {'N9': (9)}
N10 = {'N10': (10)}
N11 = {'N11': (11)}
N12 = {'N12': (12)}
N13 = {'N13': (13)}
N14 = {'N14': (14)}
N15 = {'N15': (15)}
N16 = {'N16': (16)}
N17 = {'N17': (17)}
N18 = {'N18': (18)}
N19 = {'N19': (19)}
N20 = {'N20': (20)}
N21 = {'N21': (21)}
N22 = {'N22': (22)}
N23 = {'N23': (23)}
N24 = {'N24': (24)}
N25 = {'N25': (25)}
N26 = {'N26': (26)}
N27 = {'N27': (27)}
N28 = {'N28': (28)}
N29 = {'N29': (29)}
N30 = {'N30': (30)}
N31 = {'N31': (31)}
N32 = {'N32': (32)}
N33 = {'N33': (33)}
N34 = {'N34': (34)}
N35 = {'N35': (35)}
N36 = {'N36': (36)}
I = {'I': (1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36)} # reds
J = {'J': (2, 4, 6, 8, 10, 11, 13, 15, 17, 20, 22, 24, 26, 28, 29, 31, 33, 35)} # blacks
H = {'H': tuple(x for x in range(2, 37, 2))} # Evens
@ -21,17 +61,17 @@ C = {'C': tuple(x for x in range(25, 37))} # third dozen
#-----------------------------------------------------------------------------------
# The 11 Doublestreets or Lines ------------------------------------------------------------------
X1 = {'X1': (1, 2, 3, 4, 5, 6)}
X2 = {'X2': (4, 5, 6, 7, 8, 9)}
X3 = {'X3': (7, 8, 9, 10, 11, 12)}
X4 = {'X4': (10, 11, 12, 13, 14, 15)}
X5 = {'X5': (13, 14, 15, 16, 17, 18)}
X6 = {'X6': (16, 17, 18, 19, 20, 21)}
X7 = {'X7': (19, 20, 21, 22, 23, 24)}
X8 = {'X8': (22, 23, 24, 25, 26, 27)}
X9 = {'X9': (25, 26, 27, 28, 29, 30)}
X10 = {'X10': (28, 29, 30, 31, 32, 33)}
X11 = {'X11': (31, 32, 33, 34, 35, 36)}
X1 = {'X1':(1, 2, 3, 4, 5, 6)}
X2 = {'X2':(4, 5, 6, 7, 8, 9)}
X3 = {'X3':(7, 8, 9, 10, 11, 12)}
X4 = {'X4':(10, 11, 12, 13, 14, 15)}
X5 = {'X5':(13, 14, 15, 16, 17, 18)}
X6 = {'X6':(16, 17, 18, 19, 20, 21)}
X7 = {'X7':(19, 20, 21, 22, 23, 24)}
X8 = {'X8':(22, 23, 24, 25, 26, 27)}
X9 = {'X9':(25, 26, 27, 28, 29, 30)}
X10 = {'X10':(28, 29, 30, 31, 32, 33)}
X11 = {'X11':(31, 32, 33, 34, 35, 36)}
#-------------------------------------------------------------------------------------------------
@ -137,33 +177,41 @@ C20 = {'C20':(29, 32, 30, 33)}
C21 = {'C21':(31, 34, 32, 35)}
C22 = {'C22':(32, 35, 33, 36)}
# The 2 Trios --------------------------------------------------------------------------------------------
T1 = {'T1': {0, 1, 2}}
T2 = {'T2': (0, 2, 3)}
#-----------------------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------------------
# End of wheel and board layout ----------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------------------
# Payouts for betting options (Payout = Multiple of bet less initial bet)
payouts = {'wheel': 35,
'splits': 17,
'streets': 11,
'corners': 8,
'lines': 5,
'first_doz': 2,
'second_doz': 2,
'third_doz': 2,
'first_col': 2,
'second_col': 2,
'third_col': 2,
'low': 1,
'high': 1,
'odds': 1,
'evens': 1,
'reds': 1,
'blacks': 1}
payouts = {
'numbers': 35,
'splits': 17,
'streets': 11,
'trio': 11,
'corners': 8,
'lines': 5,
'first_doz': 2,
'second_doz': 2,
'third_doz': 2,
'first_col': 2,
'second_col': 2,
'third_col': 2,
'low': 1,
'high': 1,
'odds': 1,
'evens': 1,
'reds': 1,
'blacks': 1,
'zero': 0
}
# Wager patterns
WAGER_PATTERNS = {
"splits": r"S(?:[1-9]|1[1-9]|2[1-9]|3[1-9]|4[1-9]|5[1-7])",
"numbers": r"N(?:[1-9]|[1-2][0-9]|3[0-6])",
"splits": r"S(?:[1-9]|[1-4][0-9]|5[0-7])",
"corners": r"C(?:[1-9]|1[0-9]|2[0-2])",
"streets": r"[O-Z]$", # matches exactly "O-Z"
"lines": r"X(?:[1-9]|1[0-1])",
@ -178,7 +226,10 @@ WAGER_PATTERNS = {
"third_col": r"F",
"first_doz": r"A",
"second_doz": r"B",
"third_doz": r"C$" # Matches exactly "C"
"third_doz": r"C$", # Matches exactly "C"
"zero": r"Z(?:[0])",
"Trio_1": r"T(?:[1])",
"Trio_2": r"T(?:[2])"
}
#----------------------------------------------------------------# Welcome Screen---------------------------------------------------
@ -191,18 +242,18 @@ print()
# ---------------------------------------------------------G A M E F U N C T I O N S -----------------------------------------------
def display_board():
print(" |<----------------------LOW (N)---------------------->|<---------------------HIGH (O)---------------------->|")
print(" |<----------------------LOW (G)---------------------->|<---------------------HIGH (L)---------------------->|")
print(" |-------X1-------X2-------X3-------X4-------X5----- -X6-------X7-------X8-------X9-------X10------X11-------|-----|")
print(" | | | | | | | | | | | | | | 2 |")
print(" | F 3 | 6 | 9 | 12 | 15 | 18 | 21 | 24 | 27 | 30 | 33 | 36 F TO |")
print(" | | C2 C4 C6 C8 C10 C12 C14 C16 C18 C20 C22 | 1 |")
print(" | F N3 | N6 | N9 | N12 | N15 | N18 | N21 | N24 | N27 | N30 | N33 | N36 F TO |")
print(" T2 | C2 C4 C6 C8 C10 C12 C14 C16 C18 C20 C22 | 1 |")
print(" | S2>---^S5|S7--^S10|S12-^S15|S17-^S20|S22-^S25|S27-^S30|S32-^S35|S37-^S40|S42-^S45|S47-^S50|S52-^S55|S57>----|-----|")
print(" | | | | | | | | | | | | | | 2 |")
print("| 0 E 2 | 5 | 8 | 11 | 14 | 17 | 20 | 23 | 26 | 29 | 32 | 35 E TO |")
print("| Z0 E N2 | N5 | N8 | N11 | N14 | N17 | N20 | N23 | N26 | N29 | N32 | N35 E TO |")
print(" | (M) | C1 C3 C5 C7 C9 C11 C13 C15 C17 C19 C21 | 1 |")
print(" | S1>---^S4|S6---^S9|S11-^S14|S16-^S19|S21-^S24|S26-^S29|S31-^S34|S36-^S39|S41-^S44|S46-^S49|S51-^S54|S56>----|-----|")
print(" | | | | | | | | | | | | | | 2 |")
print(" | D 1 | 4 | 7 | 10 | 13 | 16 | 19 | 22 | 25 | 28 | 31 | 34 D TO |")
print(" T1 | | | | | | | | | | | | | 2 |")
print(" | D N1 | N4 | N7 | N10 | N13 | N16 | N19 | N22 | N25 | N28 | N31 | N34 D TO |")
print(" | | | | | | | | | | | | | | 1 |")
print(" |--------S3-------S8------S13------S18------S23------S28------S33------S38------S43------S48------S53-------|-----|")
print(" | P Q R S | T U V W | X Y Z O <-Streets|")
@ -217,27 +268,22 @@ def display_board():
# ------------------------- GLOBAL GAME VARIABLES ------------------------------
playing = True
dict_list = []
wager_type_list = []
bet = ""
wagers = []
DEBUG = False
def spin_wheel()-> int:
print()
time.sleep(2)
print("The wheel is spinning. Please wait...")
time.sleep(10)
time.sleep(8)
print("The ball has dropped.")
number = random.choice(wheel)
number = random.choice(range(37))
print("The ball landed on ", end=" --------> ")
time.sleep(5)
time.sleep(3)
print(number)
return number
def build_a_list_of_wager_types():
global DEBUG
wager_type_list = []
select = True
@ -263,6 +309,8 @@ def build_a_list_of_wager_types():
def build_a_dict_of_wagers(code):
global wagers
global DEBUG
bet = input("Enter a wager for this wager_type (e.g. 10 for $10) ")
wager = {code: bet}
wagers.append(wager)
@ -270,11 +318,11 @@ def build_a_dict_of_wagers(code):
print(f"Wagers: {wagers}")
return wagers
def search_number_in_dicts(dict_list: list, number_to_find: int):
matched_results = {}
unmatched_results = {}
matching_results = {}
unmatching_results = {}
global DEBUG
for i, d in enumerate(dict_list):
if not isinstance(d, dict):
@ -295,26 +343,24 @@ def search_number_in_dicts(dict_list: list, number_to_find: int):
unmatched_keys.append(k)
if matched_keys:
matched_results[f'dict_{i+1}'] = matched_keys
matching_results[f'dict_{i+1}'] = matched_keys
if DEBUG:
print(f"Matched Results for dict_{i+1}: {matched_keys}")
if unmatched_keys:
unmatched_results[f'dict_{i+1}'] = unmatched_keys
unmatching_results[f'dict_{i+1}'] = unmatched_keys
if DEBUG:
print(f"Unmatched Results for dict_{i+1}: {unmatched_keys}")
if DEBUG:
print(f"\nFinal Matched keys: {matched_results}")
print(f"Final Unmatched keys: {unmatched_results}")
print(f"\nFinal Matched keys: {matching_results}")
print(f"Final Unmatched keys: {unmatching_results}")
return matched_results, unmatched_results
return matching_results, unmatching_results
def tuple_matches_pattern(data, pattern):
return any(re.search(pattern, item) for item in data)
def match_wager_type(code):
global DEBUG
if DEBUG:
print(f"Code: {code}")
@ -325,14 +371,14 @@ def match_wager_type(code):
return wager_type
def log_match(wager_code, wager_amount, wager_type, payout):
global DEBUG
if DEBUG:
print(f"Matched {wager_code} as {wager_type}{wager_amount} × {payout} = {wager_amount * payout}")
def log_unmatched(wager_code, wager_amount, wager_type):
global DEBUG
if DEBUG:
print(f"Unmatched wager code: {wager_code} as {wager_type}{wager_amount} × -1 = {wager_amount * -1}")
unmatched = []
@ -341,8 +387,9 @@ def log_unmatched(wager_code, wager_amount, wager_type):
unmatched.append((wager_code, wager_amount))
def calculate_profits():
profits = 0
def calculate_profits(matched_results):
profits = 0
global DEBUG
for item in wagers:
for key, value in item.items():
if DEBUG:
@ -362,8 +409,10 @@ def calculate_profits():
print(f"Wager type: {wager_type}")
return profits
def calculate_losses():
losses = 0
def calculate_losses(unmatched_results):
losses = 0
global DEBUG
for item in wagers:
for key, value in item.items():
if DEBUG:
@ -388,43 +437,59 @@ def calculate_losses():
return losses
# ----------------------------- G A M E P L A Y ---------------------------------
holdings = int(input("Enter your initial holdings at the start of play (e.g. 1000 for $1000): "))
while playing:
display_board()
# User selects which dictionaries to search for number the wheel returns
wager_type_list = build_a_list_of_wager_types()
#number = spin_wheel()
matched_results, unmatched_results = search_number_in_dicts(wager_type_list, 4)
flat_list = [item for sublist in matched_results.values() for item in sublist]
wager_types_and_wagers = list(zip(flat_list, wagers))
if DEBUG:
print(f"Wager types and wagers list: {wager_types_and_wagers}")
profits = calculate_profits()
flat_list = [item for sublist in unmatched_results.values() for item in sublist]
if DEBUG:
print(f"Line 404: {flat_list}")
wager_types_and_wagers = list(zip(flat_list, wagers))
if DEBUG:
print(f"Line 407: {wagers}")
print(f"Line 405: wager_types_and_wagers: {wager_types_and_wagers}")
losses = calculate_losses()
holdings += (profits - losses)
print(f"Your profits are: ${profits} and losses are {losses} yielding current holdings: ${holdings}")
keep_current_wagers = input("Do you want to leave your wagers on the table? (y/n): ").lower()
if keep_current_wagers != 'y':
wagers.clear()
def keep_playing():
global playing
playing_again = input("Do you want to keep playing? (y/n) ")
if playing_again != "y":
playing = False
else:
continue
print("Thanks for playing!")
exit()
else:
return playing
# ----------------------------- G A M E P L A Y ---------------------------------
def main():
holdings = int(input("Enter your initial holdings at the start of play (e.g. 1000 for $1000): "))
global playing
global DEBUG
global wagers
while playing:
display_board()
# User selects which dictionaries to search for number the wheel returns
wager_type_list = build_a_list_of_wager_types()
number = spin_wheel()
matched_results, unmatched_results = search_number_in_dicts(wager_type_list, number)
flat_list = [item for sublist in matched_results.values() for item in sublist]
wager_types_and_wagers = list(zip(flat_list, wagers))
if DEBUG:
print(f"Wager types and wagers list: {wager_types_and_wagers}")
profits = calculate_profits(matched_results)
flat_list = [item for sublist in unmatched_results.values() for item in sublist]
if DEBUG:
print(f"Line 404: {flat_list}")
wager_types_and_wagers = list(zip(flat_list, wagers))
if DEBUG:
print(f"Line 407: {wagers}")
print(f"Line 405: wager_types_and_wagers: {wager_types_and_wagers}")
losses = calculate_losses(unmatched_results)
holdings += (profits - losses)
if holdings <= 0:
print("You are busted! Thanks for playing.")
exit()
else:
print(f"\nYour profits are: ${profits} and losses are {losses} yielding current holdings: ${holdings}")
print()
keep_playing()
if __name__ == "__main__":
main()