I'm just trying to make a four-player card dealing game! But my problem is that my deck is eternal! I wan't it to deal 52 cards. So 13 card per person! The name of my persons are JLabel1,2,3,4! Can someone help me out! I'm working with Blue J and I started last week!

PS: I think the problem is somewhere in this code, but the program needs about 5 other files with codes to work! But I hope someone can help me out here.

import java.awt.*;
import javax.swing.*;
import java.util.*;
public class DumbGame extends JApplet
{
    private Deck cardDeck;




    private final int SIZE_OF_HAND = 8;
    private final String directory = "cards/";
    private JLabel[] handLbl = new JLabel[ SIZE_OF_HAND ];

 public void init()
 {
  getRootPane().putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);

  getContentPane().setLayout(null);
  getContentPane().setBackground(java.awt.Color.black);
  setSize(881,203);
  JLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
  JLabel1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
  JLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  JLabel1.setText("Card");
  JLabel1.setOpaque(true);
  JLabel1.setToolTipText("Dit is een kaart.");
  getContentPane().add(JLabel1);
  JLabel1.setForeground(java.awt.Color.black);
  JLabel1.setFont(new Font("Dialoog", Font.BOLD, 10));
  JLabel1.setBounds(12,135,101,125);
  JLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
  JLabel2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
  JLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  JLabel2.setText("Card");
  JLabel2.setOpaque(true);
  JLabel2.setToolTipText("Dit is een kaart.");
  getContentPane().add(JLabel2);
  JLabel2.setForeground(java.awt.Color.black);
  JLabel2.setFont(new Font("Dialoog", Font.BOLD, 10));
  JLabel2.setBounds(120,3,101,125);
  JLabel3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
  JLabel3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
  JLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  JLabel3.setText("Card");
  JLabel3.setOpaque(true);
  JLabel3.setToolTipText("Dit is een kaart.");
  getContentPane().add(JLabel3);
  JLabel3.setForeground(java.awt.Color.black);
  JLabel3.setFont(new Font("Dialoog", Font.BOLD, 10));
  JLabel3.setBounds(228,135,101,125);
  JLabel4.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
  JLabel4.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
  JLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  JLabel4.setText("Card");
  JLabel4.setOpaque(true);
  JLabel4.setToolTipText("Dit is een kaart.");
  getContentPane().add(JLabel4);
  JLabel4.setForeground(java.awt.Color.black);
  JLabel4.setFont(new Font("Dialoog", Font.BOLD, 10));
  JLabel4.setBounds(120,265,101,125);
  JButton1.setText("Trek een hand");
  JButton1.setActionCommand("Trek een hand");
  getContentPane().add(JButton1);
  JButton1.setBounds(62,420,212,32);
  scoreLbl.setToolTipText(" ");
  getContentPane().add(scoreLbl);
  scoreLbl.setForeground(java.awt.Color.white);
  scoreLbl.setBounds(240,156,200,31);
  //}}

  // add the JLabel array mapping here
  handLbl[0] = JLabel1;
  handLbl[1] = JLabel2;
  handLbl[2] = JLabel3;
  handLbl[3] = JLabel4;


  // add the Card instantiations here
  cardDeck = new Deck();
        cardDeck.shuffle();

  Iterator suitIterator = Suit.VALUES.iterator();
  while ( suitIterator.hasNext() ) {
      Suit suit = (Suit) suitIterator.next();
      Iterator rankIterator = Rank.VALUES.iterator();
      while ( rankIterator.hasNext() ) {
          Rank rank = (Rank) rankIterator.next();
          String imageFile = directory + Card.getFilename( suit, rank );
          ImageIcon cardImage = new ImageIcon( getImage( getCodeBase(), imageFile ) );
          Card card = new Card( suit, rank, cardImage );
          cardDeck.addCard( card );
      }
  }

  // set up the initial hand

  for ( int i = 0; i < SIZE_OF_HAND; i++ ) {
      Card drawnCard = cardDeck.dealCard();
  }

  // add the displaying of cards here
  for ( int i = 0; i < SIZE_OF_HAND; i++ ) {

  }



  //{{REGISTER_LISTENERS
  SymAction lSymAction = new SymAction();
  JButton1.addActionListener(lSymAction);
  JButton2.addActionListener(lSymAction);
  JButton3.addActionListener(lSymAction);
  //}}
 }
 //{{DECLARE_CONTROLS
 javax.swing.JLabel JLabel1 = new javax.swing.JLabel();
 javax.swing.JLabel JLabel2 = new javax.swing.JLabel();
 javax.swing.JLabel JLabel3 = new javax.swing.JLabel();
 javax.swing.JLabel JLabel4 = new javax.swing.JLabel();

 javax.swing.JButton JButton1 = new javax.swing.JButton();
 javax.swing.JButton JButton2 = new javax.swing.JButton();

 javax.swing.JLabel scoreLbl = new javax.swing.JLabel();
 javax.swing.JButton JButton3 = new javax.swing.JButton();
 //}}
 class SymAction implements java.awt.event.ActionListener
 {
  public void actionPerformed(java.awt.event.ActionEvent event)
  {
   Object object = event.getSource();
   if (object == JButton1)
    JButton1_actionPerformed(event);
   else if (object == JButton2)
    JButton2_actionPerformed(event);
   else if (object == JButton3)
       JButton3_actionPerformed(event);
  }
 }
 void JButton1_actionPerformed(java.awt.event.ActionEvent event)
 {
  // to do: code goes here.
  cardDeck.restoreDeck();
  cardDeck.shuffle();
  for ( int i = 0; i < SIZE_OF_HAND; i++ ) {
     Card c = cardDeck.dealCard();
     handLbl[i].setIcon( c.getCardImage() );
     handLbl[i].setText( c.toString() );
     }
 }
 void JButton2_actionPerformed(java.awt.event.ActionEvent event)
 {
  Card.setRankMajorSort();
  for ( int i = 0; i < SIZE_OF_HAND; i++ ) {

     }      
 }

 void JButton3_actionPerformed(java.awt.event.ActionEvent event)
 {
  Card.setSuitMajorSort();
  for ( int i = 0; i < SIZE_OF_HAND; i++ ) {

     }      
 }
}
iamthwee commented: good +9

Well, since you don't include the Deck class, it's difficult to see the whole domain, but a couple of things stand out.

First, make the Deck class responsible for it's own Card collection. Your applet shouldn't have to push things into the Deck. The Deck will have a maximum of 52 cards, which can be obtained from Deck.dealCard() until no more cards remain in the Deck.

Second, I would create a Player class to keep track of each players current hand and perhaps a Game class to contain the rules of the current game, such as number of cards per player, etc. This would allow for different games within the applet if you want to add them later.

Whether you choose to put it in a game class or keep it in the main applet class, a method dealNewGame() could iterate the number of cards and a collection of players as follows:

for ( int i = 0; i < SIZE_OF_HAND; i++ ) {
    for (Player player : players)
        player.addToHand(cardDeck.dealCard());
}

Hope those suggestions help a bit.

