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

b. In Chapter 5, you created a War Card game that randomly selects two cards (on

ID: 3726548 • Letter: B

Question

b. In Chapter 5, you created a War Card game that randomly selects two cards (one for the player and one for the computer) and declares a winner (or a tie). Modify the game to set each Card's suit as the appropriate string, then execute the game using the newly modified Card class. Figure 7-18 shows four typical executions. Recall that in this version of War, you assume that the Ace is the lowest-valued card. Save the game as War2.java. Command Prompt - OX C:Java>java War2 My card is the 10 of Diamonds Your card is the Queen of Hearts You win C:Java>java War2 My card is the Ace of Diamonds Your card is the 7 of clubs You win C:Java>java War2 My card is the 10 of Hearts Your card is the 10 of Spades It's a tie C:Java>java War2 My card is the 9 of Diamonds Your card is the 3 of Diamonds I win C:Java> Figure 7-18 Four typical executions of the War2 game

Explanation / Answer

//Card.java


class Card {
private int value;
private int set;
  
public Card(int set, int value){
this.value = value;
this.set = set;
}
  
public int getCard(){
return value;
}
  
public void setCard(int value){
this.value = value;
}
  
@Override
public String toString(){
//construct the appropriate string for each card.
//value of Jack is 11,value of Queen is 12 and so on & Ace being the lowest value =1.
StringBuilder str = new StringBuilder();
switch(value){
case 1:
str.append("Ace");
break;
case 11:
str.append("Jack");
break;
case 12:
str.append("Queen");
break;
case 13:
str.append("King");
break;
// card from 2-10
default:
str.append(value);
break;
}
str.append(" of ");
switch(set){
case 0:
str.append("Spades");
break;
case 1:
str.append("Hearts");
break;
case 2:
str.append("Clubs");
break;
case 3:
str.append("Diamonds");
break;
default:
break;
}
return str.toString();
}
}

//War2.java


import java.util.ArrayList;
import java.util.Random;   
import java.util.List;   
import java.util.Collections;
import java.util.LinkedList;   

public class War2 {
public static void main(String[] args) {
  
List<Card> deck = new ArrayList<Card>();
for(int i=0; i<4; i++){ // 4 sets
for(int j=1; j<14; j++){ //each set of 13 card
deck.add(new Card(i,j)); //create new cards
}
}   
//create deck for me and you
LinkedList<Card> deck1 = new LinkedList<Card>();
LinkedList<Card> deck2 = new LinkedList<Card>();
//shuffle the deck randomly
Collections.shuffle(deck, new Random());
//divide the cards to two
deck1.addAll(deck.subList(0, 25));   
deck2.addAll(deck.subList(26, deck.size()));
   //take one card each
Card myCard = deck1.pop();
Card yourCard = deck2.pop();
System.out.println("My card is the " + myCard.toString());
System.out.println("Your card is the " + yourCard.toString());
  
//Compare rank of two cards and higher rank player wins.
//Give both cards to the winner and placed at the last.
//I win
if(myCard.getCard() > yourCard.getCard()){
deck1.addLast(myCard);
deck1.addLast(yourCard);
System.out.println("I win");
}
//You win
else if(myCard.getCard() < yourCard.getCard()){//if player 2 win
deck2.addLast(myCard);   
deck2.addLast(yourCard);
System.out.println("You win");
}
//It's a tie
else {
System.out.println("It's a tie");   
}

}
}

Output

My card is the 9 of Diamonds
Your card is the 10 of Clubs
You win