/// <summary> /// Based upon the two prisoners determine what the result is. /// •If both Prisoner A and Prisoner B defect (blame the other) then both prisoners get 2 years in prison. /// •If Prisoner A defects and Prisoner B cooperates then Prisoner A is set free and Prisoner B gets 3 years in prison. /// •If both prisoners cooperate then both prisoners get 1 year in prison. /// </summary> /// <param name="prisoner1"></param> /// <param name="prisoner2"></param> public static void DetermineResult(Prisoner prisoner1, Prisoner prisoner2) { Console.WriteLine("*****************************************"); Console.WriteLine("RESULT FOR ITERATION IN DILEMMA CONTEST"); if (prisoner1.LastChoice == StrategyChoice.Cooperate && prisoner2.LastChoice == StrategyChoice.Cooperate) { Console.WriteLine("NONE COOPERATE: Both Prisoners Get 1 Year"); //Both cooperate - both get 1 year in prison prisoner1.AddScore(1); prisoner2.AddScore(1); } else if (prisoner1.LastChoice == StrategyChoice.Cooperate && prisoner2.LastChoice == StrategyChoice.Defect) { Console.WriteLine("ONLY 1 DEFECT: Prisoner 1 goes free, Prisoner 2 Gets 3 years"); //Defect goes free //Cooperate gets 3 year prisoner1.AddScore(0); prisoner2.AddScore(3); } else if (prisoner1.LastChoice == StrategyChoice.Defect && prisoner2.LastChoice == StrategyChoice.Cooperate) { Console.WriteLine("ONLY 1 DEFECT: Prisoner 1 gets 3 years, Prisoner 2 goes free"); prisoner1.AddScore(3); prisoner2.AddScore(0); } else { // Both get 3 years Console.WriteLine("BOTH DEFECT: Both get 2 years"); prisoner1.AddScore(2); prisoner2.AddScore(2); } }
private static void RunPrisonersDilemma() { // Various Presets to cut and paste to test certain conditions //new Prisoner("Cooperator", new Cooperator()); //new Prisoner("Defector", new Defector()); //new Prisoner("titfortat", new TitForTat()); //new Prisoner("RNG", new RandomChoice()); //new Prisoner("untitfortate", new UnTitForTat()); //Setup both prisoners for the game. var prisoner1 = new Prisoner("Random", new RandomChoice()); var prisoner2 = new Prisoner("untitfortate", new UnTitForTat()); int iterations = 10; var pd = new Game(prisoner1, prisoner2); Console.WriteLine(string.Format("Prisoner1: {0} Prisoner2: {1}", prisoner1.Name, prisoner2.Name)); // How many iterations are we going to step through for this game for (int i = 1; i < iterations; i++) { Console.WriteLine(pd.Step()); } }
/// <summary> /// Establisher a new Game with 2 prisoners /// </summary> /// <param name="prisoner1"></param> /// <param name="prisoner2"></param> public Game(Prisoner prisoner1, Prisoner prisoner2) { this.prisoner1 = prisoner1; this.prisoner2 = prisoner2; }