Results 1 to 3 of 3
  1. #1
    Cookmeup272 is offline Member
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Default Simple BlackJack Program

    Ok so for my final project for my Java programming class I decided to create a simple BlackJack GUI. Everything runs good EXCEPT for when I go to "hit". Sometimes it works fine, other times it adds to many cards to the player's hand. I cannot for the life of me figure this out and could use some help. I deleted the part where I import the pictures of cards to cut down on code. I suspect that the error has something to do with my action Listener or something because my "HitMeCount" will keep incrementing even when i only press the button once.

    Java Code:
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import java.util.*;
    
    public class BlackJack extends JFrame implements ActionListener{
    	
    	Random randomCard = new Random();
    	
    	JFrame frame;
    	//create buttons for hitting, staying and dealing
    	JButton hitMe = new JButton("Hit");
    	JButton stayMe = new JButton("Stay");
    	JButton newGame = new JButton("Deal");
    	
    	//create labels for the sting "Player Hand Value" and the actual hand value
    	JLabel playerHandValueString = new JLabel("Hand Value");
    	JLabel playerHandValue = new JLabel();
    	
    	//create a panel for the game and a panel for the buttons
    	JPanel buttonPanel = new JPanel();
    	JPanel dealerPanel = new JPanel(new GridLayout(1,5));
    	JPanel playerPanel = new JPanel(new GridLayout(1,5));
    		
    	//Create a menu to exit the game and see the About info
    	MenuBar menu = new MenuBar();
    	Menu file = new Menu();
    	MenuItem exit = new MenuItem();
    	MenuItem about = new MenuItem();
    	
    	//create JLabels to attach image of cards to
    	JLabel dealerCard1 = new JLabel();
    	JLabel dealerCard2 = new JLabel();
    	JLabel dealerCard3 = new JLabel();
    	JLabel dealerCard4 = new JLabel();
    	JLabel dealerCard5 = new JLabel();
    	
    	JLabel playerCard1 = new JLabel();
    	JLabel playerCard2 = new JLabel();
    	JLabel playerCard3 = new JLabel();
    	JLabel playerCard4 = new JLabel();
    	JLabel playerCard5 = new JLabel();
    		
    	private int dealerSum = 0;
    	private int playerSum = 0;
    	private int playerAceFlag = 0;
    	private int dealerAceFlag = 0;
    	private int randomIndex = 0;
    	private int hitMeCount = 0;
    	
    	private boolean keepOnGaming = true;
    	
    	int[] cardValue = new int[52];
    	
    	private BufferedImage myPicture;
    	
    	private Image[] cards = new Image[53];
    	
    	void initialDeal(){
    		setDealerSum(0);
    		setPlayerSum(0);
    		System.out.println("DealerSum is " + getDealerSum());
    		System.out.println("Playersum is " + getPlayerSum());
    		System.out.println("HitMe Counter: " + hitMeCount);
    		randomIndex= randomCard.nextInt(52);
    		Icon dealerFirstCard = new ImageIcon(cards[randomIndex]);
    		dealerCard1.setIcon(dealerFirstCard);
    		int dealerCard1Value = cardValue[randomIndex];
    		
    		System.out.println("Card 1: " + dealerCard1Value);
    		setDealerSum(dealerCard1Value);
    		System.out.println("DealerSum: " + getDealerSum());
    		
    		if(dealerCard1Value == 11){
    			dealerAceFlag++;
    		}
    		
    		randomIndex = randomCard.nextInt(52);
    		Icon dealerSecondCard = new ImageIcon(cards[randomIndex]);
    		dealerCard2.setIcon(dealerSecondCard);
    		dealerCard2.setVisible(false);
    		int dealerCard2Value = cardValue[randomIndex];
    		
    		System.out.println("Card 2: " + dealerCard2Value);
    		setDealerSum(dealerCard2Value);		
    		System.out.println("DealerSum: " + getDealerSum());
    		
    		if(dealerCard2Value == 11){
    			dealerAceFlag++;
    		}
    		
    		if(dealerAceFlag == 2){
    			setDealerSum(-10);
    			dealerAceFlag--;
    		}
    		
    		cardIndex++;
    		randomIndex= randomCard.nextInt(52);
    		Icon playerFirstCard = new ImageIcon(cards[randomIndex]);
    		playerCard1.setIcon(playerFirstCard);
    		int playerCard1Value = cardValue[randomIndex];
    		setPlayerSum(playerCard1Value);
    		if(playerCard1Value == 11){
    			playerAceFlag++;
    		}
    		
    		cardIndex++;
    		randomIndex= randomCard.nextInt(52);
    		Icon playerSecondCard = new ImageIcon(cards[randomIndex]);		
    		playerCard2.setIcon(playerSecondCard);
    		int playerCard2Value = cardValue[randomIndex];
    		setPlayerSum(playerCard2Value);
    		if(playerCard2Value == 11){
    			playerAceFlag++;
    		}
    		
    		if(playerAceFlag == 2){
    			setPlayerSum(-10);
    			playerAceFlag--;
    		}
    		
    		
    		if(getPlayerSum() == 21 && getDealerSum() == 21){
    			playerHandValue.setText("PUSH");
    			endGame();
    		} else if(getPlayerSum() == 21 && getDealerSum() != 21){
    			playerHandValue.setText("Blackjack");
    			endGame();
    		} else if(getDealerSum() == 21 && getPlayerSum() != 21){
    			playerHandValue.setText("Dealer Blackjack");
    			dealerCard2.setVisible(true);
    			endGame();
    		} else {
    			playerHandValue.setText(Integer.toString(getPlayerSum()));
    		}
    	
    		hitMe.addActionListener(this);
    		stayMe.addActionListener(this);
    		newGame.addActionListener(this);
    		
    		
    	}
    
    	public void actionPerformed(ActionEvent e) {
    		if (e.getSource() == hitMe){
    			hitMeCount++;
    			if(hitMeCount == 1){
    				dealPlayerCard3();
    			}else if(hitMeCount == 2){
    				dealPlayerCard4();
    			}else if(hitMeCount == 3){
    				dealPlayerCard5();
    			}
    		} 
    		
    		if(e.getSource() == stayMe){
    			stayMe();
    		}
    		if(e.getSource() == newGame){
    			dealMe();
    		}
    		
    	}
    	
    	public void setDealerSum(int n){
    		if(n == 0){
    			dealerSum = n;
    		}else{
    		dealerSum = dealerSum + n;
    		}
    	}
    	
    	public int getDealerSum(){
    		return dealerSum;
    	}
    	
    	public void setPlayerSum(int n){
    		if(n == 0){
    			playerSum = n;
    		}else{
    		playerSum = playerSum + n;
    		}
    	}
    	
    	public int getPlayerSum(){
    		return playerSum;
    	}
    	
    	public void stayMe(){
    		if(keepOnGaming == false){
    			playerHandValue.setText("Hit New Game");
    			endGame();
    		}
    		dealerCard2.setVisible(true);
    		System.out.println("DealerSum: " + getDealerSum());
    		
    		if(getDealerSum() > 21 && dealerAceFlag > 0){
    			setDealerSum(-10);
    			dealerAceFlag--;
    		}
    		if(getDealerSum() > 21){
    			playerHandValue.setText("Winner! Dealer Bust!");
    			endGame();
    		} else if(getDealerSum() == 21){
    			playerHandValue.setText("Loser! Dealer 21!");
    			endGame();
    		}else if(getDealerSum() < 21){
    			if(getDealerSum() >= 17){
    				if(getDealerSum() > getPlayerSum()){
    					playerHandValue.setText("Loser!");
    					endGame();
    				}else if(getPlayerSum() > getDealerSum()){
    					playerHandValue.setText("Winner!");
    					endGame();
    				}else if(getPlayerSum() == getDealerSum()){
    					playerHandValue.setText("Push!");
    					endGame();
    				}
    			}else if(getDealerSum() < 17){
    				randomIndex= randomCard.nextInt(52);
    				Icon dealerThirdCard = new ImageIcon(cards[randomIndex]);
    				dealerCard3.setIcon(dealerThirdCard);
    				dealerCard3.setVisible(true);
    				int dealerCard3Value = cardValue[randomIndex];
    				setDealerSum(dealerCard3Value);
    				System.out.println("DealerSum: " + getDealerSum());
    				
    				if(dealerCard3Value == 11){
    					dealerAceFlag++;
    				}
    				if(getDealerSum() > 21 && dealerAceFlag > 0){
    					setDealerSum(-10);
    					dealerAceFlag--;
    				}
    				if(getDealerSum() > 21){
    					playerHandValue.setText("Winner! Dealer Bust!");
    					endGame();
    				}else if(getDealerSum() == 21){
    					if(getPlayerSum() != 21){
    						playerHandValue.setText("Loser!");
    						endGame();
    					}else if(getPlayerSum() == 21){
    						playerHandValue.setText("Push!");
    						endGame();
    					}
    				}else if(getDealerSum() < 21){
    					if(getDealerSum() >= 17){
    						if(getDealerSum() > getPlayerSum()){
    							playerHandValue.setText("Loser!");
    							endGame();
    						}else if(getPlayerSum() > getDealerSum()){
    							playerHandValue.setText("Winner!");
    							endGame();
    						}else if(getPlayerSum() == getDealerSum()){
    							playerHandValue.setText("Push!");
    							endGame();
    						}
    					}else if(getDealerSum() < 17){
    						randomIndex= randomCard.nextInt(52);
    						Icon dealerFourthCard = new ImageIcon(cards[randomIndex]);
    						dealerCard4.setIcon(dealerFourthCard);
    						int dealerCard4Value = cardValue[randomIndex];
    						setDealerSum(dealerCard4Value);
    						dealerCard4.setVisible(true);
    						System.out.println("DealerSum: " + getDealerSum());
    						
    						if(dealerCard4Value == 11){
    							dealerAceFlag++;
    						}
    						if(getDealerSum() > 21 && dealerAceFlag > 0){
    							setDealerSum(-10);
    							dealerAceFlag--;
    						}
    						if(getDealerSum() > 21){
    							playerHandValue.setText("Winner! Dealer Bust!");
    							endGame();
    						}else if(getDealerSum() == 21){
    							if(getPlayerSum() != 21){
    								playerHandValue.setText("Loser!");
    								endGame();
    							}else if(getPlayerSum() == 21){
    								playerHandValue.setText("Push!");
    								endGame();
    							}
    						}else if(getDealerSum() < 21){
    							if(getDealerSum() >= 17){
    								if(getDealerSum() > getPlayerSum()){
    									playerHandValue.setText("Loser!");
    									endGame();
    								}else if(getPlayerSum() > getDealerSum()){
    									playerHandValue.setText("Winner!");
    									endGame();
    								}else if(getPlayerSum() == getDealerSum()){
    									playerHandValue.setText("Push!");
    									endGame();
    								}
    							}else if(getDealerSum() < 17){
    								randomIndex= randomCard.nextInt(52);
    								Icon dealerFifthCard = new ImageIcon(cards[randomIndex]);
    								dealerCard5.setIcon(dealerFifthCard);
    								int dealerCard5Value = cardValue[randomIndex];
    								setDealerSum(dealerCard5Value);
    								dealerCard5.setVisible(true);
    								System.out.println("DealerSum: " + getDealerSum());
    								
    								if(dealerCard5Value == 11){
    									dealerAceFlag++;
    								}
    								if(getDealerSum() > 21 && dealerAceFlag > 0){
    									setDealerSum(-10);
    									dealerAceFlag--;
    								}
    								if(getDealerSum() > 21){
    									playerHandValue.setText("Winner! Dealer Bust!");
    									endGame();
    								}else if(getDealerSum() <= 21){
    									playerHandValue.setText("Loser!");
    								}
    									
    							}
    								
    						}
    						
    					}
    				}
    			}
    		}
    	}
    	
    	public void dealPlayerCard3(){
    		int randomIndex= randomCard.nextInt(52);
    		Icon playerThirdCard = new ImageIcon(cards[randomIndex]);
    		playerCard3.setIcon(playerThirdCard);
    		playerCard3.setVisible(true);
    		int playerCard3Value = cardValue[randomIndex];
    		setPlayerSum(playerCard3Value);
    		if (playerCard3Value == 11){
    			playerAceFlag++;
    		}
    		if(playerAceFlag > 0 && getPlayerSum() > 21){
    			setPlayerSum(-10);
    			playerAceFlag--;
    		}
    		if(getPlayerSum() > 21){
    			System.out.println("PlayerScore: " + getPlayerSum());				playerHandValue.setText("BUSTED");
    			dealerCard2.setVisible(true);
    			endGame();			
    		} else if(getPlayerSum() == 21){
    			stayMe();
    		} else if(getPlayerSum() < 21){
    			playerHandValue.setText(Integer.toString(getPlayerSum()));
    		}
    		
    	}
    	
    	public void dealPlayerCard4(){
    		int randomIndex= randomCard.nextInt(52);
    		Icon playerFourthCard = new ImageIcon(cards[randomIndex]);
    		playerCard4.setIcon(playerFourthCard);
    		playerCard4.setVisible(true);
    		int playerCard4Value = cardValue[randomIndex];
    		setPlayerSum(playerCard4Value);
    		if (playerCard4Value == 11){
    			playerAceFlag++;
    		}
    		if(playerAceFlag > 0 && getPlayerSum() > 21){
    			setPlayerSum(-10);
    			playerAceFlag--;
    		}
    		if(getPlayerSum() > 21){
    			playerHandValue.setText("BUSTED");
    			dealerCard2.setVisible(true);
    			endGame();				
    		} else if(getPlayerSum() == 21){
    			playerHandValue.setText(Integer.toString(getPlayerSum()));
    			stayMe();
    		}else if(getPlayerSum() < 21){
    			playerHandValue.setText(Integer.toString(getPlayerSum()));
    		}
    	}
    	
    	public void dealPlayerCard5(){
    		int randomIndex= randomCard.nextInt(52);
    		Icon playerFifthCard = new ImageIcon(cards[randomIndex]);
    		playerCard5.setIcon(playerFifthCard);
    		playerCard5.setVisible(true);
    		int playerCard5Value = cardValue[randomIndex];
    		setPlayerSum(playerCard5Value);
    		if(getPlayerSum() > 21){
    			playerHandValue.setText("BUSTED");
    			dealerCard2.setVisible(true);
    			endGame();				
    		} else if(getPlayerSum() <= 21){
    			playerHandValue.setText("WINNER");
    			dealerCard2.setVisible(true);
    			endGame();
    		}		
    	}
    	
    	public void dealMe(){
    		
    		dealerCard1.setIcon(null);
    		dealerCard2.setIcon(null);
    		dealerCard3.setIcon(null);
    		dealerCard4.setIcon(null);
    		dealerCard5.setIcon(null);
    		
    		playerCard1.setIcon(null);
    		playerCard2.setIcon(null);
    		playerCard3.setIcon(null);
    		playerCard4.setIcon(null);
    		playerCard5.setIcon(null);
    		
    		
    		
    		hitMe.setVisible(true);
    		stayMe.setVisible(true);
    		dealerAceFlag = 0;
    		playerAceFlag = 0;
    		hitMeCount = 0;
    		cardIndex = 0;
    		initialDeal();
    		
    	}
    	
    	public void endGame(){
    		hitMe.setVisible(false);
    		stayMe.setVisible(false);
    		System.out.println("---------------------");
    		System.out.println("Player Score: " + getPlayerSum());
    		System.out.println("Dealer Score: " + getDealerSum());
    		
    	}
    }
    Last edited by Cookmeup272; 12-08-2011 at 06:11 PM.

  2. #2
    CHLim is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Default Re: Simple BlackJack Program

    Usually when you get problem on an off like in your case, the best thing to do is to call the setSeed method for the Random object. Using the setSeed method allows the program to be more predictable and therefore easier to debug.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Simple BlackJack Program

    The code is missing a definition for cardIndex.
    I get 3 compiler errors.

    Also its missing a main method. Very hard to test without a main method.
    Last edited by Norm; 12-09-2011 at 03:11 AM.

Similar Threads

  1. Blackjack Program Not Calculating Totals Correctly
    By skaterboy987 in forum New To Java
    Replies: 1
    Last Post: 10-19-2011, 09:59 PM
  2. Simple program, simple problem
    By taymilll in forum New To Java
    Replies: 12
    Last Post: 06-20-2011, 06:12 AM
  3. Need quick help with a Java blackjack program!
    By Enigma in forum New To Java
    Replies: 11
    Last Post: 06-01-2011, 10:59 AM
  4. Need ideas and help for a simple BlackJack Game
    By Dannii in forum New To Java
    Replies: 5
    Last Post: 04-27-2009, 11:53 AM
  5. Help with simple Blackjack Program?
    By meeper3000 in forum New To Java
    Replies: 2
    Last Post: 04-26-2009, 10: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
  •