Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: BlackJack Game can"t figure out how to finsh it.

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default BlackJack Game can"t figure out how to finsh it.

    I have the Buttons, radiobuttons and text in place and display 4 cards but what I can't get working after seeing multiple examples is:

    - deal button to display the first 2 cards for dealer and you.
    - hit draws your card
    - stay activates the dealer to draw his cards
    - new Deck starts a new game with a new set of random cards.

    I can't figure out how to get the cards to do this from what I already have in the code.

    any help would be appreciated


    Main Class
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.JOptionPane;
    import java.util.Scanner;
    import java.io.*;
     
    public class BlackJackPanel extends JPanel
    {
       // declare data
       private int bets;
       private int cash = 0;
       private JLabel cashLabel, DcardLabel, UcardLabel, moneyLabel, spaceLabel, winLabel, lossLabel;
       private JButton deal, hit, stay, newDeck;
       private int face;
       private int suit;
       private String faceName;
       private String suitName;
       private int game;
       private int win;
       private int lose;
       private int dealerTot;
       private int yourTot;
       private final int BLACKJACK = 21;
       private int value;
       private int valueTot;
     
       // create a Scanner object called scan for keyboard input
       static Scanner scan = new Scanner(System.in);
     
       // create radio buttons, labels, text areas, scroll panes
       // listeners, groups and panels
       JRadioButton fiveB, tenB, twentyFiveB, fiftyB, hundredB;
       ButtonListener listen;
       ButtonGroup group;
       JPanel panel, centerPanel, eastPanel, westPanel;
     
       // create places for the Card objects, imageicons and JLabels
       Card c1, c2, c3, c4, c5 ;
       ImageIcon iconc1, iconc2, iconc3, iconc4;
       JLabel label1,label2, label3, label4, label5;
     
       public BlackJackPanel()
       {
          // create the radio button group
          group = new ButtonGroup();
     
          // create the labels
          DcardLabel = new JLabel ("Dealer Cards:" + valueTot);
          UcardLabel = new JLabel ("Your Cards: " + yourTot);
          cashLabel  = new JLabel ("Cash:");
          moneyLabel = new JLabel ("" + cash);
          spaceLabel = new JLabel ("");
          winLabel   = new JLabel ("Wins: "+ win);
          lossLabel  = new JLabel ("          Loss: " + lose);
     
    	  // create each card instance or object
    	  c1 = new Card(); // dealer
    	  c2 = new Card(); // player
    	  c3 = new Card(); // dealer
    	  c4 = new Card(); // player
     
          // create icons for each image using the getCardName () method
    	  iconc1 = new ImageIcon(c1.getCardName());
          iconc2 = new ImageIcon(c2.getCardName());
    	  iconc3 = new ImageIcon(c3.getCardName());
    	  iconc4 = new ImageIcon(c4.getCardName());
     
    	  // Create labels for each card
    	  label1 = new JLabel (c1.toString(), iconc1,SwingConstants.CENTER);
    	  label2 = new JLabel (c2.toString(), iconc2,SwingConstants.CENTER);
    	  label5 = new JLabel ();
    	  label3 = new JLabel (c3.toString(), iconc3,SwingConstants.CENTER);
    	  label4 = new JLabel (c4.toString(), iconc4,SwingConstants.CENTER);
     
    	  // Create a panel, set the background color, set the size and add the panels
    	  JPanel primary = new JPanel();
    	  JPanel panel1  = new JPanel();
          JPanel panel5  = new JPanel();
    	  JPanel panel7  = new JPanel();
    	  JPanel panel2  = new JPanel();
     
    	  // Sets Background
    	  primary.setBackground (Color.green   );
    	  panel1.setBackground  (Color.red     );
          panel5.setBackground  (Color.green  );
    	  panel7.setBackground  (Color.green );
    	  panel2.setBackground  (Color.yellow  );
     
    	  // Sets pefered Size
    	  primary.setPreferredSize (new Dimension(1000, 650));
    	  panel1.setPreferredSize  (new Dimension(800, 200 ));
          panel5.setPreferredSize  (new Dimension(800, 100 ));
    	  panel7.setPreferredSize  (new Dimension(800, 125 ));
    	  panel2.setPreferredSize  (new Dimension(800, 200 ));
     
    	  // Adds panel to labels
    	  panel1.add (label1);
    	  panel1.add (label2);
    	  panel5.add (label5);
    	  panel5.add (winLabel);
    	  panel7.add (lossLabel);
    	  panel2.add (label3);
    	  panel2.add (label4);
     
          // Sets panel 5 layout
          panel5.setLayout(new GridLayout (1, 0));
     
    	  // Add panels to primary
    	  primary.add (panel1);
          primary.add (panel5);
    	  primary.add (panel7);
    	  primary.add (panel2);
     
          // Sets position of card and name
    	  label1.setHorizontalTextPosition (SwingConstants.CENTER);
    	  label1.setVerticalTextPosition   (SwingConstants.BOTTOM);
     
    	  label2.setHorizontalTextPosition (SwingConstants.CENTER);
    	  label2.setVerticalTextPosition   (SwingConstants.BOTTOM);
     
    	  label3.setHorizontalTextPosition (SwingConstants.CENTER);
    	  label3.setVerticalTextPosition   (SwingConstants.BOTTOM);
     
    	  label4.setHorizontalTextPosition (SwingConstants.CENTER);
    	  label4.setVerticalTextPosition   (SwingConstants.BOTTOM);
     
          ButtonListener listener = new ButtonListener();
     
          // Creates buttons and labels for them
          deal = new JButton ("Deal");
          deal.addActionListener (new ButtonListener());
     
          hit = new JButton ("Hit");
          hit.addActionListener (new ButtonListener());
     
          stay = new JButton ("Stay");
          stay.addActionListener (new ButtonListener());
     
          newDeck = new JButton ("New Deck");
          newDeck.addActionListener (new ButtonListener());
     
          // create main panel, color, layout and dimensions
          panel = new JPanel();
          panel.setBackground (Color.green);
          panel.setPreferredSize(new Dimension (1000, 650));
          panel.setLayout(new BorderLayout());
     
          // create the center panel, color and JScrollPanel(ta)
          centerPanel = new JPanel();
          centerPanel.setBackground(Color.green);
          centerPanel.add (panel1);
          centerPanel.add (panel5);
          centerPanel.add (panel7);
          centerPanel.add (panel2);
     
     
          // create the east panel with color
          eastPanel = new JPanel();
          eastPanel.setBackground (Color.lightGray);
          eastPanel.setLayout(new GridLayout (6, 3, 5, 50));
          eastPanel.add (spaceLabel);
          eastPanel.add (deal);
          eastPanel.add (hit);
          eastPanel.add (stay);
          eastPanel.add (newDeck);
     
          // create the west panel with color and Box layout
          westPanel = new JPanel();
          westPanel.setBackground (Color.lightGray);
          westPanel.setLayout (new BoxLayout (westPanel, BoxLayout.Y_AXIS));
     
          westPanel.add (DcardLabel);
          westPanel.add (UcardLabel);
          westPanel.add (Box.createRigidArea (new Dimension (0, 10)));
     
          // create the radio button group listener
          listen = new ButtonListener();
     
          // create all the radio buttons, including listener, color and group
          fiveB = new JRadioButton ("5");
          fiveB.addActionListener(listen);
          fiveB.setBackground (Color.lightGray);
          group.add(fiveB);
     
          tenB = new JRadioButton ("10");
          tenB.addActionListener(listen);
          tenB.setBackground (Color.lightGray);
          group.add(tenB);
     
          twentyFiveB = new JRadioButton ("25");
          twentyFiveB.addActionListener(listen);
          twentyFiveB.setBackground (Color.lightGray);
          group.add(twentyFiveB);
     
          fiftyB = new JRadioButton ("50");
          fiftyB.addActionListener(listen);
          fiftyB.setBackground (Color.lightGray);
          group.add(fiftyB);
     
          hundredB = new JRadioButton ("100");
          hundredB.addActionListener(listen);
          hundredB.setBackground (Color.lightGray);
          group.add(hundredB);
     
          // place radio button group in west panel
          westPanel.add (Box.createRigidArea (new Dimension (10, 450)));
          westPanel.add (cashLabel);
          westPanel.add (moneyLabel);
          westPanel.add(fiveB);
          westPanel.add(tenB);
          westPanel.add(twentyFiveB);
          westPanel.add(fiftyB);
          westPanel.add(hundredB);
     
          // place all panels in the Border Layout positions
          panel.add(centerPanel, BorderLayout.CENTER);
          panel.add(westPanel, BorderLayout.WEST);
          panel.add(eastPanel, BorderLayout.EAST);
     
          // add the main panel
          add (panel);
     
     
     
     
     
          if (valueTot == BLACKJACK)
          {
    		  game = win;
          }
          else
          {
    		  game = lose;
          }
     
     
     
     
     
     
     
     
       }
       //Button listeners and Actions Perferomed
       private class ButtonListener implements ActionListener
       {
          public void actionPerformed(ActionEvent event)
          {
    		 // Attempt for button actions
             if (event.getSource() == deal)
             {
     
     
     
    	     }
     
             // If the game is won or lost
             if (dealerTot == BLACKJACK || yourTot == BLACKJACK)
             {
    			 cash = 0;
     
    	     }
    	     else if (dealerTot == BLACKJACK)
    	     {
    			 game = lose;
    	     }
    		 else if (yourTot == BLACKJACK)
    		 {
    			 game = win;
    	     }
             // check for which radio button was clicked
     
    	  	 // is event fiveB
    	     if (event.getSource() == fiveB)
    	     {
                // bets = 5 dollors
                if (game == win)
                {
    			   cash =+ 5;
     
    		    }
    			else
    			{
    			   cash =- 5;
     
    		    }
     
    	     }
    	     // else is event tenB
    	     else if (event.getSource() == tenB)
    	     {
                // bets = 10 dollors
                if (game == win)
    			{
    			   bets =+ 10;
     
    	        }
    			else
    			{
    			   bets =- 10;
     
    		    }
    	     }
     
    	     // else is event twentyFiveB
    	     else if (event.getSource() == twentyFiveB)
    	     {
                // bets = 25 dollors
                if (game == win)
    		 	{
    		 	   bets =+ 25;
     
    		 	}
    		 	else
    		 	{
    		       bets =- 25;
     
    		    }
    	     }
             // else is event fiftyB
    	     else if (event.getSource() == fiftyB)
    	     {
                // bets = 50 dollors
                if (game == win)
    			{
    			   bets =+ 50;
     
    			}
    			else
    			{
    			   bets =- 50;
     
    		    }
    	     }
    	     // else is event hundredB
    	     else if (event.getSource() == hundredB)
    	     {
                // bets = 100 dollors
                if (game == win)
    			{
    			   bets =+ 100;
     
    			}
    			else
    			{
    			   bets =- 100;
     
    		    }
    	     }
     
          cash = bets;
          }
          public void Card ()
    	     {
    	          // Create a random value for the face and suit values
    	          face = (int) (Math.random() * 13 + 1);
     
    	          suit = (int) (Math.random() * 4 + 1);
     
    	          // Create the names for the face and suit of the card
    	          setFaceName();
     
    	          setSuitName();
     
    	     }
     
    	     //-----------------------------------------------------------------
    	     //  Sets the string representation of the face using a switch statement
    	     //  with cases for each numeric value 1-13.
    	     //-----------------------------------------------------------------
    	     private void setFaceName()
    	     {
    	         switch (face)
    	         {
    	              case 1:  value = 1;  faceName = "Ace of";
                      break;
     
    	              case 2:  value = 2; faceName = "Two of"  ;
    	              break;
     
    	              case 3:  value = 3; faceName = "Three of";
    	              break;
     
    	              case 4:  value = 4; faceName = "Four of" ;
    	              break;
     
    	              case 5:  value = 5; faceName = "Five of" ;
    	              break;
     
    	              case 6:  value = 6; faceName = "Six of"  ;
    	              break;
     
    	              case 7:  value = 7; faceName = "Seven of";
    	              break;
     
    	              case 8:  value = 8; faceName = "Eight of";
    	              break;
     
    	              case 9:  value = 9; faceName = "Nine of" ;
    	              break;
     
    	              case 10: value = 10; faceName = "Ten of"  ;
    	              break;
     
    	              case 11: value = 11; faceName = "Jack of" ;
    	              break;
     
    	              case 12: value = 12; faceName = "Queen of";
    	              break;
     
    	              case 13: value = 13; faceName = "King of" ;
    	              break;
     
     
     
    	         }
    	     }
     
    	     //-----------------------------------------------------------------
    	     //  Sets the string representation of the suit using a switch statement
    	     //  with cases for each numeric value 1-4.
    	     //-----------------------------------------------------------------
    	     private void setSuitName()
    	     {
    	         switch (suit)
    	         {
    	             case 1: suitName = " Clubs"   ; break;
     
    	             case 2: suitName = " Diamonds"; break;
     
    	             case 3: suitName = " Hearts"  ; break;
     
    	             case 4: suitName = " Spades"  ; break;
    	         }
    	     }
     
    	     //-----------------------------------------------------------------
    	     //  Returns the face (numeric value) of this card.
    	     //-----------------------------------------------------------------
    	     public int getFace ()
    	     {
    	         return face;
    	     }
     
    	     //-----------------------------------------------------------------
    	     //  Returns the suit (numeric value) of this card.
    	     //-----------------------------------------------------------------
    	     public int getSuit ()
    	     {
    	         return suit;
    	     }
     
    	     //-----------------------------------------------------------------
    	     //  Returns the string representation of this card, including
    	     //  both face and suit.
    	     //-----------------------------------------------------------------
    	     public String toString ()
    	     {
     
    	         return ( faceName + suitName);
     
    	     }
     
    	     //-----------------------------------------------------------------
    	     //  Returns the name of the card image
    	     //-----------------------------------------------------------------
    	     public String getCardName ()
    	     {
    	         return "Cards/" + faceName + suitName +".gif";
    	     }
       }
    }

    Helper class
    import javax.swing.JFrame;
     
    public class BlackJack
    {
       //-----------------------------------------------------------------
       //  Creates and displays the BlackJack Game GUI.
       //-----------------------------------------------------------------
       public static void main (String[] args)
       {
          JFrame frame = new JFrame ("BlackJack");
          frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
     
          BlackJackPanel panel = new BlackJackPanel();
     
     
          frame.getContentPane().add(panel);
          frame.pack();
          frame.setVisible(true);
       }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

    Default Re: BlackJack Game can"t figure out how to finsh it.

    Can't get the program to compile. Need the Card class definition.

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: BlackJack Game can"t figure out how to finsh it.

    what do you mean by Card class definition. the class files for them or the cards I used with the program?
    Last edited by flurr; May 12th, 2011 at 07:11 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

    Default Re: BlackJack Game can"t figure out how to finsh it.

    The code you posted does NOT have a definition for the Card class.
    There needs to be a statement like this:
    class Card ....

    You have a method named Card that looks like a constructor.

    I editted your code to make a Card class and am able to compile and execute it.

    The code you posted does NOT COMPILE. Please post the correct version of your code.
    Last edited by Norm; May 12th, 2011 at 07:34 PM.

  5. #5
    Junior Member
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: BlackJack Game can"t figure out how to finsh it.

    I am using textpad and it compiles and run for me:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.JOptionPane;
    import java.util.Scanner;
    import java.io.*;
     
    public class BlackJackPanel extends JPanel
    {
       // declare data
       private int bets;
       private int cash = 0;
       private JLabel cashLabel, DcardLabel, UcardLabel, moneyLabel, spaceLabel, winLabel, lossLabel;
       private JButton deal, hit, stay, newDeck;
       private int face;
       private int suit;
       private String faceName;
       private String suitName;
       private int game;
       private int win;
       private int lose;
       private int dealerTot;
       private int yourTot;
       private final int BLACKJACK = 21;
       private int value;
       private int valueTot;
     
       // create a Scanner object called scan for keyboard input
       static Scanner scan = new Scanner(System.in);
     
     
       // create radio buttons, labels, text areas, scroll panes
       // listeners, groups and panels
       JRadioButton fiveB, tenB, twentyFiveB, fiftyB, hundredB;
       ButtonListener listen;
       ButtonGroup group;
       JPanel panel, centerPanel, eastPanel, westPanel;
     
       // create places for the Card objects, imageicons and JLabels
       Card c1, c2, c3, c4, c5 ;
       ImageIcon iconc1, iconc2, iconc3, iconc4;
       JLabel label1,label2, label3, label4, label5;
     
     
       public BlackJackPanel()
       {
          // create the radio button group
          group = new ButtonGroup();
     
          // create the labels
          DcardLabel = new JLabel ("Dealer Cards:" + dealerTot);
          UcardLabel = new JLabel ("Your Cards: " +yourTot );
          cashLabel  = new JLabel ("Cash:");
          moneyLabel = new JLabel ("" + cash);
          spaceLabel = new JLabel ("");
          winLabel   = new JLabel ("Wins: " + win);
          lossLabel  = new JLabel ("          Loses: " + lose);
     
    	  // create each card instance or object
    	  c1 = new Card(); // dealer
    	  c2 = new Card(); // player
    	  c3 = new Card(); // dealer
    	  c4 = new Card(); // player
     
          // create icons for each image using the getCardName () method
    	  iconc1 = new ImageIcon(c1.getCardName());
          iconc2 = new ImageIcon(c2.getCardName());
    	  iconc3 = new ImageIcon(c3.getCardName());
    	  iconc4 = new ImageIcon(c4.getCardName());
     
    	  // Create labels for each card
    	  label1 = new JLabel (c1.toString(), iconc1,SwingConstants.CENTER);
    	  label2 = new JLabel (c2.toString(), iconc2,SwingConstants.CENTER);
    	  label5 = new JLabel ();
    	  label3 = new JLabel (c3.toString(), iconc3,SwingConstants.CENTER);
    	  label4 = new JLabel (c4.toString(), iconc4,SwingConstants.CENTER);
     
     
    	  // Create a panel, set the background color, set the size and add the panels
    	  JPanel primary = new JPanel();
    	  JPanel panel1  = new JPanel();
          JPanel panel5  = new JPanel();
    	  JPanel panel7  = new JPanel();
    	  JPanel panel2  = new JPanel();
     
     
    	  // Sets Background
    	  primary.setBackground (Color.green   );
    	  panel1.setBackground  (Color.red     );
          panel5.setBackground  (Color.green  );
    	  panel7.setBackground  (Color.green );
    	  panel2.setBackground  (Color.yellow  );
     
     
    	  // Sets pefered Size
    	  primary.setPreferredSize (new Dimension(1000, 650));
    	  panel1.setPreferredSize  (new Dimension(800, 200 ));
          panel5.setPreferredSize  (new Dimension(800, 100 ));
    	  panel7.setPreferredSize  (new Dimension(800, 125 ));
    	  panel2.setPreferredSize  (new Dimension(800, 200 ));
     
     
    	  // Adds panel to labels
    	  panel1.add (label1);
    	  panel1.add (label2);
    	  panel5.add (label5);
    	  panel5.add (winLabel);
    	  panel7.add (lossLabel);
    	  panel2.add (label3);
    	  panel2.add (label4);
     
          // Sets panel 5 layout
          panel5.setLayout(new GridLayout (1, 0));
     
    	  // Add panels to primary
    	  primary.add (panel1);
          primary.add (panel5);
    	  primary.add (panel7);
    	  primary.add (panel2);
     
          // Sets position of card and name
    	  label1.setHorizontalTextPosition (SwingConstants.CENTER);
    	  label1.setVerticalTextPosition   (SwingConstants.BOTTOM);
     
    	  label2.setHorizontalTextPosition (SwingConstants.CENTER);
    	  label2.setVerticalTextPosition   (SwingConstants.BOTTOM);
     
    	  label3.setHorizontalTextPosition (SwingConstants.CENTER);
    	  label3.setVerticalTextPosition   (SwingConstants.BOTTOM);
     
    	  label4.setHorizontalTextPosition (SwingConstants.CENTER);
    	  label4.setVerticalTextPosition   (SwingConstants.BOTTOM);
     
          ButtonListener listener = new ButtonListener();
     
          // Creates buttons and labels for them
          deal = new JButton ("Deal");
          deal.addActionListener (new ButtonListener());
     
          hit = new JButton ("Hit");
          hit.addActionListener (new ButtonListener());
     
          stay = new JButton ("Stay");
          stay.addActionListener (new ButtonListener());
     
          newDeck = new JButton ("New Deck");
          newDeck.addActionListener (new ButtonListener());
     
          // create main panel, color, layout and dimensions
          panel = new JPanel();
          panel.setBackground (Color.green);
          panel.setPreferredSize(new Dimension (1000, 650));
          panel.setLayout(new BorderLayout());
     
          // create the center panel, color and JScrollPanel(ta)
          centerPanel = new JPanel();
          centerPanel.setBackground(Color.green);
          centerPanel.add (panel1);
          centerPanel.add (panel5);
          centerPanel.add (panel7);
          centerPanel.add (panel2);
     
     
          // create the east panel with color
          eastPanel = new JPanel();
          eastPanel.setBackground (Color.lightGray);
          eastPanel.setLayout(new GridLayout (6, 3, 5, 50));
          eastPanel.add (spaceLabel);
          eastPanel.add (deal);
          eastPanel.add (hit);
          eastPanel.add (stay);
          eastPanel.add (newDeck);
     
     
          // create the west panel with color and Box layout
          westPanel = new JPanel();
          westPanel.setBackground (Color.lightGray);
          westPanel.setLayout (new BoxLayout (westPanel, BoxLayout.Y_AXIS));
     
          westPanel.add (DcardLabel);
          westPanel.add (UcardLabel);
          westPanel.add (Box.createRigidArea (new Dimension (0, 10)));
     
     
     
          // create the radio button group listener
          listen = new ButtonListener();
     
          // create all the radio buttons, including listener, color and group
          fiveB = new JRadioButton ("5");
          fiveB.addActionListener(listen);
          fiveB.setBackground (Color.lightGray);
          group.add(fiveB);
     
          tenB = new JRadioButton ("10");
          tenB.addActionListener(listen);
          tenB.setBackground (Color.lightGray);
          group.add(tenB);
     
          twentyFiveB = new JRadioButton ("25");
          twentyFiveB.addActionListener(listen);
          twentyFiveB.setBackground (Color.lightGray);
          group.add(twentyFiveB);
     
          fiftyB = new JRadioButton ("50");
          fiftyB.addActionListener(listen);
          fiftyB.setBackground (Color.lightGray);
          group.add(fiftyB);
     
          hundredB = new JRadioButton ("100");
          hundredB.addActionListener(listen);
          hundredB.setBackground (Color.lightGray);
          group.add(hundredB);
     
          // place radio button group in west panel
          westPanel.add (Box.createRigidArea (new Dimension (10, 450)));
          westPanel.add (cashLabel);
          westPanel.add (moneyLabel);
          westPanel.add(fiveB);
          westPanel.add(tenB);
          westPanel.add(twentyFiveB);
          westPanel.add(fiftyB);
          westPanel.add(hundredB);
     
          // place all panels in the Border Layout positions
          panel.add(centerPanel, BorderLayout.CENTER);
          panel.add(westPanel, BorderLayout.WEST);
          panel.add(eastPanel, BorderLayout.EAST);
     
          // add the main panel
          add (panel);
     
       }
       //Button listeners and Actions Perferomed
       private class ButtonListener implements ActionListener
       {
          public void actionPerformed(ActionEvent event)
          {
    		 // Attempt for button actions
             if (event.getSource() == deal)
             {
     
     
     
    	     }
             else if (event.getSource() == hit)
             {
     
     
     
    	     }
             else if (event.getSource() == stay)
             {
     
     
     
             }
             else if (event.getSource() == newDeck)
             {
     
     
     
    	     }
     
     
             // If the game is won or lost
             if (dealerTot == BLACKJACK || yourTot == BLACKJACK)
             {
    			 cash = 0;
    	     }
    	     else if (dealerTot == BLACKJACK)
    	     {
    			 game = lose;
    	         lose++;
    	     }
    		 else if (yourTot == BLACKJACK)
    		 {
    			 game = win;
    	         win++;
    	     }
             // check for which radio button was clicked
     
    	  	 // is event fiveB
    	     if (event.getSource() == fiveB)
    	     {
                // bets = 5 dollors
                if (game == win)
                {
    			   cash =+ 5;
    		    }
    			else
    			{
    			   cash =- 5;
    		    }
     
    	     }
    	     // else is event tenB
    	     else if (event.getSource() == tenB)
    	     {
                // bets = 10 dollors
                if (game == win)
    			{
    			   cash =+ 10;
    	        }
    			else
    			{
    			   cash =- 10;
    		    }
    	     }
     
    	     // else is event twentyFiveB
    	     else if (event.getSource() == twentyFiveB)
    	     {
                // bets = 25 dollors
                if (game == win)
    		 	{
    		 	   cash =+ 25;
    		 	}
    		 	else
    		 	{
    		       cash =- 25;
    		    }
    	     }
             // else is event fiftyB
    	     else if (event.getSource() == fiftyB)
    	     {
                // bets = 50 dollors
                if (game == win)
    			{
    			   cash =+ 50;
    			}
    			else
    			{
    			   cash =- 50;
    		    }
    	     }
    	     // else is event hundredB
    	     else if (event.getSource() == hundredB)
    	     {
                // bets = 100 dollors
                if (game == win)
    			{
    			   cash =+ 100;
    			}
    			else
    			{
    			   cash =- 100;
    		    }
    	     }
     
          }
          public void Card ()
    	     {
    	          // Create a random value for the face and suit values
    	          face = (int) (Math.random() * 13 + 1);
     
    	          suit = (int) (Math.random() * 4 + 1);
     
    	          // Create the names for the face and suit of the card
    	          setFaceName();
     
    	          setSuitName();
     
    	     }
     
    	     //-----------------------------------------------------------------
    	     //  Sets the string representation of the face using a switch statement
    	     //  with cases for each numeric value 1-13.
    	     //-----------------------------------------------------------------
    	     private void setFaceName()
    	     {
    	         switch (face)
    	         {
    	              case 1: value = 1; faceName = "Ace of"  ; break;
     
    	              case 2: value = 2; faceName = "Two of"  ; break;
     
    	              case 3: value = 3; faceName = "Three of"; break;
     
    	              case 4: value = 4; faceName = "Four of" ; break;
     
    	              case 5: value = 5; faceName = "Five of" ; break;
     
    	              case 6: value = 6; faceName = "Six of"  ; break;
     
    	              case 7: value = 7; faceName = "Seven of"; break;
     
    	              case 8: value = 8; faceName = "Eight of"; break;
     
    	              case 9: value = 9; faceName = "Nine of" ; break;
     
    	              case 10: value = 10; faceName = "Ten of"  ; break;
     
    	              case 11: value = 10; faceName = "Jack of" ; break;
     
    	              case 12: value = 10; faceName = "Queen of"; break;
     
    	              case 13: value = 10; faceName = "King of" ; break;
     
    	         }
    	     }
     
    	     //-----------------------------------------------------------------
    	     //  Sets the string representation of the suit using a switch statement
    	     //  with cases for each numeric value 1-4.
    	     //-----------------------------------------------------------------
    	     private void setSuitName()
    	     {
    	         switch (suit)
    	         {
    	             case 1: suitName = " Clubs"   ; break;
     
    	             case 2: suitName = " Diamonds"; break;
     
    	             case 3: suitName = " Hearts"  ; break;
     
    	             case 4: suitName = " Spades"  ; break;
    	         }
    	     }
     
    	     //-----------------------------------------------------------------
    	     //  Returns the face (numeric value) of this card.
    	     //-----------------------------------------------------------------
    	     public int getFace ()
    	     {
    	         return face;
    	     }
     
    	     //-----------------------------------------------------------------
    	     //  Returns the suit (numeric value) of this card.
    	     //-----------------------------------------------------------------
    	     public int getSuit ()
    	     {
    	         return suit;
    	     }
     
    	     //-----------------------------------------------------------------
    	     //  Returns the string representation of this card, including
    	     //  both face and suit.
    	     //-----------------------------------------------------------------
    	     public String toString ()
    	     {
     
    	         return ( faceName + suitName);
     
    	     }
     
    	     //-----------------------------------------------------------------
    	     //  Returns the name of the card image
    	     //-----------------------------------------------------------------
    	     public String getCardName ()
    	     {
    	         return "Cards/" + faceName + suitName +".gif";
    	     }
       }
    }

    import javax.swing.JFrame;
     
    public class BlackJack
    {
       //-----------------------------------------------------------------
       //  Creates and displays the BlackJack Game GUI.
       //-----------------------------------------------------------------
       public static void main (String[] args)
       {
          JFrame frame = new JFrame ("BlackJack");
          frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
     
          BlackJackPanel panel = new BlackJackPanel();
     
     
          frame.getContentPane().add(panel);
          frame.pack();
          frame.setVisible(true);
       }
    }
    This should be what your looking for
    public class Card
    {
       //----------------------------------------------------------------
       //  define class variables
       //      face 13 , suit 4
       //      faceName, suitName
       //----------------------------------------------------------------
           private int face;
           private int suit;
           private String faceName;
           private String suitName;
     
     
       //-----------------------------------------------------------------
       //  Creates a random card by creating
       //      face, suit
       //  and calling
       //      faceName() and suitName()
       //-----------------------------------------------------------------
       public Card ()
       {
            // Create a random value for the face and suit values
            face = (int) (Math.random() * 13 + 1);
     
            suit = (int) (Math.random() * 4 + 1);
     
            // Create the names for the face and suit of the card
            setFaceName();
     
            setSuitName();
     
       }
     
       //-----------------------------------------------------------------
       //  Sets the string representation of the face using a switch statement
       //  with cases for each numeric value 1-13.
       //-----------------------------------------------------------------
       private void setFaceName()
       {
           switch (face)
           {
                case 1:  faceName = "Ace of"  ; break;
     
                case 2:  faceName = "Two of"  ; break;
     
                case 3:  faceName = "Three of"; break;
     
                case 4:  faceName = "Four of" ; break;
     
                case 5:  faceName = "Five of" ; break;
     
                case 6:  faceName = "Six of"  ; break;
     
                case 7:  faceName = "Seven of"; break;
     
                case 8:  faceName = "Eight of"; break;
     
                case 9:  faceName = "Nine of" ; break;
     
                case 10: faceName = "Ten of"  ; break;
     
                case 11: faceName = "Jack of" ; break;
     
                case 12: faceName = "Queen of"; break;
     
                case 13: faceName = "King of" ; break;
     
           }
       }
     
       //-----------------------------------------------------------------
       //  Sets the string representation of the suit using a switch statement
       //  with cases for each numeric value 1-4.
       //-----------------------------------------------------------------
       private void setSuitName()
       {
           switch (suit)
           {
               case 1: suitName = " Clubs"   ; break;
     
               case 2: suitName = " Diamonds"; break;
     
               case 3: suitName = " Hearts"  ; break;
     
               case 4: suitName = " Spades"  ; break;
           }
       }
     
       //-----------------------------------------------------------------
       //  Returns the face (numeric value) of this card.
       //-----------------------------------------------------------------
       public int getFace ()
       {
           return face;
       }
     
       //-----------------------------------------------------------------
       //  Returns the suit (numeric value) of this card.
       //-----------------------------------------------------------------
       public int getSuit ()
       {
           return suit;
       }
     
       //-----------------------------------------------------------------
       //  Returns the string representation of this card, including
       //  both face and suit.
       //-----------------------------------------------------------------
       public String toString ()
       {
     
           return ( faceName + suitName);
     
       }
     
       //-----------------------------------------------------------------
       //  Returns the name of the card image
       //-----------------------------------------------------------------
       public String getCardName ()
       {
           return "Cards/" + faceName + suitName +".gif";
       }
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

    Default Re: BlackJack Game can"t figure out how to finsh it.

    I'm confused why you have a method: Card() that looks like the Constructor in the Card class?
    Plus there are several other methods (setFaceName(), setSuitName() and others ) that are in the Card class.

    Comments on your code:
    You should create the new Cards in a deal method vs in the init code
    The names for the panels could be descriptive vs numbers
    ButtonListener class should be broken up into several separate classes that do specific jobs vs putting all the code in one method
    The constructor for Card should check that the card hasn't been dealt yet. It is possible to get duplicate cards if you don't test for them.
    Last edited by Norm; May 13th, 2011 at 10:21 AM. Reason: Add comments re code

Similar Threads

  1. Writing a very simple "Poker" game in Java
    By omgar in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 7th, 2014, 05:21 PM
  2. Help with blackjack game
    By santosd1118 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 11th, 2010, 11:55 PM
  3. Java says:"Hello World". I say:"It works!"
    By Davidovic in forum Member Introductions
    Replies: 4
    Last Post: June 29th, 2010, 07:13 AM
  4. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 07:35 AM
  5. help help, ... :) "game of life"
    By sanfor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 9th, 2009, 11:40 AM