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

War is a card game for two players. A standard deck of 52 cards is dealt so that

ID: 3745160 • Letter: W

Question

War is a card game for two players.

A standard deck of 52 cards is dealt so that both players have 26 cards.

During each round of play (or "battle"), both players play a card from the top of their hand face up.

The player who plays the card of the higher rank wins both cards and places them at the bottom of his stack of cards.

If both cards played are of the same rank, then both players play three additional cards face down and then one more card face up (this is called a "war").

The player who wins the war by playing the higher card wins all ten cards.

If the ranks are still the same, additional wars are played until one player wins the turn.

If either player runs out of cards to play, he loses the game.

You will use only ArrayLists to store the cards in this program. This means cards cannot be stored in arrays.

Sample output might look like this:

Think of a Card as an object with properties (rank, suit, etc.)

Think of a Deck of Cards as a Collection of Objects

Think of the remaining objects: Hand, Discard Pile, etc. as a collection of objects

Explanation / Answer

Below is your code...

Card.java

class Card {
private int rank; //initialize the rank (2,3,4...King, Ace)
private int suit; //initialize the suit (spades, hearts...)
  
//constructor
public Card(int suit, int rank){
this.rank = rank;
this.suit = suit;
}//end construcor
  
//getter method
public int getCard(){
return rank;
}//end getCard
  
//setter method
public void setCard(int rank){
this.rank = rank;
}//end setCard
  
@Override
public String toString(){
StringBuilder displayCard = new StringBuilder();
  
//personal choice to use switch
switch(rank){
case 11:
displayCard.append("Jack");
break;
case 12:
displayCard.append("Queen");
break;
case 13:
displayCard.append("King");
break;
case 14:
displayCard.append("Ace");
break;   
default:
displayCard.append(rank); //number from 2 to 10 does not need to modify
break;
}//end rank switch
  
displayCard.append(" of "); //setting the format of the output
  
switch(suit){
case 0:
displayCard.append("Spades");
break;
case 1:
displayCard.append("Hearts");
break;
case 2:
displayCard.append("Clubs");
break;
case 3:
displayCard.append("Diamonds");
break;
default: //anything else, do nothing
break;
}//end suit switch
  
//return the result of an entire cmombined string
return displayCard.toString();
}//end toString
  
}//end Card Class

WarCardGame.java


public class WarCardGame {
public static void main(String[] args) {
  
List<Card> cardDeck = new ArrayList<Card>(); //create an ArrayList "cardDeck"
  
for(int x=0; x<4; x++){ //0-3 for suit (4 suits)
for(int y=2; y<15; y++){ //2-14 for rank (13 ranks)
cardDeck.add(new Card(x,y)); //create new card and add into the deck
} //end rank for
}//end suit for
  
Collections.shuffle(cardDeck, new Random()); //shuffle the deck randomly
  
//creating 2 decks, each for player1/player2
LinkedList<Card> deck1 = new LinkedList<Card>();
LinkedList<Card> deck2 = new LinkedList<Card>();
  
deck1.addAll(cardDeck.subList(0, 25)); //26 cards for p1
deck2.addAll(cardDeck.subList(26, cardDeck.size()));//26 cards for p2
  
while(true){
Card p1Card = deck1.pop(); //each player place one card face up
Card p2Card = deck2.pop();
  
//display the face up card
System.out.println("Player 1 plays card is " + p1Card.toString());
System.out.println("Player 2 plays card is " + p2Card.toString());
  
//rank comparation between two cards
if(p1Card.getCard() > p2Card.getCard()){//if player 1 win
deck1.addLast(p1Card); //higher rank wins both cards and
deck1.addLast(p2Card); //places them at the bottom of his deck.
System.out.println("PLayer 1 wins the round");
}//end if

else if(p1Card.getCard() < p2Card.getCard()){//if player 2 win
deck2.addLast(p1Card);
deck2.addLast(p2Card);  
System.out.println("PLayer 2 wins the round");
}//end else if
  
else { //war happens when both cards' rank matched
System.out.println("War");
  
//creating war cards
List<Card> war1 = new ArrayList<Card>();
List<Card> war2 = new ArrayList<Card>();
  
//checking do players have enough (4)cards to stay in game
for(int x=0; x<3; x++){
//either one player runs out of card is game over
if(deck1.size() == 0 || deck2.size() == 0 ){   
break;
}//end if
  
System.out.println("War card for player1 is xx War card for player2 is xx");

war1.add(deck1.pop()); //place additional card for war
war2.add(deck2.pop());   
}//end for
  
//only compare result when both players have enough cards for war
if(war1.size() == 3 && war2.size() == 3 ){
//display the war cards from each player
System.out.println("War card for player1 is " + war1.get(0).toString());
System.out.println("War card for player2 is " + war2.get(0).toString());
  
//if player 1 wins the war round
if(war1.get(2).getCard() > war2.get(2).getCard()){
deck1.addAll(war1); //player1 get all 10 cards
deck1.addAll(war2);
System.out.println("Player 1 wins the war round");
}//end if
//otherwise player 2 wins the war round
else{
deck2.addAll(war1); //player2 get all 10 cards
deck2.addAll(war2);
System.out.println("Player 2 wins the war round");
}//end else   
}//end if
  
}//end war round else
  
//game over either one player runs out of card(deck size is 0)
if(deck1.size() == 0 ){
System.out.println("game over Player 1 wins the game");
break;
}
else if(deck2.size() == 0){
System.out.println("game over Player 2 wins the game");
break;
}
}//end while  

}//end main
}//end WarCardGame class

