Solved

rock paper scissor in c#

Posted on 2012-04-07
13
4,327 Views
Last Modified: 2012-04-08
What am I missing? This is not working but the requirements are that the player plays against the computer. I need thee computer to randomly generate the r,p,s and compare it to the users input and decide if the computer or the user win not as a loop but with if, else logic.  I can only make this work with numbers.  

    {
    public class Program
    {
        static void Main(string[] args)
        {
            string inputString;
            int r, p, s;

            r = 1;
            p = 2;
            s = 3;
           
            int randomNumber;
            int numberString;

            Random ranNumberGenerator = new Random();
            randomNumber = ranNumberGenerator.Next(4);


            Console.Write("Enter r, p, or c");
            inputString = Console.ReadLine();
            numberString = Convert.ToInt32(inputString);
            randomNumber = +ranNumberGenerator.Next(4);

            if (numberString == randomNumber)
                Console.WriteLine("Draw");
            else Console.WriteLine("Lose");

            Console.Read();
            

        }
    }
}

Open in new window

0
Comment
Question by:December2000
  • 6
  • 2
  • 2
  • +2
13 Comments
 
LVL 23

Expert Comment

by:basicinstinct
ID: 37820134
perhaps something along these lines...

static void Main(string[] args)
        {
            string inputString;
          string[] rps = new string[3] {"r", "p", "s"};
           
            int randomNumber;

            Random ranNumberGenerator = new Random();
            randomNumber = ranNumberGenerator.Next(4);


            Console.Write("Enter r, p, or c");
            inputString = Console.ReadLine();
            randomNumber = +ranNumberGenerator.Next(4);

            if (rps[randomNumber].Equals(inputString))
                Console.WriteLine("Draw");
            else Console.WriteLine("Lose");

            Console.Read();

        }
0
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 37820165
a little modification for validating user inputs:-
static void Main(string[] args)
        {
            string inputString;
          string[] rps = new string[3] {"r", "p", "s"};
           
            int randomNumber;

            Random ranNumberGenerator = new Random();
            randomNumber = ranNumberGenerator.Next(4);

bool testCondition = true;

            while(testCondition)
            {
                Console.WriteLine("Enter r, p or c");
                inputString = Console.ReadLine();
                
                if (inputString!= null && (inputString.Length == 1 && rps .Contains(inputString)))
                {
randomNumber = +ranNumberGenerator.Next(4);

            if (rps[randomNumber].Equals(inputString))
                Console.WriteLine("Draw");
            else Console.WriteLine("Lose");

                    testCondition = false;
                }
                else
                {
                    Console.WriteLine("Invalid Entry");
                }

            }

           Console.Read();

        }

Open in new window

0
 

Author Comment

by:December2000
ID: 37820173
Thank you,  ... I am not sure how to use
       if (rps[randomNumber].Equals(inputString))
                Console.WriteLine("Draw");
            else Console.WriteLine("Lose");

To compare each of these conditions.

/*In the game Rock Paper Scissors, two players simultaneously choose one of three options: rock, paper, or scissors.
 * If both players choose the same option, then the result is a tie. However, if they choose differently, the winner is etermined as follows:

-Rock beats scissors, because a rock can break a pair of scissors.

-Scissors beats paper, because scissors can cut paper.

- Paper bats rock, because a piece of paper can cover a rock.

Create a console-based game in which the computer randomly chooses rock, paper or scissors.
 * Let the user enter a character, 'r', 'p', or 's', each representing one of the three choices. Then, determine the winner. */
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37820175
0
 
LVL 17

Expert Comment

by:ramrom
ID: 37820220
for what it's worth - here's a Python versison

import random
win = ('rs', 'sp', 'pr')
while True:
    c = random.choice('rps')
    p = raw_input("Enter r (rock) p (paper) s (scissors) anything else to quit")
    if p in 'rps':
        print "computer chose " + c + " and you chose " + p + " - ",
        if p + c in win:  print "you win."
        elif p == c: print "draw."
        else: print "you lose."
    else: break

Open in new window

0
 

Author Closing Comment

by:December2000
ID: 37820390
Thank you, still working on it but, I will get it with all of your help :0)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 37820475
Here's a quick implementation:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string playerChoice;
            string computerChoice;
            Boolean quitting = false;
            string[] RPS = { "R", "P", "S" };
            Random ranNumberGenerator = new Random();
            
            Console.WriteLine("Welcome to Rock, Paper, Scissors!");
            do
            {
                Console.WriteLine("");
                Console.Write("Enter R, P, S, or Q to quit: ");
                playerChoice = Console.ReadLine().ToUpper();
                switch (playerChoice)
                {
                    case "R":
                    case "P":
                    case "S":
                        computerChoice = RPS[ranNumberGenerator.Next(RPS.Length)];
                        Console.WriteLine("Computer chose: " + computerChoice);
                        if (computerChoice == playerChoice)
                        {
                            Console.WriteLine("Player and Computer chose the same!");
                            Console.WriteLine("It's a Draw!");
                        }
                        else
                        {
                            switch (computerChoice)
                            {
                                case "R":
                                    if (playerChoice == "P")
                                    {
                                        Console.WriteLine("Paper wraps Rock!");
                                        Console.WriteLine("Player wins!");
                                    }
                                    else
                                    {
                                        Console.WriteLine("Rock smashes Scissors!");
                                        Console.WriteLine("Computer wins!");
                                    }
                                    break;

                                case "P":
                                    if (playerChoice == "R")
                                    {
                                        Console.WriteLine("Paper wraps Rock!");
                                        Console.WriteLine("Computer wins!");
                                    }
                                    else
                                    {
                                        Console.WriteLine("Scissors cut Paper!");
                                        Console.WriteLine("Player wins!");
                                    }
                                    break;

                                case "S":
                                    if (playerChoice == "P")
                                    {
                                        Console.WriteLine("Scissors cut Paper!");
                                        Console.WriteLine("Computer wins!");
                                    }
                                    else
                                    {
                                        Console.WriteLine("Rock smashes Scissors!");
                                        Console.WriteLine("Player wins!");
                                    }
                                    break;
                            }
                        }
                        break;

                    case "Q":
                        quitting = true;
                        Console.WriteLine("Thanks for playing!");
                        break;

                    default:
                        Console.WriteLine("Invalid entry");
                        break;
                }
            } while (!quitting);

            Console.WriteLine("");
            Console.WriteLine("Press enter to continue...");
            Console.Read();
        }
    }
}

Open in new window

0
 

Author Comment

by:December2000
ID: 37820488
Wow, thanks! I am going to really study this program, beautiful.... awesome!
0
 
LVL 17

Expert Comment

by:ramrom
ID: 37821224
I fail to see how the accepted solution answers the question. It only replies Draw or Lose!
0
 

Author Comment

by:December2000
ID: 37821280
You are correct @ ramrom... The correct answer was from by: Idle_MindPosted on 2012-04-07 at 19:31:12ID: 37820475
0
 

Author Comment

by:December2000
ID: 37821284
My apologies to Idle_Mind, I clicked the wrong solution for the points in error.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 37821300
Hmmm...pretty sure I posted AFTER the question was CLOSED.  I had the window open for several hours before I posted...   (got busy doing other stuff)
0
 

Author Comment

by:December2000
ID: 37821304
I see the time you are correct Idle_Mind... that means you did it just because... cool. Thanks to EVERYONE for all of your help :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now