From 2d584fe87f9fc9d2f40841dc9b8ca485f81fa234 Mon Sep 17 00:00:00 2001 From: Jiahuita Date: Wed, 27 Mar 2024 13:19:05 -0400 Subject: [PATCH] Attempt 2 to fix infinite loop. --- example.py | 2 +- src/new_york_minute/__main__.py | 40 ++++++++++++++++++++++++++++----- src/new_york_minute/game.py | 24 +++++++++++--------- 3 files changed, 48 insertions(+), 18 deletions(-) diff --git a/example.py b/example.py index 47cd326..7b3cbb0 100644 --- a/example.py +++ b/example.py @@ -1,4 +1,4 @@ -from new_york_minute import solo_game, multiplayer_game, custom_game, load_game +from new_york_minute.game import solo_game, multiplayer_game, custom_game, load_game # Example 1: Solo game print("Example 1: Solo game") diff --git a/src/new_york_minute/__main__.py b/src/new_york_minute/__main__.py index 6baf9c3..3bb8662 100644 --- a/src/new_york_minute/__main__.py +++ b/src/new_york_minute/__main__.py @@ -1,11 +1,39 @@ -from new_york_minute.game import solo_game +from new_york_minute.game import solo_game, multiplayer_game, custom_game, load_game def main(): - print("Welcome to New York Minute! Starting New Game!") - player_name = input("What's your name? ") - player_dream = input("What's your dream in New York City? ") - player_background = input("Choose your background (e.g. Artist/Entrepreneur/Student): ") - solo_game(player_name, player_dream, player_background) + print("Welcome to New York Minute!") + game_type = input("Choose the type of game ('solo', 'multiplayer', 'custom', 'load'): ") + if game_type == 'solo': + print("Starting New Game!") + player_name = input("What's your name? ") + player_dream = input("What's your dream in New York City? ") + player_background = input("Choose your background (e.g. Artist/Entrepreneur/Student): ") + solo_game(player_name, player_dream, player_background) + elif game_type == 'multiplayer': + num_players = int(input("Enter the number of players: ")) + player_names = [] + player_dreams = [] + player_backgrounds = [] + for i in range(num_players): + player_names.append(input(f"Enter the name for Player {i+1}: ")) + player_dreams.append(input(f"Enter the dream for Player {i+1}: ")) + player_backgrounds.append(input(f"Enter the background for Player {i+1}: ")) + multiplayer_game(player_names, player_dreams, player_backgrounds) + elif game_type == 'custom': + num_players = int(input("Enter the number of players: ")) + player_names = [] + player_dreams = [] + player_backgrounds = [] + for i in range(num_players): + player_names.append(input(f"Enter the name for Player {i+1}: ")) + player_dreams.append(input(f"Enter the dream for Player {i+1}: ")) + player_backgrounds.append(input(f"Enter the background for Player {i+1}: ")) + custom_game(num_players, player_names, player_dreams, player_backgrounds) + elif game_type == 'load': + save_file = input("Enter the path to the save file: ") + load_game(save_file) + else: + print("Invalid game type. Please try again.") if __name__ == "__main__": main() \ No newline at end of file diff --git a/src/new_york_minute/game.py b/src/new_york_minute/game.py index a339d25..fb8b5e9 100644 --- a/src/new_york_minute/game.py +++ b/src/new_york_minute/game.py @@ -26,19 +26,21 @@ def start_game(players, actions=None): if isinstance(players, list): for player in players: print(f"Welcome, {player.name}!") + completed_players = [] + while len(completed_players) < len(players): + for player in players: + if player not in completed_players: + if play_turn(player, actions): + completed_players.append(player) + print(f"{player.name} has achieved their dream!") + else: + print(f"{player.name}'s turn is over.") + print("All players have completed their turns.") else: print(f"Welcome, {players.name}!") - - completed_players = [] - while len(completed_players) < len(players): - for player in players: - if player not in completed_players: - if play_turn(player, actions): - completed_players.append(player) - print(f"{player.name} has achieved their dream!") - else: - print(f"{player.name}'s turn is over.") - print("All players have completed their turns.") + while True: + if play_turn(players, actions): + break print("The game has ended. Thank you for playing!")