Question: Use the Card, Hand and Deck classes to implement a game Card.java public class Card { //Create int field value //Create int field suit //Create

Use the Card, Hand and Deck classes to implement a game

Card.java

public class Card {

//Create int field "value"

//Create int field "suit"

//Create a constructor that takes two ints as arguments and sets

value and suit to those values (in that order!)

//Make getters and setters

}

Hand.java

import java.util.ArrayList;

public class Hand {

private ArrayList cards;

//constructor initializes an empty array list so it can be used in other methods

public Hand(){

cards = new ArrayList();

}

//Create function to add a card to the hand

//Create a function to remove card from the hand by index and return it

//Create a function to total the value of all cards in the hand

//Create a function to print out the hand with the following format:

// Card 1: 9 of 3's

// Card 2: 8 of 4's

// Card 3: 2 of 2's

// ...and so on

}

Deck.java

import java.util.ArrayList;

public class Deck {

private ArrayList cards;

public Deck(){

//empty deck if called with no constructor

cards = new ArrayList();

}

public Deck(String type){

cards = new ArrayList();

//Create a deck of 52 cards; This constructor could be extended to build other types of decks e.g. Pinochle

if(type.equalsIgnoreCase("standard")){

for(int value = 1; value <=13; value++){

for(int suit =1; suit <=4; suit++){

//Error will go away once card is implemented

cards.add(new Card(value,suit));

}

}

}

}

//Make a function to add a card to the deck

//Make a function to draw a card (remove from deck and return the card)

//Make a function to shuffle the deck (hint: Use Collections.shuffle()

//Make a function to count the number of cards in the deck and return it

}

Activity 1 (10 points):

Download the starter code for the Card, Hand and Deck classes from Blackboard

Complete the classes based on the comments in the starter code

Most of the functions will require functions from the ArrayList API, so have that open and ready!

Notice that in this implementation the suit of a card is represented by an integer,rather than a string

Create a class called Test to test your classes and functions:

Make a deck using standard as the argument to the constructor

Shuffle the deck

Make hand object

Deal 6 cards from the deck to the hand

Print out the number of cards remaining in the deck

Print out the hand using the printHand method

Print out the total value of the hand

Remove the 3rd card from the hand and add it back to the deck

Print out the total value of the hand again

Print out the number of cards in the deck again

Activity 2 (5 points):

Implement the Simplified War game below

Simplified War:

Setup:

Make a standard deck and shuffle it

Make two empty decks

Deal half of the standard deck to each empty deck (26 cards each)

Game:

Deck 1 represents the player, Deck 2 represents the computer opponent

Each player draws a card and compares the values the higher value wins

In the event of a tie, compare the suits the higher value wins

Track the wins and losses of the computer and the player

Each round:

Print the value of the cards drawn

Print who wins

Print the current score tally for the player and the computer

Ask if the player would like to play another round

Continue playing rounds until the player quits or the decks run out of

cards

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!