Reputation: 469
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]);
}
}
}
}
}
Upvotes: 1
Views: 1643
Reputation: 670
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.
Console
which handles all user input, the rest is automatedstring
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.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
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.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
Upvotes: 3
Reputation: 1732
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]);
}
}
}
}
Upvotes: 1
Reputation: 432
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]);
}
}
}
}
Upvotes: 3
Reputation: 2731
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.
Upvotes: 4