Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Nick join a rigged rock paper scissors tournament. In every game, he knows what his opponent is going to play, and the predetermined outcome (he wins, loses, or draws). Please help him determine what should he play for each turn.

Input Format
The first line of input is n, the number of games. Followed by a list of n strings, in the form of “C S”. C is either “O”, “[]”, or “8<”, meaning the opponent is going to play rock, paper, or scissors respectively for that turn. S is either “W”, “L”, or “D”, meaning the predetermined outcome for that turn is Bob wins, loses, or draws respectively.

Output Format
Output n strings, either “O”, “[]”, or “8<”, separated by commas, the move Bob should be playing for that turn.

Sample Input
n = 5
games = ["8< W", "[] W", "O D", "8< L", "O W"]
Sample Output
O,8<,O,[],[]
Explanation
For the first game, the opponent is going to play scissor, and Bob is supposed to win, so he plays rock.
For the third game, the opponent is going to play rock, and Bob is supposed to draw with his opponent, so he plays rock as well.
For the fourth game, the opponent is going to play scissors, but Bob is supposed to lose, so he plays paper.

What I have tried:

I tried in C++,but it didn't work.
C++
int main()
{
const char* ROCK = "O";
const char* PAPER = "[]";
const char* SCISSORS = "8<";
 
const char* CurrentMove (char C, char S)
{
   const char* Result = 0;
 
   switch (S)     // Outer decision, based upon the desired result
   {
   case 'W':      // Win
      switch (C)  // Inner decision, based upon the opponent's move
      {
      case 'O':   Result = PAPER;      break;   // ROCK[0]
      case '[':   Result = SCISSORS;   break;   // PAPER[0]
      default:    Result = ROCK;                // Must be SCISSORS[0]
      }
   case 'L':      // Lose
      switch (C)  // Inner decision, based upon the opponent's move
      {
      case 'O':   Result = SCISSORS;   break;   // ROCK[0]
      case '[':   Result = ROCK;       break;   // PAPER[0]
      default:    Result = PAPER;               // Must be SCISSORS[0]
      }
   default:       // Must be Draw
      switch (C)  // Inner decision, based upon the opponent's move
      {
      case 'O':   Result = ROCK;       break;   // ROCK[0]
      case '[':   Result = PAPER;      break;   // PAPER[0]
      default:    Result = SCISSORS;            // Must be SCISSORS[0]
      }
   }
   return Result;
}
}
Posted
Updated 21-Oct-18 5:42am
v3
Comments
economistlarry 21-Oct-18 11:24am    
My code is at below but I don't know why it does not works.
Patrice T 21-Oct-18 11:31am    
your code is not here.
Use Improve question to update your question.
economistlarry 21-Oct-18 11:32am    
int main()
{
const char* ROCK = "O";
const char* PAPER = "[]";
const char* SCISSORS = "8<";

const char* CurrentMove (char C, char S)
{
const char* Result = 0;

switch (S) // Outer decision, based upon the desired result
{
case 'W': // Win
switch (C) // Inner decision, based upon the opponent's move
{
case 'O': Result = PAPER; break; // ROCK[0]
case '[': Result = SCISSORS; break; // PAPER[0]
default: Result = ROCK; // Must be SCISSORS[0]
}
case 'L': // Lose
switch (C) // Inner decision, based upon the opponent's move
{
case 'O': Result = SCISSORS; break; // ROCK[0]
case '[': Result = ROCK; break; // PAPER[0]
default: Result = PAPER; // Must be SCISSORS[0]
}
default: // Must be Draw
switch (C) // Inner decision, based upon the opponent's move
{
case 'O': Result = ROCK; break; // ROCK[0]
case '[': Result = PAPER; break; // PAPER[0]
default: Result = SCISSORS; // Must be SCISSORS[0]
}
}
return Result;
}
}
economistlarry 21-Oct-18 11:38am    
Can you tell me where is wrong,my input or the code
economistlarry 21-Oct-18 11:32am    
I input as this:
n = 5
games = ["8< W", "[] W", "O D", "8< L", "O W"]

C++
int main() {
   play(paper);
   play(something);
   play(scissors);
   play(paper);
   return 0;
   }

Hope this helps get you started < /tongue-in-cheek > Or maybe it will make you mad enough to try on your own, and show this a***hole (being me) what a jerk I am by succeeding. Or at least taking a good stab at the solution.
 
Share this answer
 
v2
Quote:
Can I have the code for this problem in C++?
...
I tried in C++,but it didn't work.

This site is for programmers, so we help you to fix your code, but we don't do your HomeWork.
So show your code and explain what is the problem.
HomeWork problems are simplified versions of the kind of problems you will have to solve in real life, their purpose is learning and practicing.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.

[Update]
- This code don't work because it is not C++, you can't define a function inside another function.
- This code don't work because it do not handle any user input or display a result.
- This code don't work because CurrentMove have nested switch and you also need to break for outer switch.
 
Share this answer
 
v2
No. We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900