/// <summary> /// Gets a card with the lowest rank /// </summary> /// <param name="cards">a list of cards</param> /// <returns>a card with the lowest rank</returns> private PlayingCard GetLowestCard(Cards cards) { PlayingCard lowestCard = cards[0]; // set the lowest card to the 1st card in the list // Loop through the list of cards for (int index = 1; index < cards.Count; index++) { // If a card is lower than the lowest card, update the lowest card if (cards[index] < lowestCard) { lowestCard = cards[index]; } } // Return a card with the lowest rank return(lowestCard); }
/// <summary> /// Gets a list of cards with the lowest rank /// </summary> /// <param name="cards">a list of cards</param> /// <returns>a list of cards with the lowest rank</returns> private Cards GetLowestCards(Cards cards) { Cards returnCards = new Cards(); // a list of cards to return PlayingCard lowestCard = GetLowestCard(cards); // get a card with the lowest rank // Loop through the list of cards for (int index = 0; index < cards.Count; index++) { // If a card has the same rank as the lowest card, and its suit is not a trump suit, // add it to the list of cards to return if (cards[index].Rank == lowestCard.Rank && cards[index].Suit != PlayingCard.TrumpSuit) { returnCards.Add(cards[index]); } } // Return a list of cards with the lowest rank return(returnCards); }
/// <summary> /// Method that implements computer attack logic /// </summary> /// <returns></returns> public PlayingCard ComputerAttacks() { ComputerFoundCard = false; // set ComputerFoundCard property to false PlayingCard returnCard = new PlayingCard(); // card to return // If computer has cards: if (Computer.PlayHand.Count != 0) { // If there are no cards on the table: if (CardsInPlay.Count == 0) { ComputerFoundCard = true; // set ComputerFoundCard property to true returnCard = GetCardToAttack(); // get a card to attack with } // If there are cards on the table else { // Determine if computer has a card to attack with bool canAttack = false; for (int index = 0; index < Computer.PlayHand.Count && !canAttack; index++) { canAttack = CanAttack(Computer.PlayHand[index]); } // If computer has a card/cards to attack with, call GetCardToAttack() method // to find the best card to attack with if (canAttack) { ComputerFoundCard = true; returnCard = GetCardToAttack(); } } } // If computer found a card to attack with, remove it from its hand if (ComputerFoundCard) { Computer.PlayHand.Remove(returnCard); } // Return a card return(returnCard); }
/// <summary> /// Method that implements computer defense logic /// </summary> /// <returns>a card to defend with</returns> public PlayingCard ComputerDefends() { PlayingCard returnCard = new PlayingCard(); // a card to return // If computer doesn't already take cards: if (!ComputerPicksUp) { Cards defendCards = new Cards(); // cards that computer can defend with // Loop through computer's cards for (int index = 0; index < Computer.PlayHand.Count; index++) { // If a card has the same suit as the player's card and its rank is higher, add card to the list if (Computer.PlayHand[index].Suit == CardsInPlay[CardsInPlay.Count - 1].Suit && Computer.PlayHand[index] > CardsInPlay[CardsInPlay.Count - 1]) { defendCards.Add(Computer.PlayHand[index]); } } // If there are cards in the defendCards list, get a card with the lowest rank if (defendCards.Count > 0) { returnCard = GetLowestCard(defendCards); } // Otherwise: else { // If player's card doesn't have a trump suit: if (CardsInPlay[CardsInPlay.Count - 1].Suit != PlayingCard.TrumpSuit) { // Loop through computer's cards for (int index = 0; index < Computer.PlayHand.Count; index++) { // If a card has a trump suit, add it to the list if (Computer.PlayHand[index].Suit == PlayingCard.TrumpSuit) { defendCards.Add(Computer.PlayHand[index]); } } // If there are cards in the list, get a card with the lowest rank from the list if (defendCards.Count > 0) { returnCard = GetLowestCard(defendCards); } // Otherwise, computer takes cards else { ComputerPicksUp = true; } } // Otherwise, computer takes cards else { ComputerPicksUp = true; } } // If computer doesn't take cards, remove a card to return from computer's hand if (!ComputerPicksUp) { Computer.PlayHand.Remove(returnCard); } } return(returnCard); }