using System;
using System.Collections.Generic;
public delegate void Notifier(string message);
public class Die {
private int numberOfEyes;
private Random randomNumberSupplier;
private int maxNumberOfEyes;
private List<int> history;
public event Notifier twoSixesInARow;
public int NumberOfEyes{
get {return numberOfEyes;}
}
public Die (): this(6){}
public Die (int maxNumberOfEyes){
randomNumberSupplier = new Random(unchecked((int)DateTime.Now.Ticks));
this.maxNumberOfEyes = maxNumberOfEyes;
numberOfEyes = randomNumberSupplier.Next(1, maxNumberOfEyes + 1);
history = new List<int>();
history.Add(numberOfEyes);
}
public void Toss (){
numberOfEyes = randomNumberSupplier.Next(1,maxNumberOfEyes + 1);
history.Add(numberOfEyes);
if (DoWeHaveTwoSixesInARow(history))
twoSixesInARow("Two sixes in a row");
}
private bool DoWeHaveTwoSixesInARow(List<int> history){
int histLength = history.Count;
return histLength >= 2 &&
history[histLength-1] == 6 &&
history[histLength-2] == 6;
}
public override String ToString(){
return String.Format("Die[{0}]: {1}", maxNumberOfEyes, NumberOfEyes);
}
} | |
A delegate type.
The class Die, now with notifications of
'two sixes in a row'.
The history list of tossing.
The event of type Notifier called
twoSixesInARow.
The usual parameterless constructor.
A more general constructor.
Die tossing.
Adding to history.
Triggering the event twoSixesInARow
in case we have two sixes
in a row in our history list.
The history predicate that
finds out about two sixes in a row;
It does it by looking in the history
list.
|