Solved

rock paper scissor in c#

Posted on 2012-04-07
13
4,248 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 
LVL 17

Expert Comment

by:ramrom
Comment Utility
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
Comment Utility
Thank you, still working on it but, I will get it with all of your help :0)
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
Comment Utility
Wow, thanks! I am going to really study this program, beautiful.... awesome!
0
 
LVL 17

Expert Comment

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

Author Comment

by:December2000
Comment Utility
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
Comment Utility
My apologies to Idle_Mind, I clicked the wrong solution for the points in error.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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 …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

744 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

16 Experts available now in Live!

Get 1:1 Help Now