Question
Need some help filling this in. You are going to build a small card game called SWITCH using expendable array and linked list. You will
Need some help filling this in.
You are going to build a small card game called SWITCH using expendable array and linked list. You will be given two abstract classes Card and Board, from which you will implement the game specific classes CardSwitch and BoardSwitch. You will also implement an array list-like data structure that keeps track of the cards in a players hand. This is the Hand class, used to represent a Player and a Deck. The Board class maintains a linked list-like data structure that keeps track of all players in the game and decides the winner of the game.
public class Hand
{
// TO DO: add your implementation and JavaDoc
private T[] cards;
private int numCards;
public Hand()
{
// constructor
// initial size of cards must be no greater than 5
}
public int numCards()
{
// return the number of cards
// O(1)
return numCards;
}
public T getCard(int index)
{
// return card at index
// throw RuntimeException for invalid index
// O(1)
checkIndex(index);
return cards[index];
}
public void setCard(int index, T c)
{
// change the card at index to be c
// throw RuntimeException for invalid index
// O(1)
}
public void addCard(T c){
// add card c at the end
// O(N)
}
public int indexOf(T c)
{
// find the index of a given card c,
// returns -1 if not found
// O(N)
}
public T removeCard(int index)
{
// remove the card at index,
// throw RuntimeException for invalid index
// O(N)
}
public boolean removeCard(T c)
{
// remove card c,
// returns false if no such card
// O(N)
}
private void checkIndex(int index)
{
if(index < 0 || index >= numCards());
{
throw new RuntimeException();
}
}
// --------------------------------------------------------
// example test code... edit this as much as you want!
// you will need a working CardSwitch class to run the given code
// Not required, update for your testing purpose
@Override
public String toString()
{
// return string representation of hand
// update if you want to include information for all cards in hand
return "Hand with "+numCards+" cards";
}
public static void main(String[] args)
{
CardSwitch card1 = new CardSwitch(Card.Rank.ACE, Card.Suit.SPADES);
CardSwitch card2 = new CardSwitch(Card.Rank.JACK, Card.Suit.SPADES);
CardSwitch card3 = new CardSwitch(Card.Rank.NINE, Card.Suit.HEARTS);
Hand
myHand.addCard(card1);
myHand.addCard(card2);
if ((myHand.numCards() == 2) && (myHand.getCard(0).equals(card1)))
{
System.out.println("Yay 1");
}
myHand.addCard(card3);
if ( card2.equals(myHand.removeCard(1)) && myHand.getCard(1).equals(card3))
{
System.out.println("Yay 2");
}
if ((myHand.indexOf(card1)==0) && (myHand.indexOf(card2) == -1 ))
{
System.out.println("Yay 3");
}
}
}
THIS IS THE CARD CLASS AND CARDSWITCH WHICH ARE DONE
public abstract class Card
{
// Creates constants of cards that are used in a normal deck of cards.
// Rank of cards are the value of what a card is worth from 2 all the way up to king and ace
enum Rank
{
ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING;
}
// There are four types of cards which are called suit and are constants.
// Each suit has has 13 cards which are have ranks from ace to king.
enum Suit
{
HEARTS, CLUBS, DIAMONDS, SPADES;
}
protected Rank rank;
protected Suit suit;
// Assigns what is a card which is rank and suit and gives them a variable r and s
public Card(Rank r, Suit s)
{
rank = r;
suit = s;
}
// Returns the value of the rank of the card
public Rank getRank()
{
return rank;
}
// Returns the value of the suit of the card
public Suit getSuit()
{
return suit;
}
abstract boolean equals(Card c);
abstract int getPoints();
@Override
public abstract String toString();
}
public class CardSwitch extends Card
{
// TO DO: fill the code below and add JavaDoc
public CardSwitch(Rank r, Suit s)
{
// constructor to create card for the game Switch
super(r,s);
}
@Override
public boolean equals(Card anotherCard)
{
// checks if two cards have the same rank and suit
// if they do then it returns true if not its false
if(this.rank.equals(anotherCard.rank)&&this.suit.equals(anotherCard.suit))
{
return true;
}
else
return false;
}
@Override
// calls get Rank method in card class and checks what enum value it has and returns the integer value of that rank
public int getPoints()
{
// return points of the card
if(getRank().equals(Rank.ACE))
return 1;
else if(getRank().equals(Rank.TWO))
return 2;
else if(getRank().equals(Rank.THREE))
return 3;
else if(getRank().equals(Rank.FOUR))
return 4;
else if(getRank().equals(Rank.FIVE))
return 5;
else if(getRank().equals(Rank.SIX))
return 6;
else if(getRank().equals(Rank.SEVEN))
return 7;
else if(getRank().equals(Rank.EIGHT))
return 8;
else if(getRank().equals(Rank.NINE))
return 9;
else if(getRank().equals(Rank.TEN)||getRank().equals(Rank.JACK)||getRank().equals(Rank.QUEEN)||getRank().equals(Rank.KING))
return 10;
return 0;
}
@Override
public String toString()
{
// convert card to string consisting of as "(rank,suit)"
// see examples below for format
// returns in capital letters of what the rank and suit are
return "C"+this.rank.toString()+","+this.suit.toString()+")";
}
//----------------------------------------------------
//example test code... edit this as much as you want!
public static void main(String[] args) {
CardSwitch card = new CardSwitch(Card.Rank.ACE, Card.Suit.SPADES);
if (card.getRank().equals(Card.Rank.ACE))
{
System.out.println("Yay 1");
}
if (card.toString().equals("(ACE,SPADES)"))
{
System.out.println("Yay 2");
}
if (card.getPoints()==1)
{
System.out.println("Yay 3");
}
}
}
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