diff --git a/projects/snake_water_gun_game/Snake_Water_Gun_Game.py b/projects/snake_water_gun_game/Snake_Water_Gun_Game.py index fd492f01c..da8b3a4b2 100644 --- a/projects/snake_water_gun_game/Snake_Water_Gun_Game.py +++ b/projects/snake_water_gun_game/Snake_Water_Gun_Game.py @@ -1,83 +1,92 @@ -""" -snake water gun -10 times play using while +import random +from rich import print as rprint -Snake water gun -Snake Water Gun Game in Python -The snake drinks the water, the gun shoots the snake, and gun has no effect on water. +def get_computer_choice(choices: list[str]) -> str: + """ + Randomly selects a choice for the computer from the given options. + """ + return random.choice(choices) -NOTE: Install rich before playing this game by the command "pip install rich". -""" -import random -from rich import print as rprint +def determine_winner(user_choice: str, computer_choice: str) -> tuple[str, int]: + """ + Determines the winner of the current round. + Returns a tuple of winner ('Human', 'Computer', or 'Tie') and the points earned (1 or 0). + """ + if user_choice == computer_choice: + return "Tie", 0 -if __name__ == "__main__": + winning_combinations = { + "s": "w", # Snake drinks water + "w": "g", # Water extinguishes gun + "g": "s", # Gun shoots snake + } + + if winning_combinations[user_choice] == computer_choice: + return "Human", 1 + else: + return "Computer", 1 + + +def display_score(chances_left: int, total_chances: int, human_points: int, computer_points: int) -> None: + """ + Displays the current score and remaining chances. + """ + rprint(f"[yellow]{chances_left} chance(s) left out of {total_chances}.[/yellow]") + rprint(f"[green]Your points: {human_points}[/green]") + rprint(f"[red]Computer points: {computer_points}[/red]\n") + + +def snake_water_gun_game() -> None: + """ + Main function to play the Snake Water Gun game. + """ CHOICES = ["s", "w", "g"] + CHANCES = 10 - CHANCE = 10 - NO_OF_CHANCE = 0 - COMPUTER_POINT = 0 - HUMAN_POINT = 0 + human_points = 0 + computer_points = 0 - print(" \t \t \t \t Snake,Water,Gun Game\n \n") - rprint( - "[yellow]s[/yellow] for snake, [blue]w[/blue] for water, [green]g[/green] for gun \n" - ) + print("\t" * 4 + "Snake, Water, Gun Game\n") + rprint("[yellow]s[/yellow] for snake, [blue]w[/blue] for water, [green]g[/green]\n") + + for chance in range(1, CHANCES + 1): + user_choice = input("Enter your choice >> ").lower() + + if user_choice not in CHOICES: + rprint("[red]Invalid input! Please choose 's', 'w', or 'g'.[/red]\n") + continue + + computer_choice = get_computer_choice(CHOICES) + winner, points = determine_winner(user_choice, computer_choice) - # making the game in while - while NO_OF_CHANCE < CHANCE: - user_choice = input("Enter your choice >> ") - computer_choice = random.choice(CHOICES) - - if user_choice.lower() not in CHOICES: - rprint("[red]Wrong input!![/red] \n") - elif user_choice == computer_choice: - rprint("Tie!! 0 point to each \n ") - else: - if user_choice.lower() == "s": - if computer_choice == "g": - COMPUTER_POINT += 1 - WINNER = "Computer" - elif computer_choice == "w": - HUMAN_POINT += 1 - WINNER = "Human" - elif user_choice.lower() == "w": - if computer_choice == "s": - COMPUTER_POINT += 1 - WINNER = "Computer" - elif computer_choice == "g": - HUMAN_POINT += 1 - WINNER = "Human" - elif user_choice.lower() == "g": - if computer_choice == "s": - HUMAN_POINT += 1 - WINNER = "Human" - elif computer_choice == "w": - COMPUTER_POINT += 1 - WINNER = "Computer" - rprint( - f"You guessed [yellow]{user_choice.lower()}[/yellow] and Computer guessed [cyan]{computer_choice}.[/cyan]\n" - ) - rprint( - f"[{'green' if WINNER == 'Human' else 'red'}]{WINNER} wins 1 point[/{'green' if WINNER == 'Human' else 'red'}] \n" - ) - - NO_OF_CHANCE += 1 - rprint(f"{CHANCE - NO_OF_CHANCE} chance(s) are left out of {CHANCE} chances.\n") - - print("Game over!") - - if COMPUTER_POINT == HUMAN_POINT: - rprint("[yellow]Tie![/yellow]") - - elif COMPUTER_POINT > HUMAN_POINT: - rprint("[red]Computer won and you lost.[/red]") + if winner == "Human": + human_points += points + elif winner == "Computer": + computer_points += points + rprint( + f"You chose [yellow]{user_choice}[/yellow], Computer chose [cyan]{computer_choice}[/cyan].\n" + ) + rprint( + f"[{'green' if winner == 'Human' else 'red'}]{winner} wins this round![/{'green' if winner == 'Human' else 'red'}]\n" + ) + display_score(CHANCES - chance, CHANCES, human_points, computer_points) + + rprint("[bold magenta]Game over![/bold magenta]\n") + + if human_points > computer_points: + rprint("[green]Congratulations! You won the game![/green]") + elif human_points < computer_points: + rprint("[red]Computer wins! Better luck next time.[/red]") else: - rprint("[green]You won and Computer lost.[/green]") + rprint("[yellow]It's a tie![/yellow]") rprint( - f"[green]Your points: {HUMAN_POINT}\tComputer points: {COMPUTER_POINT}[/green]" + f"[green]Final Score:[/green] [blue]You: {human_points}, Computer: {computer_points}[/blue]" ) + + +if __name__ == "__main__": + snake_water_gun_game()