///// Complete the Deck, Hand and CrazyEights Classes. Complete the provided Dec
ID: 3919015 • Letter: #
Question
///// Complete the Deck, Hand and CrazyEights Classes.
Complete the provided Deck class. The class has two constructors:
You will also complete the following public methods:
Update: provided Deck class updated to match this specification.
Your must use encapsulation for this clas! You can add any private/protected attributes and helper methods that you need. You should not have any static attributes or methods (except possibly amain method that you use for testing the class).
Note: You can use any concrete List that Java’s Collection Framework (JCF) provides.
Now complete the Hand class that is provided. You need to implement two methods (remove and add). You do not need to add very much code to this class.
Now finally,
consider the provided abstract Player class. You will create a new class called CrazyEightsPlayer that is a subclass the Player class.
The crazy eights player class simulates a valid player of the game crazy eights. The rules forour version of Crazy Eights is as follows:
-The game starts with each player being given some number of cards from the deck.
-A single card is taken from the deck and placed on top of the discard pile (and is visible to the player).
-A player’s turn consists of taking zero or more cards from the deck (adding them to their hand) and then playing a card on the top of the discard pile (removing it from their hand).
-A player can always play a card from their hand that has the same rank as the top card in
the discard pile.
-A player can always play a card from their hand that has the same suit as the top card in the discard pile.
-A player can always play a card with rank 8. When a player plays an 8, they are allowed to change the suit of the card to any of the four suits of their choosing. (You will do this by removing the eight from your hand and then returning a new Card object with the desired suit.)
-A player is allowed to (repeatedly) take a card from the deck before playing a card to the discard pile. You must play a card if you are able to. If the deck runs out of cards, the game ends.
-The player that discards all their cards first is the winner. If the deck is exhausted before any player can play all their cards then there is no winner.
Note: You are NOT implementing a crazy eights game. You are building the classes that would be needed for such a game. You are building the model for the game.
ALL JAVA CLASSES YOU NEED:
CARD
STANDARD CARD
public class StandardCard extends Card{
public StandardCard(String rank,String suit){
super(suit,rank);
}
public StandardCard(int rank,String suit){
super(suit,Card.RANKS[rank]);
}
public int getRank(){
return rank;
}
public String getRankString(){
return Card.RANKS[rank];
}
public String getSuit(){
return Card.SUITS[suit];
}
public int compareTo(Card card){
if(card instanceof StandardCard){
StandardCard c = (StandardCard)card;
if(c.getRank() == 1 && getRank() == 1){
return 0;
}
if(c.getRank() == 1 && getRank() != 1){
return -1;
}
if(getRank() == 1 && c.getRank() != 1){
return 1;
}
int s = 0;
for(int i = 0;i<Card.SUITS.length;i++){
if(c.getSuit().equals(Card.SUITS[i])){
s = i;
break;
}
}
if(s < this.suit){
return -1;
}
if(s > this.suit){
return 1;
}
if(s == this.suit){
if(this.rank < c.getRank()){
return -1;
}
if(this.rank > c.getRank()){
return 1;
}
if(this.rank == c.getRank()){
return 0;
}
}
}
return -2;
}
}
DECK
import java.util.List;
/* finish all of the constructors and methods given here */
/* do NOT add any public attributes or methods */
/* add any protected/private attributes you need */
public class Deck{
public Deck(){}
public Deck(int nun_jokers){}
public List<Card> getCards(int num_cards){return null;}
public Card getCard(){return null;}
public void addCard(Card c){}
}
HAND
import java.util.List;
public class Hand{
protected List<Card> cards;
public Hand(List<Card> cards){
this.cards = cards;
}
public int numberOfCards(){
if( this.cards == null ){
return -1;
}else{
return this.cards.size();
}
}
public List<Card> getCards(){ return this.cards; }
/* remove and return the specified card from the hand */
/* return null if the card is not in the hand */
public Card remove(Card card){return new StandardCard(Card.SUITS[4], Card.RANKS[1]);}
/* add the specified card to the hand */
public void add(Card card){}
}
PLAYER
public abstract class Player{
protected Hand hand;
public Player(Hand hand){ this.hand = hand; }
/* play a card from the player's hand */
/* returns null if they cannot play a card and the deck becomes empty */
/* do NOT return null unless you cannot play a card and the deck is empty */
public abstract Card play(Card top_of_discard_pile, Deck deck);
public final int cardsLeft(){ return this.hand.numberOfCards(); }
}
Thanks :)
Explanation / Answer
Deck.java : ----------------------------------
import java.util.ArrayList;
/* finish all of the constructors and methods given here */
/* do NOT add any public attributes or methods */
/* add any protected/private attributes you need */
public class Deck{
private ArrayList<Card> cards;
public Deck(){
cards = new ArrayList<>(52);
for(int i = 2;i<15;i++){
cards.add(new StandardCard(i,Card.SUITS[0]));
}
for(int i = 2;i<15;i++){
cards.add(new StandardCard(i,Card.SUITS[1]));
}
for(int i = 2;i<15;i++){
cards.add(new StandardCard(i,Card.SUITS[2]));
}
for(int i = 2;i<15;i++){
cards.add(new StandardCard(i,Card.SUITS[3]));
}
}
public Deck(int nun_jokers){
cards = new ArrayList<>(52);
for(int i = 2;i<15;i++){
cards.add(new StandardCard(i,Card.SUITS[0]));
}
for(int i = 2;i<15;i++){
cards.add(new StandardCard(i,Card.SUITS[1]));
}
for(int i = 2;i<15;i++){
cards.add(new StandardCard(i,Card.SUITS[2]));
}
for(int i = 2;i<15;i++){
cards.add(new StandardCard(i,Card.SUITS[3]));
}
for(int i = 0;i<nun_jokers;i++){
cards.add(new StandardCard(1,Card.SUITS[4]));
}
}
public ArrayList<Card> getCards(int num_cards){
ArrayList<Card> res_card = new ArrayList<>();
for(int i = 0;i<num_cards;i++){
res_card.add(cards.remove(0));
}
return res_card;
}
public Card getCard(){
if(cards.size() > 0){
return cards.remove(0);
}
return null;
}
public void addCard(Card c){
cards.add(c);
}
}
Hand.java : -------------------->>>>>>>>
import java.util.ArrayList;
public class Hand{
protected ArrayList<Card> cards;
public Hand(ArrayList<Card> cards){
this.cards = cards;
}
public int numberOfCards(){
if( this.cards == null ){
return -1;
}else{
return this.cards.size();
}
}
public ArrayList<Card> getCards(){ return this.cards; }
/* remove and return the specified card from the hand */
/* return null if the card is not in the hand */
public Card remove(Card card){
if(cards.remove(card)){
return card;
}
return null;
}
/* add the specified card to the hand */
public void add(Card card){
cards.add(card);
}
}
CrazyEightsPlayer.java : -------------------------------------
import java.util.ArrayList;
public class CrazyEightsPlayer extends Player{
public CrazyEightsPlayer(Hand hand){
super(hand);
}
public Card play(Card top_of_discard_pile,Deck deck){
ArrayList<Card> cards = hand.getCards();
for(int i = 0;i<cards.size();i++){
if(cards.get(i).getSuit().equals(top_of_discard_pile.getSuit())){
hand.remove(cards.get(i));
return cards.get(i);
}
if(cards.get(i).getRank() == top_of_discard_pile.getRank()){
hand.remove(cards.get(i));
return cards.get(i);
}
if(cards.get(i).getRank() == 8){
hand.remove(cards.get(i));
return new StandardCard(8,top_of_discard_pile.getSuit());
}
}
Card card = null;
while((card = deck.getCard()) != null){
hand.add(card);
for(int i = 0;i<cards.size();i++){
if(cards.get(i).getSuit().equals(top_of_discard_pile.getSuit())){
hand.remove(cards.get(i));
return cards.get(i);
}
if(cards.get(i).getRank() == top_of_discard_pile.getRank()){
hand.remove(cards.get(i));
return cards.get(i);
}
if(cards.get(i).getRank() == 8){
hand.remove(cards.get(i));
return new StandardCard(8,top_of_discard_pile.getSuit());
}
}
}
return null;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.