Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Read carefully. You are to simulate a game called TrumpWar , which is a variatio

ID: 3838859 • Letter: R

Question

Read carefully.

You are to simulate a game called TrumpWar, which is a variation of the child’s card game called War. Like War, there is no strategy to playing the game, so you are writing the mechanics of 2 people playing against each other according to the rules.   Unlike War, there is no risking 3 cards in case of a tie and trump cards have been added (that beat non-trump cards).

The setup of a TrumpWar is as follows:

Each player has an ordered pile of Cards. There is also a pile of Cards that are designated as “Trump Cards”; this means that they automatically will beat any non-trump card, no matter the rank of each card is. (As you picture this, remember that the Card instances will be references, so a player’s pile and the trump pile can both refer to the same Card).   Each of these piles of Cards should be stored in an ArrayList so your logic can use ArrayList’s methods.

They then play “hands” against each other until one player wins. The game is over when one player has MORE THAN twice as many Cards as the other player (checked after the hand is played). They will always play at least one hand in the game.

During each hand, each player takes a card out of their ArrayList (from position 0) and then plays the Cards against each other according to the rules below. Ace is considered the high card. So a key thing is to be able to determine (in an OOP way) which Card is higher…

During each hand, each player removes their top Card (from position 0 of the ArrayList) to play. Then the specific rules for the hand are:

If one player has a trump card and the other does not, then the player with the trump card wins and gets both cards and also the contents of the Treasury (see the last rule below) and adds them to the bottom of their pile (ArrayList). Be sure that Player1’s Card, THEN Player2’s Card, then the contents of the Treasury are added in that order. The player that won the hand adds 1 to their current winning streak. Also keep track of that player’s longest winning streak for the whole game.

But if both players have trump cards, then the player whose trump card has the higher rank wins and gets both cards and also the contents of the Treasury (see the last rule below) and adds them to the bottom of their pile (ArrayList). Be sure that Player1’s Card, THEN Player2’s Card, then the contents of the Treasury are added in that order. The player that won the hand adds 1 to their current winning streak. Also keep track of that player’s longest winning streak for the whole game.

But if neither player has a trump card, then the player whose card has the higher rank wins and gets both cards and also the contents of the Treasury (see the last rule below) and adds them to the bottom of their pile (ArrayList). Be sure that Player1’s Card, THEN Player2’s Card, then the contents of the Treasury are added in that order. The player that won the hand adds 1 to their current winning streak. Also keep track of that player’s longest winning streak for the whole game.

But if both players have trump cards with the same rank or non-trump cards with the same rank, then it is a tie. The player with the longer winning streak for the whole game gets both cards and also the contents of the Treasury (see the last rule below) and puts them on the bottom of their pile (ArrayList). Be sure that Player1’s Card, THEN Player2’s Card, then the contents of the Treasury are added in that order. Since this hand was a tie, both players current winning streaks must start over at 0.

But if both players have trump cards with the same rank or non-trump cards with the same rank and also have the same longest winning streak for the whole game, then it is a tie and the cards go into another pile (ArrayList) called the Treasury. Since this hand was a tie, both player’s current winning streaks must start over at 0.

Using ArrayList’s methods: Once your ArrayLists are created and filled with Cards (for details, see the constructor requirements below), most of the functionality can be done with the ArrayList methods. Specifically, it would help if you were familiar with:

ArrayList’s add method

ArrayList’s remove method

ArrayList’s size() method

ArrayList’s contains method

ArrayList’s toString() method

ArrayList’s addAll method (if you want, when you move Cards from the Treasury).

ArrayList’s clear() method (if you use addAll, since the Treasury will not be emptied)

Card’s compareTo method.

Your TrumpWar class should have:

Data that will have 4 ArrayList’s; one for player1’s Cards, one for player2’s Cards, one for the trump Cards, and one for the treasury Cards. Note that they can be declared here

       ( private ArrayList p1Cards;   for example)

and then created in the constructors. You can also have additional data to hold the current and longest winning streak of each player.

