I'm making a text simulation game in C# and I've made the calculations and all. But I know there's a better, faster and more efficient way of doing this.
Basically, the first calculation is that there's a 5% chance that there would be a 80-100 yard gain and so on.
public static class Program
{
public static Random r = new Random();
public static int gained;
public static int yardsLeft = 100;
public static int i = 0;
public static int chance = r.Next(1, 101);
public static void Main()
{
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Clear();
//Console.WriteLine(BuffaloBills.TeamName + " will be facing off against the Seattle Seahawks" + "\n");
//Console.WriteLine("The Bills starting lineup is Quarterback " + BuffaloBills.QB + ", " + "Running Back " + BuffaloBills.RB + " and Wide Receiver " + BuffaloBills.WR + "\n");
while (yardsLeft > 0)
{
int[] playResult = new int[999];
playResult[i] = r.Next(1, 4);
switch (playResult[i])
{
case 1:
Console.WriteLine(BuffaloBills.QB + " hands it off to " + BuffaloBills.RB + " for a gain of " + Calculations.Play() + " yards. \n");
yardsLeft -= gained;
i++;
break;
case 2:
Console.WriteLine(BuffaloBills.QB + " passes it " + BuffaloBills.WR + " for a gain of " + Calculations.Play() + " yards. \n");
yardsLeft -= gained;
i++;
break;
case 3:
Console.WriteLine(BuffaloBills.QB + " doesn't find anyone open so he rushes for a gain of " + Calculations.Play() + " yards. \n");
yardsLeft -= gained;
i++;
break;
}
}
Console.ReadKey();
}
}
public static int Play()
{
if (Program.yardsLeft >= 80)
{
if (Program.chance >= 1 && Program.chance <= 5)
Program.gained = Program.r.Next(80, 101);
else if (Program.chance >= 6 && Program.chance <= 12)
Program.gained = Program.r.Next(60, 80);
else if (Program.chance >= 13 && Program.chance <= 24)
Program.gained = Program.r.Next(20, 60);
else if (Program.chance >= 25 && Program.chance <= 100)
Program.gained = Program.r.Next(1, 15);
}
else if (Program.yardsLeft <= 79 && Program.yardsLeft >= 60)
{
if (Program.chance >= 1 && Program.chance <= 10)
Program.gained = Program.r.Next(60, 80);
else if (Program.chance >= 11 && Program.chance <= 30)
Program.gained = Program.r.Next(20, 60);
else if (Program.chance >= 31 && Program.chance <= 100)
Program.gained = Program.r.Next(1, 20);
}
else if (Program.yardsLeft <= 59 && Program.yardsLeft >= 40)
{
if (Program.chance >= 1 && Program.chance <= 10)
Program.gained = Program.r.Next(40, 60);
else if (Program.chance >= 11 && Program.chance <= 30)
Program.gained = Program.r.Next(20, 40);
else if (Program.chance >= 31 && Program.chance <= 100)
Program.gained = Program.r.Next(1, 20);
}
else if (Program.yardsLeft <= 39 && Program.yardsLeft >= 20)
{
if (Program.chance >= 1 && Program.chance <= 10)
Program.gained = Program.r.Next(30, 40);
else if (Program.chance >= 11 && Program.chance <= 30)
Program.gained = Program.r.Next(10, 30);
else if (Program.chance >= 31 && Program.chance <= 100)
Program.gained = Program.r.Next(1, 10);
}
else if (Program.yardsLeft < 20)
{
if (Program.chance >= 1 && Program.chance <= 10)
Program.gained = Program.r.Next(15, 20);
if (Program.chance >= 11 && Program.chance <= 30)
Program.gained = Program.r.Next(10, 15);
if (Program.chance >= 31 && Program.chance <= 100)
Program.gained = Program.r.Next(1, 10);
}
return Program.gained;
}
Random.Next
is exclusive (not inclusive as the first argument). And BTW, how do you know there's a better, faster and more efficient way in doing this? ;) \$\endgroup\$20-30
you need to invoke like this:r.Next(20, 31)
orr.Next(20, (30 + 1))
. This way it might generate the number30
but not31
. \$\endgroup\$