Notices

Overclockers Forums > Software > Programming Tips and Tricks
Programming Tips and Tricks
Forum Jump

Rock, Paper, Scissors Game (C#)

Post Reply New Thread Subscribe Search this Thread
 
 
Thread Tools
Old 10-29-09, 05:27 PM Thread Starter   #1
asusradeon
Member

 
asusradeon's Avatar 

Join Date: Oct 2004
Location: 127.0.0.1

 
Rock, Paper, Scissors Game (C#)


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.

Quote:
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
Attached Files
File Type: txt Ex63.txt (4.5 KB, 846 views)

__________________
Intel C2D T6400 2.0Ghz -- Dell Studio 1555 -- 4048MB -- Windows 7 Pro
AMD Athlon X2 4200+ -- Asus V3-M2V890 -- 2GB Kingston -- Windows 7 Pro
Intel C2D T5870 2.0GHZ -- Vostro 1510 -- 2048MB -- Windows XP SP2

ASCII stupid question, get a stupid ANSI !


asusradeon is offline   QUOTE Thanks
Old 10-29-09, 06:11 PM   #2
Cowboy Shane
Member



Join Date: Jan 2002
Location: Augusta, GA

10 Year Badge
 
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...

__________________
I contend that we are both atheists. I just believe in one fewer god than you do. When you understand why you dismiss all the other possible gods, you will understand why I dismiss yours. -- Stephen Roberts
Cowboy Shane is offline   QUOTE Thanks
Old 10-29-09, 06:41 PM   #3
Cowboy Shane
Member



Join Date: Jan 2002
Location: Augusta, GA

10 Year Badge
 
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.

__________________
I contend that we are both atheists. I just believe in one fewer god than you do. When you understand why you dismiss all the other possible gods, you will understand why I dismiss yours. -- Stephen Roberts
Cowboy Shane is offline   QUOTE Thanks
Old 10-30-09, 07:26 AM   #4
Trombe
Member

 
Trombe's Avatar 

Join Date: Mar 2005
Location: Austin, Texas

 
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.
Trombe is offline   QUOTE Thanks
Old 10-31-09, 06:04 AM Thread Starter   #5
asusradeon
Member

 
asusradeon's Avatar 

Join Date: Oct 2004
Location: 127.0.0.1

 
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

__________________
Intel C2D T6400 2.0Ghz -- Dell Studio 1555 -- 4048MB -- Windows 7 Pro
AMD Athlon X2 4200+ -- Asus V3-M2V890 -- 2GB Kingston -- Windows 7 Pro
Intel C2D T5870 2.0GHZ -- Vostro 1510 -- 2048MB -- Windows XP SP2

ASCII stupid question, get a stupid ANSI !


asusradeon is offline   QUOTE Thanks
Old 10-31-09, 10:17 PM   #6
seadave77

 
seadave77's Avatar 

Join Date: Jul 2002
Location: Newnan, GA

 
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.
Attached Files
File Type: txt Ext63_CD.txt (4.5 KB, 329 views)
seadave77 is offline   QUOTE Thanks
Old 11-01-09, 04:30 AM Thread Starter   #7
asusradeon
Member

 
asusradeon's Avatar 

Join Date: Oct 2004
Location: 127.0.0.1

 
Quote:
Originally Posted by seadave77 View Post
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

__________________
Intel C2D T6400 2.0Ghz -- Dell Studio 1555 -- 4048MB -- Windows 7 Pro
AMD Athlon X2 4200+ -- Asus V3-M2V890 -- 2GB Kingston -- Windows 7 Pro
Intel C2D T5870 2.0GHZ -- Vostro 1510 -- 2048MB -- Windows XP SP2

ASCII stupid question, get a stupid ANSI !


asusradeon is offline   QUOTE Thanks

Post Reply New Thread Subscribe


Overclockers Forums > Software > Programming Tips and Tricks
Programming Tips and Tricks
Forum Jump

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Mobile Skin
All times are GMT -5. The time now is 08:53 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
You can add these icons by updating your profile information to include your Heatware ID, Benching Profile ID or your Folding/SETI profile ID. Edit your profile!
X

Welcome to Overclockers.com

Create your username to jump into the discussion!

New members like you have made this the best community on the Internet since 1998!


(4 digit year)

Why Join Us?

  • Share experience
  • Max out your hardware
  • Best forum members anywhere
  • Customized forum experience

Already a member?