A default constructor:   Normally, the code in the default constructor would create a deck of 52 Cards, shuffle them, deal 26 to each player, and choose some at random to put in the trump ArrayList.   But…this is too non-specific for Hypergrade to handle and too many Cards to work with.   Therefore, your default constructor could just create the 4 ArrayLists.

     (   p1Cards = new ArrayList();   for example)

The tester will not use the default constructor, but will create specific situations using the parameterized constructor.

A parameterized constructor that receives 3 arrays of Cards, which represent player1’s Cards, player2’s Cards, and the trump cards respectively. It should:

Create the 4 new ArrayList instances

           (   p1Cards = new ArrayList();    for example)

Fill player1’s ArrayList of Cards by traversing the first array of Cards (player1’s), starting at position 0, and tell player1’s ArrayList to add each Card to itself.

Fill player1’s ArrayList of Cards by traversing the second array of Cards (player2’s), starting at position 0, and tell player2’s ArrayList to add each Card to itself.

Fill the ArrayList of trump Cards by traversing the third array of Cards (the trump cards), starting at position 0, and tell the trump ArrayList to add each Card to itself.

(Be sure this tests correctly, as this constructor will be used to generate different situations to test your TrumpWar)

A method called play(), which actually contains the loop and logic to play a game of TrumpWar. The same logic should happen over and over again using the rules above until one player has more than twice as many cards as the other player. It should be printing the results (see below) as it loops through the logic.  

Card code:

public class Card implements Comparable<Card>
{
//data
private int rank;
private char suit;
  
  
//Default Constructor
public Card()
{
this.rank = 14;
this.suit = 'S';
}
  
//Parameterized Constructor
public Card(int newrank, char newsuit)
{
if (newrank < 2 || newrank > 14)
{
throw new IllegalArgumentException("Invalid Card rank. Please"
+ "a rank between 2 and 14");   
}
  
if (newsuit != 'C' && newsuit != 'D' && newsuit != 'H' && newsuit != 'S')
{
throw new IllegalArgumentException("Invalid Card suit. Please enter"
+ "a valid standard Card suit (Hearts: H, Clubs: C,"
+ " Diamonds: D, Spades: S");
}
  
  
this.rank = newrank;
this.suit = newsuit;
  
}
  
// Copy Constructor
public Card( Card copy)
{
this.suit = copy.suit;
this.rank = copy.rank;
}
  
//Methods
public String toString()
{
  
switch(this.rank)
{
case 11:
return "J"+this.suit;
case 12:
return "Q"+this.suit;
case 13:
return "K"+this.suit;
case 14:
return "A"+this.suit;
default:
return this.rank + ""+this.suit;
  
}
  
  
}
  

  
public int compareTo(Card other)
{
if(this.rank == other.rank)
return 0;
else if(this.rank > other.rank)
return 1;
else
return -1;
  
}
  
  
public boolean equals(Object obj)
{
if(obj == null)
return false;
  
if(obj.getClass() != this.getClass())
return false;
  
Card objCard = (Card)obj;

return this.rank == objCard.rank && this.suit == objCard.suit;

}
  
  
  
  
  
}

Tester:

