static void Main(string[] args) { List <Duelist> list = new List <Duelist>(); list.Add(new Duelist("Aaron", 1 / 3.0)); list.Add(new Duelist("Charlie", 995 / 1000.0)); list.Add(new Duelist("Bob", 1 / 2.0)); //PrintList<Duelist>(list); #region Sort ///Find firing order list.Sort(); PrintList <Duelist>(list); #endregion #region Simulation for (int i = 0; i < MAX_SIMULATIONS; i++) { Duelist winner = RunSimulation(list); winner.AddWin(); ResetDuelers(list); } PrintList <Duelist>(list); #endregion Console.ReadLine(); }
public virtual void ShootAt(Duelist duelist) { if (random.NextDouble() < this.probability) { duelist.Alive = false; } }
public override void ShootAt(Duelist duelist) { if (shotAlready) { base.ShootAt(duelist); } else { shotAlready = true; } }
/// <summary> /// Run a simulation /// </summary> /// <param name="list"></param> /// <returns></returns> static Duelist RunSimulation(List <Duelist> list) { while (KeepPlaying(list)) { foreach (var dueler in list) { if (dueler.Alive) { Duelist target = FindDuelistToShotAt(list, dueler); if (target != null) { dueler.ShootAt(target); } } } } return(FindWinner(list)); }
public int CompareTo(object obj) { if (obj == null) { return(1); } Duelist otherDuelist = obj as Duelist; if (otherDuelist != null) { return(this.probability.CompareTo(otherDuelist.probability)); } else { throw new ArgumentException("Object is not a Duelist"); } }
/// <summary> /// Find a duelist to shoot at /// </summary> /// <param name="list">All the duelists</param> /// <param name="shooter"> who is shooting</param> /// <returns></returns> static Duelist FindDuelistToShotAt(List <Duelist> list, Duelist shooter) { Duelist target = null; foreach (var dueler in list) { if (dueler.Alive && !(dueler.Equals(shooter))) { if (target != null && target.GetProbablity() < dueler.GetProbablity()) { target = dueler; } else if (target == null) { target = dueler; } } } return(target); }