Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Game is fully functional. #12

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

SUDEEP-M-SHETTY
Copy link

@SUDEEP-M-SHETTY SUDEEP-M-SHETTY commented Apr 4, 2024

###This Python code implements a simple text-based game of Rock, Paper, Scissors between a player and the computer.
`import random

def determine_winner(player_choice, computer_choice):
if player_choice == computer_choice:
return "Tie"
elif (player_choice == "rock" and computer_choice == "scissors") or
(player_choice == "scissors" and computer_choice == "paper") or
(player_choice == "paper" and computer_choice == "rock"):
return "Win"
else:
return "Lose"

def print_result(result):
if result == "Win":
print("Congratulations! You won!")
elif result == "Lose":
print("Sorry! You lost.")
else:
print("It's a tie!")

def main():
player_score = 0
rounds_played = 0

while True:
    player_choice = input("Enter your choice (rock, paper, or scissors): ").lower()
    if player_choice not in ["rock", "paper", "scissors"]:
        print("Invalid option! Please choose from rock, paper, or scissors.")
        continue
    
    computer_choice = random.choice(["rock", "paper", "scissors"])
    print("Computer chose:", computer_choice)
    
    result = determine_winner(player_choice, computer_choice)
    print_result(result)
    
    if result == "Win":
        player_score += 1
    elif result == "Lose":
        player_score -= 1
    
    rounds_played += 1
    
    play_again = input("Do you want to play again? (yes/no): ").lower()
    if play_again != "yes":
        break

print("Game over!")
print("Your final score:", player_score)
print("Total rounds played:", rounds_played)

if name == "main":
main()
`

Below is the description of the code:

  1. Imports: The code imports the random module, which is used to generate random choices for the computer.

  2. determine_winner function: This function takes two arguments, player_choice and computer_choice, representing the choices made by the player and the computer respectively. It determines the winner based on the rules of Rock, Paper, Scissors and returns a string indicating the result ("Win", "Lose", or "Tie").

  3. print_result function: This function takes the result as input and prints a message to the player based on the outcome of the game ("Congratulations! You won!", "Sorry! You lost.", or "It's a tie!").

  4. main function: This function orchestrates the main logic of the game. It initializes variables for player_score (to keep track of the player's score) and rounds_played (to keep track of the total rounds played). It contains a while loop that continues until the player decides not to play again. Within the loop:

    • The player is prompted to enter their choice (rock, paper, or scissors).
    • If the input is not valid, the player is prompted again until a valid choice is entered.
    • The computer randomly chooses its option among rock, paper, and scissors.
    • The winner is determined using the determine_winner function, and the result is printed using the print_result function.
    • The player's score is updated based on the result.
    • The total rounds played is incremented.
    • The player is asked whether they want to play again. If the input is not "yes", the loop breaks, and the game ends.
  5. Execution: The main function is called when the script is run, which starts the game.

  6. End of Game: Once the player decides not to play again, a "Game over!" message is printed along with the player's final score and the total rounds played.

This provides a simple implementation of the classic game of Rock, Paper, Scissors, allowing players to play against a computer opponent.

@SUDEEP-M-SHETTY
Copy link
Author

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant