fork download
  1. /**
  2.  * PROGRAM: rps.cpp
  3.  * AUTHOR: <#Type your own name here#>
  4.  * UNIQNAME: <#Type your uniqname here#>
  5.  *
  6.  * EECS 183: Project 2, Fall 2014
  7.  *
  8.  * <#A description of the project here#>
  9.  */
  10.  
  11. #include <iostream>
  12. #include <string>
  13.  
  14. using namespace std;
  15.  
  16. /*************************************************************************
  17.  * The following functions have already been implemented for you.
  18.  * You should use them when writing the other functions.
  19.  *************************************************************************/
  20.  
  21. /**
  22.  * Requires: Nothing.
  23.  * Modifies: Nothing.
  24.  * Effects : Prints a pretty header to introduce the user to the game.
  25.  */
  26. void printInitialHeader();
  27.  
  28. /**
  29.  * Requires: error_number be either 1 or 2.
  30.  * Modifies: Nothing.
  31.  * Effects : if error_number is 1, prints an error message indicating
  32.  * an illegal name was entered.
  33.  * if error_number is 2, prints an error message indicating
  34.  * an illegal move was entered.
  35.  */
  36. void printErrorMessage(int error_number);
  37.  
  38.  
  39. /*************************************************************************
  40.  * You must implement all of the following functions.
  41.  *************************************************************************/
  42.  
  43. /**
  44.  * Requires: player_number is either 1 or 2.
  45.  * Modifies: Nothing.
  46.  * Effects : Prompts the user to enter their name. Names may have spaces
  47.  * within them. Example: Kermit the Frog, Fozzie
  48.  * If an empty name is given, print error message 1, and
  49.  * return a default name.
  50.  * For player 1, the default name is: Rocky
  51.  * For player 2, the default name is: Hulk
  52.  * Prompt : Player [player_number], enter your name:
  53.  * Used In : main()
  54.  */
  55. string getName(int player_number);
  56.  
  57. /**
  58.  * Requires: Nothing.
  59.  * Modifies: Nothing.
  60.  * Effects : Returns true if and only if move is a valid move character.
  61.  * Used In : getMove()
  62.  */
  63. bool isMoveGood(char move);
  64.  
  65. /**
  66.  * Requires: player_name is the name of the player being prompted for a move.
  67.  * Modifies: Nothing.
  68.  * Effects : Prompts the player for their move and returns it.
  69.  * This function should accept the first non-whitespace character as
  70.  * the move
  71.  * If an illegal character is entered for their move, print error
  72.  * message 2 and return rock as a default.
  73.  * Prompt : [player_name], enter your move:
  74.  * Used In : doRound()
  75.  */
  76. char getMove(string player_name);
  77.  
  78. /**
  79.  * Requires: move is the move of the player being checked for a win.
  80.  * opponent_move is the move of the opponent.
  81.  * Modifies: Nothing.
  82.  * Effects : Returns true if and only if the player who made move won.
  83.  * Used In : doRound()
  84.  */
  85. bool isRoundWinner(char move, char opponent_move);
  86.  
  87. /**
  88.  * Requires: Nothing.
  89.  * Modifies: Nothing.
  90.  * Effects : If winner_name is empty, prints a message indicating the round is
  91.  * a draw. Otherwise, prints a congratulatory message to the winner.
  92.  * Prompt : This round is a draw!
  93.  * [winner_name] wins the round!
  94.  * Used In : doRound()
  95.  */
  96. void announceRoundWinner(string winner_name);
  97.  
  98. /**
  99.  * Requires: p1_name and p2_name are the names of the respective players.
  100.  * Modifies: Nothing.
  101.  * Effects : Simulates a complete round, returning 0 if the round was a draw;
  102.  * 1 if player 1 won; 2 if player 2 won.
  103.  * Used In : main()
  104.  */
  105. int doRound(string p1_name, string p2_name);
  106.  
  107. /**
  108.  * Requires: player_num is 1 or 2,
  109.  * r1_result, r2_result, r3_result is 0,1, or 2
  110.  * r1_result is result of round 1 play as determined by doRound(...)
  111.  * similar for r2_result and r3_result.
  112.  * Modifies: Nothing.
  113.  * Effects : Returns the number of times that player_num won.
  114.  * Used In : main()
  115.  */
  116. int getWinCount(int player_num, int r1_result, int r2_result, int r3_result);
  117.  
  118. /**
  119.  * Requires: Nothing.
  120.  * Modifies: Nothing.
  121.  * Effects : If winner_name is blank, prints that the game was a draw.
  122.  * Otherwise, prints a congratulatory message to the winner.
  123.  * Prompt : No winner!
  124.  * Congratulations [winner_name]!
  125.  * You won EECS 183 Rock-Paper-Scissors!
  126.  * Used In : main()
  127.  */
  128. void announceWinner(string winner_name);
  129.  
  130.  
  131. int main() {
  132. int player_number1 = 1;
  133. int player_number2 = 2;
  134. int round1;
  135. int round2;
  136. int round3 = 0;
  137. int player1_count;
  138. int player2_count;
  139. string player_name1;
  140. string player_name2;
  141. string no_winner = "";
  142.  
  143. // Print the header
  144. printInitialHeader();
  145.  
  146. // Get the player names
  147. player_name1 = getName(player_number1);
  148. player_name2 = getName(player_number2);
  149. cout << endl;
  150.  
  151. // Simulate the first 2 rounds
  152. round1 = doRound(player_name1, player_name2);
  153. cout << endl << endl;
  154. round2 = doRound(player_name1, player_name2);
  155.  
  156. // Check if either player already clinched a win:
  157. player1_count = getWinCount(player_number1, round1, round2, round3);
  158. player2_count = getWinCount(player_number2, round1, round2, round3);
  159.  
  160. // If they have, announce the winner.
  161. if ( (player1_count == 2) ) {
  162. cout << endl << endl;
  163. announceWinner(player_name1);
  164. }
  165. else if (player2_count == 2) {
  166. cout << endl << endl;
  167. announceWinner(player_name2);
  168. }
  169. else { // Otherwise, simulate the third round, find who has more wins,
  170. cout << endl << endl;
  171. round3 = doRound(player_name1, player_name2);
  172.  
  173. if ( (round3 == 1) ) { // and announce the winner.
  174. cout << endl << endl;
  175. announceWinner(player_name1);
  176. }
  177. else if ( (round3 == 2) ) {
  178. cout << endl << endl;
  179. announceWinner(player_name2);
  180. }
  181. else if ( (round3 == 0) ) {
  182. cout << endl << endl;
  183. announceWinner(no_winner);
  184. }
  185. }
  186. }
  187.  
  188.  
  189. /*************************************************************************
  190.  * Put all function implementations below here.
  191.  *************************************************************************/
  192.  
  193. void printInitialHeader() {
  194. cout << "----------------------------------------" << endl;
  195. cout << " EECS 183 " << endl;
  196. cout << " Rock-Paper-Scissors " << endl;
  197. cout << "----------------------------------------" << endl << endl;
  198. }
  199.  
  200. void printErrorMessage(int error_number) {
  201. if (error_number == 1) {
  202. cout << endl << "ERROR: Illegal name given, using default" << endl;
  203. } else if (error_number == 2) {
  204. cout << endl << "ERROR: Illegal move given, using default" << endl;
  205. } else {
  206. cout << "This should never print!";
  207. }
  208. }
  209.  
  210. string getName(int player_number) {
  211. string player_name = "";
  212. int error_number1 = 1;
  213.  
  214. if (player_number == 1) {
  215. cout << "Player " << player_number << ", enter your name: ";
  216. getline(cin, player_name);
  217. if (player_name == "") {
  218. printErrorMessage(error_number1);
  219. return player_name = "Rocky";
  220. }
  221. else {
  222. return player_name;
  223. }
  224. }
  225. else if (player_number == 2) {
  226. cout << "Player " << player_number << ", enter your name: ";
  227. getline(cin, player_name);
  228. if (player_name == "") {
  229. cout << endl;
  230. printErrorMessage(error_number1);
  231. return player_name = "Hulk";
  232. }
  233. else {
  234. return player_name;
  235. }
  236. }
  237.  
  238. return 0;
  239. }
  240.  
  241. bool isMoveGood(char move) {
  242. if ( (move == 'r') || (move == 'p') || (move == 's') ) {
  243. return true;
  244. }
  245. else {
  246. return false;
  247. }
  248. }
  249.  
  250. char getMove(string player_name) {
  251. int error_number2 = 2;
  252. char move;
  253.  
  254. cout << endl << player_name << ", enter your move: ";
  255. cin >> move;
  256. if (isMoveGood(move) == false) {
  257. cout << endl;
  258. printErrorMessage(error_number2);
  259. return move = 'r';
  260. }
  261. else {
  262. return move;
  263. }
  264. }
  265.  
  266. bool isRoundWinner(char move, char opponent_move) {
  267. if ( move == 'r' && opponent_move == 's') {
  268. return true;
  269. }
  270. else if (move == 'p' && opponent_move == 'r') {
  271. return true;
  272. }
  273. else if (move == 's' && opponent_move == 'p') {
  274. return true;
  275. }
  276. else {
  277. return false;
  278. }
  279. }
  280.  
  281. void announceRoundWinner(string winner_name) {
  282. if (winner_name == "") {
  283. cout << "This round is a draw!";
  284. }
  285. else {
  286. cout << endl << winner_name << " wins the round!";
  287. }
  288. }
  289.  
  290.  
  291. int doRound(string p1_name, string p2_name) {
  292. char p1_move = getMove(p1_name);
  293. char p2_move = getMove(p2_name);
  294. string no_winner = "";
  295.  
  296. if ( isRoundWinner(p1_move, p2_move) == false || isRoundWinner(p2_move, p1_move) ) {
  297. announceRoundWinner(no_winner);
  298. return 0;
  299. }
  300. else if ( isRoundWinner(p1_move, p2_move) == true ) {
  301. announceRoundWinner(p1_name);
  302. return 1;
  303. }
  304. else if ( isRoundWinner(p2_move, p1_move) == true) {
  305. announceRoundWinner(p2_name);
  306. return 2;
  307. }
  308. }
  309.  
  310. int getWinCount(int player_num, int r1_result, int r2_result, int r3_result) {
  311.  
  312. if (player_num == 1) {
  313. if (r1_result == 1 && r2_result == 1) {
  314. return 2;
  315. }
  316. }
  317. else if (player_num == 2) {
  318. if (r1_result == 2 && r2_result == 2) {
  319. return 2;
  320. }
  321. }
  322. return 0;
  323. }
  324.  
  325. void announceWinner(string winner_name) {
  326. if (winner_name == "") {
  327. cout << "No winner!";
  328. }
  329. else {
  330. cout << "Congratulations " << winner_name << "!" << endl;
  331. cout << "You won EECS 183 Rock-Paper-Scissors!";
  332. }
  333. }
Success #stdin #stdout 0.01s 5404KB
stdin
Standard input is empty
stdout
----------------------------------------
               EECS 183                 
          Rock-Paper-Scissors           
----------------------------------------

Player 1, enter your name: 
ERROR: Illegal name given, using default
Player 2, enter your name: 

ERROR: Illegal name given, using default


Rocky, enter your move: 

ERROR: Illegal move given, using default

Hulk, enter your move: 

ERROR: Illegal move given, using default
This round is a draw!


Rocky, enter your move: 

ERROR: Illegal move given, using default

Hulk, enter your move: 

ERROR: Illegal move given, using default
This round is a draw!


Rocky, enter your move: 

ERROR: Illegal move given, using default

Hulk, enter your move: 

ERROR: Illegal move given, using default
This round is a draw!

No winner!