943,969 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1347
  • Java RSS
May 30th, 2007
1

Card dealing game

Expand Post »
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++ ) {

}
}
}
Similar Threads
Reputation Points: 19
Solved Threads: 0
Newbie Poster
sportbear is offline Offline
1 posts
since May 2007
May 30th, 2007
0

Re: Card dealing game

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:
Java Syntax (Toggle Plain Text)
  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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
May 30th, 2007
0

Re: Card dealing game

Quote ...
Java Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: java compilers
Next Thread in Java Forum Timeline: java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC