Jump to content

So for my project, I need to make a deck class and a tester that creates three Deck objects and tests each method for each Deck object. I am lost in how to setup the deck objects for this however. The class should be correct. Any help is greatly appreciated. Thank you!! here is the code for the deck class. 

 

Edit: So I made a few changes to the code to avoid OutofBounds errors. I changed the <= under the deck method to >=. I have my tester someone made however the deal method is coming back null every time because the tester is only dealing 1 card per deck. How do I make it deal the appropriate amount? Thanks! 

package elevens;import java.util.List;import java.util.ArrayList;/** * The Deck class represents a shuffled deck of cards. * It provides several operations including *      initialize, shuffle, deal, and check if empty. */public class Deck {	/**	 * cards contains all the cards in the deck.	 */	private List<Card> cards;        private Card[] deck;        private String suit;        private String card;        private int deck_size = 52;        private boolean isEmpty = false;        private int cardsDealt = 0;                        	/**	 * size is the number of not-yet-dealt cards.	 * Cards are dealt from the top (highest index) down.	 * The next card to be dealt is at size - 1.	 */	private int size;	/**	 * Creates a new <code>Deck</code> instance.<BR>	 * It pairs each element of ranks with each element of suits,	 * and produces one of the corresponding card.	 * @param ranks is an array containing all of the card ranks.	 * @param suits is an array containing all of the card suits.	 * @param values is an array containing all of the card point values.	 */	public Deck(String[] ranks, String[] suits, int[] values) {                String suit[] = {"Spades", "Diamonds", "Clubs", "Hearts"};                String rank[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Joker", "Queen", "King"};                Integer pValue[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};                int Suits = suit.length;                int Ranks = rank.length;                int PValue = pValue.length;                int cardnumber = 0;                {                for (int a = 0; a <= Suits; a++)                     for (int b = 0; b <= Ranks; b++)                         for (int c = 0; c <= PValue; c++)                         deck[cardnumber] = new Card(suit[a], rank[b], pValue[c]);                cardnumber++;                }        }	/**	 * Determines if this deck is empty (no undealt cards).	 * @[member='ReturnToLobby'] true if this deck is empty, false otherwise.	 */	public boolean isEmpty() {            {            if (deck_size < 52)            isEmpty = false;            else             isEmpty = true;            }        return isEmpty;        }	/**	 * Accesses the number of undealt cards in this deck.	 * @[member='ReturnToLobby'] the number of undealt cards in this deck.	 */	public int size() {		size = ( deck.length - cardsDealt);                return size;	}	/**	 * Randomly permute the given collection of cards	 * and reset the size to represent the entire deck.	 */	public void shuffle() {		/* *** TO BE IMPLEMENTED IN ACTIVITY 4 *** */	}	/**	 * Deals a card from this deck.	 * @[member='ReturnToLobby'] the card just dealt, or null if all the cards have been	 *         previously dealt.	 */	public Card deal() {		if (cardsDealt == deck.length)                    System.out.println("There are no more cards left to dealt in the deck.");                cardsDealt++;                return deck[cardsDealt -1];	}	/**	 * Generates and returns a string representation of this deck.	 * @[member='ReturnToLobby'] a string representation of this deck.	 */	@[member='OverRide']	public String toString() {		String rtn = "size = " + size + "\nUndealt cards: \n";		for (int k = size - 1; k >= 0; k--) {			rtn = rtn + cards.get(k);			if (k != 0) {				rtn = rtn + ", ";			}			if ((size - k) % 2 == 0) {				// Insert carriage returns so entire deck is visible on console.				rtn = rtn + "\n";			}		}		rtn = rtn + "\nDealt cards: \n";		for (int k = cards.size() - 1; k >= size; k--) {			rtn = rtn + cards.get(k);			if (k != size) {				rtn = rtn + ", ";			}			if ((k - cards.size()) % 2 == 0) {				// Insert carriage returns so entire deck is visible on console.				rtn = rtn + "\n";			}		}		rtn = rtn + "\n";		return rtn;	}}

i7 3930k @ 4.4Ghz | AMD 7970 @ 1150/1823 | Asus P9X79 Pro | 16GB G.Skill 2133mhz | 2x WD Caviar Black 1TB in RAID 0 & 250GB Samsung Evo SSD | Rosewill Blackhawk Ultra with Side Window | XSPC RX360 Watercooling Kit w/ UV Blue Tubing | Corsair Vengeance K70 Keyboard w/ Corsair M65 Mouse

 

Link to comment
https://linustechtips.com/topic/292387-need-help-creating-a-deck-tester/
Share on other sites

Link to post
Share on other sites

 

So for my project, I need to make a deck class and a tester that creates three Deck objects and tests each method for each Deck object. I am lost in how to setup the deck objects for this however. The class should be correct. Any help is greatly appreciated. Thank you!! here is the code for the deck class. 

 

Edit: So I made a few changes to the code to avoid OutofBounds errors. I changed the <= under the deck method to >=. I have my tester someone made however the deal method is coming back null every time because the tester is only dealing 1 card per deck. How do I make it deal the appropriate amount? Thanks! 

--snip--

 

Could you post the Card class and your tester?

It'll be much easier to help you if I can run it myself.

 

EDIT:

A few questions and things you should change IMO:

1) Why does the constructor accept 3 arrays which aren't used in it?

2)the "deck_size" variable is used only in the isEmpty() method and nowhere else. It will never be changed by anything so it will allways return true.

naming a variable something_something is used for constants and you should name them with all capital letters.

private static final int FULL_DECK_SIZE is more appropriate and it should always be equal to 52.

the isEmpty variable is not needed. Just use the isEmpty() method.

The isEmpty() method itself should look something more like:

public boolean isEmpty(){

return cardsDealt == FULL_DECK_SIZE; //will return true only when all the cards have been dealt.

}

 

There are a lot other stuff to be changed.

Take a second look on your implementation.

A lot of things do not make sense.

 

After you give us your other code I'll gladly help you with it.

 

EDIT2:

just saw you have a size variable.

The isEmpty() method can easily be 

return size == 0; //when there are no more cards to deal the deck is empty

CPU: Ryzen 3 3600 | GPU: Gigabite GTX 1660 super | Motherboard: MSI Mortar MAX | RAM: G Skill Trident Z 3200 (2x8GB) | Case: Cooler Master Q300L | Storage: Samsung 970 EVO 250G + Samsung 860 Evo 1TB | PSU: Corsair RM650x | Displays: LG 27'' G-Sync compatible 144hz 1080p | Cooling: NH U12S black | Keyboard: Logitech G512 carbon | Mouse: Logitech g900 

Link to post
Share on other sites

Here's an idea:

 

The Card class:

package nn;public class Card{	private Suits suit;	private Values value;		public enum Suits{		SPADES, HEARTS, DIAMONDS, CLUBS	};		public enum Values{		ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING	};		public Card(Suits suit, Values value) {		this.suit = suit;		this.value = value;	}	public String toString(){		return String.format("%s of %s", value, suit);	}			public Values geValue(){		return value;	}		public Suits getSuit(){		return suit;	}}

The Deck class:

package nn;import java.util.Collections;import java.util.LinkedList;import java.util.List;import nn.Card.Suits;import nn.Card.Values;public class Deck {	private static final int FIRST_CARD_INDEX = 0;	private List<Card> deck;	public Deck() {		deck = new LinkedList<Card>();		fillDeck();	}	private void fillDeck() {		Suits[] suits = Suits.values();		Values[] values = Values.values();		for (int i = 0; i < suits.length; i++) {			for (int j = 0; j < values.length; j++) {				Card card = new Card(suits[i], values[j]);				deck.add(card);			}		}	}	public boolean isEmpty() {		return deck.isEmpty();	}	public int size() {		return deck.size();	}	public void shuffle() {		Collections.shuffle(deck);	}	public Card deal() {		Card cardToDeal = deck.get(FIRST_CARD_INDEX);		deck.remove(cardToDeal);		return cardToDeal;	}	@[member=OverRide]	public String toString() {		if (isEmpty()) {			return "The deck is empty!";		}		StringBuilder builder = new StringBuilder();		for (Card card : deck) {			builder.append(card.toString());			builder.append("\n");		}		builder.setLength(builder.length() - 1);// this will remove the last new line		return builder.toString();	}}

A simple Tester class:

package nn;public class Tester {	public static void main(String[] args) {		System.out.println("----------------Unshuffled deck------------------------");		Deck deck = new Deck();		System.out.println(deck.toString());				System.out.println("---------------Shuffled deck---------------------");				deck.shuffle();				System.out.println(deck.toString());				System.out.println("-----------------Dealing 50 cards-------------------");				System.out.println("Dealing 50 cards");		for (int i = 0; i < 50; i++) {			Card dealtCard = deck.deal();			System.out.println("Dealt: " + dealtCard.toString());		}				System.out.println("-----------------The remaining deck----------------------");				System.out.println(deck.toString());	}}

CPU: Ryzen 3 3600 | GPU: Gigabite GTX 1660 super | Motherboard: MSI Mortar MAX | RAM: G Skill Trident Z 3200 (2x8GB) | Case: Cooler Master Q300L | Storage: Samsung 970 EVO 250G + Samsung 860 Evo 1TB | PSU: Corsair RM650x | Displays: LG 27'' G-Sync compatible 144hz 1080p | Cooling: NH U12S black | Keyboard: Logitech G512 carbon | Mouse: Logitech g900 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×