I HAVE CODE FOR FIRST TWO CLASSES NEED HELP WITH THE REST THREE i.e. WITH DECK,
ID: 3591538 • Letter: I
Question
I HAVE CODE FOR FIRST TWO CLASSES NEED HELP WITH THE REST THREE i.e. WITH DECK, GAME AND CLIENT.. PLEASE REPLY ASAP..
1. Complete assignment P-7.60 on page 305 of the textbook that implements the CardHand class with the additional requirements as listed. When arranging your cards:
a. you should not worry about the relative values of the suites (i.e. does a Heart come before a Diamond)
b. Rather you should arrange the suits so that the first suit you are dealt is to the “left” (first) and additional suits are added to the “right” (later) as they are received
c. Implement your CardHand class so that adding a new card can be done in constant time, O( 1 ).
d. You must use a LinkedPositionalList to represent the card hand.
e. You must use the “four fingers” method suggested in the textbook.
2. Implement a Card class that represents a playing card.
a. A card has a suit ( Club, Diamond, Heart, or Spade )
b. A card has a value ( 2 through 10, Jack, Queen, King, or Ace) with 2 being the lowest value and Ace being the highest value.
c. You do not need to support Jokers
3. Implement a Deck class that represents a deck of cards. This Deck class must include the following methods:
a. Deck( ): the constructor that creates a deck of 52 cards, one of each possible suit-value combination.
b. card( ): deals a random card from the deck, where: 1) cards are dealt on a non-replacement basis (i.e. once a card is dealt it cannot be dealt again), and 2) on each deal all cards (remaining) in the deck have an equal probability of being dealt.
c. Selecting the correct data structure should make this easy to implement.
4. Implement a Game class that represents a card game. This Game class must include:
a. An instance variable that represents the number of players in the game (should be passed in as a parameter).
b. An instance variable that represents the maximum number of cards that can be in each player’s hand (should be passed in as a parameter).
c. An instance variable for the deck of cards used in the game.
d. An instance variable that is an array of the player’s card hands.
e. A method getCard() that deals one card to each player in the array of players (item c). When a player gets a new card they should immediately add this card to their hand so that they are ordered correctly by suit and value (item 1).
f. Note that the player must immediately insert each new card into the correct position in their hand. The player cannot wait until they have received all of their cards and then sort the cards.
5. A client that tests your classes by dealing a card game. Your client should:
a. Create a new instance of a game with four ( 4 ) players, and 13 cards per player (e.g. like a game of Bridge).
b. Have cards dealt to each player one card at a time and display the hands of each player after each round is dealt. You output should look something like the following (Keep in mind that the order of the suits in each player’s hand is determined by the order the suits are received):
Card 01:
Player 1: 2C
Player 2: KH
Player 3: 8H
Player 4: 2D
Card 02:
Player 1: 2C 7D
Player 2: KH 9S
Player 3: 3H 8H
Player 4: 9D 2D
And so on until
Card 13:
Player 1: 7C QC 10C 9C 5C KC 2C 5D 7D 6D KS AH 4H
Player 2: 3H QH JH KH 4S 9S 7S JS 4D 8D 4D 3D 10C
Player 3: 5H 8H 6H 7H 10H 8C 3C AD KD QS 6S 5S 3S
Player 4: JD QD 2D 9D AS 8S 10S 2S 9H 2H 6C JC AC
The above list of methods represents the methods that must be present. You will most likely need additional methods in each of the classes to complete the assignment.
--> THIS IS WHAT WAS GIVEN IN P-7.60 ON PAGE 305
P-7.60 Implement a CardHand class that supports a person arranging a group of cards in his or her hand. The simulator should represent the sequence of cards using a single positional list ADT so that cards of the same suit are kept together. Implement this strategy by means of four “fingers” into the hand, one for each of the suits of hearts, clubs, spades, and diamonds, so that adding a new card to the person’s hand or playing a correct card from the hand can be done in constant time. The class should support the following methods:
• addCard(r, s): Add a new card with rank r and suit s to the hand.
• play(s): Remove and return a card of suit s from the player’s hand; if there is no card of suit s, then remove and return an arbitrary card from the hand.
• iterator( ): Return an iterator for all cards currently in the hand.
• suitIterator(s): Return an iterator for all cards of suit s that are currently in the hand.
--------------------> HERE IS THE CODE FOR CARD HAND:
import java.util.ArrayList;
import java.util.NoSuchElementException;
/**
*
*
*/
public class CardHand {
private LinkedPositionalList cardhand = null;
private String suit, value;
private static String[] suits = { "h", "s", "d", "c" };
private static String[] values = { "2", "3", "4", "5", "6", "7",
"8", "9", "10", "J", "Q", "K", "A" };
private ArrayList valuelist;
public CardHand(){
cardhand= new LinkedPositionalList<>();
valuelist= new ArrayList();
for( int i =0; i valuelist.add(values[i]);
}
public void addCard(Card c){
cardhand.iterator();
Iterator ci = cardhand.iterator();
boolean foundsuit= false;
while(ci.hasNext()){
ci.next();
Card temp = ci.next();
if(temp.getSuit().equals(c.getSuit()))
{
foundsuit= true;
if(valuelist.indexOf(temp.getValue())> valuelist.indexOf(c.getValue())){
cardhand.addBefore((Position)temp, c);
break;
}
}
else if(foundsuit){
cardhand.addBefore((Position)temp, c);
break;
}
else if(temp==cardhand.last()){
cardhand.addLast(c);
break;
}
}
}
public Card play(String suit){
Iterator> c2 =suitPositions(suit).iterator();
if(c2.hasNext()){
Position card = c2.next();
return cardhand.remove(card);
}
else{
Iterator> c3 = new PositionIterator();
Position card = c3.next();
return cardhand.remove(card);
}
}
public Iterable> suitPositions(String suit ) {
return new SuitPositionIterable( suit); // create a new instace of the inner class
}
private class SuitPositionIterable implements Iterable>{
String suit;
public SuitPositionIterable(String suit){
this.suit = suit;
}
@Override
public Iterator> iterator() { return new SuitPositionIterator(suit ); }
}
private class SuitPositionIterator implements Iterator>{
private String suit;
private Position cursor = cardhand.first(); // position of the next element to report
private Position recent = null; // position of last reported element
/** Tests whether the iterator has a next object. */
public SuitPositionIterator(String suit){
this.suit=suit;
boolean suitfound=false;
while ( cursor != null && !( cursor.getElement().getSuit().equals(suit)) ) {
//<<< new code
suitfound=true;
cursor = cardhand.after( cursor );
}
if(!(suitfound)){
cursor=null;
}
}
@Override
public boolean hasNext( ) { return ( cursor != null ); }
/** Returns the next position in the iterator. */
@Override
public Position next( ) throws NoSuchElementException {
// On the first call to next (i.e. when recent == null) you need to //<<< new code
// advance recent until it is pointing to a vowel element. //<<< new code
if ( recent == null ) //<<< new code
{ //<<< new code
while ( cursor != null && !( cursor.getElement().getSuit().equals(suit)) ) //<<< new code
cursor = cardhand.after( cursor ); //<<< new code
} //<<< new code
if ( cursor == null ) throw new NoSuchElementException( "nothing left " );
recent = cursor;
cursor = cardhand.after( cursor );
// advance cursor to the next suit
while ( cursor != null && !( cursor.getElement().getSuit().equals(suit)) ) //<<< new code
cursor = cardhand.after( cursor );
return recent;
}
/** Removes the element returned by most recent call to next. */
@Override
public void remove( ) throws IllegalStateException {
if ( recent == null ) throw new IllegalStateException( "nothing to remove" );
cardhand.remove( recent ); // remove from outer list
recent = null; // do not allow remove again until next is called
}
}
private class PositionIterator implements Iterator>{
private Position cursor = cardhand.first(); // position of the next element to report
private Position recent = null; // position of last reported element
/** Tests whether the iterator has a next object. */
@Override
public boolean hasNext( ) { return ( cursor != null ); }
/** Returns the next position in the iterator. */
@Override
public Position next( ) throws NoSuchElementException {
if ( cursor == null ) throw new NoSuchElementException( "nothing left " );
recent = cursor;
cursor = cardhand.after( cursor );
return recent;
}
/** Removes the element returned by most recent call to next. */
@Override
public void remove( ) throws IllegalStateException {
if ( recent == null ) throw new IllegalStateException( "nothing to remove" );
cardhand.remove( recent ); // remove from outer list
recent = null;
}
}
}
-----------------------> HERE IS THE CODE FOR CARD
/**
*
*
*/
public class Card {
private static String suit ;
private static String value;
Card(String suit, String value)
{
this.value=value;
this.suit=suit;
}
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
Card.suit = suit;
}
public String getValue() {
return value;
}
public void setValue(String value) {
Card.value = value;
}
@Override
public String toString() {
return suit + value;
}
}
Explanation / Answer
/
import java.util.*;
/**
* Represents the basic functionality of a hand of cards.
* Extensions of this class will provide the
* definition of what constitutes a hand for that game and how hands are compared
* to one another by overriding the <code>compareTo</code> method.
* @author John K. Estell
* @version 1.0
*/
public abstract class Hand implements Comparable {
private java.util.List hand = new ArrayList();
/**
* Adds a card to this hand.
* @param card card to be added to the current hand.
*/
public void addCard( Card card ) {
hand.add( card );
}
/**
* Obtains the card stored at the specified location in the hand. Does not
* remove the card from the hand.
* @param index position of card to be accessed.
* @return the card of interest, or the null reference if the index is out of
* bounds.
*/
public Card getCard( int index ) {
return (Card) hand.get( index );
}
/**
* Removes the specified card from the current hand.
* @param card the card to be removed.
* @return the card removed from the hand, or null if the card
* was not present in the hand.
*/
public Card removeCard( Card card ) {
int index = hand.indexOf( card );
if ( index < 0 )
return null;
else
return (Card) hand.remove( index );
}
/**
* Removes the card at the specified index from the hand.
* @param index poisition of the card to be removed.
* @return the card removed from the hand, or the null reference if
* the index is out of bounds.
*/
public Card removeCard( int index ) {
return (Card) hand.remove( index );
}
/**
* Removes all the cards from the hand, leaving an empty hand.
*/
public void discardHand() {
hand.clear();
}
/**
* The number of cards held in the hand.
* @return number of cards currently held in the hand.
*/
public int getNumberOfCards() {
return hand.size();
}
/**
* Sorts the card in the hand.
* Sort is performed according to the order specified in the {@link Card} class.
*/
public void sort() {
Collections.sort( hand );
}
/**
* Checks to see if the hand is empty.
* @return <code>true</code> is the hand is empty.
*/
public boolean isEmpty() {
return hand.isEmpty();
}
/**
* Determines whether or not the hand contains the specified card.
* @param card the card being searched for in the hand.
* @return <code>true</code> if the card is present in the hand.
*/
public boolean containsCard( Card card ) {
return false;
}
/**
* Searches for the first instance of the specified card in the hand.
* @param card card being searched for.
* @return position index of card if found, or <code>-1</code> if not found.
*/
public int findCard( Card card ) {
return hand.indexOf( card );
}
/**
* Compares two hands.
* @param otherHandObject the hand being compared.
* @return < 0 if this hand is less than the other hand, 0 if the two hands are
* the same, or > 0 if this hand is greater then the other hand.
*/
public int compareTo( Object otherHandObject ) {
Hand otherHand = (Hand) otherHandObject;
return evaluateHand() - otherHand.evaluateHand();
}
/**
* Evaluates the hand. Must be defined in the subclass that implements the hand
* for the game being written by the client programmer.
* @return an integer corresponding to the rating of the hand.
*/
public abstract int evaluateHand();
/**
* Returns a description of the hand.
* @return a list of cards held in the hand.
*/
public String toString() {
return hand.toString();
}
/**
* Replaces the specified card with another card. Only the first
* instance of the targeted card is replaced. No action occurs if
* the targeted card is not present in the hand.
* @return <code>true</code> if the replacement occurs.
*/
public boolean replaceCard( Card oldCard, Card replacementCard ) {
int location = findCard( oldCard );
if ( location < 0 )
return false;
hand.set( location, replacementCard );
return true;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.