Use the Card, Hand and Deck classes to implement a game Card.java public class C
ID: 3739617 • Letter: U
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 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
Explanation / Answer
I hope this code will help you.
1. Card.java
public class Card {
private int value;
private int suit;
public Card(int value,int suit) {
this.value=value;
this.suit=suit;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public int getSuit() {
return suit;
}
public void setSuit(int suit) {
this.suit = suit;
}
}
2. Hand.java
import java.util.ArrayList;
public class Hand {
private ArrayList<Card> cards;
public Hand() {
cards=new ArrayList<>();
}
public void addCardToHand(int value,int suit){
Card card=new Card(value, suit);
cards.add(card);
}
public void removeCardFromHandByIndex(int index){
cards.remove(index);
}
}
3. Deck.java
import java.util.ArrayList;
import java.util.Collections;
public class Deck {
private ArrayList<Card> cards;
public Deck() {
cards=new ArrayList<Card>();
}
public Deck(String type){
cards=new ArrayList<Card>();
if(type.equalsIgnoreCase("standard")){
for(int value=1;value<=13;value++){
for(int suit=1;suit<=4;suit++){
cards.add(new Card(value, suit));
}
}
}
}
public void addCardToDeck(int value,int suit){
Card card= new Card(value, suit);
cards.add(card);
}
public Card drawCardFromDeck(int i){
Card card=cards.remove(i);
return card;
}
public void shuffleCardDeck(ArrayList<Card> cards){
cards=this.cards;
Collections.shuffle(cards);
}
public int numberOfCardInDeck(ArrayList<Card> cards){
cards=this.cards;
return cards.size();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.