Link to home
Start Free TrialLog in
Avatar of December2000
December2000

asked on

rock paper scissor in c#

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

Avatar of basicinstinct
basicinstinct
Flag of Australia image

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();

        }
ASKER CERTIFIED SOLUTION
Avatar of BuggyCoder
BuggyCoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of December2000
December2000

ASKER

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. */
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

Thank you, still working on it but, I will get it with all of your help :0)
Avatar of Mike Tomlinson
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

Wow, thanks! I am going to really study this program, beautiful.... awesome!
I fail to see how the accepted solution answers the question. It only replies Draw or Lose!
You are correct @ ramrom... The correct answer was from by: Idle_MindPosted on 2012-04-07 at 19:31:12ID: 37820475
My apologies to Idle_Mind, I clicked the wrong solution for the points in error.
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)
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 :)