Not logged in. Log in with GitHub. About Websheets.

List of all available Websheets


Viewing cpp/functions/rps by redekopp@usc.edu. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
PropertyValue
Description
html markup
shown to student
 
<p>In a previous exercise you wrote code to implement a best of n match for <strong>Rock, Paper, Scissors</strong>.</p>
<p>Now take that code and extract a function <tt>int rps()</tt> that simulates a <strong>SINGLE</strong> game and returns:</p>
<ul><li>1 if Player 1 wins
   <li>2 if Player 2 wins
      <li>0 if it is a tie
</ul>
<p>Also update the code to make a <strong>best-of-n</strong> match where <tt>n</tt> is an odd number entered by the user at the start of the program.</p>
<p>Your program should output the result of each game in the format:</p>
<p><tt>Game {1,2,3} Winner = P1</tt> ...or...
   <tt>Game {1,2,3} Winner = P2</tt> ...or...
   <tt>Game {1,2,3} Tie</tt>
</p>
<p>As soon as a winner of the best-of-n match can be determined, quit the program and output:</p>
<p><tt>P1 wins match</tt> ...or...
   <tt>P2 wins match</tt> ...or...
   <tt>No winner</tt> (if there is a tie)
</p>
Public permissions remove
Remarks
Comments, history, license, etc.
 
Copied from problem cpp/for/rps-bestof3 (author: redekopp@usc.edu)
Copied from problem cpp/conditionals/rps (author: redekopp@usc.edu)
Copied from problem cpp/conditionals/taxbrackets (author: redekopp@usc.edu)
Copied from problem cpp/conditionals/nestedec (author: redekopp@usc.edu)
Copied from problem cpp/conditionals/extracredit (author: redekopp@usc.edu)
Copied from problem cpp/control/flag (author: daveagp@gmail.com)
remove
Engine
Template / Reference solution
 
#include <iostream>
#include <iomanip>
using namespace std;
int rps()
{
   int winner = 0;
   \[
      cout << "Enter the choices of the 2 players: " << endl;
      int choice1, choice2;      
      cin >> choice1 >> choice2;
      if( choice1 == choice2){
         winner = 0;
      }
      else if( ((choice1 == 0) && (choice2 == 2)) ||
              ((choice1 == 1) && (choice2 == 0)) ||
              ((choice1 == 2) && (choice2 == 1)) )
      {
         winner = 1;
      }
      else
      {
         winner = 2;
      }
  ]\ 
  return winner;
}
int main() {
   int p1 = 0, p2 = 0;
   int n;
   cin >> n; // get n for best-of-n match
   \[
   for(int i = 1; i <= n; i++)
   {
      int winner = rps();
      if(winner == 1){
         cout << "Game " << i << " winner = P1" << endl;
         p1++;
      }
      else if(winner == 2){
         cout << "Game " << i << " winner = P2" << endl;
         p2++;
      }
      else {
         cout << "Game " << i << " Tie" << endl;         
      }
      if(p1 == (n+1)/2 || p2 == (n+1)/2){
         break;  
      }
   }
   if(  p1 > p2  ) {
     cout << "P1 wins match" << endl;  
   }
   else if(p1 < p2){
     cout << "P2 wins match" << endl;        
   }
   else {
     cout << "No winner" << endl;  
   }
            
\show:      
   for(int i = 1; i <= 3; i++)
   {
      cout << "Enter the choices of the 2 players: " << endl;
      int choice1, choice2;      
      cin >> choice1 >> choice2;
      if( choice1 == choice2){
         cout << "Game " << i << " Tie" << endl;
      }
      else if( ((choice1 == 0) && (choice2 == 2)) ||
               ((choice1 == 1) && (choice2 == 0)) ||
               ((choice1 == 2) && (choice2 == 1)) )
      {
         cout << "Game " << i << " winner = P1" << endl;
         p1++;
      }
      else
      {
         cout << "Game " << i << " winner = P2" << endl;
         p2++;
      }
      if(p1 == 2 || p2 == 2){
         break;  
      }
   }
   if(  p1 > p2  ) {
     cout << "P1 wins match" << endl;  
   }
   else if(p1 < p2){
     cout << "P2 wins match" << endl;        
   }
   else {
     cout << "No winner" << endl;  
   }
   ]\
   return 0;
}
C++ test suite
json list of stdin/args tests
e.g. [{"stdin":"hi", "args":["4", "5"]},
{"stdin":"noargs"}]

to just run once with no input use [{}]
 
[
   {"stdin": "3 0 1 0 2 1 2", "args": ""},
   {"stdin": "3 0 2 0 2 1 2", "args": ""},
   {"stdin": "3 2 0 2 0 2 1", "args": ""},
   {"stdin": "7 0 1 0 1 0 1 0 1 1 0 1 0 1 0", "args": ""},
   {"stdin": "5 2 0 0 2 2 2 2 0 0 2", "args": ""}
]
Solution visibility remove


Optional properties:

Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.