Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help With 5 card stud program

Ryan Saunders
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all. I'm Looking for some assistance on a 5 card stud poker program. So far I can shuffle and deal, now I am trying to evaluate the hands each player gets. I was shown how to do this, but forgot. I'm a total beginner at programming. First, I am not sure if I am filling the arrays hand1 and hand2 correctly, can someone help me confirm that they are filled. Second, I've been trying to get one of the text fields to simply show the face and suit of the card in position tthree of hand one, I can't get it to show the string. Last, I am pretty sure i can figure out the algorithms for my method handEval, yet once again need to check and make sure the array is filled.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class poker2 extends JFrame {
private Card deck[]; //this is an array of objects of Card type. See the Card class at the end
private int currentCard; //this keeps track of the number of cards dealt
private JButton dealButton, shuffleButton, testButtton; //GUI stuff, the two buttons
private JTextField displayField; //GUI stuff, the textfield that displays the card dealt
private JLabel statusLabel; //GUI stuff, Displays the number of cards dealt
private JLabel cardback, card1, card2,card3,card4,card5,card6,card7,card8,card9,card10;//GUI stuff The labels that displays the cards
private JTextField hand1TF, hand2TF;
Card dealt; //this is an object of Card type for a dealt card
Icon card = new ImageIcon(".\\cards\\back2.GIF"); //this is the cardback graphics file
Card hand1[];
Card hand2[];
int handNums[];
int test;
// set up deck of cards and GUI
public poker2()
super( "5-Card Stud" ); //Title

String faces[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
String suits[] = { "Hearts", "Diamonds", "Clubs", "Spades" };

deck = new Card[ 52 ];
hand1 = new Card[5];
hand2 = new Card[5];
handNums = new int[13]; //creates array to hold 52 cards
currentCard = -1;
test = 4;

// populate deck with Card objects
for ( int count = 0; count < deck.length; count++ )
deck[ count ] = new Card( faces[ count % 13 ],suits[ count / 13 ] );

// set up GUI and event handling
Container container = getContentPane();
container.setLayout( new FlowLayout() );

//Diplay the cardbacks
card1 = new JLabel();
container.add( card1 );

card3 = new JLabel();
container.add( card3 );

card5 = new JLabel();
container.add( card5 );

card7 = new JLabel();
container.add( card7 );

card9 = new JLabel();
container.add( card9 );

hand1TF = new JTextField(32);
hand1TF.setEditable( false );
hand1TF.setText( "Here it Is" );
container.add( hand1TF );

card2 = new JLabel();
container.add( card2 );

card4 = new JLabel();
container.add( card4 );

card6 = new JLabel();
container.add( card6 );

card8 = new JLabel();
container.add( card8 );

card10 = new JLabel();
container.add( card10 );

hand2TF = new JTextField(32);
hand2TF.setEditable( false );
hand2TF.setText( "Here Theirs Are" );
container.add( hand2TF );

// Initialize the "Deal Button"
dealButton = new JButton( "DEAL 'EM" );

new ActionListener() { // anonymous inner class

// deal a card. This listens for the "Deal CArd" button to be clicked
public void actionPerformed( ActionEvent actionEvent )
dealt = dealCard();

if ( dealt != null ) {
displayField.setText( dealt.viewcard() );
statusLabel.setText( "Card #: " + (currentCard + 1) );

else {
displayField.setText( "NO MORE CARDS TO DEAL" );
statusLabel.setText( "Shuffle cards to continue" );
//This is the right spot1 ???
int i;
i = 0;
if(currentCard % 2 == 0)
{hand2 = dealt;
hand1[currentCard / 2] = dealt;

} // end anonymous inner class

); // end call to addActionListener

container.add( dealButton ); //this adds the button to the GUI
dealButton.setEnabled(false); //this disables the button. Don't deal a card until the deck is shuffled

//initialize the "Shuffle Deck" button
shuffleButton = new JButton( "Shuffle cards" );

new ActionListener() { // anonymous inner class

// shuffle deck. This listens for the "Shuffle cards" button to be clicked
public void actionPerformed( ActionEvent actionEvent )
displayField.setText( "SHUFFLING ..." );
displayField.setText( "DECK IS SHUFFLED" );

//reset the card graphics to the back every shuffle
card = new ImageIcon(".\\cards\\back2.GIF");
dealButton.setEnabled(true); //enable the dealbutton after every shuffle
private void evalHand()
int i;
for(i = 0; i < hand1.length;i++)

} // end anonymous inner class

); // end call to addActionListener

container.add( shuffleButton );

displayField = new JTextField( 20 );
displayField.setEditable( false );
container.add( displayField );

statusLabel = new JLabel();
container.add( statusLabel );

setSize( 400, 400 ); // set window size
setVisible( true ); // show window

// shuffle deck of cards with one-pass algorithm
private void shuffle()
currentCard = -1;

// for each card, pick another random card and swap them
for ( int first = 0; first < deck.length; first++ ) {
int second = ( int ) ( Math.random() * 52 );
Card temp = deck[ first ];
deck[ first ] = deck[ second ];
deck[ second ] = temp;

//dealButton.setEnabled( true );

// deal one card
private Card dealCard()
if ( ++currentCard < deck.length )
return deck[ currentCard ];
else {
dealButton.setEnabled( false );
return null;

// c is the currentcard, just check for the card value and suit and display the appropriate graphics file
private void showcard(int c)
// CASES 0,2,4,6,8 Are your Cards
// CASES 1,3,5,7,9 are your opponents cards
card = new ImageIcon(getCard(dealt));
switch (c)
case 0: card1.setIcon(card); break;
case 1: card2.setIcon(card); break;
case 2: card3.setIcon(card); break;
case 3: card4.setIcon(card); break;
case 4: card5.setIcon(card); break;
case 5: card6.setIcon(card); break;
case 6: card7.setIcon(card); break;
case 7: card8.setIcon(card); break;
case 8: card9.setIcon(card); break;
case 9: card10.setIcon(card);
}//end card switch
}//end showcard

private String getCard(Card myCard)
String str_return = ".\\cards\\";
// Suits Thingy
if (myCard.suit.equals("Hearts")) str_return += "H";
if (myCard.suit.equals("Diamonds")) str_return += "D";
if (myCard.suit.equals("Clubs")) str_return += "C";
if (myCard.suit.equals("Spades")) str_return += "S";

// Face Thingy

if (myCard.face.equals("Ace")) str_return += "14";
if (myCard.face.equals("Deuce")) str_return += "2";
if (myCard.face.equals("Three")) str_return += "3";
if (myCard.face.equals("Four")) str_return += "4";
if (myCard.face.equals("Five")) str_return += "5";
if (myCard.face.equals("Six")) str_return += "6";
if (myCard.face.equals("Seven")) str_return += "7";
if (myCard.face.equals("Eight")) str_return += "8";
if (myCard.face.equals("Nine")) str_return += "9";
if (myCard.face.equals("Ten")) str_return += "10";
if (myCard.face.equals("Jack")) str_return += "11";
if (myCard.face.equals("Queen")) str_return += "12";
if (myCard.face.equals("King")) str_return += "13";

return (str_return += ".gif");
}//end getCard

// execute application
public static void main( String args[] )
poker application = new poker();

application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

} // end class poker

// class to represent a card
class Card {
public String face;
public String suit;

// constructor to initialize a card
public Card( String cardFace, String cardSuit )
face = cardFace;
suit = cardSuit;

// return String represenation of Card
public String viewcard()
return face + " of " + suit;

}//end viewcard

} // end class Card
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The class name is "poker3". In the main method you are creating an instance of "poker"; not the same class.
To check the "hand1" and "hand2" arrays you could make up a method to print out their values to the console and call it wherever you like inside your event code. For starters you could call it at the end of the shuffle event handler (shuffleButton anonymous ActionListener "actionPerformed" method) and again in the deal event handler (dealButton anonymous ActionListener "actionPerformed" method). You could try something like this to start with

or, if using j2se 1.5+ we have java.util.Arrays.toString(hand1)
Place the showHands method as an instance method of "poker3", ie, on the same level as the shuffle and showcard methods vis–a–vis inside an anonymous ActionListener class. Then anyone can call it.
A couple of other ideas:
1 — you only need to load the "card" ImageIcon one time.
2 — in the Card class you have the viewcard method that returns a String representation of the Card. If you change the name of the method to "toString" then java will automatically call this method when you use a Card instance variable by itself, eg,

This is called overriding the "toString" method.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic