Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to record the data for a random generator dice game

Hi, I have a piece of code here that I don't know how to record the data for a random dice role. I want to record the data and display at the end that Player 1 roled this amount in 5 rounds while player 2 roled this amount in 5 rounds. I would really appreciate it if someone knows the answer. Thanks

Here is the Work that I have done so far


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


namespace Coursework

{

  class Program

  {

    static void Main(string[] args)

    {

    Start:

      Console.WriteLine("Enter Players One's Name");            // Enters Players ones name and checks with the system to validate if the if the name is an authorised Player

      string Player1 = Console.ReadLine();

      if (Player1 == "Percy Williams")

      {

        Console.Clear();

        Console.WriteLine("Access Granted");


      }

      else

      {

        Console.Clear();

        Console.WriteLine("Access Denied");               // If they are not an authorised player it will say Access Denied 


        goto Start;

      }

    SecondPlayer:


      Console.WriteLine("Enter Player Two's Name");          // Asks for Player 2 Name and checks to see if they are an authorised Player

      string Player2 = Console.ReadLine();

      if (Player2 == "Alex Mohammed")

      {

        Console.Clear();

        Console.WriteLine("Access Granted");


      }

      else

      {

        Console.Clear();

        Console.WriteLine("Access Denied");


        goto SecondPlayer;

      }

      int x = 0;                            //

      int PlayerScore1;

      int PlayerScore2;

      while (x < 5)

      {

        x = x + 1;


        Random NumberGenerator = new Random();

        int Dice1 = NumberGenerator.Next(1, 7);

        int Dice2 = NumberGenerator.Next(1, 7);


        Console.Clear();

        Console.WriteLine("Player 1 rolled " + Dice1 );

         


        Console.WriteLine("While Player 2 rolled " + Dice2);

        Console.ReadKey();


         



      };




    }

  }

}


Btw this is on Mac not windows so some of the programming may look different.

Answers

  • Store them in arrays or lists

  • Create an int list for both players . Add each value to that list. then createa foreach loop where you preform the addition.

    Example :

    using System;

    using System.Collections.Generic;


    namespace ConsoleApp4

    {

      class Program

      {

        static int t1;

        static int t2;

        static List<int> P1list = new List<int>();

        static List<int> P2list = new List<int>();

        static void Main()

        {

          Console.ForegroundColor = ConsoleColor.Green;

          Dice();

        }


        public static void Dice()

        {


          for (int f = 0; f < 5; f++)

          {

            Console.WriteLine("Player 1 enter value: ");

            int P1 = int.Parse(Console.ReadLine());

            P1list.Add(P1);//adds to the list 

            Console.WriteLine("Player 2 enter value: ");

            int P2 = int.Parse(Console.ReadLine());

            P2list.Add(P2);

          }

          int i = 0;


          foreach (int it in P1list)//Loops through once for each object in list

          {

            t1 = t1 + P1list[i];//Adds the number in the list at a particular index in this case i to a total

            i++;//we add 1 to i so that the index will move on to the next number

          }

          Console.ForegroundColor = ConsoleColor.Red;

          Console.WriteLine( "Player 1 got: " + t1);



          int a = 0;


          foreach (int at in P2list)

          {

            t2 = t2 + P2list[a];

            a++;


          }

          Console.WriteLine("Player 2 got: " + t2);

        

          Main();

        }


      }

      }

    I didn't include the dice logic but you can do that instead of using the console.

Sign In or Register to comment.