Leroy de Smet
Leroy de Smet

Reputation: 63

How do I count the number of times a method is used?

enter image description hereenter image description hereI have 2 dice that generate a number between 1 and 6. I want to display their values as follows: "The value of dice 1 is: (Generated number)" "The value of dice 2 is: (Generated number)"

My counter doesn't seem to work because my output is as following: "The value of dice 1 is: (Generated number)" "The value of dice 1 is: (Generated number)"

    static void Main(string[] args)
    {
        Program DungeonDiceMasters = new Program();
        DungeonDiceMasters.Start();
    }

    void Start()
    {
        Dice d1, d2;
        d1 = new Dice();
        d2 = new Dice();

        d1.Throw();
        d2.Throw();

        d1.ShowValue();
        d2.ShowValue();

        Console.Write("\n");

        Console.ReadKey();
        Start();
    }


class Dice
{
    static Random NumberOneToSix = new Random();
    public int value;
    public int counter = 0;

    public void Throw()
    {
        value = NumberOneToSix.Next(1, 7);
    }

    public void ShowValue()
    {
        counter = counter + 1;
        Console.WriteLine("The value of dice " + counter + " is: " + value);
    }
}

Upvotes: 0

Views: 1576

Answers (4)

ColinM
ColinM

Reputation: 2681

You need to track the number of die outside of the Dice instances because each instance will have their own local copy of counter which will start at 0 in both instances.

Add a constructor to the Dice class which accepts an int parameter to track the ID of which instance it is.

public class Program
{
    static void Main(string[] args)
    {
        Start();
    }

    static void Start()
    {
        // Hold the Dice Counter outside of the scope of the instances
        int diceCounter = 0;
        Dice d1, d2;
        // Instantiate both dice, while incrementing the diceCounter and passing the value to the ctor
        d1 = new Dice(++diceCounter);
        d2 = new Dice(++diceCounter);

        d1.Throw();
        d2.Throw();

        d1.ShowValue();
        d2.ShowValue();

        Console.Write("\n");

        Console.ReadKey();
        Start();
    }
}

class Dice
{
    static Random NumberOneToSix = new Random();
    // int field which contains the ID/instance number for this instance.
    private readonly int identifier;
    public int value;

    // Constructor which accepts an int parameter
    public Dice(int identifier)
    {
        this.identifier = identifier;
    }

    public void Throw()
    {
        value = NumberOneToSix.Next(1, 7);
    }

    public void ShowValue()
    {
        Console.WriteLine("The value of dice " + identifier + " is: " + value);
    }
}

You can read more about constructors here

Here is also some information on the this keyword as well as the readonly keyword

Upvotes: 0

Mehrdad Dowlatabadi
Mehrdad Dowlatabadi

Reputation: 1335

you can use a static field for the counter as follows:

public static int counter = 0;

but after each throw value is changed so it is a throw counter not the number of dice. output:

  The value of dice 1 is: 3
  The value of dice 2 is: 4

so you can use properties to save track of the dice count and the thrown number for each :

class Dice
{
    static Random NumberOneToSix = new Random();
    public int value;
    public static int diceCounter = 0;
    public  int diceNumber =0;
    public  int throwCounter = 0;
    //set id of dice in constructor method
    public  Dice(){
            diceCounter=diceCounter+1;
            diceNumber=diceCounter;
    }
    public void Throw()
    {
        value = NumberOneToSix.Next(1, 7);
        throwCounter=throwCounter+1;
    }

    public void ShowValue()
    {

        Console.WriteLine("The value of dice " + diceNumber +" in throw #"+throwCounter+ " is: " + value);
    }
}

and the output is :

The value of dice 1 in throw #1 is: 6
The value of dice 2 in throw #1 is: 3

Upvotes: 0

Joel Trauger
Joel Trauger

Reputation: 732

I'd suggest modifying your class just a bit. Reason being, that without some sort of external tracker like Jonas has suggested in his answer, you will not be able to keep track of dice rolls between method calls.

Try something like this:

    static void Main(string[] args)
    {
        Program DungeonDiceMasters = new Program();
        DungeonDiceMasters.Start();
    }

    void Start()
    {
        DiceRoller dr = new DiceRoller();

        dr.Throw();
        dr.ShowValue(true);
        dr.Throw();
        dr.ShowValue();

        Console.Write("\n");

        Console.ReadKey();
        Start();
    }


class DiceRoller
{
    static Random NumberOneToSix = new Random();
    public int value = 0;
    public int counter = 0;
    private bool diceThrown = false; //add throw tracker to avoid losing dice rolls

    public void Throw()
    {
        value = NumberOneToSix.Next(1, 7);
        diceThrown = true; //dice have been thrown
    }

    public void ShowValue(bool startOver = false) //optional parameter to reset dice counter
    {
        if(startOver) //if param set, reset counter
            counter = 0;
        counter = counter + 1;
        if(!diceThrown) // self-explanatory
            Throw(); //internal call to method to throw dice
        Console.WriteLine("The value of dice " + counter + " is: " + value);
        diceThrown = false; //reset dice throw tracker
    }
}

I added some comments to hopefully explain what's going on and why I made the edits I did. Hope this helps!

Upvotes: 0

Jonas Høgh
Jonas Høgh

Reputation: 10874

Each instance of the Dice class has a counter field, since the field is not static. They both have the value 1 as the ShowValue method has been called once for each dice instance.

You might want to add the dice numbering concept as a constructor parameter and manage the counting in your Start method instead.

Upvotes: 1

Related Questions