public class TrumpWarTester
{
   public static void main(String[ ] args)
   {
       //get the arguments for the test
       java.util.Scanner kb = new java.util.Scanner(System.in);
       String test = kb.nextLine();

       //***************************************************
       //***************************************************

       if (test.equalsIgnoreCase("1. In first hand, everything ties, so cards go into Treasury. Player1 wins second hand (and Treasury) because they have a trump."))
       {
           boolean printDescription = true;
           boolean checkChanges = true;

           try
           {
               Card[ ] p1CardArray = { new Card(2, 'S'), new Card(3, 'C') };
               Card[ ] p2CardArray = { new Card(2, 'D'), new Card(12, 'H') };
               Card[ ] trumpArray = { new Card(3, 'C'), new Card(8, 'S'), new Card(8, 'H'), new Card(11, 'D') };

               TrumpWar myTW = new TrumpWar(p1CardArray, p2CardArray, trumpArray);
               myTW.play();
           }
           catch (Throwable ex)
           {
               //System.out.println(" " + ex);
               System.out.println(" " + ex.getClass().getName());
           }
       }

       else if (test.equalsIgnoreCase("2. In first hand, everything ties, so cards go into Treasury. Player2 wins second hand (and Treasury) because they have a trump."))
       {
           boolean printDescription = true;
           boolean checkChanges = true;

           try
           {
               Card[ ] p2CardArray = { new Card(2, 'S'), new Card(3, 'C') };
               Card[ ] p1CardArray = { new Card(2, 'D'), new Card(12, 'H') };
               Card[ ] trumpArray = { new Card(3, 'C'), new Card(8, 'S'), new Card(8, 'H'), new Card(11, 'D') };

               TrumpWar myTW = new TrumpWar(p1CardArray, p2CardArray, trumpArray);
               myTW.play();
           }
           catch (Throwable ex)
           {
               //System.out.println(" " + ex);
               System.out.println(" " + ex.getClass().getName());
           }
       }

       else if (test.equalsIgnoreCase("3. In first hand, everything ties, so cards go into Treasury. Player1 wins second hand (and Treasury) because they have a higher trump."))
       {
           boolean printDescription = true;
           boolean checkChanges = true;

           try
           {
               Card[ ] p1CardArray = { new Card(2, 'S'), new Card(8, 'H') };
               Card[ ] p2CardArray = { new Card(2, 'D'), new Card(3, 'C') };
               Card[ ] trumpArray = { new Card(3, 'C'), new Card(8, 'S'), new Card(8, 'H'), new Card(11, 'D') };

               TrumpWar myTW = new TrumpWar(p1CardArray, p2CardArray, trumpArray);
               myTW.play();
           }
           catch (Throwable ex)
           {
               //System.out.println(" " + ex);
               System.out.println(" " + ex.getClass().getName());
           }
       }

       else if (test.equalsIgnoreCase("4. In first hand, everything ties, so cards go into Treasury. Player2 wins second hand (and Treasury) because they have a higher trump."))
       {
           boolean printDescription = true;
           boolean checkChanges = true;

           try
           {
               Card[ ] p2CardArray = { new Card(2, 'S'), new Card(8, 'H') };
               Card[ ] p1CardArray = { new Card(2, 'D'), new Card(3, 'C') };
               Card[ ] trumpArray = { new Card(3, 'C'), new Card(8, 'S'), new Card(8, 'H'), new Card(11, 'D') };

               TrumpWar myTW = new TrumpWar(p1CardArray, p2CardArray, trumpArray);
               myTW.play();
           }
           catch (Throwable ex)
           {
               //System.out.println(" " + ex);
               System.out.println(" " + ex.getClass().getName());
           }
       }

       else if (test.equalsIgnoreCase("5. In first hand, everything ties, so cards go into Treasury. Player1 wins second hand (and Treasury) because they have a higher non-trump."))
       {
           boolean printDescription = true;
           boolean checkChanges = true;

           try
           {
               Card[ ] p1CardArray = { new Card(2, 'S'), new Card(12, 'D') };
               Card[ ] p2CardArray = { new Card(2, 'D'), new Card(11, 'C') };
               Card[ ] trumpArray = { new Card(3, 'C'), new Card(8, 'S'), new Card(8, 'H'), new Card(11, 'D') };

               TrumpWar myTW = new TrumpWar(p1CardArray, p2CardArray, trumpArray);
               myTW.play();
           }
           catch (Throwable ex)
           {
               //System.out.println(" " + ex);
               System.out.println(" " + ex.getClass().getName());
           }
       }

       else if (test.equalsIgnoreCase("6. In first hand, everything ties, so cards go into Treasury. Player2 wins second hand (and Treasury) because they have a higher non-trump."))
       {
           boolean printDescription = true;
           boolean checkChanges = true;

           try
           {
               Card[ ] p2CardArray = { new Card(2, 'S'), new Card(12, 'D') };
               Card[ ] p1CardArray = { new Card(2, 'D'), new Card(11, 'C') };
               Card[ ] trumpArray = { new Card(3, 'C'), new Card(8, 'S'), new Card(8, 'H'), new Card(11, 'D') };

               TrumpWar myTW = new TrumpWar(p1CardArray, p2CardArray, trumpArray);
               myTW.play();
           }
           catch (Throwable ex)
           {
               //System.out.println(" " + ex);
               System.out.println(" " + ex.getClass().getName());
           }
       }

       else if (test.equalsIgnoreCase("7. Hands #6 and #8 are ties but Player1 wins them (and Treasury) because they have longer overall streaks."))
       {
           boolean printDescription = true;
           boolean checkChanges = true;

           try
           {
               Card[ ] p1CardArray = {    new Card(5, 'C'), new Card(14, 'H'), new Card(9, 'H'), new Card(3, 'H'), new Card(5, 'D'),
                                                           new Card(4, 'H'), new Card(8, 'C'), new Card(5, 'D'), new Card(6, 'C'), new Card(14, 'C'), };
               Card[ ] p2CardArray = {    new Card(2, 'S'), new Card(10, 'D'), new Card(6, 'C'), new Card(9, 'D'), new Card(7, 'S'),
                                                           new Card(4, 'D'), new Card(9, 'S'), new Card(5, 'S'), new Card(4, 'S'), new Card(11, 'S'), };

               Card[ ] trumpArray = { new Card(3, 'C'), new Card(8, 'S'), new Card(8, 'H'), new Card(11, 'D') };

               TrumpWar myTW = new TrumpWar(p1CardArray, p2CardArray, trumpArray);
               myTW.play();
           }
           catch (Throwable ex)
           {
               //System.out.println(" " + ex);
               System.out.println(" " + ex.getClass().getName());
           }
       }

       else if (test.equalsIgnoreCase("8. Hands #6 and #8 are ties but Player2 wins them (and Treasury) because they have longer overall streaks."))
       {
           boolean printDescription = true;
           boolean checkChanges = true;

           try
           {
               Card[ ] p2CardArray = {    new Card(5, 'C'), new Card(14, 'H'), new Card(9, 'H'), new Card(3, 'H'), new Card(5, 'D'),
                                                           new Card(4, 'H'), new Card(8, 'C'), new Card(5, 'D'), new Card(6, 'C'), new Card(14, 'C'), };
               Card[ ] p1CardArray = {    new Card(2, 'S'), new Card(10, 'D'), new Card(6, 'C'), new Card(9, 'D'), new Card(7, 'S'),
                                                           new Card(4, 'D'), new Card(9, 'S'), new Card(5, 'S'), new Card(4, 'S'), new Card(11, 'S'), };

               Card[ ] trumpArray = { new Card(3, 'C'), new Card(8, 'S'), new Card(8, 'H'), new Card(11, 'D') };

               TrumpWar myTW = new TrumpWar(p1CardArray, p2CardArray, trumpArray);
               myTW.play();
           }
           catch (Throwable ex)
           {
               //System.out.println(" " + ex);
               System.out.println(" " + ex.getClass().getName());
           }
       }

       else if (test.equalsIgnoreCase("9. Hand #5 and #6 are ties and both players have same overall streaks. Cards go into Treasury until Player1 wins Hand #7. No winner until > twice as many cards."))
       {
           boolean printDescription = true;
           boolean checkChanges = true;

           try
           {
               Card[ ] p2CardArray = {    new Card(14, 'H'), new Card(9, 'H'), new Card(3, 'H'), new Card(5, 'D'),
                                                           new Card(4, 'H'), new Card(8, 'S'), new Card(13, 'S'), new Card(4, 'C'), new Card(5, 'S') };
               Card[ ] p1CardArray = {    new Card(10, 'D'), new Card(6, 'C'), new Card(9, 'D'), new Card(7, 'S'),
                                                           new Card(4, 'D'), new Card(8, 'H'), new Card(14, 'D'), new Card(6, 'S'), new Card(10, 'C') };

               Card[ ] trumpArray = { new Card(3, 'C'), new Card(8, 'S'), new Card(8, 'H'), new Card(11, 'D') };

               TrumpWar myTW = new TrumpWar(p1CardArray, p2CardArray, trumpArray);
               myTW.play();
           }
           catch (Throwable ex)
           {
               //System.out.println(" " + ex);
               System.out.println(" " + ex.getClass().getName());
           }
       }

       else if (test.equalsIgnoreCase("10. Hand #5 and #6 are ties and both players have same overall streaks. Cards go into Treasury until Player2 wins Hand #7. No winner until > twice as many cards."))
       {
           boolean printDescription = true;
           boolean checkChanges = true;

           try
           {
               Card[ ] p1CardArray = {    new Card(14, 'H'), new Card(9, 'H'), new Card(3, 'H'), new Card(5, 'D'),
                                                           new Card(4, 'H'), new Card(8, 'S'), new Card(13, 'S'), new Card(4, 'C'), new Card(5, 'S') };
               Card[ ] p2CardArray = {    new Card(10, 'D'), new Card(6, 'C'), new Card(9, 'D'), new Card(7, 'S'),
                                                           new Card(4, 'D'), new Card(8, 'H'), new Card(14, 'D'), new Card(6, 'S'), new Card(10, 'C') };

               Card[ ] trumpArray = { new Card(3, 'C'), new Card(8, 'S'), new Card(8, 'H'), new Card(11, 'D') };

               TrumpWar myTW = new TrumpWar(p1CardArray, p2CardArray, trumpArray);
               myTW.play();
           }
           catch (Throwable ex)
           {
               //System.out.println(" " + ex);
               System.out.println(" " + ex.getClass().getName());
           }
       }


       else if (test.equalsIgnoreCase("11. A game with every possibility."))
       {
           boolean printDescription = true;
           boolean checkChanges = true;

           try
           {
               Card[ ] p1CardArray = { new Card(8, 'H'),
                                                       new Card(3, 'H'),
                                                       new Card(13, 'C'),
                                                       new Card(10, 'D'),
                                                       new Card(11, 'C'),
                                                       new Card(8, 'D'),
                                                       new Card(4, 'D'),
                                                       new Card(7, 'H'),
                                                       new Card(11, 'H'),
                                                       new Card(6, 'D'),
                                                       new Card(2, 'C'),
                                                       new Card(3, 'D'),
                                                       new Card(7, 'H'),
                                                       new Card(10, 'S'),
                                                       new Card(2, 'H'),
                                                       new Card(5, 'D') };

               Card[ ] p2CardArray = { new Card(8, 'C'),
                                                       new Card(14, 'D'),
                                                       new Card(13, 'H'),
                                                       new Card(6, 'C'),
                                                       new Card(2, 'S'),
                                                       new Card(5, 'S'),
                                                       new Card(10, 'D'),
                                                       new Card(7, 'S'),
                                                       new Card(14, 'C'),
                                                       new Card(12, 'H'),
                                                       new Card(14, 'S'),
                                                       new Card(8, 'S'),
                                                       new Card(7, 'S'),
                                                       new Card(12, 'C'),
                                                       new Card(7, 'D'),
                                                       new Card(10, 'H') };

               Card[ ] trumpArray = {    new Card(3, 'H'),
                                                       new Card(4, 'D'),
                                                       new Card(5, 'S'),
                                                       new Card(6, 'C'),
                                                       new Card(7, 'H'),
                                                       new Card(7, 'S'),
                                                       new Card(10, 'D'),
                                                       new Card(12, 'S'),
                                                       new Card(12, 'H') };

               TrumpWar myTW = new TrumpWar(p1CardArray, p2CardArray, trumpArray);
               myTW.play();
           }
           catch (Throwable ex)
           {
               //System.out.println(" " + ex);
               System.out.println(" " + ex.getClass().getName());
           }
       }

   }
}

