1

I'm new to coding so please excuse my terminology..

I'm trying to make a random league of legends skin selector. I want the user to input the champion they want, in this case I have ahri and leeSin, then with the user input, I want it to select the string array and randomly print one of the elements. I think I'm pretty close but I cannot use a string with a string[]. Any help would be very appreciated.

namespace RandomLolChampSelector
{
    class Program
    {
        static void Main(string[] args)

        {
            string[] ahri = { "Academy", "Challenger", "Dynasty", "Foxfire", "Midnight", "Popstar" };
            string[] leeSin = { "Traditional", "Acolyte", "Dragon Fist", "Musy Thai", "Pool Party", "SKT T1", "Knockout" };

            // Creates title for application
            Console.WriteLine("Conor's Random League of Legends Skin Selector v0.1");
            Console.WriteLine(" ");
            Console.WriteLine(" ");

            Random rnd = new Random();

            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine("What champion would you like to select a skin for?..    ");
            string champion = Console.ReadLine();

            foreach (string s in champion)
            {
                while (true)
                {
                    // Gets user to press a key to run the code
                    Console.Write("Press the 'enter' key for a random champion..     ");
                    string question = Console.ReadLine();

                    int randomNumber = rnd.Next(ahri.Length);
                    Console.WriteLine(ahri[randomNumber]);
                }
            }
        }
    }
}

4 Answers 4

4

One approach is an array of arrays. Take a look at https://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx, specifically where they talk about Array-of-arrays (jagged).

A (probably) more intuitive approach is a dictionary of arrays. Think about something like:

Dictionary<string, string[]> myList = new Dictionary<string, string[]>();
myList.Add("ahri",new string[] { "Academy", "Challenger", "Dynasty", "Foxfire", "Midnight", "Popstar" });
//your other code
Console.WriteLine(myList[champion][randomNumber]);

Think about using the length of the array myList[champion].Length as the bounds for your random number to keep from getting an out of bounds error.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the idea! I'll read up on it tomorrow and let you know if I have any issues.
@Conor you mentioned that you used more than 2 arrays, this would be a great solution and it will keep the code really simple. one more thing to add tho is code to avoid overflow in case the element arrays are of different lengths
3

Give this a shot:

    static void Main(string[] args)
        {
            Dictionary<string, string[]> skins = new Dictionary<string, string[]>();
            skins.Add("ahri", new string[] { "Academy", "Challenger", "Dynasty", "Foxfire", "Midnight", "Popstar" });
            skins.Add("leesin", new string[] { "Traditional", "Acolyte", "Dragon Fist", "Musy Thai", "Pool Party", "SKT T1", "Knockout" });

            // Creates title for application
            Console.WriteLine("Conor's Random League of Legends Skin Selector v0.1\r\n\r\n");

            Random rnd = new Random();

            Console.ForegroundColor = ConsoleColor.Gray;

            Console.WriteLine("What champion would you like to select a skin for?..    ");
            string champion = Console.ReadLine().ToLower();

            Console.Write("Press the 'enter' key for a random champion..     ");
            Console.ReadLine();
            if(skins.ContainsKey(champion))
            {
                //return a random champion skin from the user input key in dict based on a random number from the length of the string array
                Console.WriteLine(skins[champion][rnd.Next(skins[champion].Length)]);

            }

        }

Adding to a Dictionary allows you to simplify the process by being able to check against the champion name or Key and then randomly select a skin from the string[] array or Value based on its length thanks to a random number between 0 and the count of the elements within the array.

Hope this helps bud :)


Edit: I got bored and played around with this a bit (by bored I mean trying to avoid going my assignments) so I came up with something a little more user friendly for you. People will probably hate me for giving you the entire solution but it shows you ways to handle these situations so I see it as teaching by example. Have a look.

static void Main(string[] args)
{
    Console.Clear(); //For a 'clean slate' on every execution, can ditch this if you want
    Console.ForegroundColor = ConsoleColor.Gray;

    Dictionary<string, string[]> skins = new Dictionary<string, string[]>();
    skins.Add("ahri", new string[] { "Academy", "Challenger", "Dynasty", "Foxfire", "Midnight", "Popstar" });
    skins.Add("leesin", new string[] { "Traditional", "Acolyte", "Dragon Fist", "Musy Thai", "Pool Party", "SKT T1", "Knockout" });

    Console.WriteLine("Conor's Random League of Legends Skin Selector v0.1\r\n\r\n");
    Console.WriteLine("What champion would you like to select a skin for? \r\nPress enter for a random champion...    ");

    var champion = Console.ReadLine(); 
    var rnd = new Random();
    if (champion.Equals(string.Empty))
    {
        var tmpList = Enumerable.ToList(skins.Keys);
        champion = tmpList[rnd.Next(tmpList.Count)];
    }
    else
    {
        champion = champion.Trim().ToLower(); 
    }

    Console.Write(string.Format("Champion {0} Selected \r\n", champion));

    if (skins.ContainsKey(champion)) 
    {
        Console.WriteLine(string.Format("Your random skin for {0} is: {1}\r\n", champion, skins[champion][rnd.Next(skins[champion].Length)]));
    }
    else
    {
        Console.Clear(); //clear the slate so console doesn't look cluttered.
        Main(args); 
    }
}

Now this is pretty much complete except for your 127 odd additions in champions and their skins.

