user3769297
user3769297

Reputation: 179

How to get my poker hand to print?

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);


    }

}

Upvotes: 0

Views: 228

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500805

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.

Upvotes: 1

Related Questions