Basic C# Dice Game

c#, dice

Solution

There are a number of concerns with your code here:

- you are asking the player name inside the loop

- You are initializing the Random generator inside the loop (this can cause same results on different loops because the random generator can use the same seed)

- You are resetting totals using = instead of +=

- The stop condition is totals == 20 instead of total < 20

- you are using two while statement instead of an AND (&&) condition

This is just a brief overview..

This can be a solution:

  Console.Write("Enter the name of Player 1: ");
  Player[0] = Console.ReadLine();
  Console.Write("Enter the name of Player 2: ");
  Player[1] = Console.ReadLine();

  Random DiceRandom = new Random();

  do
    {

        int i = 0;
        while (i <= 1)
        {
            int thisThrow = DiceRandom.Next(1, 6);
            DiceThrow[0 + i] += thisThrow;
            Console.ReadLine();
            Console.Write(Player[0 + i] + " rolled a " + thisThrow);
            if (thisThrow != 6) i++;
        }

        Console.ReadLine();
        PlayerTotal[0] += DiceThrow[0];
        PlayerTotal[1] += DiceThrow[1];


        Console.ReadLine();
        Console.Write(Player[0] + " currently has " + PlayerTotal[0]);
        Console.ReadLine();
        Console.Write(Player[1] + " currently has " + PlayerTotal[1]);
        Console.ReadLine();

    }
    while (PlayerTotal[0] < 20 && PlayerTotal[1] < 20);

Problem

I am new to c# and coding in general. To try and improve my skills I am trying to create a basic game where two players roll a dice and keep record of their score. The player wins by reaching 20. Each player takes turns rolling a dice adding their first roll to their second and so on until one of them reaches 20. A player can roll the dice again if they roll a six. The current code I have is: ``` do { Console.Write("Enter the name of Player 1: "); Player[0] = Console.ReadLine(); Console.Write("Enter the name of Player 2: "); Player[1] = Console.ReadLine(); Random DiceRandom = new Random(); DiceThrow[0] = DiceRandom.Next(1, 7); int i = 0; while (i <= 1) { DiceThrow[0 + i] = DiceRandom.Next(1, 7); Console.ReadLine(); Console.Write(Player[0 + i] + " rolled a " + DiceThrow[0 + i]); if (DiceThrow[0 + i] != 6) i++; } Console.ReadLine(); PlayerTotal[0] = DiceThrow[0]; PlayerTotal[1] = DiceThrow[1]; Console.ReadLine(); Console.Write(Player[0] + " currently has " + PlayerTotal[0]); Console.ReadLine(); Console.Write(Player[1] + " currently has " + PlayerTotal[1]); Console.ReadLine(); } while (PlayerTotal[0] == 20); while (PlayerTotal[1] == 20); ``` What I am specifically struggling with is adding the players first roll to there second roll. And if a player rolls a six that it adds the 6 to what they get in the re-roll. Any help at all will be greatly appreciated.

Original source