Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- # Function to initialize game state
- def initialize_game():
- return {
- 'tokens': {
- 'red': {'big_eyes': 3, 'small_eyes': 3},
- 'green': {'big_eyes': 3, 'small_eyes': 3},
- 'blue': {'big_eyes': 3, 'small_eyes': 3},
- 'yellow': {'big_eyes': 3, 'small_eyes': 3}
- },
- 'grey_pieces': 0
- }
- def roll_dice():
- """Rolls a 6-sided dice and returns the result."""
- return random.randint(1, 6)
- def remove_eye_type(tokens, eye_type):
- """Removes a token of the specified eye type from the color with the most total tokens."""
- max_total_tokens = -1
- max_color = None
- # Find the color with the most total tokens
- for color, values in tokens.items():
- total_tokens = values['big_eyes'] + values['small_eyes']
- if total_tokens > max_total_tokens and values[eye_type] > 0: # Ensure it has the specified eye type
- max_total_tokens = total_tokens
- max_color = color
- if max_color:
- tokens[max_color][eye_type] -= 1
- else:
- return False # No valid token to remove
- return True # Successfully removed a token
- def remove_color(tokens, color):
- """Removes a token from the specified color based on the eye type with the highest total value."""
- # Calculate the total big eyes and small eyes across all colors
- total_big_eyes = sum(values['big_eyes'] for values in tokens.values())
- total_small_eyes = sum(values['small_eyes'] for values in tokens.values())
- # Choose the eye type with the highest total across all colors
- if total_big_eyes >= total_small_eyes and tokens[color]['big_eyes'] > 0:
- tokens[color]['big_eyes'] -= 1
- elif tokens[color]['small_eyes'] > 0:
- tokens[color]['small_eyes'] -= 1
- else:
- return False # No valid token to remove
- return True # Successfully removed a token
- def all_tokens_removed(tokens):
- """Checks if all tokens have been removed from the board."""
- return all(values['big_eyes'] == 0 and values['small_eyes'] == 0 for values in tokens.values())
- def remove_token(game_state, roll):
- """Removes a token based on the dice roll (color or eye type)."""
- tokens = game_state['tokens']
- # Roll outcomes: 1-4 = color, 5 = big eyes, 6 = small eyes
- if 1 <= roll <= 4:
- color_map = {1: 'red', 2: 'green', 3: 'blue', 4: 'yellow'}
- color = color_map[roll]
- if not remove_color(tokens, color):
- game_state['grey_pieces'] += 1 # No valid move, add a grey piece
- elif roll == 5:
- if not remove_eye_type(tokens, 'big_eyes'):
- game_state['grey_pieces'] += 1 # No valid move, add a grey piece
- elif roll == 6:
- if not remove_eye_type(tokens, 'small_eyes'):
- game_state['grey_pieces'] += 1 # No valid move, add a grey piece
- def play_game():
- """Plays a single game and returns whether the player won or lost."""
- game_state = initialize_game()
- # Game continues until either a win (all tokens removed) or loss (3 grey pieces)
- while True:
- roll = roll_dice()
- remove_token(game_state, roll)
- print(f"Rolled: {["","Red ","Green ","Blue ", "Yellow", "SmallE", "BigE "][roll]}, Tokens: {game_state}")
- if game_state['grey_pieces'] >= 3:
- return False # Loss condition
- elif all_tokens_removed(game_state['tokens']):
- return True # Win condition
- def simulate_games(num_games):
- """Simulates multiple games and counts wins and losses."""
- wins = 0
- losses = 0
- for _ in range(num_games):
- if play_game():
- wins += 1
- else:
- losses += 1
- print()
- return wins, losses
- # Simulate 1000 games
- num_games = 1000
- wins, losses = simulate_games(num_games)
- # Print the results
- print(f"Out of {num_games} games:")
- print(f"Wins: {wins}")
- print(f"Losses: {losses}")
- input()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement