Question: These are the requirements for this code: Refer to Activity 11 Example, and write a Dice class. The class should have: A private field for
These are the requirements for this code:
Refer to Activity 11 Example, and write a Dice class. The class should have:
A private field for the number of sides of the die.
A constructor that takes an integer between 4 and 20, inclusive and sets the number of sides of the die.
A method, rollDie(), returns the face value when the die is rolled. Use a Random number to roll the die.
Write a main method that will create two die with the same number of sides, continually roll the dice, and report the result of each roll. It should stop when snake eyes are rolled (double 1s) and report the number of rolls. You may write this as a console application or a Windows Forms application; include a screenshot of your program running with successful output.
This is my code so far:
class Die { private int sides;
public Die(int sides) { if (sides < 4 || sides > 20) { throw new ArgumentException("Number of sides must be between 4 and 20, inclusive."); } this.sides = sides; }
public int RollDie() { Random rand = new Random(); return rand.Next(1, sides + 1); } }
class Program { static void Main(string[] args) { int numSides = 6; // you can change this value to set the number of sides for the dice Die die1 = new Die(numSides); Die die2 = new Die(numSides);
int rolls = 0; while (true) { int roll1 = die1.RollDie(); int roll2 = die2.RollDie(); Console.WriteLine("Roll {0}: Die 1 = {1}, Die 2 = {2}", rolls + 1, roll1, roll2); rolls++;
if(roll1 == 1 && roll2 == 1); { Console.WriteLine("Snake eyes!"); break; } }
Console.WriteLine("It of the took {0} rolls to get snake eyes!", rolls.ToString()); Console.ReadLine(); } } }
This is my code, however it doesnt seem to be randomizing the dice rolls correctly as this is the only input it does, every time:
Roll 1: Die 1 = 3, Die 2 = 3 Snake eyes! It of the took 1 rolls to get snake eyes! The program '[15964] DiceRolls.exe' has exited with code 0 (0x0).
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
