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

List of all available Websheets


Viewing cpp/conditionals/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>Implement the logic to decide who wins the classic game of chance: <strong>Rock, Paper, Scissors</strong></p>
<p>The user will type:</p>
<table
   <tr>
      <th>User input</th><th>Meaning</th>
   </tr>
   <tr>
      <td>0</td><td>Rock</td>
   </tr>
   <tr>
      <td>1</td><td>Paper</td>
   </tr>
   <tr>
      <td>2</td><td>Scissors</td>
   </tr>
</table>
     
<p>Recall: </p>
<ul>
   <li>Paper (1) beats rock (0) </li>
   <li>Scissors (2) beats paper (1)</li>
   <li>Rock (0) beats scissors (2)</li>
   <li>If the same choice is made, the result is a tie</li>
</ul>
<p>Your program will take in the 2 choices of the two players (first player1, then player2) and then output the winner in the format:</p>
<p><tt>Winner = P1</tt> ...or...
   <tt>Winner = P2</tt> ...or...
   <tt>Tie</tt>
</p>
Public permissions remove
Remarks
Comments, history, license, etc.
 
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 main() {
   int choice1, choice2;
   cout << "Enter the choices of the 2 players: " << endl;
   \[
   cin >> choice1 >> choice2;
 
   if( choice1 == choice2){
      cout << "Tie" << endl;
   }
   else if( ((choice1 == 0) && (choice2 == 2)) ||
            ((choice1 == 1) && (choice2 == 0)) ||
            ((choice1 == 2) && (choice2 == 1)) )
   {
      cout << "Winner = P1" << endl;
   }
   else
   {
      cout << "Winner = P2" << 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": "0 1", "args": ""},
   {"stdin": "0 2", "args": ""},
   {"stdin": "1 2", "args": ""},
   {"stdin": "1 0", "args": ""},
   {"stdin": "2 0", "args": ""},
   {"stdin": "2 1", "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'.