-6

This is the code I have wrote for a dice game, I am new the C# so excuse me for it being inefficient:

public class Dice
{
    static public void DiceRoll()
    {
        Random rnd = new Random();
        int roll1 = rnd.Next(1, 7);
        int roll2 = rnd.Next(1, 7);
        int roll3 = rnd.Next(1, 7);
        int roll4 = rnd.Next(1, 7);
        int roll5 = rnd.Next(1, 7);
        int roll6 = rnd.Next(1, 7);
        int roll7 = rnd.Next(1, 7);
        int roll8 = rnd.Next(1, 7);
        int roll9 = rnd.Next(1, 7);
        int roll10 = rnd.Next(1, 7);
        int roll11 = rnd.Next(1, 7);
        int roll12 = rnd.Next(1, 7);
        int Player1Total = roll1 + roll2 + roll3;
        int Player2Total = roll4 + roll5 + roll6;
        int Player1Total2roll = roll7 + roll8;
        int Player2Total2roll = roll9 + roll10;
        int P1froll = roll11;
        int P2froll = roll12;
        int overallP1 = Player1Total + Player1Total2roll + P1froll;
        int overallP2 = Player2Total + Player2Total2roll + P2froll;
        int Player1Score = 0;
        int Player2Score = 0;
        bool gamerunning = false;
        while (gamerunning == false)
        {
            Console.WriteLine("Press anything to roll your die");
            Console.ReadKey();
            Console.Clear();
            Console.WriteLine("\nPlayer1 first dice is ", roll1);
            Console.WriteLine("Player1 second dice is " + roll2);
            Console.WriteLine("Player1 third dice is " + roll3);
            Console.WriteLine("Your total is " + Player1Total);
            Console.WriteLine("\nYour opponents first dice roll is " + roll4);
            Console.WriteLine("Your opponents second dice roll is " + roll5);
            Console.WriteLine("Your opponents third dice roll is " + roll6);
            Console.WriteLine("Your opponents total is " + Player2Total);
            Console.WriteLine("\nPlayer1 first dice is " + roll7);
            Console.WriteLine("Player1 second dice is " + roll8);
            Console.WriteLine("Your total is " + Player1Total2roll);
            Console.WriteLine("\nYour opponents first dice roll is " + roll9);
            Console.WriteLine("Your opponents second dice roll is " + roll10);
            Console.WriteLine("Your total is " + Player2Total2roll);
            Console.WriteLine("\nPlayer1 final roll is " + roll11);
            Console.WriteLine("Your total is " + P1froll);
            Console.WriteLine("\nYour opponents final roll is " + roll12);
            Console.WriteLine("Your total is " + P2froll);
            if (overallP1 > overallP2)
            {
                Player1Score++;
                Console.WriteLine("\nPlayer 1 Wins");
                Console.WriteLine("The Current Score is:");
                Console.WriteLine("Player 1: " + Player1Score);
                Console.WriteLine("Player 2: " + Player2Score);
            }
            else if (overallP2 > overallP1)
            {
                Player2Score++;
                Console.WriteLine("\nPlayer 2 Wins");
                Console.WriteLine("The Current Score is:");
                Console.WriteLine("Player 1: " + Player1Score);
                Console.WriteLine("Player 2: " + Player2Score);
            }
            if (overallP1 == overallP2)
            {
                Console.WriteLine("\nIt's a draw");
            }
            if (Player1Score >= 5)
            {
                gamerunning = true;
            }
            else if (Player2Score >= 5)
            {
                gamerunning = true;
            }
            //if ( Player1Score == Player2Score)
            //{
            //    DiceRoll();
            //}
        }
    }
}

The problem is that I am always getting the same numbers from the generator even though the loop is outside of where I created the random numbers. Does anyone know the problem and has any tips. Thanks in advance.

MethodMan
  • 18,625
  • 6
  • 34
  • 52
