HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Chances to gain yards (text simulation game)

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
chancessimulationyardsgaintextgame

Problem

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();
    }
}


Solution

You could set up a static 2 dimensional array with pre-calculated values to pass to r.Next() for each yardsLeft and chance combination.

e.g.
array[80,1] would contain int tuple {80, 101}

Context

StackExchange Code Review Q#102408, answer score: 7

Revisions (0)

No revisions yet.