Explanation / Answer

import java.util.ArrayList;

public class TrumpWar {

   // Instance variables
   private ArrayList<Card> player1;
   private ArrayList<Card> player2;
   private ArrayList<Card> trumpCards;
   private ArrayList<Card> treasury;
   private int player1WinningStreak;
   private int player2WinningStreak;

   /**
   * Constructor
   */
   public TrumpWar() {
       this.player1 = new ArrayList<Card>();
       this.player2 = new ArrayList<Card>();
       this.trumpCards = new ArrayList<Card>();
       this.treasury = new ArrayList<Card>();
       this.player1WinningStreak = 0;
       this.player2WinningStreak = 0;
   }

   /**
   * @param player1
   * @param player2
   * @param trumpCards
   */
   public TrumpWar(Card[] player1, Card[] player2, Card[] trumpCards) {
       this.player1 = new ArrayList<Card>();
       this.player2 = new ArrayList<Card>();
       this.trumpCards = new ArrayList<Card>();
       setCards(this.player1, player1);
       setCards(this.player2, player2);
       setCards(this.trumpCards, trumpCards);
       this.treasury = new ArrayList<Card>();
       this.player1WinningStreak = 0;
       this.player2WinningStreak = 0;
   }

   /**
   * Copies contents of array to the list
   */
   private void setCards(ArrayList<Card> cardList, Card[] cardArray) {
       for (int i = 0; i < cardArray.length; i++) {
           cardList.add(i, cardArray[i]);
       }
   }

