Rama assignment3 Refactor

move dicecontainer from roll class to store class
mail@pastecode.io avatar
unknown
csharp
a year ago
2.4 kB
3
Indexable
// See https://aka.ms/new-console-template for more information

using System;

//object instance call
Roll roll6 = new Roll(6);
Roll roll10 = new Roll(10);
Roll roll20 = new Roll(20);

Store store6 = new Store(roll6.RolledNumber,roll6.DiceSides);
Store store10 = new Store(roll6.RolledNumber, roll10.DiceSides);
Store store20 = new Store(roll6.RolledNumber, roll20.DiceSides);

int rollCount = 20;

//rolling the dices
for (int i = 0; i < rollCount; i++)
{
    //using 6 sided dice
    roll6.rollDice();
    store6.storeNumber(roll6.RolledNumber);

    roll10.rollDice();
    store10.storeNumber(roll10.RolledNumber);

    roll20.rollDice();
    store20.storeNumber(roll20.RolledNumber);
}

//displaying the dice roll results on the console
Display.printDisplay(store6.DiceContainer); //showing the result of 6 side dice
Display.printDisplay(store10.DiceContainer);
Display.printDisplay(store20.DiceContainer);


//class accountable for rolling the dice
class Roll
{
    //class member
    public int RolledNumber;
    public int DiceSides;

    //constructor of the class
    public Roll(int diceSides)
    {
        DiceSides = diceSides;       
    }



    //to generate random number
    Random random = new Random();

    //function to roll (generate) the number
    public void rollDice()
    {
        RolledNumber = random.Next(1, DiceSides + 1);
    }
}

//class accountable for storing the dice roll result
class Store
{

    //practicing using getter setter for DiceContainer variable below
    private int[] _diceContainer;
    public int[] DiceContainer
    {
        get { return _diceContainer; }
        set { _diceContainer = value; }
    }

    public Store(int rolledNumber, int DiceSides)
    {
       DiceContainer = new int[DiceSides + 1];
    }

    //function to store the number
    public void storeNumber(int counter)
    {
        
        DiceContainer[counter]++;
    }
}

//class accountable for showing the dice roll results on the console
class Display
{
    //function to print the number
    public static void printDisplay(int[] diceContainer)
    {
        Console.WriteLine("Dice Roll results for the " + (diceContainer.Length - 1) + " Sided dice");
        for (int j = 1; j < diceContainer.Length; j++)
        {
            Console.WriteLine(j + ": " + diceContainer[j]);
        }
        Console.WriteLine();
    }
}