Question
Task 1: Card.java Use the following for SUITS and RANKS: public final static String [] SUITS = { Diamonds , Clubs , Hearts , Spades
Task 1: Card.java
Use the following for SUITS and RANKS:
public final static String[] SUITS = { "Diamonds", "Clubs", "Hearts", "Spades" }; public final static String[] RANKS = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" };
Constructor method
1) Sets the rank and suit of the card : setRank()
Ensures that the rank is within the valid range. Prints an error otherwise.
2) setSuit()
Ensures that the suit is within the valid range.
Prints an error otherwise.
3) getRankIndex()
Returns the rank.
4) getShortName()Should return a string with the following format:
D for diamond, C for clubs, H for hearts, or S for spades followed by,
2, 3, 4, ..., T, J, Q, K, or an A.
For example, the two of diamonds should be D2
5) toString()Should return the long name of the card.
"2 of Diamonds", "Ace of Spades", etc.
- Task 2: Deck.java
Set the SHUFFLES constant to 100.
1) Constructor method
Initialize the cards variable
Add all 52 possible cards to the deck (in order)
2 of Diamonds, 3 of Diamonds, etc...
Order should be all of the Diamonds, then Clubs, then Hearts, and then Spades
2) shuffle()
Use a random seed of 1234567
Loop from 0 to SHUFFLES
get two random numbers between 0 and 51
swap the cards
3) deal()
If the deck is empty, return null
Otherwise, return the card at the top of the deck (the highest index available)
4) toString()Return a string in the following format:
This deck has 3 cards. D2 SA HT
-Task 3: Hand.java
Represents a players hand, ie. what cards they are holding.
1) Constructor
intializes the array
2) addCard()
Add the card to cards.
3) playCard()
Return the card from selected index
4) totalInHand()
Return the size of cards.
5) toString()
Return a string in the following format: Currently in hand: S7, S2, D9, S9, CQ, ...
-Task 4: War.java
Use this to test Card, Deck, and Hand classes with this War.java file.
$ javac Card.java Deck.java Hand.java War.java $ java War
import java.util.ArrayList; public class War { private Deck deck; private Hand playerOne; private Hand playerTwo; private ArrayList<Card> stack = new ArrayList<>(); public War() { System.out.println("Generating a new deck..."); deck = new Deck(); System.out.println(deck); System.out.println("Shuffling..."); deck.shuffle(); System.out.println(deck); System.out.println("Dealing to two players..."); playerOne = new Hand(); playerTwo = new Hand(); Card card = deck.deal(); int counter = 0; while (card != null) { if (counter % 2 == 0) { playerOne.addCard(card); } else { playerTwo.addCard(card); } ++counter; card = deck.deal(); } System.out.print("Player One: "); System.out.println(playerOne); System.out.print("Player Two: "); System.out.println(playerTwo); } private void play() { int plays = 0; while (playerOne.totalInHand() != 0 && playerTwo.totalInHand() != 0) { Card p1 = playerOne.playCard(playerOne.totalInHand() - 1); Card p2 = playerTwo.playCard(playerTwo.totalInHand() - 1); int rank1 = p1.getRankIndex(); int rank2 = p2.getRankIndex(); System.out.println("============================="); System.out.println("Player 1 played: " + p1); System.out.println("Player 2 played: " + p2); if (rank1 == rank2) { System.out.println("Tie!"); System.out.println("Adding " + p1 + " and " + p2 + " to the stack!"); stack.add(p1); stack.add(p2); } else if (rank1 > rank2) { System.out.println("Player 1 wins the round!"); playerOne.addCard(p1); playerOne.addCard(p2); while (stack.size() > 0) { playerOne.addCard(stack.remove(stack.size() - 1)); } } else { System.out.println("Player 2 wins the round!"); playerTwo.addCard(p1); playerTwo.addCard(p2); while (stack.size() > 0) { playerTwo.addCard(stack.remove(stack.size() - 1)); } } System.out.println("Player 1's hand size is: " + playerOne.totalInHand()); System.out.println("Player 1: " + playerOne); System.out.println("Player 2's hand size is: " + playerTwo.totalInHand()); System.out.println("Player 2: " + playerTwo); plays++; } System.out.println("============================="); System.out.println("This game had " + plays + " rounds."); if (playerOne.totalInHand() == 0) { System.out.println("Player 2 won!"); } else { System.out.println("Player 1 won!"); } } public static void main(String[] args) { War war = new War(); war.play(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started