简体   繁体   中英

Basic C# Dice Game

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.

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

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 while s... 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);
    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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM