In: Computer Science
Create a java program.
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:
Your menu should look like this:
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.
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