Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your task this week is to create a \'Dice\' class which contains a collection of

ID: 3747868 • Letter: Y

Question

Your task this week is to create a 'Dice' class which contains a collection of 'Die' as a private variable and simulates the rolling of multiple dice.

You should include your 'Die' class in the code, in the same namespace as the 'Dice' class, and make a private variable to store an array of these 'Die' in the 'Dice' class.

The 'Dice' class must have 2 constructors:

public Dice(int dice)

This constructor creates an instance of Dice with the specified number of dice. These dice should have the same default number of faces as the 'Die' class (6), and the same default face value (1).

public Dice(int dice, int faces)

This constructor creates an instance of Dice with the specified number of dice, each with faces number of faces, and the same default face value as the 'Die' class (1). As with the 'Die' class, the minimum number of faces is 3.

The class must also have two other methods, which are similar to the methods of the 'Die' class:

public void RollDice()

This method must roll the dice.

public int GetFaceValue()

This method must return the total face value of all the dice (e.g. if there was 2 dice with face values 3 and 6, this should return 9)

Now, given the defaults, the code Dice myDice = new Dice(1); should create a single six-sided die, and rolling this should produce values between 1 and 6.

Dice myDice = new Dice(2, 4);, however, should create two four-sided dice. Rolling these dice should produce values between 2 and 8.

USE CODE:

Explanation / Answer

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace DiceRoller {

public class Die {

int faces; //number of faces on dices

int face_value = 1; //value on the top face of dice, default is 1

public Die(int faces){

//check if number of faces are 3 or more

if(faces>=3){

this.faces = faces;

}else{

this.faces = 3; // minimum numberof faces are 3

}

}

public void RollDice(){

Random r = new Random();

face_value = r.Next(1,faces+1); //random face value

}

public int GetFaceValue(){

return face_value;

}

}// end class Die

public class Dice {

private int no_of_dices = 1; //default 1 dice

private Die[] all_dice;

public Dice(int dice){

no_of_dices = dice;

all_dice = new Die[no_of_dices];

for(int i=0;i<no_of_dices;i++){

all_dice[i] = new Die(6);// default number of faces are 6

}

}

public Dice(int dice,int faces){

no_of_dices = dice;

all_dice = new Die[no_of_dices];

for(int i=0;i<no_of_dices;i++){

all_dice[i] = new Die(faces);

}

}

public void RollDice(){

for(int i=0;i<no_of_dices;i++){ //rolling all dices

all_dice[i].RollDice();

}

}

public int GetFaceValue(){

int sum=0;

for(int i=0;i<no_of_dices;i++){ //summing values of all dices

sum = sum + all_dice[i].GetFaceValue();

}

return sum;

}

}// end class Dice

}