0

I am having some trouble getting my poker hand to print. I'm not sure if there is something wrong in the hand class or in my dealCard method in my deck class. When I try and run it, It just keeps running. It went over 4 minutes before I finally stopped it.

When I try and call the dealCard method i need a variable, but I'm sort of confused as to how to do that? I was getting help from a tutor on this, but I had to leave before I could get the main, and now I'm a little confused about the dealCard Method and Hand class.

Card:

public class Card 
{


    String suits;
    String values;


    public Card(int suit, int value)
    {
        if (suit == 0)
        {
            suits = "Spades";
        }
        else if (suit == 1)
        {
            suits = "Clubs";
        }
        else if (suit == 2)
        {
            suits = "Hearts";
        }
        else 
        {
            suits = "Diamonds";
        }

        if (value == 0)
        {
            values = "2";
        }
        else if (value == 1)
        {
            values = "3";
        }
        else if (value == 2)
        {
            values = "4";
        }
        else if (value == 3)
        {
            values = "5";
        }
        else if (value == 4)
        {
            values = "6";
        }
        else if (value == 5)
        {
            values = "7";
        }
        else if (value == 6)
        {
            values = "8";
        }
        else if (value == 7)
        {
            values = "9";
        }
        else if (value == 8)
        {
            values = "10";
        }
        else if (value == 9)
        {
            values = "Jack";
        }
        else if (value == 10)
        {
            values = "Queen";
        }
        else if (value == 11)
        {
            values = "King";
        }
        else
        {
            values = "Ace";
        }
    }
}

Deck:

public class Deck 
{
    Card[] deck = new Card[52];

    public Deck()
    {
        int element;
        for(int iSuit = 0; iSuit < 4; iSuit++)
        {
            for(int iValue = 0; iValue < 13; iValue++)
            {
                element = iSuit * 13 + iValue;
                deck[element] = new Card(iSuit, iValue);
            }
        }
    }

    public void shuffle()
    {
        Card[] newDeck = new Card[52];
        int element = (int) (Math.random()*52);

        for(int index= 0; index < 52; index++)
        {
            do
            {
               if (newDeck[element]== null)
               {
                   newDeck[element]=deck[index];
               }
               else
               {
                element = (int) (Math.random()*52);
               }
            }while(newDeck[element]!=null);
        }
        deck = newDeck;
    }

    public Card dealCard(int card)
    {
        return deck[card];
    }
}

Hand:

public class Hand 
{
    Card[] hand = new Card[5];

    public Hand(Deck deck)
    {
       int element;
       for(int card = 0; card < 4; card++)
        {
            hand[card]=deck.dealCard(card);
        }
    }
}

Main:

public class FiveCardPoker 
{

    public static void main(String[] args)
    {

        Deck timsDeck = new Deck();

        timsDeck.shuffle();

        Hand timsHand = new Hand(timsDeck);

        System.out.println(timsHand);


    }

}
user3769297
  • 179
  • 1
  • 2
  • 12

1 Answers1

1

Firstly, it's important to be able to diagnose this yourself more. Either adding some logging or using a debugger should help you work out what the code is doing - you shouldn't just need to depend on perfect output.

Secondly, some unit tests would help you to validate individual sections of your code more clearly.

Thirdly, I suspect the issue is your while loop in shuffle:

do
{
   if (newDeck[element]== null)
   {
       newDeck[element]=deck[index];
   }
   else
   {
    element = (int) (Math.random()*52);
   }
}while(newDeck[element]!=null);

On the last iteration of the enclosing loop, you'll have filled every slot of newDeck - so it's impossible to find a value of element such that newDeck[element] = null. So your while loop will go on forever.

This is actually a pretty painful way of shuffling anyway - consider using Collections.shuffle(Arrays.asList(deck)) instead.

You then also need to override toString() in places in order to get good output. Additionally, I'd recommend string arrays for the suits and values, to massively cut down on the size of your Card constructor:

private static final String[] SUIT_NAMES = { "Hearts", "Clubs", "Diamonds",
    "Spades" };
// Fill in the rest yourself
private static final String[] VALUE_NAMES = { "Ace", "2", "3" ... "King" };

private final String suitName;
private final String valueName;

public Card(int suit, int value) {
    this.suitName = SUIT_NAMES[suit];
    this.valueName = VALUE_NAMES[value];
}

Alternatively, use enums for both the suit and value - a card deck is often used as an example of a situation where an enum makes a lot of sense.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • unfortunately enums and lists are not something we have covered in my class and so I'm not able to use them despite everything I have read suggesting these as better alternatives. I think the purpose is just work with arrays. I see what you are saying about the while loop. Is there a way to just stop this loop after it fills them all. I tried replacing it with the Collections.shuffle, but it just outputs Hand@56b3951d – user3769297 Jul 18 '14 at 05:53
  • @user3769297: Well the last part is due to you not overriding toString. As for the shuffle... Consider different loop forms. (For example, consider a loop to find an empty element number and *then* populate it.) – Jon Skeet Jul 18 '14 at 05:57
  • I went ahead and kept the Collections.shuffle because it is way nicer to use and read. and I added the override, but I'm still getting a strange output Hand@507d811a – user3769297 Jul 18 '14 at 06:24
  • @user3769297: Well what have you added the override to? You probably want to override it for both `Hand` *and* `Card`. – Jon Skeet Jul 18 '14 at 06:30
  • I added @Override public String toString() { return values + " of " + suits; } – user3769297 Jul 18 '14 at 06:33
  • to the card class. I'm having some issues with the hand class. I cant return hand because it is a card array? – user3769297 Jul 18 '14 at 06:34
  • I'm not sure what you mean, but this sounds like a significantly different question to the one you originally asked. Put some more thought into it, and ask a new question if necessary. (Stack Overflow isn't designed for questions which mutate from asking about one thing to asking about something else.) – Jon Skeet Jul 18 '14 at 06:38