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

Object oriented java using eclipse: I need help programming the war game using t

ID: 3791324 • Letter: O

Question

Object oriented java using eclipse:

I need help programming the war game using the deck.java and card.java classes(code displayed below) using eclipse following the steps below. The output of the code should also look like the sample output(also displayed below)

deck.java:

package assignment3;                               // change to your package
import java.util.Random;                           // import Random class

public class Deck {
  
   private Card [ ] deck = new Card [52];           // array of 52 Card references
   private int topCard = 0;                       // top card of deck
  
   public Deck ( )                                   // POST: deck is initialized by suit and rank
   {   for (int k=0; k<52; k++)
           deck[k] = new Card(k);                   // card object created with parameterized constructor setting card number
   }
      
   public void shuffle ( )                           // POST: deck is randomly shuffled
   {   int index;                                   // random index 0 .. 51
       int temp;                                   // temporary variable for swap
       Random rand = new Random();                   // Random number object

       for (int k = 0; k < 52; k++)               // process each card number
       {   index = rand.nextInt(52);               // generate random new location
           temp = deck[k].getCardNumber();           // swap card numbers in two Card objects
           deck[k].setCardNumber(deck[index].getCardNumber());  
           deck[index].setCardNumber(temp);          
       }
   }
  
   public String toString ( )                       // POST: return string of 4 lines of deck
   {   String result = new String ();
       for (int k = 0; k < 52; k++)               // process each card number
       {   if (k % 13 == 0)                        // append newline so 4 lines of card values
               result = result + " ";              
                                                   // use field width of 4 spaces
           result = result.concat(String.format("%4s",deck[k].toString()));
       }
       result = result.concat(" ");               // append newline
       return result;
   }

   public Card deal ( )                           // POST: return the top card
   {   topCard++;                                   // increase top card marker
       return deck[topCard-1];                       // return previous top card
   }                                               // code could be shortened to: return deck[topCard++] using postfix ++ operator
}

Card.java:

package assignment3;                       // change to your package name

public class Card {
   private int cardNumber;                   // card number 0 .. 51
  
   public Card ( )                        // POST: card number set to 0 (2 Diamonds)
   {   cardNumber = 0;  
   }

   public Card (int n)                       // PRE: 0 <= n <= 51
   {   cardNumber = n;                       // POST: card number set to n
   }
  
   public int getCardNumber( )               // POST: return card number
   {   return cardNumber;
   }
  
   public void setCardNumber (int n)       // PRE: 0 <= n <= 51
   {   cardNumber = n;                       // POST: card number set to n
   }
   public String getRank ( )               // POST: return card rank "2" .. "A"
   {   int rank = cardNumber % 13;          
       if (rank == 0) return "2";
       else if (rank == 1) return "3";
       else if (rank == 2) return "4";
       else if (rank == 3) return "5";
       else if (rank == 4) return "6";
       else if (rank == 5) return "7";
       else if (rank == 6) return "8";
       else if (rank == 7) return "9";
       else if (rank == 8) return "10";
       else if (rank == 9) return "J";
       else if (rank == 10) return "Q";
       else if (rank == 11) return "K";
       else return "A";
   }
  
   public String getSuit ( )               // POST: return card suit
   {   int suit = cardNumber / 13;
       if (suit == 1) return "u2660";           // spade
       else if (suit == 2) return "u2663";   // club
       else if (suit == 3) return "u2665";   // heart
       else return "u2666";                   // diamond
      
   }
  
   public String toString()               // POST: return rank/suit
   {   return getRank() + getSuit();
   }
}