Member Avatar for iamthwee
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class DumbGame extends JApplet
{
   private Deck cardDeck;

   private final int SIZE_OF_HAND = 8;
   private final String directory = "cards/";
   private JLabel[] handLbl = new JLabel[ SIZE_OF_HAND ];

   public void init()
   {
      getRootPane().putClientProperty ( "defeatSystemEventQueueCheck", Boolean.TRUE );

      getContentPane().setLayout ( null );
      getContentPane().setBackground ( java.awt.Color.black );
      setSize ( 881, 203 );
      JLabel1.setHorizontalTextPosition ( javax.swing.SwingConstants.CENTER );
      JLabel1.setVerticalTextPosition ( javax.swing.SwingConstants.BOTTOM );
      JLabel1.setHorizontalAlignment ( javax.swing.SwingConstants.CENTER );
      JLabel1.setText ( "Card" );
      JLabel1.setOpaque ( true );
      JLabel1.setToolTipText ( "Dit is een kaart." );
      getContentPane().add ( JLabel1 );
      JLabel1.setForeground ( java.awt.Color.black );
      JLabel1.setFont ( new Font ( "Dialoog", Font.BOLD, 10 ) );
      JLabel1.setBounds ( 12, 135, 101, 125 );
      JLabel2.setHorizontalTextPosition ( javax.swing.SwingConstants.CENTER );
      JLabel2.setVerticalTextPosition ( javax.swing.SwingConstants.BOTTOM );
      JLabel2.setHorizontalAlignment ( javax.swing.SwingConstants.CENTER );
      JLabel2.setText ( "Card" );
      JLabel2.setOpaque ( true );
      JLabel2.setToolTipText ( "Dit is een kaart." );
      getContentPane().add ( JLabel2 );
      JLabel2.setForeground ( java.awt.Color.black );
      JLabel2.setFont ( new Font ( "Dialoog", Font.BOLD, 10 ) );
      JLabel2.setBounds ( 120, 3, 101, 125 );
      JLabel3.setHorizontalTextPosition ( javax.swing.SwingConstants.CENTER );
      JLabel3.setVerticalTextPosition ( javax.swing.SwingConstants.BOTTOM );
      JLabel3.setHorizontalAlignment ( javax.swing.SwingConstants.CENTER );
      JLabel3.setText ( "Card" );
      JLabel3.setOpaque ( true );
      JLabel3.setToolTipText ( "Dit is een kaart." );
      getContentPane().add ( JLabel3 );
      JLabel3.setForeground ( java.awt.Color.black );
      JLabel3.setFont ( new Font ( "Dialoog", Font.BOLD, 10 ) );
      JLabel3.setBounds ( 228, 135, 101, 125 );
      JLabel4.setHorizontalTextPosition ( javax.swing.SwingConstants.CENTER );
      JLabel4.setVerticalTextPosition ( javax.swing.SwingConstants.BOTTOM );
      JLabel4.setHorizontalAlignment ( javax.swing.SwingConstants.CENTER );
      JLabel4.setText ( "Card" );
      JLabel4.setOpaque ( true );
      JLabel4.setToolTipText ( "Dit is een kaart." );
      getContentPane().add ( JLabel4 );
      JLabel4.setForeground ( java.awt.Color.black );
      JLabel4.setFont ( new Font ( "Dialoog", Font.BOLD, 10 ) );
      JLabel4.setBounds ( 120, 265, 101, 125 );
      JButton1.setText ( "Trek een hand" );
      JButton1.setActionCommand ( "Trek een hand" );
      getContentPane().add ( JButton1 );
      JButton1.setBounds ( 62, 420, 212, 32 );
      scoreLbl.setToolTipText ( " " );
      getContentPane().add ( scoreLbl );
      scoreLbl.setForeground ( java.awt.Color.white );
      scoreLbl.setBounds ( 240, 156, 200, 31 );
//}}

// add the JLabel array mapping here
      handLbl[0] = JLabel1;
      handLbl[1] = JLabel2;
      handLbl[2] = JLabel3;
      handLbl[3] = JLabel4;

// add the Card instantiations here
      cardDeck = new Deck();
      cardDeck.shuffle();

      Iterator suitIterator = Suit.VALUES.iterator();
      while ( suitIterator.hasNext() )
      {
         Suit suit = ( Suit ) suitIterator.next();
         Iterator rankIterator = Rank.VALUES.iterator();
         while ( rankIterator.hasNext() )
         {
            Rank rank = ( Rank ) rankIterator.next();
            String imageFile = directory + Card.getFilename ( suit, rank );
            ImageIcon cardImage = new ImageIcon ( getImage ( getCodeBase(), imageFile ) );
            Card card = new Card ( suit, rank, cardImage );
            cardDeck.addCard ( card );
         }
      }

// set up the initial hand

      for ( int i = 0; i < SIZE_OF_HAND; i++ )
      {
         Card drawnCard = cardDeck.dealCard();
      }

// add the displaying of cards here
      for ( int i = 0; i < SIZE_OF_HAND; i++ )
      {
      }

//{{REGISTER_LISTENERS
      SymAction lSymAction = new SymAction();
      JButton1.addActionListener ( lSymAction );
      JButton2.addActionListener ( lSymAction );
      JButton3.addActionListener ( lSymAction );
//}}
   }
//{{DECLARE_CONTROLS
   javax.swing.JLabel JLabel1 = new javax.swing.JLabel();
   javax.swing.JLabel JLabel2 = new javax.swing.JLabel();
   javax.swing.JLabel JLabel3 = new javax.swing.JLabel();
   javax.swing.JLabel JLabel4 = new javax.swing.JLabel();

   javax.swing.JButton JButton1 = new javax.swing.JButton();
   javax.swing.JButton JButton2 = new javax.swing.JButton();

   javax.swing.JLabel scoreLbl = new javax.swing.JLabel();
   javax.swing.JButton JButton3 = new javax.swing.JButton();
//}}
   class SymAction implements java.awt.event.ActionListener
   {
      public void actionPerformed ( java.awt.event.ActionEvent event )
      {
         Object object = event.getSource();
         if ( object == JButton1 )
            JButton1_actionPerformed ( event );
         else if ( object == JButton2 )
            JButton2_actionPerformed ( event );
         else if ( object == JButton3 )
            JButton3_actionPerformed ( event );
      }
   }
   void JButton1_actionPerformed ( java.awt.event.ActionEvent event )
   {
// to do: code goes here.
      cardDeck.restoreDeck();
      cardDeck.shuffle();
      for ( int i = 0; i < SIZE_OF_HAND; i++ )
      {
         Card c = cardDeck.dealCard();
         handLbl[i].setIcon ( c.getCardImage() );
         handLbl[i].setText ( c.toString() );
      }
   }
   void JButton2_actionPerformed ( java.awt.event.ActionEvent event )
   {
      Card.setRankMajorSort();
      for ( int i = 0; i < SIZE_OF_HAND; i++ )
      {
      }
   }

   void JButton3_actionPerformed ( java.awt.event.ActionEvent event )
   {
      Card.setSuitMajorSort();
      for ( int i = 0; i < SIZE_OF_HAND; i++ )
      {
      }
   }
}

>But my problem is that my deck is eternal! I wan't it to deal 52 cards. So 13 card per person!

Well wouldn't it be something of the lines of deal 13 cards times by four.

Where is this in your code thus far. I can't see it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.