Card dealing game

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2007
Posts: 1
Reputation: sportbear is an unknown quantity at this point 
Solved Threads: 0
sportbear sportbear is offline Offline
Newbie Poster

Card dealing game

 
1
  #1
May 30th, 2007
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++ ) {

}
}
}
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,493
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 520
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Card dealing game

 
0
  #2
May 30th, 2007
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:
  1. for ( int i = 0; i < SIZE_OF_HAND; i++ ) {
  2. for (Player player : players)
  3. player.addToHand(cardDeck.dealCard());
  4. }
Hope those suggestions help a bit.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Card dealing game

 
0
  #3
May 30th, 2007
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.util.*;
  4. public class DumbGame extends JApplet
  5. {
  6. private Deck cardDeck;
  7.  
  8. private final int SIZE_OF_HAND = 8;
  9. private final String directory = "cards/";
  10. private JLabel[] handLbl = new JLabel[ SIZE_OF_HAND ];
  11.  
  12. public void init()
  13. {
  14. getRootPane().putClientProperty ( "defeatSystemEventQueueCheck", Boolean.TRUE );
  15.  
  16. getContentPane().setLayout ( null );
  17. getContentPane().setBackground ( java.awt.Color.black );
  18. setSize ( 881, 203 );
  19. JLabel1.setHorizontalTextPosition ( javax.swing.SwingConstants.CENTER );
  20. JLabel1.setVerticalTextPosition ( javax.swing.SwingConstants.BOTTOM );
  21. JLabel1.setHorizontalAlignment ( javax.swing.SwingConstants.CENTER );
  22. JLabel1.setText ( "Card" );
  23. JLabel1.setOpaque ( true );
  24. JLabel1.setToolTipText ( "Dit is een kaart." );
  25. getContentPane().add ( JLabel1 );
  26. JLabel1.setForeground ( java.awt.Color.black );
  27. JLabel1.setFont ( new Font ( "Dialoog", Font.BOLD, 10 ) );
  28. JLabel1.setBounds ( 12, 135, 101, 125 );
  29. JLabel2.setHorizontalTextPosition ( javax.swing.SwingConstants.CENTER );
  30. JLabel2.setVerticalTextPosition ( javax.swing.SwingConstants.BOTTOM );
  31. JLabel2.setHorizontalAlignment ( javax.swing.SwingConstants.CENTER );
  32. JLabel2.setText ( "Card" );
  33. JLabel2.setOpaque ( true );
  34. JLabel2.setToolTipText ( "Dit is een kaart." );
  35. getContentPane().add ( JLabel2 );
  36. JLabel2.setForeground ( java.awt.Color.black );
  37. JLabel2.setFont ( new Font ( "Dialoog", Font.BOLD, 10 ) );
  38. JLabel2.setBounds ( 120, 3, 101, 125 );
  39. JLabel3.setHorizontalTextPosition ( javax.swing.SwingConstants.CENTER );
  40. JLabel3.setVerticalTextPosition ( javax.swing.SwingConstants.BOTTOM );
  41. JLabel3.setHorizontalAlignment ( javax.swing.SwingConstants.CENTER );
  42. JLabel3.setText ( "Card" );
  43. JLabel3.setOpaque ( true );
  44. JLabel3.setToolTipText ( "Dit is een kaart." );
  45. getContentPane().add ( JLabel3 );
  46. JLabel3.setForeground ( java.awt.Color.black );
  47. JLabel3.setFont ( new Font ( "Dialoog", Font.BOLD, 10 ) );
  48. JLabel3.setBounds ( 228, 135, 101, 125 );
  49. JLabel4.setHorizontalTextPosition ( javax.swing.SwingConstants.CENTER );
  50. JLabel4.setVerticalTextPosition ( javax.swing.SwingConstants.BOTTOM );
  51. JLabel4.setHorizontalAlignment ( javax.swing.SwingConstants.CENTER );
  52. JLabel4.setText ( "Card" );
  53. JLabel4.setOpaque ( true );
  54. JLabel4.setToolTipText ( "Dit is een kaart." );
  55. getContentPane().add ( JLabel4 );
  56. JLabel4.setForeground ( java.awt.Color.black );
  57. JLabel4.setFont ( new Font ( "Dialoog", Font.BOLD, 10 ) );
  58. JLabel4.setBounds ( 120, 265, 101, 125 );
  59. JButton1.setText ( "Trek een hand" );
  60. JButton1.setActionCommand ( "Trek een hand" );
  61. getContentPane().add ( JButton1 );
  62. JButton1.setBounds ( 62, 420, 212, 32 );
  63. scoreLbl.setToolTipText ( " " );
  64. getContentPane().add ( scoreLbl );
  65. scoreLbl.setForeground ( java.awt.Color.white );
  66. scoreLbl.setBounds ( 240, 156, 200, 31 );
  67. //}}
  68.  
  69. // add the JLabel array mapping here
  70. handLbl[0] = JLabel1;
  71. handLbl[1] = JLabel2;
  72. handLbl[2] = JLabel3;
  73. handLbl[3] = JLabel4;
  74.  
  75. // add the Card instantiations here
  76. cardDeck = new Deck();
  77. cardDeck.shuffle();
  78.  
  79. Iterator suitIterator = Suit.VALUES.iterator();
  80. while ( suitIterator.hasNext() )
  81. {
  82. Suit suit = ( Suit ) suitIterator.next();
  83. Iterator rankIterator = Rank.VALUES.iterator();
  84. while ( rankIterator.hasNext() )
  85. {
  86. Rank rank = ( Rank ) rankIterator.next();
  87. String imageFile = directory + Card.getFilename ( suit, rank );
  88. ImageIcon cardImage = new ImageIcon ( getImage ( getCodeBase(), imageFile ) );
  89. Card card = new Card ( suit, rank, cardImage );
  90. cardDeck.addCard ( card );
  91. }
  92. }
  93.  
  94. // set up the initial hand
  95.  
  96. for ( int i = 0; i < SIZE_OF_HAND; i++ )
  97. {
  98. Card drawnCard = cardDeck.dealCard();
  99. }
  100.  
  101. // add the displaying of cards here
  102. for ( int i = 0; i < SIZE_OF_HAND; i++ )
  103. {
  104. }
  105.  
  106. //{{REGISTER_LISTENERS
  107. SymAction lSymAction = new SymAction();
  108. JButton1.addActionListener ( lSymAction );
  109. JButton2.addActionListener ( lSymAction );
  110. JButton3.addActionListener ( lSymAction );
  111. //}}
  112. }
  113. //{{DECLARE_CONTROLS
  114. javax.swing.JLabel JLabel1 = new javax.swing.JLabel();
  115. javax.swing.JLabel JLabel2 = new javax.swing.JLabel();
  116. javax.swing.JLabel JLabel3 = new javax.swing.JLabel();
  117. javax.swing.JLabel JLabel4 = new javax.swing.JLabel();
  118.  
  119. javax.swing.JButton JButton1 = new javax.swing.JButton();
  120. javax.swing.JButton JButton2 = new javax.swing.JButton();
  121.  
  122. javax.swing.JLabel scoreLbl = new javax.swing.JLabel();
  123. javax.swing.JButton JButton3 = new javax.swing.JButton();
  124. //}}
  125. class SymAction implements java.awt.event.ActionListener
  126. {
  127. public void actionPerformed ( java.awt.event.ActionEvent event )
  128. {
  129. Object object = event.getSource();
  130. if ( object == JButton1 )
  131. JButton1_actionPerformed ( event );
  132. else if ( object == JButton2 )
  133. JButton2_actionPerformed ( event );
  134. else if ( object == JButton3 )
  135. JButton3_actionPerformed ( event );
  136. }
  137. }
  138. void JButton1_actionPerformed ( java.awt.event.ActionEvent event )
  139. {
  140. // to do: code goes here.
  141. cardDeck.restoreDeck();
  142. cardDeck.shuffle();
  143. for ( int i = 0; i < SIZE_OF_HAND; i++ )
  144. {
  145. Card c = cardDeck.dealCard();
  146. handLbl[i].setIcon ( c.getCardImage() );
  147. handLbl[i].setText ( c.toString() );
  148. }
  149. }
  150. void JButton2_actionPerformed ( java.awt.event.ActionEvent event )
  151. {
  152. Card.setRankMajorSort();
  153. for ( int i = 0; i < SIZE_OF_HAND; i++ )
  154. {
  155. }
  156. }
  157.  
  158. void JButton3_actionPerformed ( java.awt.event.ActionEvent event )
  159. {
  160. Card.setSuitMajorSort();
  161. for ( int i = 0; i < SIZE_OF_HAND; i++ )
  162. {
  163. }
  164. }
  165. }

>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?
Last edited by iamthwee; May 30th, 2007 at 5:03 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC