/// <summary> /// AI gameplay logic /// </summary> /// <param name="AiPlayState"> Wether the AI attacks or defends</param> private void AiTurn(char AiPlayState) { //btnAccept.Enabled = false; if (AiPlayState == 'D') { if (defender.WinStatus == false) { //Check if the AI is able to play int playableCards = 0; int selected = 0; for (int i = 0; i < defender.PlayHand.Count; i++) { if (defender.PlayHand[i] > game.Table.LastPlayed()) { playableCards++; selected = i; } } //If they are, defend if (playableCards > 0) { DisplayOutput(defender.Name + " Defends with " + defender.PlayHand[selected].ToString()); game.Defend(defender, defender.PlayHand[selected]); } else//Else pick up { DisplayOutput(defender.Name + " Was unable to defend"); game.TakeCards(defender); Players.SkipTurn(); } //Determine if someone won game.DetermineWinner(attacker); game.DetermineWinner(defender); //Determine if the defender has won. Defender is always an AI if (defender.WinStatus == true) { MessageBox.Show(defender.Name + " is Not the durak"); } //Determine if the attacker has won. if (attacker.WinStatus == true) { //If the Defender is an AI, display message. Otherwise, end the game if (attacker.IsAi) { MessageBox.Show(attacker.Name + " is Not the durak"); } else { MessageBox.Show("Congratulations, you are not the durak"); //this.Close(); Application.Exit(); } } //End the turn EndTurn(); } else//If the current player has already won { //Move to the next defender defender = Players.GetNextPlayer(); //If the defender is an AI, recursively call the function if (defender.IsAi) { AiTurn('D'); } else//Otherwise, Player defends { //btnAccept.Enabled = true; DisplayOutput("You must Defend"); //lblTempOutput.Text = game.ShowHand(defender); playState = 'D'; } } } else if (AiPlayState == 'A')//IF the Ai is attacking { //Check if they've won if (attacker.WinStatus == false) { //If they haven't won, Play a card at random int attackingCard = seed.Next(0, attacker.PlayHand.Count); DisplayOutput(attacker.Name + " Plays " + attacker.PlayHand[attackingCard].ToString()); game.Attack(attacker, attacker.PlayHand[attackingCard]); //advance the turn based on if its an Ai next if (defender.IsAi) { AiTurn('D'); } else { playState = 'D'; DisplayOutput("You must Defend2"); //lblTempOutput.Text = game.ShowHand(defender); //btnAccept.Enabled = true; } } else { //If the attacker has already won, Advance the turn. attacker = Players.GetNextPlayer(); defender = Players.PeakNextPlayer(); //Advance turn based on if its an Ai next if (attacker.IsAi) { AiTurn('A'); } else { //btnAccept.Enabled = true; DisplayOutput("\nIt is now your turn"); //lblTempOutput.Text = game.ShowHand(attacker); playState = 'A'; } } } }