rock paper scisors


SUBMITTED BY: smiley

DATE: Nov. 26, 2015, 2:34 a.m.

FORMAT: C++

SIZE: 3.2 kB

HITS: 1095

  1. /**********************************/
  2. /* Rock, Paper, Scissors */
  3. /* Made by: smiley */
  4. /**********************************/
  5. #include <stdio.h> //Input/Output
  6. #include <stdlib.h> //For rand, srand
  7. #include <stdbool.h> //For booleans
  8. #include <time.h> //For the seed
  9. /*Global data related to rock, paper, scissors*/
  10. typedef enum{
  11. ROCK = 0,
  12. PAPER,
  13. SCISSORS
  14. }Moves_t;
  15. typedef enum{
  16. VICTORY,
  17. DRAW,
  18. DEFEAT
  19. }Result;
  20. /*Returns a char array to output both moves*/
  21. char* getMove(Moves_t move){
  22. char* out = "Invalid";
  23. switch (move){
  24. case ROCK:
  25. out = "Rock";
  26. break;
  27. case PAPER:
  28. out = "Paper";
  29. break;
  30. case SCISSORS:
  31. out = "Scissors";
  32. break;
  33. }
  34. return out;
  35. }
  36. /*Returns the result*/
  37. Result getVictory(Moves_t my, Moves_t opp){
  38. int out = 0;
  39. switch (my){
  40. case ROCK:
  41. if (opp == PAPER){ out = DEFEAT; }
  42. else if (opp == ROCK){ out = DRAW; }
  43. else{ out = VICTORY; }
  44. break;
  45. case PAPER:
  46. if (opp == SCISSORS){ out = DEFEAT; }
  47. else if (opp == PAPER){ out = DRAW; }
  48. else{ out = VICTORY; }
  49. break;
  50. case SCISSORS:
  51. if (opp == ROCK){ out = DEFEAT; }
  52. else if (opp == SCISSORS){ out = DRAW; }
  53. else{ out = VICTORY; }
  54. break;
  55. }
  56. return out;
  57. }
  58. int main(int argc, char** argv){
  59. Moves_t myMove, oppMove; //Player's move, Opponent's move
  60. int myScore = 0, oppScore = 0; //Current score for both players
  61. char input = 0; //Input of the player
  62. bool isPlaying = true; //Bool to help the main game loop
  63. bool wrongInput = false; //Bool to restart the input if the user doesn't specify a valid move
  64. Result result = DRAW;
  65. char* menuText =
  66. "You: %i Opp: %i\n"
  67. "***************************\n"
  68. "* (R)ock *\n"
  69. "* (P)aper *\n"
  70. "* (S)cissors *\n"
  71. "***************************\n"
  72. "Enter a letter(R, P, S): "; //end of menuText
  73. srand(time(NULL));
  74. while (isPlaying){
  75. wrongInput = false; //reset our flag
  76. system("cls"); //clean the screen, WARNING: only for windows..
  77. /*Show the instructions*/
  78. printf(menuText, myScore, oppScore);
  79. /*Get the player's move*/
  80. input = fgetchar();
  81. switch (input){
  82. case 'R': case 'r':
  83. myMove = ROCK;
  84. break;
  85. case 'P': case 'p':
  86. myMove = PAPER;
  87. break;
  88. case 'S': case 's':
  89. myMove = SCISSORS;
  90. break;
  91. default:
  92. printf("\n\nYou have to choose either 'r', 'p' or 's'!\n\n");
  93. getchar();
  94. wrongInput = true; //set our flag to true
  95. break;
  96. }
  97. if (wrongInput){ continue; } //restart the input, invalid move was selected..
  98. else{
  99. /*Generate opponent's move*/
  100. oppMove = (Moves_t)rand() / (RAND_MAX / 3 + 1);
  101. printf("You: %s\nOpponent: %s\n\n", getMove(myMove), getMove(oppMove));
  102. result = getVictory(myMove, oppMove);
  103. /*Output the result and update the score*/
  104. switch (result){
  105. case VICTORY:
  106. printf("You Win!");
  107. myScore++;
  108. break;
  109. case DRAW:
  110. printf("DRAW !");
  111. break;
  112. case DEFEAT:
  113. printf("Opponent Wins!");
  114. oppScore++;
  115. break;
  116. }
  117. getchar(); getchar();
  118. }
  119. }
  120. }

comments powered by Disqus