Question: 1. Download the following source files: - Deck.java - DeckInterface.java - Card.java - WarCardGame.java (Note: The only class you need to implement.) 2. In Eclipse,
1. Download the following source files:
- Deck.java
- DeckInterface.java
- Card.java
- WarCardGame.java (Note: The only class you need to implement.)
2. In Eclipse, create a new Java project called Lab 9, and import the 4 files.
3. Compile and run the program. A prompt should appear asking the user to select a game mode. When prompted to select a game mode, choose Playable. Pressing Q will quit the playable version. If you do not select playable, the program will be stuck in an infinite loop and will need to be terminated manually.
4. Read through the downloaded files to understand the methods that you will need to utilize in the WarCardGame.java file.
- Deck.java
- public void shuffle()
- public void push(Card card)
- public Card pop()
- public boolean isEmpty()
- public Card peek()
5. Following the commented steps inside of WarCardGame.java, implement the functionality of the card game. Pay attention to the order of the steps as it may require you to complete methods before calling them
6. Compile and run the program again. Make sure it works correctly.]
Classes
public class Card {
// Class Level Attributes
// value: int value of the card (Aces high)
// name : String representation of the name of the card
private int value = 0;
private String name = "";
// Constructors
// Dummy constructor to prevent uninitialized values
private Card(){}
// Public constructor
// Inputs: aSuit - String value of the suit
// : aValue - String value of the value (converted to int)
public Card(String aSuit, String aValue){
name = aValue + " of " + aSuit;
switch(aValue){
case "Ace":
value = 14;
break;
case "Jack":
value = 11;
break;
case "Queen":
value = 12;
break;
case "King":
value = 13;
break;
default:
try{
value = Integer.parseInt(aValue);
} catch (Exception e){
System.out.println("Critical Error: Unable to parse the card value. Exiting game...");
System.exit(-1);
}
}
}
// Get Methods
// Public method to get the card name
public String getName(){
return name;
}
// Public method to get the card value
public int getValue(){
return value;
}
}
import java.util.ArrayList;
import java.util.Collections;
public class Deck implements DeckInterface{
// Class Level Attributes
// deckOfCards: ArrayList that contains Card objects
private ArrayList
// Deck Methods
// Method to create a new deck of 52 unique Card objects
public void newDeck(){
for (int value = 1; value <= 13; value++){
for (int suit = 1; suit <= 4; suit++){
String newSuit;
String newValue;
switch(value){
case 11:
newValue = "Jack";
break;
case 12:
newValue = "Queen";
break;
case 13:
newValue = "King";
break;
case 1:
newValue = "Ace";
break;
default:
newValue = String.valueOf(value);
break;
}
switch(suit){
case 1:
newSuit = "Clubs";
break;
case 2:
newSuit = "Diamonds";
break;
case 3:
newSuit = "Hearts";
break;
default:
newSuit = "Spades";
break;
}
deckOfCards.add(new Card(newSuit, newValue));
}
}
// Shuffle the newly formed deck
shuffle();
}
// Method to shuffle the current deck of cards
public void shuffle(){
Collections.shuffle(deckOfCards);
}
// Method to print out the current deck of cards
public void displayDeck(){
for (Card card : deckOfCards){
System.out.println(card.getName());
}
}
// Stack Methods
// Method to add a new card to the top of the deck (push to stack)
public void push(Card card){
deckOfCards.add(card);
}
// Method to draw a card from the top of the deck (pop from stack)
public Card pop(){
Card card = deckOfCards.get(deckOfCards.size()-1);
deckOfCards.remove(deckOfCards.size()-1);
return card;
}
// Method to determine if the deck is empty
public boolean isEmpty(){
return deckOfCards.isEmpty();
}
// Method to view the card at the top of the deck (peek stack)
public Card peek(){
Card card = deckOfCards.get(deckOfCards.size()-1);
return card;
}
}
public interface DeckInterface {
// Default Constructor
// Precondition: None
// Postcondition: New Deck instance has been initialized
public void newDeck();
// Method to shuffle the current deck of cards
// Precondition: None
// Postcondition: Deck is shuffled.
public void shuffle();
// Method to print out the current deck of cards
// Precondition: None
// Postcondition: All elements in Deck is printed to console
public void displayDeck();
// Stack Methods
// Method to add a new card to the top of the deck (push to stack)
// Precondition: Card must be initialized
// Postcondition: New element is added to top of Deck
public void push(Card card);
// Method to draw a card from the top of the deck (pop from stack)
// Precondition: Deck must not be empty
// Postcondition: Card on top of deck is removed and returned
public Card pop();
// Method to determine if the deck is empty
// Precondition: None
// Postcondition: Returned boolean value: true if Deck is empty, false is Deck is not empty
public boolean isEmpty();
// Method to view the card at the top of the deck (peek stack)
// Precondition: Deck must not be empty
// Postcondition: Card on top of deck is returned but not removed
public Card peek();
}
import java.util.Scanner;
public class WarCardGame {
// Class Level Attributes
// input: Scanner for user input
static Scanner input = new Scanner(System.in);
// gameMode: boolean variable to determine the Game Mode (false:simulated || true:playable)
static boolean gameMode = false;
/* TODO: Your code here.
* 1) Initialize 6 instances of Deck
* x2 Player Decks
* x2 Player War Decks
* x2 Player Win Decks
*/
/*******************************************************************************/
private static Deck Player1Deck = new Deck();
/*******************************************************************************/
// Main Method
public static void main(String[] args) {
// Start the game with a single deck of cards (pre-shuffled)
Player1Deck.newDeck();
//Player1Deck.displayDeck();
/* TODO: Your code here.
* 2) Split the deck by removing 26 cards from Player1Deck and adding them to Player2's
*/
/*******************************************************************************/
/*******************************************************************************/
// Choose whether to play a simulated version or controlled version
selectMode();
// Play the game until completion or until the user quits
while (true){
// If the gameMode is Playable, give the user a turn
if (gameMode)
userTurn();
boolean war = true;
while (war)
{
/* TODO: Your code here.
* 10) Check if either player has won the game
* 11) Check if either players' Deck is empty, if so, then shuffle their Win pile into their Deck
* 12) Remove 1 card from either players' Deck, and add it to their respective War pile
* 13) Print the top cards from each players' War pile
* 14) Compare the top cards from each players' War pile
* 14a) If Player1 has a higher card than Player2, remove all cards from the War piles and add them to Player1's Win pile
* 14b) If Player2 has a higher card than Player1, remove all cards from the War piles and add them to Player2's Win pile
* 14c) If Player1 and Player2 have equal cards, both players remove 3 (or however many are remaining if less) cards and
* add them to their respective War piles, then repeat step 6
*/
/*******************************************************************************/
/*******************************************************************************/
}
}
}
// Method to shuffle Player 1's Win pile back into the deck
private static void shuffleWinP1(){
System.out.println("Player 1 is out of cards in their deck. Shuffling in their win pile...");
/* TODO: Your code here.
* 3) Remove all of the cards from Player 1's Win pile and add them to Player 1's Deck
* Then, shuffle Player 1's Deck
*/
/*******************************************************************************/
/*******************************************************************************/
}
// Method to shuffle Player 2's Win pile back into the deck
private static void shuffleWinP2(){
System.out.println("Player 2 is out of cards in their deck. Shuffling in their win pile...");
/* TODO: Your code here.
* 4) Remove all of the cards from Player 2's Win pile and add them to Player 2's Deck
* Then, shuffle Player 2's Deck
*/
/*******************************************************************************/
/*******************************************************************************/
}
// Method to draw 3 cards for War (or however many are remaining if less) for Player 1
private static void drawWarP1(){
/* TODO: Your code here.
* 5) Remove 3 cards, or however many are remaining if less, from Player 1's Deck and add them to Player 1's War pile
*/
/*******************************************************************************/
/*******************************************************************************/
}
// Method to draw 3 cards for War (or however many are remaining if less) for Player 2
private static void drawWarP2(){
/* TODO: Your code here.
* 6) Remove 3 cards, or however many are remaining if less, from Player 2's Deck and add them to Player 2's War pile
*/
/*******************************************************************************/
/*******************************************************************************/
}
// Method to take all of the war cards and place them in Player 1's Win pile
private static void winWarP1(){
System.out.println("Player 1 wins the battle!");
/* TODO: Your code here.
* 7) Remove all of the cards from Player 1's War pile and add them to Player 1's Win pile
* Then, remove all of the cards from Player 2's War pile and add them to Player 1's Win pile
*/
/*******************************************************************************/
/*******************************************************************************/
}
// Method to take all of the war cards and place them in Player 2's Win pile
private static void winWarP2(){
/* TODO: Your code here.
* 8) Remove all of the cards from Player 1's War pile and add them to Player 2's Win pile
* Then, remove all of the cards from Player 2's War pile and add them to Player 2's Win pile
*/
/*******************************************************************************/
/*******************************************************************************/
}
// Method to check if either player has won the game
private static void checkForWin(){
/* TODO: Your code here.
* 9) Check if either player has won the game (The opposite player's Deck and Win pile are empty)
* Or if a tie occurs. If a tie occurs, print an end game message and exit.
* If a player wins, print a victory message, then exit the game.
*/
/*******************************************************************************/
/*******************************************************************************/
}
// Method to ask the user for the game mode
private static void selectMode(){
System.out.println("Game Modes Available: 1: Simulated 2: Playable");
System.out.print("Enter a Game Mode to play (Invalid inputs will result in Simulated):");
String choice = input.nextLine();
if (choice.equalsIgnoreCase("Playable") || choice.equalsIgnoreCase("2")){
gameMode = true;
System.out.println(" Starting a Playable game...");
System.out.println("During each turn, press Q to quit or any other key to continue. ");
} else {
System.out.println(" Starting a Simulated game... ");
}
}
// Method to ask the user to continue playing
private static void userTurn(){
System.out.print("Player Turn: ");
String choice = input.nextLine();
if (choice.equalsIgnoreCase("Q")){
System.out.println("Thank you for playing!");
System.exit(0);
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
