Basic C# Dice Game

17,452

Solution 1

There are a number of concerns with your code here:

  1. you are asking the player name inside the loop
  2. 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)
  3. You are resetting totals using = instead of +=
  4. The stop condition is totals == 20 instead of total < 20
  5. 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);

Solution 2

Your problem is that you're reseting the previous scores with these lines:

PlayerTotal[0] = DiceThrow[0];
PlayerTotal[1] = DiceThrow[1];

You should change it to use += like this:

PlayerTotal[0] += DiceThrow[0];
PlayerTotal[1] += DiceThrow[1];

Which is like saying: PlayerTotal[0] = PlayerTotal[0] + DiceThrow[0];

Other then that, there are a few more problems in your code.

For instance, you have one Do at the beginning of the code but 2 whiles... You probably want to create one While with an AND statement. Also, the Do statement needs to be after you get the user's names...

For instance: // Get User names

 do
 {
    // All your Dice throwing logic
 }
 while (PlayerTotal[0] != 20 && PlayerTotal[1] != 20);
Share:
17,452

Related videos on Youtube

user1891182
Author by

user1891182

Updated on June 08, 2022

Comments

  • user1891182
    user1891182 over 1 year

    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.

  • user1891182
    user1891182 about 11 years
    Thank you for your reply and the concerns, coding is completely new to me so I was not aware of those concerns. The solution you have provided is almost perfect. Except if a player rolls a 6 and then rolls a 2 it saves the number 2 as their PlayerTotal instead of an 8.
  • Tobia Zambon
    Tobia Zambon about 11 years
    yes I've corrected the code: DiceThrow[0 + i] should be incremented (with +=) instead of overwritten (with =)
  • user1891182
    user1891182 about 11 years
    It does add the numbers together, but players are able to roll numbers greater than 6, which should not be allowed. Any idea how I could fix this?
  • Tobia Zambon
    Tobia Zambon about 11 years
    this is because you are using Next(1, 7), this will return number from 1 to 7, I think you should use Next(1, 6) in order to stop at 6. Next(minValue, MaxValue) includes bounds
  • user1891182
    user1891182 about 11 years
    No I was getting results such as: Player1 rolled a 12 and Player 2 rolled a 8
  • Tobia Zambon
    Tobia Zambon about 11 years
    Ah I've understand, I've edited my code above, it is simpler for you to use a local variable in order to save the current result and operate with it. This result is then added to the total.