Part II: (80 pts In this problem you will use the Deck class to play a simplified version of the card game War. A player wins when all cards are taken from the other player. We will do a shorter variation where one only pass is made through the original hands. When all 26 cards have been processed the winner is the player with the larger win pile or it is a draw. In a real game, once the hand is empty the player uses the win pile as the hand. Rules of War: Each player begins with 26 cards from the shuffled deck. During a round each player displays the top card from their hand. The player with the higher card takes both cards and puts them in a win pile If the cards are the same rank a War loop begins. Each player displays five cards from the top of their hand. The player with the higher fifth card takes all cards and puts them in their win pile. If there is a tie another War begins and the winner of that takes all cards. So continues the War loop If a player cannot place five cards from the hand, as many as possible are placed and the top card is used. Submit Turn in neatly formatted source code of WarGame.java and TestWarGame.java printed from Eclipse in landscape orientation and output of a game showing hands and win piles each round with at least one win for each player and one war example. The output can be printed landscaped, no word wrap accepted. See attached sample; your output should display similar information for each round. Use your name in the output. Notes: 1. The Deck class illustrated in lecture should be used. Code for Deck.java and Card.java is available on First Class. You must use arrays in the WarGame class; no higher level data structures permitted. Command option

Explanation / Answer

WarGame.java:

public class WarGame {
   Card[] A = new Card[26];
   Card[] B = new Card[26];

   Card[] AWinPile = new Card[52];
   Card[] BWinPile = new Card[52];

   int AtopIndex = 0;
   int BtopIndex = 0;

   int AwinPileIndex = 0;
   int BwinPileIndex = 0;

   public WarGame(Deck deck) {
       for (int i = 0; i < 26; i++) {
           A[i] = deck.deal();
           B[i] = deck.deal();
       }
       for (int i = 0; i < 52; i++) {
           AWinPile[i] = BWinPile[i] = null;
       }
   }

   public void startGame() {
       System.out.println("War Game Started:");
       boolean drawn = false;
       while (AtopIndex < 26 && BtopIndex < 26) {
           printStats();

           Card a = A[AtopIndex];
           Card b = B[BtopIndex];
           System.out.println("Player 1:" + a);
           System.out.println("Player 2:" + b);

           AtopIndex++;
           BtopIndex++;

           int aCardNo = a.getCardNumber() % 13;
           int bCardNo = b.getCardNumber() % 13;

           if (aCardNo > bCardNo) {
               System.out.println("** Player1 Wins **");

               AWinPile[AwinPileIndex++] = a;
               AWinPile[AwinPileIndex++] = b;

               printWinPiles();
               System.out.println();
           } else if (aCardNo < bCardNo) {
               System.out.println("** Player2 Wins **");

               BWinPile[BwinPileIndex++] = a;
               BWinPile[BwinPileIndex++] = b;
               printWinPiles();
               System.out.println();
           } else {

               System.out.println("** Tie-WAR **");
               int size = 5;
               while (true) {
                   int winner = fetchCardsAndTellWinner(size);
                  
                   if (winner == 1) {
                       System.out.println("** Player1 Wins **");
                       int count = 0;

                       AWinPile[AwinPileIndex++] = A[AtopIndex - 1];
                       AWinPile[AwinPileIndex++] = B[BtopIndex - 1];
                      
                       for (int i = AtopIndex; i < 26; i++) {
                           AWinPile[AwinPileIndex++] = A[i];
                           AtopIndex++;
                           count++;
                           if (count > size) {
                               break;
                           }
                       }
                       count = 0;
                       for (int i = BtopIndex; i < 26; i++) {
                           AWinPile[AwinPileIndex++] = B[i];
                           BtopIndex++;
                           count++;
                           if (count > size) {
                               break;
                           }
                       }
                       printWinPiles();
                       System.out.println();
                       break;
                   } else if (winner == -1) {
                       System.out.println("** Player2 Wins **");
                       int count = 0;
                      
                       BWinPile[BwinPileIndex++] = A[AtopIndex - 1];
                       BWinPile[BwinPileIndex++] = B[BtopIndex - 1];

                       for (int i = AtopIndex; i < 26; i++) {
                           BWinPile[BwinPileIndex++] = A[i];
                           AtopIndex++;
                           count++;
                           if (count > size) {
                               break;
                           }
                       }

                       count = 0;
                       for (int i = BtopIndex; i < 26; i++) {
                           BWinPile[BwinPileIndex++] = B[i];
                           BtopIndex++;
                           count++;
                           if (count > size) {
                               break;
                           }
                       }

                       printWinPiles();
                       System.out.println();
                       break;
                   } else if (winner == 0) {
                       size += 5;
                   } else if (winner == -2) {
                       // draw now..
                       drawn = true;
                       System.out.println("match is drawn now.");
                       break;
                   }
               }
           }
       }
      
       System.out.println(" +++++++++++++++++++ Result ++++++++++++++");
       if(drawn) {
           System.out.println("match is drwan");
       } else {
           if(AwinPileIndex > BwinPileIndex) {
               System.out.println("A has won");
           } else if(AwinPileIndex < BwinPileIndex) {
               System.out.println("B has won");
           } else {
               System.out.println("Match is drawn");
           }
       }
   }

   private int fetchCardsAndTellWinner(int size) {
       Card aTop = null, bTop = null;
       int i = 0, j = 0;

       System.out.print("Player 1 war cards:");
       for (i = AtopIndex; i < 26 && i < (AtopIndex + size); i++) {
           System.out.print(" " + A[i]);
           aTop = A[i];
       }
       System.out.println();
       System.out.print("Player 2 war cards:");
       for (j = BtopIndex; j < 26 && j < (BtopIndex + size); j++) {
           System.out.print(" " + B[j]);
           bTop = B[j];
       }
       System.out.println();

       if (aTop != null && bTop != null) {
           int aCardNo = aTop.getCardNumber() % 13;
           int bCardNo = bTop.getCardNumber() % 13;

           if (aCardNo > bCardNo)
               return 1;
           else if (aCardNo < bCardNo)
               return -1;
           else if (i == 26 && j == 26) // no further comparison can be done..
                                           // draw
               return -2;
           else
               return 0;
       } else {
           // now it is draw.. no more card are left
           return -2;
       }
   }

   public void printStats() {
       System.out.print("Player 1 Hand:");
       for (int i = AtopIndex; i < A.length; i++) {
           if (A[i] == null) {
               break;
           }
           System.out.print(" " + A[i]);
       }
       System.out.println();
       System.out.print("Player 2 Hand:");
       for (int i = BtopIndex; i < B.length; i++) {
           if (B[i] == null) {
               break;
           }
           System.out.print(" " + B[i]);
       }
       System.out.println();
   }

   public void printWinPiles() {
       System.out.print("Player 1 win pile:");
       for (int i = 0; i < AwinPileIndex; i++) {
           System.out.print(" " + AWinPile[i]);
       }
       System.out.println();
       System.out.print("Player 2 win pile:");
       for (int i = 0; i < BwinPileIndex; i++) {
           System.out.print(" " + BWinPile[i]);
       }
       System.out.println();
   }

   // reshuffle A and B arrays
   public void reshuffle() {
       int i;
       for (i = 0; i < 52; i++) {
           if (A[i] != null) {
               i++;
               break;
           }
       }
       // means there is no card in A's array
       if (i != 52) {
           // starting from 1, there are i null elements
           int j = 0;
           for (; j < i; j++) {
               A[j] = A[j + i];
           }
           AtopIndex = 0;
           // AendIndex = j;
           for (; j < 52; j++) {
               A[j] = null;
           }
       }

       for (i = 0; i < 52; i++) {
           if (B[i] != null) {
               i++;
               break;
           }
       }
       // means there is no card in A's array
       if (i != 52) {
           // starting from 1, there are i null elements
           int j = 0;
           for (; j < i; j++) {
               B[j] = B[j + i];
           }
           BtopIndex = 0;
           // BendIndex = j;
           for (; j < 52; j++) {
               B[j] = null;
           }
       }
   }
}

TestWarGames.java:


public class TestWarGames {
   public static void main(String a[]) {
       Deck deck = new Deck();
       deck.shuffle();
      
       WarGame warGame = new WarGame(deck);
       warGame.startGame();
   }
}

Sample Output:

War Game Started:
Player 1 Hand: 10 J 3 2 K 6 10 4 2 2 3 Q 2 A 9 10 5 7 A 3 9 4 7 9 3 5
Player 2 Hand: 5 Q 4 6 J 8 6 Q J K 8 4 Q 8 10 9 8 K 5 7 7 A J K A 6
Player 1:10
Player 2:5
** Player1 Wins **
Player 1 win pile: 10 5
Player 2 win pile:

Player 1 Hand: J 3 2 K 6 10 4 2 2 3 Q 2 A 9 10 5 7 A 3 9 4 7 9 3 5
Player 2 Hand: Q 4 6 J 8 6 Q J K 8 4 Q 8 10 9 8 K 5 7 7 A J K A 6
Player 1:J
Player 2:Q
** Player2 Wins **
Player 1 win pile: 10 5
Player 2 win pile: J Q

Player 1 Hand: 3 2 K 6 10 4 2 2 3 Q 2 A 9 10 5 7 A 3 9 4 7 9 3 5
Player 2 Hand: 4 6 J 8 6 Q J K 8 4 Q 8 10 9 8 K 5 7 7 A J K A 6
Player 1:3
Player 2:4
** Player2 Wins **
Player 1 win pile: 10 5
Player 2 win pile: J Q 3 4

Player 1 Hand: 2 K 6 10 4 2 2 3 Q 2 A 9 10 5 7 A 3 9 4 7 9 3 5
Player 2 Hand: 6 J 8 6 Q J K 8 4 Q 8 10 9 8 K 5 7 7 A J K A 6
Player 1:2
Player 2:6
** Player2 Wins **
Player 1 win pile: 10 5
Player 2 win pile: J Q 3 4 2 6

Player 1 Hand: K 6 10 4 2 2 3 Q 2 A 9 10 5 7 A 3 9 4 7 9 3 5
Player 2 Hand: J 8 6 Q J K 8 4 Q 8 10 9 8 K 5 7 7 A J K A 6
Player 1:K
Player 2:J
** Player1 Wins **
Player 1 win pile: 10 5 K J
Player 2 win pile: J Q 3 4 2 6

Player 1 Hand: 6 10 4 2 2 3 Q 2 A 9 10 5 7 A 3 9 4 7 9 3 5
Player 2 Hand: 8 6 Q J K 8 4 Q 8 10 9 8 K 5 7 7 A J K A 6
Player 1:6
Player 2:8
** Player2 Wins **
Player 1 win pile: 10 5 K J
Player 2 win pile: J Q 3 4 2 6 6 8

Player 1 Hand: 10 4 2 2 3 Q 2 A 9 10 5 7 A 3 9 4 7 9 3 5
Player 2 Hand: 6 Q J K 8 4 Q 8 10 9 8 K 5 7 7 A J K A 6
Player 1:10
Player 2:6
** Player1 Wins **
Player 1 win pile: 10 5 K J 10 6
Player 2 win pile: J Q 3 4 2 6 6 8

Player 1 Hand: 4 2 2 3 Q 2 A 9 10 5 7 A 3 9 4 7 9 3 5
Player 2 Hand: Q J K 8 4 Q 8 10 9 8 K 5 7 7 A J K A 6
Player 1:4
Player 2:Q
** Player2 Wins **
Player 1 win pile: 10 5 K J 10 6
Player 2 win pile: J Q 3 4 2 6 6 8 4 Q

Player 1 Hand: 2 2 3 Q 2 A 9 10 5 7 A 3 9 4 7 9 3 5
Player 2 Hand: J K 8 4 Q 8 10 9 8 K 5 7 7 A J K A 6
Player 1:2
Player 2:J
** Player2 Wins **
Player 1 win pile: 10 5 K J 10 6
Player 2 win pile: J Q 3 4 2 6 6 8 4 Q 2 J

Player 1 Hand: 2 3 Q 2 A 9 10 5 7 A 3 9 4 7 9 3 5
Player 2 Hand: K 8 4 Q 8 10 9 8 K 5 7 7 A J K A 6
Player 1:2
Player 2:K
** Player2 Wins **
Player 1 win pile: 10 5 K J 10 6
Player 2 win pile: J Q 3 4 2 6 6 8 4 Q 2 J 2 K

Player 1 Hand: 3 Q 2 A 9 10 5 7 A 3 9 4 7 9 3 5
Player 2 Hand: 8 4 Q 8 10 9 8 K 5 7 7 A J K A 6
Player 1:3
Player 2:8
** Player2 Wins **
Player 1 win pile: 10 5 K J 10 6
Player 2 win pile: J Q 3 4 2 6 6 8 4 Q 2 J 2 K 3 8

Player 1 Hand: Q 2 A 9 10 5 7 A 3 9 4 7 9 3 5
Player 2 Hand: 4 Q 8 10 9 8 K 5 7 7 A J K A 6
Player 1:Q
Player 2:4
** Player1 Wins **
Player 1 win pile: 10 5 K J 10 6 Q 4
Player 2 win pile: J Q 3 4 2 6 6 8 4 Q 2 J 2 K 3 8

Player 1 Hand: 2 A 9 10 5 7 A 3 9 4 7 9 3 5
Player 2 Hand: Q 8 10 9 8 K 5 7 7 A J K A 6
Player 1:2
Player 2:Q
** Player2 Wins **
Player 1 win pile: 10 5 K J 10 6 Q 4
Player 2 win pile: J Q 3 4 2 6 6 8 4 Q 2 J 2 K 3 8 2 Q

Player 1 Hand: A 9 10 5 7 A 3 9 4 7 9 3 5
Player 2 Hand: 8 10 9 8 K 5 7 7 A J K A 6
Player 1:A
Player 2:8
** Player1 Wins **
Player 1 win pile: 10 5 K J 10 6 Q 4 A 8
Player 2 win pile: J Q 3 4 2 6 6 8 4 Q 2 J 2 K 3 8 2 Q

Player 1 Hand: 9 10 5 7 A 3 9 4 7 9 3 5
Player 2 Hand: 10 9 8 K 5 7 7 A J K A 6
Player 1:9
Player 2:10
** Player2 Wins **
Player 1 win pile: 10 5 K J 10 6 Q 4 A 8
Player 2 win pile: J Q 3 4 2 6 6 8 4 Q 2 J 2 K 3 8 2 Q 9 10

Player 1 Hand: 10 5 7 A 3 9 4 7 9 3 5
Player 2 Hand: 9 8 K 5 7 7 A J K A 6
Player 1:10
Player 2:9
** Player1 Wins **
Player 1 win pile: 10 5 K J 10 6 Q 4 A 8 10 9
Player 2 win pile: J Q 3 4 2 6 6 8 4 Q 2 J 2 K 3 8 2 Q 9 10

Player 1 Hand: 5 7 A 3 9 4 7 9 3 5
Player 2 Hand: 8 K 5 7 7 A J K A 6
Player 1:5
Player 2:8
** Player2 Wins **
Player 1 win pile: 10 5 K J 10 6 Q 4 A 8 10 9
Player 2 win pile: J Q 3 4 2 6 6 8 4 Q 2 J 2 K 3 8 2 Q 9 10 5 8

Player 1 Hand: 7 A 3 9 4 7 9 3 5
Player 2 Hand: K 5 7 7 A J K A 6
Player 1:7
Player 2:K
** Player2 Wins **
Player 1 win pile: 10 5 K J 10 6 Q 4 A 8 10 9
Player 2 win pile: J Q 3 4 2 6 6 8 4 Q 2 J 2 K 3 8 2 Q 9 10 5 8 7 K

Player 1 Hand: A 3 9 4 7 9 3 5
Player 2 Hand: 5 7 7 A J K A 6
Player 1:A
Player 2:5
** Player1 Wins **
Player 1 win pile: 10 5 K J 10 6 Q 4 A 8 10 9 A 5
Player 2 win pile: J Q 3 4 2 6 6 8 4 Q 2 J 2 K 3 8 2 Q 9 10 5 8 7 K

Player 1 Hand: 3 9 4 7 9 3 5
Player 2 Hand: 7 7 A J K A 6
Player 1:3
Player 2:7
** Player2 Wins **
Player 1 win pile: 10 5 K J 10 6 Q 4 A 8 10 9 A 5
Player 2 win pile: J Q 3 4 2 6 6 8 4 Q 2 J 2 K 3 8 2 Q 9 10 5 8 7 K 3 7

Player 1 Hand: 9 4 7 9 3 5
Player 2 Hand: 7 A J K A 6
Player 1:9
Player 2:7
** Player1 Wins **
Player 1 win pile: 10 5 K J 10 6 Q 4 A 8 10 9 A 5 9 7
Player 2 win pile: J Q 3 4 2 6 6 8 4 Q 2 J 2 K 3 8 2 Q 9 10 5 8 7 K 3 7

Player 1 Hand: 4 7 9 3 5
Player 2 Hand: A J K A 6
Player 1:4
Player 2:A
** Player2 Wins **
Player 1 win pile: 10 5 K J 10 6 Q 4 A 8 10 9 A 5 9 7
Player 2 win pile: J Q 3 4 2 6 6 8 4 Q 2 J 2 K 3 8 2 Q 9 10 5 8 7 K 3 7 4 A

Player 1 Hand: 7 9 3 5
Player 2 Hand: J K A 6
Player 1:7
Player 2:J
** Player2 Wins **
Player 1 win pile: 10 5 K J 10 6 Q 4 A 8 10 9 A 5 9 7
Player 2 win pile: J Q 3 4 2 6 6 8 4 Q 2 J 2 K 3 8 2 Q 9 10 5 8 7 K 3 7 4 A 7 J

Player 1 Hand: 9 3 5
Player 2 Hand: K A 6
Player 1:9
Player 2:K
** Player2 Wins **
Player 1 win pile: 10 5 K J 10 6 Q 4 A 8 10 9 A 5 9 7
Player 2 win pile: J Q 3 4 2 6 6 8 4 Q 2 J 2 K 3 8 2 Q 9 10 5 8 7 K 3 7 4 A 7 J 9 K

Player 1 Hand: 3 5
Player 2 Hand: A 6
Player 1:3
Player 2:A
** Player2 Wins **
Player 1 win pile: 10 5 K J 10 6 Q 4 A 8 10 9 A 5 9 7
Player 2 win pile: J Q 3 4 2 6 6 8 4 Q 2 J 2 K 3 8 2 Q 9 10 5 8 7 K 3 7 4 A 7 J 9 K 3 A

Player 1 Hand: 5
Player 2 Hand: 6
Player 1:5
Player 2:6
** Player2 Wins **
Player 1 win pile: 10 5 K J 10 6 Q 4 A 8 10 9 A 5 9 7
Player 2 win pile: J Q 3 4 2 6 6 8 4 Q 2 J 2 K 3 8 2 Q 9 10 5 8 7 K 3 7 4 A 7 J 9 K 3 A 5 6

+++++++++++++++++++ Result ++++++++++++++
B has won