   /**
   * Updates the cards at hand of the winner of the current play
   *
   * @param winner
   *            - cards at hand of the winner
   * @param winnerCard
   *            - card played by player1
   * @param loserCard
   *            - card played by player2
   */
   private void updatePlayer(ArrayList<Card> winner, Card winnerCard, Card loserCard) {

       winner.add(winner.size(), winnerCard);
       winner.add(winner.size(), loserCard);
       winner.addAll(winner.size(), this.treasury);
   }

   /**
   * Plays the top card from both the players Contains the logic to decide the
   * winner
   */
   public void play() {
       while (true) {
           // Get the top cards
           Card p1 = this.player1.remove(0);
           Card p2 = this.player2.remove(0);
  
           // Check if any of the players has a trump card
           if (this.trumpCards.contains(p1) && !this.trumpCards.contains(p2)) {
               // If player1 has the trump card
               updatePlayer(this.player1, p1, p2);
               // Update winning streak
               this.player1WinningStreak += 1;
              
           } else if (this.trumpCards.contains(p2) && !this.trumpCards.contains(p1)) {
               // If player2 has the trump card
               updatePlayer(this.player2, p2, p1);

               // Update winning streak
               this.player2WinningStreak += 1;
              
           } else {
               // Check if both player has the same trump card or the same
               // non-trump
               int result = p1.compareTo(p2);
  
               if (result > 0) {// Player1 has the highest card
                   updatePlayer(this.player1, p1, p2);
                  
                   // Update winning streak
                   this.player1WinningStreak += 1;
                  
               } else if (result < 0) { // Player2 has the highest card
                   updatePlayer(this.player2, p2, p1);

                   // Update winning streak
                   this.player2WinningStreak += 1;
                  
               } else {
                   // If both players have the same rank card
                   // Check if the winning streak of each player
                   if (this.player1WinningStreak > this.player2WinningStreak) {
                       // If player1 has longest winning streak
                       updatePlayer(this.player1, p1, p2);

                       // Update winning streak
                       this.player1WinningStreak += 1;
                      
                   } else if (this.player2WinningStreak > this.player1WinningStreak) {
                       // If player2 has longest winning streak
                       updatePlayer(this.player2, p2, p1);
                      
                       // Update winning streak
                       this.player2WinningStreak += 1;
                      
                   } else { // If both players has the same winning streak
                       // Add both cards to the treasury
                       this.treasury.add(this.treasury.size(), p1);
                       this.treasury.add(this.treasury.size(), p2);
                      
                       // Reset both player's winning streak
                       this.player1WinningStreak = 0;
                       this.player2WinningStreak = 0;
                   }
               }
           }
          
           // Check if there is a winner
           if (this.player1.size() > (2 * this.player2.size())) {
               System.out.println("Player1 wins");
               break;
           } else if (this.player2.size() > (2 * this.player1.size())) {
               System.out.println("Player2 wins");
               break;
           }
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote