• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Rock, Paper, Scissors Game (C#)

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

asusradeon

Member
Joined
Oct 25, 2004
Location
127.0.0.1
Hi Guys,

For my Uni course one of the exercises is to do a rock paper scissors game.. I've done how I think it should be done, and it works fine, however i'm sure there must be a simpler way to do it. I've attached my code and here is the task given on how it should function...

If anyone could point out an easier way to do it that would be good.

Exercise 6.3
Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows:
1. The computer generates a random number in the range of 1 to 3. If the number is 1, then the computer has chosen Rock. If the number is 2, then the computer has chosen Paper. If the number is 3, then the computer has chosen Scissors. Do not display the computer‟s choice yet.
2. The user enters his or her choice of „rock, „paper‟, „scissors‟ or „quit‟ at the keyboard.
3. If the user has entered „quit‟, then continue at step 7.
4. The computer‟s choice is displayed.
5. A winner is selected according to the following rules:
a. If one player chooses Rock and the other player chooses Scissors, then Rock wins (The rock smashes the scissors)
b. If one player chooses Scissors and the other player chooses Paper, then Scissors wins (Scissors cut paper)
c. If one player chooses Paper and the other player chooses Rock, then Paper wins (Paper wraps rock)
d. If both players make the same choice, then it is a draw.
6. Go back to step 1.
7. Display the number of wins for the user and the computer as well as the number of draws.

Thanks. J
 

Attachments

  • Ex63.txt
    4.5 KB · Views: 1,064
My first thought was to use an enumeration of the possible choices and convert the input to the string values in the enumeration, so I looked and found this site. That will remove your case statement and allow you to use more readable conditional statements.

Good job on what you have so far, though. Even with the case statement, I had no trouble following your logic. That is more than I can say for some of the code generated by a couple of my coworkers...
 
You could get really fancy with it and write a class for the players and overload the ==, <, and > operators.

Then, you could do stuff like if (player.Play > computer.Play), player wins.
 
Looks good the way it is: clarity is far more important than anything else.

You should try porting it to C, and you will quickly learn how easy C# is making your life.
 
OK, Thanks for the comments.. I'll have a look at enumerations.

Trombe: Might give that a try if i get bored at some point.. Hehe :p
 
LOL. I started trying to make it simpler but I don't know if it is. I broke it out into it's own class. The main loop is simpler I think but the class adds some complexity that may be difficult if you're not used to it. I'm a VB programmer so forgive me if my C# is out of whack.
 

Attachments

  • Ext63_CD.txt
    4.5 KB · Views: 423
LOL. I started trying to make it simpler but I don't know if it is. I broke it out into it's own class. The main loop is simpler I think but the class adds some complexity that may be difficult if you're not used to it. I'm a VB programmer so forgive me if my C# is out of whack.

Yeh most of that makes sense.. Thanks for this. Think i might recode it using the same ideas you've used but with a few alterations..

J
 
Back