Quick rundown, short and sweet.

  • Does a single call to the Console which handles all user input, the rest is automated
  • Checks for an empty string if it finds one this is effectively and enter keystroke as if they enter an incorrect value it won't be found in the Dict so it will 'restart' the program anyway. So they're left with two options: enter a correct value that is represented as a string Key or leave it blank and hit enter.
  • If and empty string is found (enter) it converts the Keys of the skins Dict to a List and randomly selects one based on a random number between 0 and the Count() of the elements within said Dict
  • Else it converts the read input to a useable string for the purpose here and moves on
  • Checks if the adjusted user input or the randomly selected string key against the Keys of the Dict if found, outputs a random skin based on the Length or Count of the array elements under that Keys Value, at which time the program exists.
  • If the Key isn't found (only based on incorrect user input) then it clears the console and 'restarts' the application.

This is far from the best possible implementation available but it works and I can't really see a problem with it's logic, can feel free to update me if it has an issue somewhere. Suppose I should do my assignments now. This was 15 mins well spent, entertaining. Gotta love coding :)

Hope this helps and maybe you have a few things you can research now and expand your knowledge on.

Later

4 Comments

Thanks a lot, this is exactly what I'm looking for. I'll make sure to read up on dictionaries so I can use them myself.
No problem, happy to help - Goodluck
@Conor update it a bit for you, hope you learn something :) was bored. have a good day mate :)
Wow thanks, didn't expect such an in depth reply :D I definitely have some research to do but that's a good thing. Thanks a lot. I'll mess around once I get home later today. Good luck with your assignments.
3

Try this:

namespace RandomLolChampSelector
{
    class Program
    {
        static void Main(string[] args)

        {
            string[] ahri = { "Academy", "Challenger", "Dynasty", "Foxfire", "Midnight", "Popstar" };
            string[] leeSin = { "Traditional", "Acolyte", "Dragon Fist", "Musy Thai", "Pool Party", "SKT T1", "Knockout" };

            // Creates title for application
            Console.WriteLine("Conor's Random League of Legends Skin Selector v0.1");
            Console.WriteLine(" ");
            Console.WriteLine(" ");

            Random rnd = new Random();

            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine("What champion would you like to select a skin for?..    ");
            string champion = Console.ReadLine();


                    // Gets user to press a key to run the code
             Console.Write("Press the 'enter' key for a random champion..     ");
             string question = Console.ReadLine();

             if(champion == "ahri")
             {
                int randomNumber = rnd.Next(ahri.Length-1);
                Console.WriteLine(ahri[randomNumber]);
             }
             else //If you have more than 2 arrays you will need another if or a case statement
             {
                int randomNumber = rnd.Next(leeSin.Length-1);
                Console.WriteLine(leeSin[randomNumber]);
             }
        }
    }
}

3 Comments

The problem with this is, I was only doing 2 arrays for testing purposes. I need to eventually add 127 so using this method would be really inefficient. Thanks for the suggestion though.
Good catch on the Length-1 for the random number gen! And I agree, a straight forward KISS (people.apache.org/~fhanik/kiss.html) approach is good for readability. Moving from the singleton, to an if/else if, to a switch/case are all steps along the way to generalizing the solution.
After testing you do not need an -1 compensation as Random.Next(maxValue) returns 0 to maxValue -1 msdn link was thinking about it before and it was bugging me. Figured out what I guess
1

I have modified your original program (preserving as much of your code as possible) to produce the intended behavior. Please feel free to run as-is here and research what I have done to make it work:

class Program
{
    static void Main(string[] args)
    {
        string[] ahri = { "Academy", "Challenger", "Dynasty", "Foxfire", "Midnight", "Popstar" };
        string[] leeSin = { "Traditional", "Acolyte", "Dragon Fist", "Musy Thai", "Pool Party", "SKT T1", "Knockout" };

        // Creates title for application
        Console.WriteLine("Conor's Random League of Legends Skin Selector v0.1");
        Console.WriteLine(" ");
        Console.WriteLine(" ");

        Random rnd = new Random();

        Console.ForegroundColor = ConsoleColor.Gray;

        // Stores what array has been selected:
        string[] champions;

        Console.WriteLine("What champion would you like to select a skin for?..    ");
        string championName = Console.ReadLine();
        if (championName.Equals("ahri", StringComparison.CurrentCultureIgnoreCase))
        {
            champions = ahri;
        }
        else if (championName.Equals("leeSin", StringComparison.CurrentCultureIgnoreCase))
        {
            champions = leeSin;
        }
        else 
        {
            // No champion selected, exit program:
            Console.WriteLine("No champion selected, quitting...");
            return;
        }

        while (true)
        {
            // Gets user to press a key to run the code
            Console.WriteLine("Press the 'enter' key for a random champion..     ");
            if (Console.ReadKey(true).Key == ConsoleKey.Enter)
            {                    
                int randomNumber = rnd.Next(champions.Length);
                Console.WriteLine(champions[randomNumber]);
            }
        }
    }
}

2 Comments

This would work if there were only going to be 2 arrays however, I plan on adding 127+ so this wouldn't work for me as it would be inefficient, thanks anyway though.
I see. Agreed, while this addresses the example in your question, it sounds like you will need something more along the lines of the Dictionary approach as proposed by Gabe, if you have that many collections to content with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.