Write a Program to Score the Paper-rock-scissor Game C++
How to simplfy my code? Paper-rock-scissor game
This is an exercise from the textbook by Savitch
Write a program to score the paper-rock-scissor game. Each of two users types
in either P, R, or S. The program then announces the winner as well as the basis
for determining the winner: Paper covers rock, Rock breaks scissors,
Scissors cut paper, or Nobody wins. Be sure to allow the users to use lowercase
as well as uppercase letters. Your program should include a loop that lets
the user play again until the user says she or he is done.
And here's my code
| |
My program can run without problem but my question is:
Is it possible to simplify the code? It seems I can use less lines to finish the program.
What I have in mind is: I have every winning case, can I simply use the else statement to summarize all draw situations?
Or there is a even better way to make my code shorter?
Last edited on
Personally I would use toupper so you don't have to keep checking uppercase vs lowercase and I would use nested if statements.
http://www.cplusplus.com/reference/cctype/toupper/
You could only need 1 check for a tie if you do it correctly. How you have it is almost correct here.
| |
Here is 1 way of doing it, I'm sure there are nicer ways that it could be done though.
| |
Last edited on
| |
How about Player 1 types "P" and Player 2 types "p" or vice versa?
So the check should be like this?
if(p1 == p2 || toupper(p1) == p2 || tolower(p1) == p2)
which requires at least 3 checks.
Perhaps change the input to upper case immediately after input,
| |
From that point on, everything is certain to be upper case only.
| |
Is it simple enough?
Thanks everyone! I learn so many new codes now!
Topic archived. No new replies allowed.
Write a Program to Score the Paper-rock-scissor Game C++
Source: http://cplusplus.com/forum/beginner/119015
0 Response to "Write a Program to Score the Paper-rock-scissor Game C++"
Post a Comment