Solved

rock paper scissor in c#

Posted on 2012-04-07
13
4,549 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

861 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