I'm not sure why I am getting this output. when I try to print a hand of cards for my poker program. I know there are lots of things I could use to make this more efficient like enums. but this is for a class so I am limited on what I can use. I have never worked with the Collections.Shuffle in my Deck class, so I'm not sure that I did it right. Same with the override in my card class.
Any suggestions on how to get this program to print out 5 cards?
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";
}
}
@Override
public String toString()
{
return values + " of " + suits;
}
}
Deck:
import java.util.Arrays;
import java.util.Collections;
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);
Collections.shuffle(Arrays.asList(deck[element]));
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);
}
}
Hand
doesn`t override the toString method of Object. So you get the standard toString method.
Override the toString methode with something like that:
public String toString() {
StringBuffer sb = new StringBuffer();
for(int card = 0; card < 4; card++)
{
sb.append(hand[card]+",")
}
return sb.toString()
}