Output

Player 1 plays card is Jack of Diamonds
Player 2 plays card is Jack of Clubs
War
War card for player1 is xx
War card for player2 is xx
War card for player1 is xx
War card for player2 is xx
War card for player1 is xx
War card for player2 is xx
War card for player1 is 5 of Spades
War card for player2 is 9 of Spades
Player 2 wins the war round
Player 1 plays card is Queen of Diamonds
Player 2 plays card is Ace of Hearts
PLayer 2 wins the round
Player 1 plays card is 8 of Hearts
Player 2 plays card is Queen of Clubs
PLayer 2 wins the round
Player 1 plays card is Queen of Spades
Player 2 plays card is 5 of Diamonds
PLayer 1 wins the round
Player 1 plays card is 8 of Clubs
Player 2 plays card is 2 of Hearts
PLayer 1 wins the round
Player 1 plays card is King of Clubs
Player 2 plays card is 3 of Diamonds
PLayer 1 wins the round
Player 1 plays card is 9 of Clubs
Player 2 plays card is 9 of Hearts
War
War card for player1 is xx
War card for player2 is xx
War card for player1 is xx
War card for player2 is xx
War card for player1 is xx
War card for player2 is xx
War card for player1 is 2 of Diamonds
War card for player2 is 6 of Clubs
Player 1 wins the war round
Player 1 plays card is 7 of Spades
Player 2 plays card is King of Diamonds
PLayer 2 wins the round
Player 1 plays card is 2 of Spades
Player 2 plays card is 7 of Clubs
PLayer 2 wins the round
Player 1 plays card is Jack of Spades
Player 2 plays card is 3 of Hearts
PLayer 1 wins the round
Player 1 plays card is Ace of Spades
Player 2 plays card is 5 of Hearts
PLayer 1 wins the round
Player 1 plays card is Queen of Hearts
Player 2 plays card is 8 of Spades
PLayer 1 wins the round
Player 1 plays card is 10 of Spades
Player 2 plays card is King of Hearts
PLayer 2 wins the round
Player 1 plays card is 10 of Diamonds
Player 2 plays card is 10 of Clubs
War
War card for player1 is xx
War card for player2 is xx
War card for player1 is xx
War card for player2 is xx
War card for player1 is xx
War card for player2 is xx
War card for player1 is 2 of Clubs
War card for player2 is Ace of Clubs
Player 2 wins the war round
Player 1 plays card is 4 of Spades
Player 2 plays card is 9 of Diamonds
PLayer 2 wins the round
Player 1 plays card is 10 of Hearts
Player 2 plays card is Jack of Hearts
PLayer 2 wins the round
Player 1 plays card is Queen of Spades
Player 2 plays card is 7 of Diamonds
PLayer 1 wins the round
Player 1 plays card is 5 of Diamonds
Player 2 plays card is 5 of Spades
War
War card for player1 is xx
War card for player2 is xx
War card for player1 is xx
War card for player2 is xx
War card for player1 is xx
War card for player2 is xx
War card for player1 is 8 of Clubs
War card for player2 is King of Spades
Player 1 wins the war round
Player 1 plays card is 3 of Diamonds
Player 2 plays card is Ace of Diamonds
PLayer 2 wins the round
Player 1 plays card is 2 of Diamonds
Player 2 plays card is 7 of Hearts
PLayer 2 wins the round
Player 1 plays card is 6 of Spades
Player 2 plays card is Queen of Diamonds
PLayer 2 wins the round
Player 1 plays card is 8 of Diamonds
Player 2 plays card is Ace of Hearts
PLayer 2 wins the round
Player 1 plays card is 6 of Clubs
Player 2 plays card is 8 of Hearts
PLayer 2 wins the round
Player 1 plays card is 6 of Hearts
Player 2 plays card is Queen of Clubs
PLayer 2 wins the round
Player 1 plays card is 3 of Clubs
Player 2 plays card is 7 of Spades
PLayer 2 wins the round
Player 1 plays card is Jack of Spades
Player 2 plays card is King of Diamonds
PLayer 2 wins the round
Player 1 plays card is 3 of Hearts
Player 2 plays card is 2 of Spades
PLayer 1 wins the round
Player 1 plays card is Ace of Spades
Player 2 plays card is 7 of Clubs
PLayer 1 wins the round
Player 1 plays card is 5 of Hearts
Player 2 plays card is 10 of Spades
PLayer 2 wins the round
Player 1 plays card is Queen of Hearts
Player 2 plays card is King of Hearts
PLayer 2 wins the round
Player 1 plays card is 8 of Spades
Player 2 plays card is 2 of Clubs
PLayer 1 wins the round
Player 1 plays card is Queen of Spades
Player 2 plays card is 3 of Spades
PLayer 1 wins the round
Player 1 plays card is 7 of Diamonds
Player 2 plays card is 5 of Clubs
PLayer 1 wins the round
Player 1 plays card is 8 of Clubs
Player 2 plays card is Ace of Clubs
PLayer 2 wins the round
Player 1 plays card is 2 of Hearts
Player 2 plays card is 4 of Diamonds
PLayer 2 wins the round
Player 1 plays card is King of Clubs
Player 2 plays card is 6 of Diamonds
PLayer 1 wins the round
Player 1 plays card is King of Spades
Player 2 plays card is 4 of Spades
PLayer 1 wins the round
Player 1 plays card is 4 of Hearts
Player 2 plays card is 9 of Diamonds
PLayer 2 wins the round
Player 1 plays card is 9 of Spades
Player 2 plays card is 10 of Hearts
PLayer 2 wins the round
Player 1 plays card is 3 of Hearts
Player 2 plays card is Jack of Hearts
PLayer 2 wins the round
Player 1 plays card is 2 of Spades
Player 2 plays card is 3 of Diamonds
PLayer 2 wins the round
Player 1 plays card is Ace of Spades
Player 2 plays card is Ace of Diamonds
War
War card for player1 is xx
War card for player2 is xx
War card for player1 is xx
War card for player2 is xx
War card for player1 is xx
War card for player2 is xx
War card for player1 is 7 of Clubs
War card for player2 is 2 of Diamonds
Player 2 wins the war round
Player 1 plays card is Queen of Spades
Player 2 plays card is Queen of Diamonds
War
War card for player1 is xx
War card for player2 is xx
War card for player1 is xx
War card for player2 is xx
War card for player1 is xx
War card for player2 is xx
War card for player1 is 3 of Spades
War card for player2 is 8 of Diamonds
Player 2 wins the war round
Player 1 plays card is King of Clubs
Player 2 plays card is 8 of Hearts
PLayer 1 wins the round
Player 1 plays card is 6 of Diamonds
Player 2 plays card is 6 of Hearts
War
War card for player1 is xx
War card for player2 is xx
War card for player1 is xx
War card for player2 is xx
War card for player1 is xx
War card for player2 is xx
War card for player1 is King of Spades
War card for player2 is Queen of Clubs
Player 1 wins the war round
Player 1 plays card is 8 of Hearts
Player 2 plays card is Jack of Spades
PLayer 2 wins the round
Player 1 plays card is King of Spades
Player 2 plays card is King of Diamonds
War
War card for player1 is xx
War card for player2 is xx
War card for player1 is xx
War card for player2 is xx
War card for player1 is xx
War card for player2 is xx
War card for player1 is 4 of Spades
War card for player2 is 5 of Hearts
Player 2 wins the war round
Player 1 plays card is 3 of Clubs
Player 2 plays card is King of Hearts
PLayer 2 wins the round
Player 1 plays card is 7 of Spades
Player 2 plays card is 8 of Clubs
PLayer 2 wins the round
game over
Player 1 wins the game