public static String duelRound(Duelist[] duelistArray) { int numAlive = 0; String winner = null; for (int i = 0; i < duelistArray.Length; i++) { if (duelistArray[i].alive) { numAlive++; } } do { for (int i = 0; i < duelistArray.Length; i++) { //////// TODO /////////////// Duelist target = duelistArray[i].determineGreatestThreat(duelistArray); if (duelistArray[i].alive) { duelistArray[i].shoot(target); } if (target.alive == false) { Console.WriteLine(target.name + " died"); numAlive--; } } } while (numAlive > 1); //Console.WriteLine(winner); return(winner); }
static void Main(string[] args) { Duelist aaron = new Duelist("Aaron", 1, 3); Duelist bob = new Duelist("Bob", 1, 2); Duelist charlie = new Duelist("Charlie", 995, 1000); Duelist[] duelists = { aaron, bob, charlie }; int aaronWins = 0; int bobWins = 0; int charlieWins = 0; aaron.determineGreatestThreat(duelists); for (int i = 0; i < 10; i++) { String winner = duelRound(duelists); if (winner == "Aaron") { aaronWins++; } else if (winner == "Bob") { bobWins++; } else { charlieWins++; } } Console.WriteLine($"Aaron wins: {aaronWins}"); Console.WriteLine($"Bob wins: {bobWins}"); Console.WriteLine($"Charlie wins: {charlieWins}"); }
//Simulates and prints result for a contest with three duelers //Aaron, Bob, and Charlie, with the given number of test and //missFirst = true if Aaron intentionally misses his first shot. public static void ConstestAaronBobCharlie(int testQuantity, bool missFirst) { String[] names = new string[] { "Aaron", "Bob", "Charlie" }; double[] accuracy = new double[] { (1.0 / 3.0), (1.0 / 2.0), 0.95 }; int[] winningCount = new int[] { 0, 0, 0 }; for (int i = 0; i < testQuantity; i++) { //Creating duelers Duelist aaron = new Duelist(names[0], accuracy[0], true); Duelist bob = new Duelist(names[1], accuracy[1], true); Duelist charlie = new Duelist(names[2], accuracy[2], true); Duelist[] duelers = new Duelist[] { aaron, bob, charlie }; //Getting winner from a single duel String winnerName = SingleDuel(duelers, missFirst).Name; //counting number of time winning for each contestant. winningCount[Array.IndexOf(names, winnerName)]++; } //Printing results for (int i = 0; i < 3; i++) { Console.WriteLine("{0, -10} = {1, -10} = {2:P}", names[i], winningCount[i], winningCount[i] / (double)testQuantity); } }
public void ShootAtTarget(Duelist target) { double rand = generator.NextDouble(); if (rand < accuracy) { target.Alive = false; } }
//Simulates this dueler shooting at target (another dueler): //sets the other dueler to isAlive = false if //randomGenerator.NextDouble() <= accuracy and returns true; false otherwise. public bool ShootAtTarget(Duelist target) { if (null != target && randomGenerator.NextDouble() <= accuracy) { target.IsAlive = false; return(true); } return(false); }
// OTHER METHODS public bool Equals(Object o) { if (o == null) { return(false); } Duelist otherDuelist = (Duelist)o; return(this.name == otherDuelist.name && this.accuracyNum == otherDuelist.accuracyNum && this.accuracyDenom == otherDuelist.accuracyDenom && this.alive == otherDuelist.alive); }
public override bool Equals(object obj) { Duelist other = (Duelist)obj; if (GetAccuracy() == other.GetAccuracy() && GetName().Equals(other.GetName())) { return(true); } return(false); }
//Compares all of the instance variable to be equal by values. //return true if all of the instance variable to be equal by values; false otherwise. public override bool Equals(object obj) { if (null == obj || GetType() != obj.GetType()) { return(false); } Duelist other = (Duelist)obj; return(name.Equals(other.name) && accuracy == other.accuracy && isAlive == other.isAlive); }
public Duelist determineGreatestThreat(Duelist[] duelistArray) { Duelist greatestThreat = duelistArray[0]; for (int i = 0; i < duelistArray.Length; i++) { if (duelistArray[i].alive && duelistArray[i].threat > this.threat || duelistArray[i].alive && greatestThreat.threat < duelistArray[i].threat) { greatestThreat = (Duelist)duelistArray[i]; } } return(greatestThreat); }
public bool shoot(Duelist target) { bool hit = false; var rand = new Random(); int roll = rand.Next(1, accuracyDenom + 1); if (roll <= accuracyNum) { hit = true; target.alive = false; } return(hit); }
private static int[] RunSimulation(bool strategy, int rounds) { int[] counts = { 0, 0, 0 }; for (int i = 0; i < rounds; i++) { Duelist winner = SimulateDuel(strategy); if (winner.Equals(aaron)) { counts[0]++; } else if (winner.Equals(bob)) { counts[1]++; } else if (winner.Equals(charlie)) { counts[2]++; } } return(counts); }
//Contest simulates a contest where duelers take turn starting from the least deadly. //Each contestant uses will target the most deadly that is still alive // //Precondition: For accurate results, Dueler array must be ordered by the least to most deadly. // //if missFirst == true the first dueler misses intentionally. //Return the winner duelist in this contest or null if undefined. public static Duelist SingleDuel(Duelist[] duelers, bool missFirst) { Duelist winner = null;//selects the winner if (null != duelers) { int countAlive = duelers.Length; //All duelers are alive at the beginning for (int i = missFirst ? 1 : 0; countAlive > 1; i++) //i = 1 if missFirst = true; else i = 0 { int index = i % duelers.Length; //Selecting dueler by turn. if (null == duelers[index]) //FIXED: checks null elements { countAlive--; } else if (duelers[index].IsAlive && CurrentShooter(duelers, index)) { winner = duelers[index]; countAlive--; } } } return(winner); }