patterncsharpModerate
Random name-picker with array
Viewed 0 times
randompickerarraywithname
Problem
I created a program that picks a champion for a role on the game League of Legends using arrays and random numbers.
Is there a quicker and more efficient way of doing this?
There is one form that contains this:
Class that contains the arrays (I removed most champ names for this post not to be cluttered):
```
namespace Random_Champ_Picker_V2
{
internal class arrays
{
internal string[] supp = new string[] {"Taric","Thresh"};
internal string[] marksmen = new string[]{"Ezreal","Ashe"};
internal string[] top = new string[]{"Zac","Aatrox"};
Is there a quicker and more efficient way of doing this?
There is one form that contains this:
namespace Random_Champ_Picker_V2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
internal void button1_Click( object sender, EventArgs e )
{
arrays array = new arrays();
if( radioButton1.Checked == true )
{
Random T = new Random();
int t = T.Next( 0, 57 );
string toop = (string) array.top.GetValue( t );
textBox1.Text = toop;
}
else if( radioButton2.Checked == true )
{
Random T = new Random();
int t = T.Next( 0, 38 );
string toop = (string) array.jung.GetValue( t );
textBox1.Text = toop;
}
else if( radioButton3.Checked == true )
{
Random T = new Random();
int t = T.Next( 0, 41 );
string toop = (string) array.mid.GetValue( t );
textBox1.Text = toop;
}
else if( radioButton4.Checked == true )
{
Random T = new Random();
int t = T.Next( 0, 15 );
string toop = (string) array.marksmen.GetValue( t );
textBox1.Text = toop;
}
else if( radioButton5.Checked == true )
{
Random T = new Random();
int t = T.Next( 0, 18 );
string toop = (string) array.supp.GetValue( t );
textBox1.Text = toop;
}
}Class that contains the arrays (I removed most champ names for this post not to be cluttered):
```
namespace Random_Champ_Picker_V2
{
internal class arrays
{
internal string[] supp = new string[] {"Taric","Thresh"};
internal string[] marksmen = new string[]{"Ezreal","Ashe"};
internal string[] top = new string[]{"Zac","Aatrox"};
Solution
First of all, you do not need to create an instance of
Then, you may use the ternary operator (the
And, assuming that you have to take a random element form the array, you may use just
Random class every time.Then, you may use the ternary operator (the
?: operator) to select the array from which you want to take the element.And, assuming that you have to take a random element form the array, you may use just
a[T.Next(a.Length).Random T = new Random();
. . .
string[] a = radioButton1.Checked? array.top:
radioButton2.Checked? array.jung:
radioButton3.Checked? array.mid:
radioButton4.Checked? array.marksmen:
radioButton5.Checked? array.supp:
null;
if (a != null)
textBox1.Text = a[T.Next(a.Length)];Code Snippets
Random T = new Random();
. . .
string[] a = radioButton1.Checked? array.top:
radioButton2.Checked? array.jung:
radioButton3.Checked? array.mid:
radioButton4.Checked? array.marksmen:
radioButton5.Checked? array.supp:
null;
if (a != null)
textBox1.Text = a[T.Next(a.Length)];Context
StackExchange Code Review Q#60599, answer score: 14
Revisions (0)
No revisions yet.