Kamil
  • 17
  • 1
  • 1
    Are you saying you're seeing `roll1==roll2==roll3==...==roll12`? – Blorgbeard Dec 19 '17 at 22:41
  • 4
    You're getting the same numbers each time _because_ you're generating them outside the loop. – cHao Dec 19 '17 at 22:43
  • I would highly suggest that you set breakpoints in your code @Blorgbeard and use the debugger to step thru your code evaluating all the variables then you will find your own mistake. Don't just write code and run it expecting that everything will work flawlessly. – MethodMan Dec 19 '17 at 22:43
  • 1
    Take a look at the accepted answer here: https://stackoverflow.com/questions/2706500/how-do-i-generate-a-random-int-number-in-c – hivie7510 Dec 19 '17 at 22:45
  • Oh, and when you see variable names like `something1`, `something2`, `something3`, etc, your first thought should be to use an array instead. This code would be about half as long if you did. – cHao Dec 19 '17 at 22:46
  • No, I am seeing that for example in the line "Console.WriteLine("Player1 second dice is " + roll2);" After looping this part of the code roll2 would be the same number instead of a different random number and I loop the code 5 times and each time the same number would appear for roll2 so it's not like I'm actually rolling a dice because I'm always getting the same result. – Kamil Dec 19 '17 at 22:47
  • 2
    You property create the `Random` outside of the `while` loop, but you are also assigning values to the _variables_ outside of the `while` loop, so their values never change. – D Stanley Dec 19 '17 at 22:47
  • @cHao You are right. That was causing the problem. I thought I read somewhere online that you're suppose to keep these generators outside the loops. I'll remember this from now. Thanks a lot! – Kamil Dec 19 '17 at 22:51
  • 3
    You're supposed to keep the _creation_ of the generators outside of the loop. (Some flavors of RNGs can be expensive to create, and in some cases can end up with the same seed if two are generated in rapid succession.) The _use_, on the other hand, is fine in a loop, and needs to be there if you want different numbers each time. – cHao Dec 19 '17 at 22:55
  • Please read [ask] and take the [tour] – Ňɏssa Pøngjǣrdenlarp Dec 20 '17 at 01:27

1 Answers1

2

Just by moving your variable declarations into the loop, your code works like you'd want it to. You want get a new random number every game - and each game is one iteration of your while (gamerunning == false) loop.

private static void Main(string[] args)
    {
        Random rnd = new Random();
        bool gamerunning = false;
        while (gamerunning == false)
        {
            int roll1 = rnd.Next(1, 7);
            int roll2 = rnd.Next(1, 7);
            int roll3 = rnd.Next(1, 7);
            int roll4 = rnd.Next(1, 7);
            int roll5 = rnd.Next(1, 7);
            int roll6 = rnd.Next(1, 7);
            int roll7 = rnd.Next(1, 7);
            int roll8 = rnd.Next(1, 7);
            int roll9 = rnd.Next(1, 7);
            int roll10 = rnd.Next(1, 7);
            int roll11 = rnd.Next(1, 7);
            int roll12 = rnd.Next(1, 7);
            int Player1Total = roll1 + roll2 + roll3;
            int Player2Total = roll4 + roll5 + roll6;
            int Player1Total2roll = roll7 + roll8;
            int Player2Total2roll = roll9 + roll10;
            int P1froll = roll11;
            int P2froll = roll12;
            int overallP1 = Player1Total + Player1Total2roll + P1froll;
            int overallP2 = Player2Total + Player2Total2roll + P2froll;
            int Player1Score = 0;
            int Player2Score = 0;

            Console.WriteLine("Press anything to roll your die");
            Console.ReadKey();
            Console.Clear();
            Console.WriteLine("\nPlayer1 first dice is ", roll1);
            Console.WriteLine("Player1 second dice is " + roll2);
            Console.WriteLine("Player1 third dice is " + roll3);
            Console.WriteLine("Your total is " + Player1Total);
            Console.WriteLine("\nYour opponents first dice roll is " + roll4);
            Console.WriteLine("Your opponents second dice roll is " + roll5);
            Console.WriteLine("Your opponents third dice roll is " + roll6);
            Console.WriteLine("Your opponents total is " + Player2Total);
            Console.WriteLine("\nPlayer1 first dice is " + roll7);
            Console.WriteLine("Player1 second dice is " + roll8);
            Console.WriteLine("Your total is " + Player1Total2roll);
            Console.WriteLine("\nYour opponents first dice roll is " + roll9);
            Console.WriteLine("Your opponents second dice roll is " + roll10);
            Console.WriteLine("Your total is " + Player2Total2roll);
            Console.WriteLine("\nPlayer1 final roll is " + roll11);
            Console.WriteLine("Your total is " + P1froll);
            Console.WriteLine("\nYour opponents final roll is " + roll12);
            Console.WriteLine("Your total is " + P2froll);
            if (overallP1 > overallP2)
            {
                Player1Score++;
                Console.WriteLine("\nPlayer 1 Wins");
                Console.WriteLine("The Current Score is:");
                Console.WriteLine("Player 1: " + Player1Score);
                Console.WriteLine("Player 2: " + Player2Score);
            }
            else if (overallP2 > overallP1)
            {
                Player2Score++;
                Console.WriteLine("\nPlayer 2 Wins");
                Console.WriteLine("The Current Score is:");
                Console.WriteLine("Player 1: " + Player1Score);
                Console.WriteLine("Player 2: " + Player2Score);
            }
            if (overallP1 == overallP2)
                Console.WriteLine("\nIt's a draw");
            if (Player1Score >= 5)
                gamerunning = true;
            else if (Player2Score >= 5)
                gamerunning = true;
            //if ( Player1Score == Player2Score)
            //{
            //    DiceRoll();
            //}
        }
    }