Gargit

Functions Exercise Part 17

Aug 20th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <time.h>
  4.  
  5. int EnemiesChoice()
  6. {
  7.     int randomlygeneratedchoice = (rand() % 3) + 1;
  8.     return randomlygeneratedchoice;
  9. }
  10.  
  11. int PlayGame(int compChoice, int userChoice)
  12. {
  13.     if (compChoice == userChoice)
  14.     {
  15.         return 0;
  16.     }
  17.     if (compChoice == 1 && userChoice == 2)
  18.     {
  19.         return 1;
  20.     }
  21.     if (compChoice == 2 && userChoice == 3)
  22.     {
  23.         return 1;
  24.     }
  25.     if (compChoice == 3 && userChoice == 1)
  26.     {
  27.         return 1;
  28.     }
  29.  
  30.     return 2;
  31. }
  32.  
  33. int main()
  34. {
  35.     srand(time(0));
  36.     bool isGameOver = false;
  37.     int enemiesChoice, usersChoice;
  38.  
  39.     do
  40.     {
  41.         enemiesChoice = EnemiesChoice();
  42.         std::cout << "Please chose one fo the following: " << std::endl << std::endl;
  43.         std::cout << "1. Rock" << std::endl;
  44.         std::cout << "2. Paper" << std::endl;
  45.         std::cout << "3. Scissors" << std::endl << std::endl;
  46.         std::cout << "Your Choice: ";
  47.         std::cin >> usersChoice;
  48.  
  49.         if (usersChoice == 1 || usersChoice == 2 || usersChoice == 3)
  50.         {
  51.             int gameResult = PlayGame(enemiesChoice, usersChoice);
  52.  
  53.             if (gameResult == 1) //User won
  54.             {
  55.                 std::cout << "Congrats! You won!" << std::endl;
  56.                 isGameOver = true;
  57.             }
  58.             else if (gameResult == 2) //Computer won
  59.             {
  60.                 std::cout << "Sorry, You lost!" << std::endl;
  61.                 isGameOver = true;
  62.             }
  63.             else // it was a tie :(
  64.             {
  65.                 std::cout << "It was a tie! Play again to determine the winner!" << std::endl << std::endl;
  66.             }
  67.         }
  68.         else
  69.         {
  70.             std::cout << "Please enter a valid choice!" << std::endl << std::endl;
  71.         }
  72.  
  73.     } while (!isGameOver);
  74.  
  75.     std::cout << "Thanks for playing!" << std::endl;
  76.  
  77.     system("pause");
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment