/// <summary> /// Has the aspect of the AI vs. AI gameplay: /// two AIs battle thousands of times, /// reinforcing and subtracting to make each other well trained. /// </summary> /// <param name="ai">the untrained AI that will play with another AI to train</param> /// <param name="aiTrainer">another untrained AI that will play with the other AI to train</param> /// <param name="dp1">a drawpanel to show AI's learning progress</param> /// <param name="dp2">a drawpanel to show second AI's learning progress</param> /// <param name="sticksInGame">the number of sticks the user starts with</param> /// <returns>a better trained AI that will play the user</returns> static AI AiVsAi(AI ai, AI aiTrainer, DrawPanel dp1, DrawPanel dp2, int sticksInGame) { int winAI = 0; int winAITrainer = 0; int turn = rand.Next(2); for (int round = 0; round < TotalRound; round++) { // resets the starting number of sticks each game int sticks = sticksInGame; while (sticks >= 1) { AI selectedAI; if (turn % 2 == 1) { selectedAI = ai; } else { selectedAI = aiTrainer; } sticks -= selectedAI.Chosen(sticks); turn++; } // Reinforces and subtracts the appropriate AI if (turn % 2 == 1) { ai.AiWon(); aiTrainer.AiLost(); winAI++; } else { ai.AiLost(); aiTrainer.AiWon(); winAITrainer++; } // For bugging purposes Console.Clear(); int[,] aiCups = ai.BrainDump(); Console.WriteLine(); int[,] aiTrainerCups = aiTrainer.BrainDump(); dp1.UpdateAI(aiCups); dp2.UpdateAI(aiTrainerCups); } // smarter AI is chosen if (winAI < winAITrainer) { dp1.EliminateLoser("AI 1"); return(aiTrainer); } else { dp2.EliminateLoser("AI 2"); return(ai); } }
/// <summary> /// Calls the methods in AI class that either /// reinforces or subtracts based on the winner. /// </summary> /// <param name="ai">the same AI the user played with</param> /// <param name="winner">human or AI that won the previous game</param> /// <param name="dp1">a drawpanel to show AI's learning progress</param> static void TrainingAI(AI ai, int winner, DrawPanel dp, int option) { if (winner % 2 == 1) { Console.WriteLine("Player, you win.\n"); ai.AiLost(); } else { Console.WriteLine("AI wins.\n"); ai.AiWon(); } // Only update the Drawing Panel if the user is playing Human Vs. AI if (option == 2) { int[,] cups = ai.BrainDump(); Console.WriteLine(); dp.UpdateAI(cups); } }
static void Main(string[] args) { int option = Intro(); int sticks = NumberOfSticks(option); // Creates two AI's that are needed for vs. AI games AI ai = new AI(sticks, PossibleToChoose); AI aiTrainer = new AI(sticks, PossibleToChoose); if (option == 1) { HumanVsHuman(sticks); } // Drawing Panel compatible gameplay if (option == 2) { DrawPanel dp = new DrawPanel(sticks, PossibleToChoose, "AI"); do { int winner = HumanVsAi(ai, sticks); TrainingAI(ai, winner, dp, option); }while (GetRange("Play again (1 = yes, 0 = no)", 0, 1) == 1); } // Drawing Panel compatible gameplay if (option == 3) { DrawPanel dp1 = new DrawPanel(sticks, PossibleToChoose, "AI 1"); DrawPanel dp2 = new DrawPanel(sticks, PossibleToChoose, "AI 2"); ai = AiVsAi(ai, aiTrainer, dp1, dp2, sticks); Console.WriteLine("\nThe AI has finished its training. Let's Begin."); do { int winner = HumanVsAi(ai, sticks); TrainingAI(ai, winner, null, option); }while (GetRange("Play again (1 = yes, 0 = no)", 0, 1) == 1); } ExitGame(); }