Question

In: Computer Science

Create a java program. War is a card game for two players. A standard deck of...

Create a java program.

  • 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 we have a WAR! Both players play three additional cards face down and then one more card face up.
  • 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.

Sample output might look like this:

player 1 plays Card is 9 of Clubs

player 2 plays Card is Ace of Diamonds

player 2 wins the round

player 1 plays Card is 10 of Diamonds

player 2 plays Card is Ace of Hearts

player 2 wins the round

player 1 plays Card is 7 of Clubs

player 2 plays Card is 6 of Hearts

player 1 wins the round

player 1 plays Card is 9 of Hearts

player 2 plays Card is 9 of Clubs

WAR TIME!

war card for player1 Card is xx

war card for player2 Card is xx

war card for player1 Card is xx

war card for player2 Card is xx

war card for player1 Card is xx

war card for player2 Card is xx

war card for player1 Card is 7 of Clubs

war card for player2 Card is 10 of Diamonds

player 2 wins the war round

player 1 plays Card is 6 of Hearts

player 2 plays Card is Ace of Hearts

player 2 wins the round

game over

player 2 wins the game


You already have a Deck Class and a Card Class. You can reuse them. However, you must make some changes and additions:

  • You must use enum for your ranks and suits
  • You must create a new class for a hand – each player has a hand that has a variable number of cards. Is a hand like a deck? Is there a possibility for inheritance? Or is it best to create a new class?

Your menu should look like this:

  1. Create new deck
  2. Shuffle deck
  3. Show deck
  4. Play War
  5. Exit

When option 4 is selected, you will start the War game. The program will ask the user for the maximum battle number. This will allow the game to stop in case it goes on too long. This will be in integer. Then next thing you will do is deal 26 cards to player 1 and 26 cards to player 2. Then you will show all the cards for each player. Then you will pause and ask the user to press any key to continue.

The game of war will play out as described above. Each time the players compare a card, it is called a battle. If the Max Battle Number is reached – the game stops and both players show all of their cards. If one player collects all the cards before the Max Battle Number is reached, then that player is declared the winner and the program asks the user to press any key to continue. Then the program returns to the main menu.

Solutions

Expert Solution

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

public class WarCardGame {
public static void main(String[] args) {
  
List<Card> cardDeck = new ArrayList<Card>();
  
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\nWar 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\nPlayer 1 wins the game");
break;
}
else if(deck2.size() == 0){
System.out.println("game over\nPlayer 2 wins the game");
break;
}
}//end while

}//end main   
}//end WarCardGame class


class Card {
private int rank;
private int suit;
  
public Card(int suit, int rank){
this.rank = rank;
this.suit = suit;
}
public int getCard(){
return rank;
}
public void setCard(int rank){
this.rank = rank;
}
  
@Override
public String toString(){
  
StringBuilder displayCard = new StringBuilder();
  
  
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);
break;
}
  
displayCard.append(" of ");
  
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:
break;
}
return displayCard.toString();
}
}//end Card Class

hits the thumpsup


Related Solutions

Question 1: Part a) A game consists of drawing 1 card from a standard deck of...
Question 1: Part a) A game consists of drawing 1 card from a standard deck of 52 cards. If you draw an Ace you win $50. If you draw a Jack, Queen, or King you win $20. If you draw a 10, 9 or 8 you win $15. If you draw anything lower than an 8, you don’t win anything. If it costs $10 to play, what is your expected value? Part b) It costs $180/yr to buy insurance for...
We are creating a new card game with a new deck. Unlike the normal deck that...
We are creating a new card game with a new deck. Unlike the normal deck that has 13 ranks (Ace through King) and 4 Suits (hearts, diamonds, spades, and clubs), our deck will be made up of the following. Each card will have: i) One rank from 1 to 15. ii) One of 5 different suits. Hence, there are 75 cards in the deck with 15 ranks for each of the 5 different suits, and none of the cards will...
We are creating a new card game with a new deck. Unlike the normal deck that...
We are creating a new card game with a new deck. Unlike the normal deck that has 13 ranks (Ace through King) and 4 Suits (hearts, diamonds, spades, and clubs), our deck will be made up of the following. Each card will have: i) One rank from 1 to 15. ii) One of 5 different suits. Hence, there are 75 cards in the deck with 15 ranks for each of the 5 different suits, and none of the cards will...
We are creating a new card game with a new deck. Unlike the normal deck that...
We are creating a new card game with a new deck. Unlike the normal deck that has 13 ranks (Ace through King) and 4 Suits (hearts, diamonds, spades, and clubs), our deck will be made up of the following. Each card will have: i) One rank from 1 to 16. ii) One of 5 different suits. Hence, there are 80 cards in the deck with 16 ranks for each of the 5 different suits, and none of the cards will...
If you draw a card with a value of two or less from a standard deck...
If you draw a card with a value of two or less from a standard deck of cards, I will pay you $137.  If not, you pay me $9.  (Aces are considered the highest card in the deck.) Step 2 of 2:  If you played this game 828 times how much would you expect to win or lose? Round your answer to two decimal places. Losses must be entered as negative.
from a standard 52N deck card, how many five – card hands consist of two –...
from a standard 52N deck card, how many five – card hands consist of two – pair? (Two-pair: two cards of one rank, two cards of another rank, and one other card.)
1) We are creating a new card game with a new deck. Unlike the normal deck...
1) We are creating a new card game with a new deck. Unlike the normal deck that has 13 ranks (Ace through King) and 4 Suits (hearts, diamonds, spades, and clubs), our deck will be made up of the following. Each card will have: i) One rank from 1 to 10. ii) One of 9 different suits. Hence, there are 90 cards in the deck with 10 ranks for each of the 9 different suits, and none of the cards...
1) We are creating a new card game with a new deck. Unlike the normal deck...
1) We are creating a new card game with a new deck. Unlike the normal deck that has 13 ranks (Ace through King) and 4 Suits (hearts, diamonds, spades, and clubs), our deck will be made up of the following. Each card will have: i) One rank from 1 to 16. ii) One of 5 different suits. Hence, there are 80 cards in the deck with 16 ranks for each of the 5 different suits, and none of the cards...
Create a simple dice game in Java. Add screenshots and the program here.
Create a simple dice game in Java. Add screenshots and the program here.
A game involves selecting a card from a regular 52-card deck and tossing a coin. The...
A game involves selecting a card from a regular 52-card deck and tossing a coin. The coin is a fair coin and is equally likely to land on heads or tails. •     If the card is a face card, and the coin lands on Heads, you win $9 •     If the card is a face card, and the coin lands on Tails, you win $3 •     If the card is not a face card, you lose $3, no matter what...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT