Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

rock paper scissor in c#

Posted on 2012-04-07
13
Medium Priority
?
4,785 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 86

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 86

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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

Make the most of your online learning experience.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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 …

715 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