Can someone please take a look at this for me. I have to create a program that models a simple card game. I have to shuffle the card and deal 1 card at a time and display the card that I have dealt. Not sure how to do this. The code I have is returning 00. I also have to declair a winner. I guess I need to do a nested for loop to check against suit and face of card right.(havent tried it yet). Looking for suggestions or directions in the correct place.
Thanks.

Dani AI

Generated

Quick diagnosis: getting "00" or the same pair like "12 3" repeatedly usually means one of three things is wrong — the Card objects never get the right face/suit values, the same Card instance was placed into every deck slot, or the index that tracks the next card is being reset every time you deal. was on the right track about the constructor; if face/suit stay at their default value the printed result will look like zeros. ’s note about moving initialization into the deal method points to the other common pitfall: reinitializing the pointer each deal.

Concrete checklist to fix the logic

  • Populate the deck with 52 distinct new Card(...) objects (one per array/list slot).
  • Call the population method once, then shuffle. Don’t repopulate before every deal.
  • Keep a Deck field (e.g. nextIndex or top) that starts at 0 and is incremented by the deal method; do not declare or reset it inside the deal method.
  • Use a correct shuffle (Fisher–Yates or Collections.shuffle) so cards are permuted, not deterministic.

Example snippets (minimal)

// Fisher–Yates shuffle (in Deck)
Random rnd = new Random();
for (int i = cards.length - 1; i > 0; i--) {
  int j = rnd.nextInt(i + 1);
  Card tmp = cards[i]; cards[i] = cards[j]; cards[j] = tmp;
}
// deal one card (Deck field: private int next = 0;)
public Card dealOne() {
  if (next >= cards.length) return null;
  return cards[next++]; // persistent pointer, not reset each call
}

Final troubleshooting tips: add short debug prints when constructing the deck (index and new Card identity) to detect reuse of one object, confirm toString() maps face/suit indexes to readable names (watch off-by-one), and test using a List with Collections.shuffle() + remove(0) if mutation semantics are acceptable.

Recommended Answers

All 5 Replies

I did few small changes to your files

CARDS change in card constructor

public Card(int f, int s)
{
    // initialise instance variables
    face = f;
    suit = s;        //you had suit = SPADE
}

DECK change your constructor public Deck() (with 2 for loops) to a method to initialize cards, I call it public void setCards()

DRIVER use the new method setCards() before you suffle cards

if (choices[0].equals((String) selected))
{
            inDeck.setCards();
            inDeck.shuffle();
            System.out.println("card mixed");
            playCards();}

I would create random function to get my set of cards and check if these cards hasn't been dealt. So dealt cards should be store somewhere :?:

I'm now able to see the card. But it will not display different cards. I keep getting 12 3

Did you tried a random function as I sugested???

Not sure what you mean by creating a random fucntion. I moved the intialization of dealt to the deal method. Is it because I needs some kind of equals function?

I think I poineted you in wrong direction, you don't need random function that is useful dice games. Sorry my mistake. ;)
But you may want to create another array of type card to store data about cards which been used and should not be dealth again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.