Results 1 to 7 of 7
  1. #1
    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.

    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 Attached Files
    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 !



  2. #2
    Member
    10 Year Badge
    Join Date
    Jan 2002
    Location
    Augusta, GA
    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

  3. #3
    Member
    10 Year Badge
    Join Date
    Jan 2002
    Location
    Augusta, GA
    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

  4. #4
    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.

  5. #5
    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 !



  6. #6
    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 Attached Files

  7. #7
    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 !



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •