Skip to content

Instantly share code, notes, and snippets.

@ehlzi
Created February 27, 2024 06:58
Show Gist options
  • Select an option

  • Save ehlzi/bf18944fff0ddca01f35760c5c2488ff to your computer and use it in GitHub Desktop.

Select an option

Save ehlzi/bf18944fff0ddca01f35760c5c2488ff to your computer and use it in GitHub Desktop.
Module Main();
// Constants
Constant Integer num_choices = 3;
// Local variables
Declare String userChoice;
Declare String computerChoice;
Declare String result;
Declare String playAgain = "yes";
// Main game loop
While playAgain == "yes" Do
// Get user's choice
userChoice = GetUserChoice();
// Get computer's choice
computerChoice = GetComputerChoice();
// Determine the winner
result = DetermineWinner(userChoice, computerChoice);
// Display results
Display "You chose " + userChoice;
Display "Computer chose " + computerChoice;
Display result;
// Prompt for replay
Display "Do you want to play again? (yes/no):";
Input playAgain;
// Validate play again input
While playAgain != "yes" And playAgain != "no" Do
Display "Please enter 'yes' or 'no':";
Input playAgain;
EndWhile;
EndWhile;
// End of game message
Display "Thank you for playing!";
End Module;
// Function to get the user's choice
Function GetUserChoice() Returns String;
// Variable for user's input
Declare String choice;
// Prompt for choice
Display "Choose rock, paper, or scissors:";
Input choice;
// Input validation
While choice != "rock" And choice != "paper" And choice != "scissors" Do
Display "Invalid choice. Please select rock, paper, or scissors:";
Input choice;
EndWhile;
Return choice;
End Function;
// Function to get the computer's choice
Function GetComputerChoice() Returns String;
// Array of choices
Declare String choices[3] = {"rock", "paper", "scissors"};
// Random choice
Declare Integer randomIndex = GenerateRandomNumber(0, num_choices - 1);
Return choices[randomIndex];
End Function;
// Function to determine the game winner
Function DetermineWinner(userChoice As String, computerChoice As String) Returns String;
// Game logic
If userChoice == computerChoice Then
Return "It's a tie!";
ElseIf (userChoice == "rock" And computerChoice == "scissors") Or (userChoice == "scissors" And computerChoice == "paper") Or (userChoice == "paper" And computerChoice == "rock") Then
Return "You win!";
Else
Return "Computer wins!";
EndIf;
End Function;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment