Objective: Write a program in which simulates a card game of WAR! The game is si
ID: 639592 • Letter: O
Question
Objective:
Write a program in which simulates a card game of WAR! The game is simple. There are two players who each are dealt out half of an entire shuffled deck of cards. Each player lays down a card one at a time. The player with highest value on the card wins both cards back. The player who runs out of cards loses. I
n this version of the game we will not be using standard playing cards, but instead cards that have unique values ranging from 0-9 (IE 10 cards). This will make the simulation simpler and quicker.
Also remember this program is a simulation of two players playing the game so there is no user input besides whether or not to run the simulation again.
Suggested Methodology You can solve this in any number of ways, and here
Explanation / Answer
Card Class
//has all of the values for the card class
public class Card
{
private String suit;
private int value;
public Card(int initValue)
{
value = findCard(initValue);
suit = findSuit(initValue);
}
public Card(String initSuit, int initValue)
{
suit = initSuit;
value = initValue;
}
//pre: takes a number from the array
//post: returns what suit it is
public String getSuit()
{
return suit;
}
//pre: takes number from array
//post: returns a numerical value for it
public int getValue()
{
return this.value;
}
//pre: enter an integer
//post: finds the number of the card
public int findCard(int cardNum)
{
int card = (cardNum % 13);
return card;
}
//pre: enter an integer
//post: finds the suit of the card
public String findSuit(int cardNum)
{
String suit = "";
if ((cardNum / 13) == 0) //If the card is a Diamond
suit = "DIAMOND";
else if((cardNum / 13) == 1) //If card is a Heart
suit = "HEART";
else if((cardNum / 13) == 2) //If card is a Spade
suit = "SPADE";
else if((cardNum / 13) == 3) //If card is a Club
suit = "CLUB";
else
System.out.println("INVALID: " + cardNum);
return suit;
}
//pre: takes two cards
//post: returns which 1 if THIS card is bigger. -1 if second is bigger. 0 if equal
public int compare(Card a)
{
int compared;
if (this.value != 0 && a.value != 0) //if neither cards are aces
{
if (this.value > a.value)
compared = 1;
else if (this.value < a.value)
compared = -1;
else
compared = 0;
}
else if(this.value == 0 && a.value != 0) //if the first card is an ace
compared = 1;
else if(this.value != 0 && a.value == 0) //if the second card is an ace
compared = -1;
else //if both cards are aces
compared = 0;
return compared;
}
//pre: takes its value
//post: converts it to a name. ie jack, king, queen, ace
public String toString()
{
String name = "";
if (value >= 10 || value == 0)
{
if (value == 12)
name = "king";
else if (value == 11)
name = "queen";
else if (value == 10)
name = "jack";
else if (value == 0)
name = "ace";
return name + suit;
}
else
return "" + (value + 1) + suit;
}
}
[/highlight]
Hand Class
[highlight=java]
//Jordan Kestler & Calvin Hawkes
//Nov 8
//makes two decks for each player
import java.util.*;
public class Hand
{
ArrayList <Card> cardList = new ArrayList <Card> (53);
//pre: takes top card off deck
//Post: returns top card
public Card playCard()
{
return cardList.remove(0);
}
//pre: takes cards that are won
//Post: puts cards won on bottom
public void addCard(Card a)
{
cardList.add(a);
}
//pre: takes the array of the hand
//post: returns how many cards are in it and checks if its empty
public int numCards()
{
int numCards = cardList.size();
return numCards;
}
//pre: none
//post: prints out hand
public void printHand()
{
for(int i=0; i < cardList.size(); i++)
System.out.println(i + ". " + cardList.get(i));
}
}
[/highlight]
Deck Class
[highlight=java]
/*Calvin Hawkes & Jordon Kestler
*Nov. 8 2007
*Deck Class That should work with any card game
*/
import java.util.*;
public class Deck
{
private final int CARDS = 52; //amount of cards in a deck
private Card [] deck; //creating an array of cards
private int top = 0; //the top of the deck
public Deck()
{
deck = new Card[CARDS];
//Creates all 52 cards
for(int i=0; i < CARDS; i++)
deck[i] = new Card(i);
}
//pre: must be passed a deck of cards
//post: returns the deck with cards in random order
public void shuffle()
{
Random rand = new Random();
for (int i=0; i < CARDS; i++)
{
int ranCard = rand.nextInt(52); //random number 0-51
Card temp;
temp = deck[i];
deck[i] = deck[ranCard];
deck[ranCard] = temp;
}
}
//pre: must be passed a deck of cards
//post: returns true if the deck is empty else false
public boolean isEmpty()
{
boolean empty = false;
if (top == 52)
empty = true;
return empty;
}
//pre: must be passed a deck of cards
//post: returns the top card on the deck and removes it from the deck
public Card deal()
{
int temp = top;
top ++;
return deck[temp];
}
//pre: must be passed a deck of cards
//post: prints out every card in the deck in order. Used for testing
public void printDeck()
{
for(int i=0; i < CARDS; i++)
System.out.println(i + ". " + deck[i]);
}
}[/highlight]
Game Class
[highlight=java]
/*Calvin Hawkes & Jordon Kestler
*Nov. 9 2007
*The Game Class for a Card Game Called War
*/
public class Game
{
public static void main(String[] args)
{
Deck warDeck = new Deck(); //Creating the Deck of Cards
Hand player1 = new Hand(); //Creating player 1's hand
Hand player2 = new Hand(); //Creating player 2's hand
warDeck.shuffle(); //Shuffling the Deck
boolean done = false;
while(!done) //Dealing the Cards
{
if(!(warDeck.isEmpty()))
player1.addCard(warDeck.deal());
else
done = true;
if(!(warDeck.isEmpty()))
player2.addCard(warDeck.deal());
else
done = true;
}
while(player1.numCards() != 0 && player2.numCards() != 0)
{
//Playing the Game
Card p1Card = player1.playCard();
Card p2Card = player2.playCard();
if (p1Card.compare(p2Card) == 1) //if player1 wins
{
System.out.println("Playerns!");
player1.addCard(p1Card);
player1.addCard(p2Card);
}
else if (p1Card.compare(p2Card) == -1) //if player2 wins
{
System.out.println("Playerns!");
player2.addCard(p1Card);
player2.addCard(p2Card);
}
else //if a war is declared
{
if (player1.numCards() >= 4 && player2.numCards() >= 4)
war(player1 , player2);
else if (player1.numCards() == 3 || player2.numCards() == 3)
war2(player1 , player2);
else if (player1.numCards() == 2 || player2.numCards() == 2)
war3(player1 , player2);
else
war4(player1 , player2);
}
}
//Declaring the Winner
if(player2.numCards() == 0)
System.out.println("PLAYERNS THE GAME!!!");
else
System.out.println("PLAYERNS THE GAME!!!");
}
//pre: if two hands are created and both users have 4 or more cards
//post: plays war when two cards are equal
public static void war(Hand player1, Hand player2)
{
System.out.println("WAR;
//Playing 3 Cards Facedown
Card p1Card1 = player1.playCard();
Card p1Card2 = player1.playCard();
Card p1Card3 = player1.playCard();
Card p1Card4 = player1.playCard();
Card p2Card1 = player2.playCard();
Card p2Card2 = player2.playCard();
Card p2Card3 = player2.playCard();
Card p2Card4 = player2.playCard();
if (p1Card4.compare(p2Card4) == 1) //if player1 wins
{
System.out.println("Playerns!");
player1.addCard(p1Card1);
player1.addCard(p1Card2);
player1.addCard(p1Card3);
player1.addCard(p1Card4);
player1.addCard(p2Card1);
player1.addCard(p2Card2);
player1.addCard(p2Card3);
player1.addCard(p2Card4);
}
else if (p1Card4.compare(p2Card4) == -1) //if player2 wins
{
System.out.println("Playerns!");
player2.addCard(p1Card1);
player2.addCard(p1Card2);
player2.addCard(p1Card3);
player2.addCard(p1Card4);
player2.addCard(p2Card1);
player2.addCard(p2Card2);
player2.addCard(p2Card3);
player2.addCard(p2Card4);
}
else //if a war is declared
{
if (player1.numCards() >= 4 && player2.numCards() >= 4)
war(player1 , player2);
else if (player1.numCards() == 3 || player2.numCards() == 3)
war2(player1 , player2);
else if (player1.numCards() == 2 || player2.numCards() == 2)
war3(player1 , player2);
else
war4(player1 , player2);
}
}
//pre: if two hands are one user has 3 cards
//post: plays war when two cards are equal
public static void war2(Hand player1, Hand player2)
{
System.out.println("WAR;
//Playing 2 Cards Facedown
Card p1Card1 = player1.playCard();
Card p1Card2 = player1.playCard();
Card p1Card4 = player1.playCard();
Card p2Card1 = player2.playCard();
Card p2Card2 = player2.playCard();
Card p2Card4 = player2.playCard();
if (p1Card4.compare(p2Card4) == 1) //if player1 wins
{
System.out.println("Playerns!");
player1.addCard(p1Card1);
player1.addCard(p1Card2);
player1.addCard(p1Card4);
player1.addCard(p2Card1);
player1.addCard(p2Card2);
player1.addCard(p2Card4);
}
else if (p1Card4.compare(p2Card4) == -1) //if player2 wins
{
System.out.println("Playerns!");
player2.addCard(p1Card1);
player2.addCard(p1Card2);
player2.addCard(p1Card4);
player2.addCard(p2Card1);
player2.addCard(p2Card2);
player2.addCard(p2Card4);
}
else //if a war is declared
{
if (player1.numCards() >= 4 && player2.numCards() >= 4)
war(player1 , player2);
else if (player1.numCards() == 3 || player2.numCards() == 3)
war2(player1 , player2);
else if (player1.numCards() == 2 || player2.numCards() == 2)
war3(player1 , player2);
else
war4(player1 , player2);
}
}
//pre: if two hands are one user has 2 cards
//post: plays war when two cards are equal
public static void war3(Hand player1, Hand player2)
{
System.out.println("WAR;
//Playing 1 Cards Facedown
Card p1Card1 = player1.playCard();
Card p1Card4 = player1.playCard();
Card p2Card1 = player2.playCard();
Card p2Card4 = player2.playCard();
if (p1Card4.compare(p2Card4) == 1) //if player1 wins
{
System.out.println("Playerns!");
player1.addCard(p1Card1);
player1.addCard(p1Card4);
player1.addCard(p2Card1);
player1.addCard(p2Card4);
}
else if (p1Card4.compare(p2Card4) == -1) //if player2 wins
{
System.out.println("Playerns!");
player2.addCard(p1Card1);
player2.addCard(p1Card4);
player2.addCard(p2Card1);
player2.addCard(p2Card4);
}
else //if a war is declared
{
if (player1.numCards() >= 4 && player2.numCards() >= 4)
war(player1 , player2);
else if (player1.numCards() == 3 || player2.numCards() == 3)
war2(player1 , player2);
else if (player1.numCards() == 2 || player2.numCards() == 2)
war3(player1 , player2);
else
war4(player1 , player2);
}
}
//pre: if two hands are one user has 1 card
//post: plays war when two cards are equal
public static void war4(Hand player1, Hand player2)
{
System.out.println("WAR;
//Playing 1 Card Faceup
Card p1Card4 = player1.playCard();
Card p2Card4 = player2.playCard();
if (p1Card4.compare(p2Card4) == 1) //if player1 wins
{
System.out.println("Playerns!");
player1.addCard(p1Card4);
player1.addCard(p2Card4);
}
else if (p1Card4.compare(p2Card4) == -1) //if player2 wins
{
System.out.println("Playerns!");
player2.addCard(p1Card4);
player2.addCard(p2Card4);
}
else //if a war is declared
{
System.out.println("Gameied. We all win!");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.