Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <time.h>
- int EnemiesChoice()
- {
- int randomlygeneratedchoice = (rand() % 3) + 1;
- return randomlygeneratedchoice;
- }
- int PlayGame(int compChoice, int userChoice)
- {
- if (compChoice == userChoice)
- {
- return 0;
- }
- if (compChoice == 1 && userChoice == 2)
- {
- return 1;
- }
- if (compChoice == 2 && userChoice == 3)
- {
- return 1;
- }
- if (compChoice == 3 && userChoice == 1)
- {
- return 1;
- }
- return 2;
- }
- int main()
- {
- srand(time(0));
- bool isGameOver = false;
- int enemiesChoice, usersChoice;
- do
- {
- enemiesChoice = EnemiesChoice();
- std::cout << "Please chose one fo the following: " << std::endl << std::endl;
- std::cout << "1. Rock" << std::endl;
- std::cout << "2. Paper" << std::endl;
- std::cout << "3. Scissors" << std::endl << std::endl;
- std::cout << "Your Choice: ";
- std::cin >> usersChoice;
- if (usersChoice == 1 || usersChoice == 2 || usersChoice == 3)
- {
- int gameResult = PlayGame(enemiesChoice, usersChoice);
- if (gameResult == 1) //User won
- {
- std::cout << "Congrats! You won!" << std::endl;
- isGameOver = true;
- }
- else if (gameResult == 2) //Computer won
- {
- std::cout << "Sorry, You lost!" << std::endl;
- isGameOver = true;
- }
- else // it was a tie :(
- {
- std::cout << "It was a tie! Play again to determine the winner!" << std::endl << std::endl;
- }
- }
- else
- {
- std::cout << "Please enter a valid choice!" << std::endl << std::endl;
- }
- } while (!isGameOver);
- std::cout << "Thanks for playing!" << std::endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment