Download:Rock Paper Scissors/Source

From LizardWiki, FastLizard4's wiki and website
Jump to: navigation, search

This is the source code for the rudimentary rock paper scissors program. Return to the description page.

  1. /* PROGRAM WRITTEN BY FastLizard4 (http://lizardwiki.dyndns.org)
  2.    Licensed under the GNU General Public License. */
  3. #include <iostream>
  4. #include <cstdlib>
  5. #include <ctime>
  6. using namespace std;
  7.  
  8.  
  9. int main() {
  10. 	char proceed = 'n', humanPlayerInput = 'x';
  11. 	bool invalid = false;
  12. 	int randLowest = 1, randHighest = 3, computerPlayerInput = 0;
  13. 	int randRange = (randHighest - randLowest) + 1;
  14. 	int statsWin = 0, statsLoss = 0, statsDraw = 0;
  15. 	srand((unsigned)time(0));
  16.  
  17. 	do {
  18. 		do {
  19. 			invalid = false;
  20.  
  21. 			cout << "Please enter your choice, Rock, Paper, or Scissors (R/P/S): ";
  22. 			cin >> humanPlayerInput;
  23.  
  24. 			cout << "The computer is now choosing Rock, Paper or Scissors...\n";
  25. 			computerPlayerInput = randLowest + int(randRange * rand() / (RAND_MAX + 1.0));
  26.  
  27. 			switch(humanPlayerInput) {
  28. 				/*
  29. 				NOTE:
  30. 				FOR THE COMPUTER, 1 IS ROCK, 2 IS PAPER, AND 3 IS SCISSORS
  31. 				*/
  32. 				case 'r':
  33. 				case 'R':
  34. 					switch(computerPlayerInput) {
  35. 						case 1:
  36. 							cout << "The computer chose Rock.\n"
  37. 								 << "Nobody wins.\n";
  38. 							statsDraw++;
  39. 						break;
  40. 						case 2:
  41. 							cout << "The computer chose Paper.\n"
  42. 								 << "Paper covers rock; computer wins.\n";
  43. 							statsLoss++;
  44. 						break;
  45. 						case 3:
  46. 							cout << "The computer chose Scissors.\n"
  47. 								 << "Rock breaks scissors; you win!\n";
  48. 							statsWin++;
  49. 					}
  50. 				break;
  51. 				case 'p':
  52. 				case 'P':
  53. 					switch(computerPlayerInput) {
  54. 						case 1:
  55. 							cout << "The computer chose Rock.\n"
  56. 								 << "Paper covers rock; you win!\n";
  57. 							statsWin++;
  58. 						break;
  59. 						case 2:
  60. 							cout << "The computer chose Paper.\n"
  61. 								 << "Nobody wins.\n";
  62. 							statsDraw++;
  63. 						break;
  64. 						case 3:
  65. 							cout << "The computer chose Scissors.\n"
  66. 								 << "Scissors cut paper; computer wins.\n";
  67. 							statsLoss++;
  68. 					}
  69. 				break;
  70. 				case 's':
  71. 				case 'S':
  72. 					switch(computerPlayerInput) {
  73. 						case 1:
  74. 							cout << "The computer chose Rock.\n"
  75. 								 << "Rock breaks scissors; computer wins.\n";
  76. 						break;
  77. 						case 2:
  78. 							cout << "The computer chose Paper.\n"
  79. 								 << "Scissors cut paper; you win!\n";
  80. 						break;
  81. 						case 3:
  82. 							cout << "The computer chose Scissors.\n"
  83. 								 << "Nobody wins.\n";
  84. 					}
  85. 				break;
  86. 				default:
  87. 					invalid = true;
  88. 					cout << "ERROR: You did not make a valid choice.  Please try again.\n";
  89. 			}
  90. 		} while(invalid);
  91.  
  92. 		cout << "Play another round? (y/n) ";
  93. 		cin >> proceed;
  94. 		while(proceed != 'y' && proceed != 'Y' && proceed != 'n' && proceed != 'N') {
  95. 			cout << "Please enter 'y' for yes or 'n' for no.  Play another round? (y/n) ";
  96. 			cin >> proceed;
  97. 		}
  98. 	} while(proceed == 'y' || proceed == 'Y');
  99.  
  100. 	return 0; //Indicate successful program execution to the O.S.
  101. }