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