Die-variants/die-0/die.cs - A slightly more general Die class. | Lecture 3 - slide 2 : 29 Program 6 |
using System; public class Die { private int numberOfEyes; private Random randomNumberSupplier; private readonly int maxNumberOfEyes; public Die (): this(6){} public Die (int maxNumberOfEyes){ randomNumberSupplier = new Random(unchecked((int)DateTime.Now.Ticks)); this.maxNumberOfEyes = maxNumberOfEyes; numberOfEyes = NewTossHowManyEyes(); } public void Toss (){ numberOfEyes = NewTossHowManyEyes(); } private int NewTossHowManyEyes (){ return randomNumberSupplier.Next(1,maxNumberOfEyes + 1); } public int NumberOfEyes() { return numberOfEyes; } public override String ToString(){ return String.Format("Die[{0}]: {1}", maxNumberOfEyes, numberOfEyes); } }