Advertisement
Guest User

game "s p s"

a guest
Oct 17th, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string>
  4. #include <random>
  5. #include <iostream>
  6. #include <time.h>
  7. using namespace std;
  8.  
  9. int checking(string user_ans) {
  10. if (user_ans == "Stone" || user_ans == "Paper" || user_ans == "Scissors" || user_ans == "Quit"); else {
  11. printf("Your word is uncorrect \n");
  12. exit(0);
  13. }
  14. return 0;
  15. }
  16. string generate_comp_ans() {
  17. srand(time(NULL) + rand());
  18. string comp_ans;
  19. int comp_answer= 1+rand()%3;
  20. if (comp_answer == 1) comp_ans = "Stone"; else
  21. if (comp_answer == 2) comp_ans = "Paper"; else
  22. if (comp_answer == 3) comp_ans = "Scissors";
  23. cout << "Computer answer is "<< comp_ans<<endl;
  24. return comp_ans;
  25. }
  26. int game(string user_ans, string comp_ans ) {
  27. int uscore = 1; int cscore = 2;
  28. if ((user_ans == "Stone" && comp_ans == "Scissors") || (user_ans == "Scissors" && comp_ans == "Paper") || (user_ans == "Paper" && comp_ans == "Stone"))
  29. return uscore; else
  30. if ((user_ans == "Stone" && comp_ans == "Paper") || (user_ans == "Scissors" && comp_ans == "Stone") || (user_ans == "Paper" && comp_ans == "Scissors"))
  31. return cscore; else
  32. if ((user_ans == "Stone" && comp_ans == "Stone") || (user_ans == "Scissors" && comp_ans == "Scissors") || (user_ans == "Paper" && comp_ans == "Paper"))
  33. return 0;
  34. }
  35.  
  36. int main(){
  37. string user_ans,comp_ans;
  38. int uscore = 0; int cscore = 0;
  39. while (user_ans != "Quit") {
  40. printf("Make a move \n");
  41. getline(std::cin, user_ans);
  42. checking(user_ans);
  43. comp_ans = generate_comp_ans();
  44. if (game(user_ans, comp_ans) == 0) cout<<"Dead heat "<<endl; else
  45. if (game(user_ans, comp_ans) == 1) {
  46. uscore++;
  47. cout << " You won " << endl;
  48. } else
  49. if (game(user_ans, comp_ans) == 2) {
  50. cscore++;
  51. cout << " You lost " << endl;
  52. }
  53. }
  54. cout << endl;
  55. cout << "Total score: " << endl;
  56. cout << "Your score is " << uscore << endl;
  57. cout << "Computer score is " << cscore << endl;
  58. return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement