Results 1 to 12 of 12
  1. #1
    Enigma is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default Need quick help with a Java blackjack program!

    Ok, so this is a very simple program i'm stuck on. In class, the instructor gave us some base code to start with, and asked us to modify it. It needs to deal 5 cards, have buttons for deal and new game, needs to have a label that tells you how much your hand is worth, needs to be able to shuffle the deck when it runs out of cards, and I need to make sure the ace is either a 1 or an 11 depending on the value of the hand. I'm completely stuck.

    Java Code:
    //Card
    import javax.swing.*;
    import java.awt.*;
    
    class Card
    {
    	private int cardSuit;
    	private int cardRank;
    	private ImageIcon cardImage;
    	
    	public Card()
    	{
    		cardSuit = 0;
    		cardRank = 2;
    		cardImage = null;
    	}
    	
    	public Card(int r, int s)
    	{
    		cardSuit = s;
    		cardRank = r;
    	}
    	
    	public ImageIcon getImage()
    	{
    		return cardImage;
    	}
    	
    	public void setImage(ImageIcon newImage)
    	{
    		cardImage = newImage;
    	}
    	
    	public void setRank(int r)
    	{
    		cardRank = r;
    	}
    	
    	public void setSuit(int s)
    	{
    		cardSuit = s;
    	}
    }
    
    
    
    
    
    
    
    //Deck
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Deck
    {
    	private Card[] cardDeck;
    	private int topCard;
    	private String imagePath;
    	
    	public Deck()
    	{
    		cardDeck = new Card[52];
    		imagePath = "c:\\Java 1\\images\\";
    		makeDeck();
    		topCard = 0;
    	}
    	
    	private void makeDeck()
    	{
    		int i, j, cardCounter;
    		String imageFile = "";
    		ImageIcon cardImage;
    		
    		cardCounter = 0;
    		for(i = 0; i < 4; i++)
    		{
    			for(j=2; j < 15; j++)
    			{
    				cardDeck[cardCounter] = new Card();
    				cardDeck[cardCounter].setSuit(i);
    				cardDeck[cardCounter].setRank(j);
    				if(j == 14)
    				{
    					switch(i)
    					{
    						case 0: imageFile = "1c.png"; break;
    						case 1: imageFile = "1d.png"; break;
    						case 2: imageFile = "1h.png"; break;
    						case 3: imageFile = "1s.png"; break;
    					}
    				}
    				
    				else
    				{
    					switch(i)
    					{
    						case 0: imageFile = "" + j + "c.png"; break;
    						case 1: imageFile = "" + j + "d.png"; break;
    						case 2: imageFile = "" + j + "h.png"; break;
    						case 3: imageFile = "" + j + "s.png"; break;
    					}
    				}
    				
    				cardImage = new ImageIcon(imagePath + imageFile);
    				cardDeck[cardCounter].setImage(cardImage);
    				cardCounter++;
    			}
    		}
    	}
    	
    	public Card getTopCard()
    	{
    		if(topCard < 52)
    			return cardDeck[topCard++];
    		else
    			return null;
    	}
    	
    	public void shuffleDeck()
    	{
    		int nextNumber = 0;
    		boolean[] selected = new boolean[52];
    		boolean goodCard;
    		Card[] tempDeck = new Card[52];
    		for(int i = 0; i < 52; i++)
    			selected[i] = false;
    		for(int i = 0; i < 52; i++)
    		{
    			goodCard = false;
    			while(!goodCard)
    			{
    				nextNumber = (int)(Math.random()*52);
    				if(!selected[nextNumber])
    				{
    					goodCard = true;
    					selected[nextNumber] = true;
    				}
    			}
    			tempDeck[i] = cardDeck[nextNumber];
    		}
    		
    		for(int i = 0; i < 52; i++)
    			cardDeck[i] = tempDeck[i];
    		topCard = 0;
    	}
    	
    	public boolean hasNextCard()
    	{
    		return (topCard < 52);
    	}
    }
    
    //CardGame
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class CardGame extends JFrame implements ActionListener
    {
    	private JButton dealerDeal, playerDeal, dealerNewGame, playerNewGame;
    	private Deck myDeck;
    	private JLabel[] dealerCardLabels, playerCardLabels;
    	private JLabel lblDealerHand, lblPlayerHand, lblDealerHandValue, lblPlayerHandValue;
    	private ImageIcon cardBack;
    	private String imagePath;
    	private int numPlayerCards, numDealerCards;
    	private JPanel playerPanel, dealerPanel;
    	
    	public CardGame()
    	{
    		setTitle("Card Game");
    		setSize(900,300);
    		setBackground(Color.white);
    		setLayout(new GridLayout(2,1));
    		setLocationRelativeTo(null);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		imagePath = "c:\\Java 1\\images\\";
    		cardBack = new ImageIcon(imagePath + "cardback.png");
    		
    		dealerPanel = new JPanel();
    		dealerPanel.setSize(600,150);
    		dealerPanel.setLayout(new FlowLayout());
    		lblDealerHand = new JLabel("DEALER HAND:");
    		lblDealerHandValue = new JLabel("Dealer Hand Value: ");
    		//dealerPanel.add(lblDealerHand);
    		dealerPanel.add(lblDealerHandValue);
    		dealerCardLabels = new JLabel[5];
    		for(int i = 0; i < 5; i++)
    		{
    			dealerCardLabels[i] = new JLabel(cardBack);
    			dealerPanel.add(dealerCardLabels[i]);
    		}
    		
    		dealerDeal = new JButton("Deal");
    		dealerDeal.setBackground(Color.red);
    		dealerDeal.setForeground(Color.white);
    		dealerDeal.addActionListener(this);
    		dealerPanel.add(dealerDeal);
    		add(dealerPanel);
    		
    		dealerNewGame = new JButton("New Game");
    		dealerNewGame.setBackground(Color.red);
    		dealerNewGame.setForeground(Color.white);
    		dealerNewGame.addActionListener(this);
    		dealerPanel.add(dealerNewGame);
    		add(dealerPanel);
    		
    		playerPanel = new JPanel();
    		playerPanel.setSize(600,150);
    		playerPanel.setLayout(new FlowLayout());
    		lblPlayerHand = new JLabel("PLAYER HAND:");
    		lblPlayerHandValue = new JLabel("Player Hand Value: ");
    		//playerPanel.add(lblPlayerHand);
    		playerPanel.add(lblPlayerHandValue);
    		playerCardLabels = new JLabel[5];
    		for (int i = 0; i < 5; i++)
    		{
    			playerCardLabels[i] = new JLabel(cardBack);
    			playerPanel.add(playerCardLabels[i]);
    		}
    		
    		playerDeal = new JButton("Deal");
    		playerDeal.setBackground(Color.blue);
    		playerDeal.setForeground(Color.white);
    		playerDeal.addActionListener(this);
    		playerPanel.add(playerDeal);
    		add(playerPanel);
    		
    		playerNewGame = new JButton("New Game");
    		playerNewGame.setBackground(Color.blue);
    		playerNewGame.setForeground(Color.white);
    		playerNewGame.addActionListener(this);
    		playerPanel.add(playerNewGame);
    		add(playerPanel);
    		
    		myDeck = new Deck();
    		myDeck.shuffleDeck();
    		numPlayerCards = 0;
    		numDealerCards = 0;
    		setVisible(true);
    	}
    	
    	public void actionPerformed(ActionEvent e)
    	{
    		if(e.getSource() == dealerDeal)
    		{
    			Card myCard = myDeck.getTopCard();
    			dealerCardLabels[numDealerCards].setIcon(myCard.getImage());
    			numDealerCards++;
    		}
    		if(e.getSource() == playerDeal)
    		{
    			Card myCard = myDeck.getTopCard();
    			playerCardLabels[numPlayerCards].setIcon(myCard.getImage());
    			numPlayerCards++;
    		}
    	}
    	
    	public static void main(String args[])
    	{
    		CardGame myCardGame = new CardGame();
    	}
    }

    I can't figure out how to assign values to the individual card images at all. I know to somehow store the value of a hand in a variable, which I can then output in the lable i've created. -_- Please help, guys, i'm at a complete loss.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    Quote Originally Posted by Enigma View Post
    I can't figure out how to assign values to the individual card images at all.
    The values are stored in the Card class. I fail to see what your problem is.

  3. #3
    Enigma is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default

    Well I can't figure out how to return the collective values of a players hand so I can output it in lblPlayerHandValue and lblDealerHandValue in the CardGame file.And I have absolutely no idea how to set the ace to multiple values.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    Write a getRank method in the Card class that returns a int. Personally I would have the rank of the Ace as 1 and not 14. Then the method can have an if stament:
    Java Code:
    if(rank > 10) {
        return 10;
    }
    Then back wherever you are calculating the value of the hand you would need another if statement to determine if the Ace should be counted as 1 or 11.

  5. #5
    Enigma is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default

    So, considering the Card file is using r as the rank variable; would it be something like this?
    Java Code:
    	public getRank()
    	{
    		if (r > 10)
    		{
    			return 10;
    		}
    		else
    		{
    			return r
    		}
    	}
    And if so, I still can't figure out how to combine all the values into one value for the output of the total hand value. :( I know i'm being a pain, I apologize; but I am really, really stuck.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    Quote Originally Posted by Enigma View Post
    So, considering the Card file is using r as the rank variable; would it be something like this?
    Yes except r is not the name of the variable.
    I still can't figure out how to combine all the values into one value for the output of the total hand value.
    If I was to call out to you a series of numbers and at the end I wanted you to tell me the total, how would you do it? Explain the detailed steps in English. No Java. Once you can do that, writing the code should be simple.

  7. #7
    Enigma is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default

    So I placed the getRank method in the card class, and I added '+ playerHandValue' and '+ dealerHandValue' to my two JLabels. Both of these new variables are initialized and set to 0 at the beginning of the program. Then I added statements at the bottom of each of my action listeners for the two deal buttons, saying 'dealerHandValue += cardRank' and the same with player. ERRORS EVERYWHERE. :( Anywho, it said I need to declare a return type for the new method, CardGame didn't recognize the cardRank variable, and that i can't return values from a return method whose type is void. I am at such a loss.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    Quote Originally Posted by Enigma View Post
    CardGame didn't recognize the cardRank variable
    Of course it doesn't. The cardRank variable is in the Card class. That is why I suggested writing the getRank method in the Card class. Then the CardGame class calls the getRank method on all the Card objects.

    i can't return values from a return method whose type is void. I am at such a loss.
    Once again it is obvious. If you want to return something from a method then you cannot have void as the return type. On the other hand if your method should/must be void then you cannot have a return statement. Make up your mind.

  9. #9
    Enigma is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default

    That's the problem, I didn't specify void! The exact code I use was:
    Java Code:
    	public getRank()
    	{
    		if (cardRank > 10)
    		{
    			return 10;
    		}
    		else
    		{
    			return cardRank;
    		}
    	}
    And I have no idea how to call that method every time it deals a new card.
    My best guess was...

    Java Code:
    Card dealerCardValue = myCardValue.getRank;
    dealerHandValue += dealerCardValue;
    Unfortunately, that made more errors, and i'm thinking i'm going to be failing a project.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    Only constructors do not have a return type. All methods must have a return type, either void if they return nothing or the type of the "thing" being returned. So what type is the getRank method returning?

    I haven't looked closely at the code due to there being way to much to wade through. However, I assume somewhere in the code it has a bunch of Card objects representing the players hand, an array Or List etc. You loop over each Card and call the getRank method.
    Java Code:
    Card[] playersHand = .....;
    int handScore = 0;
    for(int index = 0; index < playersHand.length; index++) {
        handScore += playersHand[index].getRank();
    }
    //or
    for(Card c : playersHand) {
        handScore += c.getRank();
    }
    It appears that you are making this way more difficult than it really is. This is probably due to you going "ARGGGGGGGGGGGGGGGGGGGH! I don't know how to do this" and then madly bashing your keyboard in an attempt at writing code. Instead of logically thinking about what you need to do. This results in you typing a bunch of crappy code which doesn't do what you want which only frustrates you even further.

    Two choices: give up as this part of the assingment may be above you skill level at this point in time or get some rest, clear your head and have another go tomorrow.

  11. #11
    Enigma is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default

    Thank you, I appreciate your patience. I think I understand your code there. Create a new array equal to the one used for either player or dealer, then cycle through it and add each rank to the hand score. I'm still having issues with this bloody return statement though. I inserted int before the '10' and the 'cardRank' but it still doesn't seem to like me much. -_-

  12. #12
    Enigma is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default

    Sorry for the double post; I can't just let it rest for now either, the assignment is due in two days and i'm at work or attempting to get some sleep for my final tomorrow.

Similar Threads

  1. Help With BlackJack Game !
    By TangoMango in forum New To Java
    Replies: 1
    Last Post: 01-24-2011, 05:40 PM
  2. Replies: 5
    Last Post: 11-27-2010, 12:28 AM
  3. blackjack help
    By jordaejava in forum New To Java
    Replies: 1
    Last Post: 12-11-2009, 05:44 AM
  4. Require quick program revision please
    By SF163 in forum New To Java
    Replies: 2
    Last Post: 11-07-2009, 12:04 AM
  5. Help with simple Blackjack Program?
    By meeper3000 in forum New To Java
    Replies: 2
    Last Post: 04-26-2009, 09:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •