-1

I am making a rock, paper, scissors game. I am using an array to the three variables and am trying to randomize the outcome and compare the random string to the user input. I have tried many methods with the random function and the one in the code makes the most sense to me, but it doesn't work. I am trying to make this code as concise as possible. I have seen other ways of creating this game, but they seem inefficient to me. I believe sticking with an array will allow my code to shorten and run faster. Please help me out and guide me on the right direction, thank you.

 using System;

namespace ConsoleApp1
{


    class MyArray
    {




        static void Main(string[] args)
        {

            var array = new string[] { "rock", "paper", "scissors" };
            var array = new Random();


            Console.WriteLine("Rock, paper, or scissors?");
            string rps = Console.ReadLine();

            switch (rps)
            {
                case "Rock":
                //??
                    break;
                case "Paper":
                //??
                    break;
                case "Scissors":
                //??
                    break;
            }

        }
        }
}

1 Answers1

-1

In C# we use the System.Random class to generate a random value that you can use to select from an array or case logic. What you want to use is the .Next(int) method to get the index to select from an array.

See me docs on rand: https://learn.microsoft.com/en-us/dotnet/api/system.random?view=netframework-4.8

Once you have the ai response value, we can use simple comparison logic and print out the result.

using System;

namespace ConsoleApp1
{
    class MyArray
    {
        static void Main(string[] args)
        {

            var array = new string[] { "rock", "paper", "scissors" };
            var rand = new Random();
            Console.WriteLine("Rock, paper, or scissors?");
            string rps = Console.ReadLine().ToLower().Trim();   // simple sanitation

            var aiResponse = array[rand.Next(array.Length)]; // this is the PC's response
            Console.WriteLine(" PC plays: {0}", aiResponse);

            // Now compare input to ai
            if (rps == aiResponse)
                Console.WriteLine("Draw!!!");
            else
            {
                bool win = false;
                switch (rps.ToLower())
                {
                    case "rock":
                        win = aiResponse == "scissors";
                        break;
                    case "paper":
                        win = aiResponse == "rock";
                        break;
                    case "scissors":
                        win = aiResponse == "paper";
                        break;
                    default:
                        throw new ApplicationException("invalid input, expected: rock, paper or scissors");
                }

                if (win)
                    Console.WriteLine("You win!");
                else
                    Console.WriteLine("You lose!");
            }
        }
    }
}
Chris Schaller
  • 13,704
  • 3
  • 43
  • 81
  • Hi, thanks for your answer. So I am confused what you mean by having an ai response value. Do I have to assign a variable to it? When I typed this code, it gave me an error that aiResponse does not make sense in the context. Thank you – JimmyLimmy Jan 27 '20 at 05:49
  • You code doesn't compile. – Enigmativity Jan 27 '20 at 08:13
  • Sorry @JimmyLimmy, I've updated to code example (writing code in the mobile view is a bit tricky sometimes). I am assuming that you want the user to play against the computer program, the AI. So the AI response is the computers selection to play against the user – Chris Schaller Jan 27 '20 at 08:13