I need a little help with my BlackJack program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jfeldman
    New Member
    • Apr 2010
    • 1

    #1

    I need a little help with my BlackJack program

    I am a little confused with how to get this method to determine what the best choice is for the number of aces that should be counted as 1 or 11. I will post the method and the code so you can run it. Any help is greatly appreciated

    Code:
    public static int weHaveAces(int player[], int deal[])
    {
    	int plyttl = 0;
    
    	for(int x = 0; x < player.length - 1;x++)
    
    		{
    			plyttl += player[x];
    			System.out.println(plyttl);
    		}
    		int bestttl = plyttl;
    	for(int z = 0; z <= player[2];z++)
    		{
    			//System.out.println(( plyttl - (10*z)));
    			//System.out.println(21 - plyttl);
    			while((21 - ( plyttl - 10*z)) < 21 - plyttl)
    			{
    
    				bestttl = (plyttl - 10*z);
    				//System.out.println(bestttl);
    				//System.out.println("test"+ z);
    			}
    		}
    
    
    Whole Code:
    
    /*Jordan Feldman
    Pitt BlackJack
    4/5/2010*/
    
    import javax.swing.JOptionPane;
    
    class BlackJack
    {
    public static void main(String args[])
    	{
    		int playerCard1 = 0, playerCard2 = 0, dealerCard1 = 0, dealerCard2 = 0, playerAces = 0, dealerAces = 0;
    
    		int[] playerCards =  new int [3];// Creates an int array with 3 positions
    
    		makeDeck(playerCards);
    
    		playerCard1 = playerCards[0];
    		playerCard2 = playerCards[1];
    		playerAces = playerCards[2];
    
    		int[] dealerCards =  new int [3];// Creates an int array with 3 positions
    
    		makeDeck(dealerCards);
    
    		dealerCard1 = dealerCards[0];
    		dealerCard2 = dealerCards[1];
    		dealerAces = dealerCards[2];
    
    
    		printMethod(playerCards,dealerCards);
    		firstLogic(playerCards,dealerCards);
    
    	}
    
    public static void makeDeck(int cards[])
    	{
    		int y = 0;
    
    		for(int x = 0;x < 2; x++)
    		{
    		cards[x] = 1;//(int)(Math.random()* 13 +1); //Generates a random value between 1-13
    		if (cards[x] == 11 || cards[x] == 12 || cards[x] == 13)
    			{
    				cards[x] = 10;
    			}
    		else if (cards[x] == 1)
    		{
    			cards[x] = 11;
    			++y;
    
    		}
    	}
    		cards[2] = y;
    		//System.out.println(cards[2]);
    	}
    public static void printMethod(int playerCards[], int dealerCards[])
    	{
    		String name;
    
    		int x = playerCards[0];
    		int y = playerCards[1];
    		int playerTotal = x + y;
    
    		System.out.println("Welcome to the River's Casino!");
    		name = JOptionPane.showInputDialog("What is your name?");
    		System.out.println("Let's play BlackJack " + name + ".\n");
    
    		System.out.println("The dealer:\n? + "+ dealerCards[1] + "\n");
    
    		System.out.println("You:\n" + playerCards[0] + " + " + playerCards[1] + " = " + playerTotal);
    		int plyttl = weHaveAces(playerCards,dealerCards);
    		System.out.println("Your best choice is " + plyttl);
    
    	}
    public static void firstLogic(int player[], int dealer[])
    	{
    		int acecnt;
    		if(dealer[2] >= 1 || player[2] >= 1)
    		{
    			// weHaveAces(player,dealer);
    		}
    		else
    		{
    			noAces(player,dealer);
    		}
    	}
    public static int weHaveAces(int player[], int deal[])
    {
    	int plyttl = 0;
    
    	for(int x = 0; x < player.length - 1;x++)
    
    		{
    			plyttl += player[x];
    			System.out.println(plyttl);
    		}
    		int bestttl = plyttl;
    	for(int z = 0; z <= player[2];z++)
    		{
    			//System.out.println(( plyttl - (10*z)));
    			//System.out.println(21 - plyttl);
    			while((21 - ( plyttl - 10*z)) < 21 - plyttl)
    			{
    
    				bestttl = (plyttl - 10*z);
    				//System.out.println(bestttl);
    				//System.out.println("test"+ z);
    			}
    		}
    
    	return bestttl;
    
    }
    
    
    
    public static void noAces(int ply[], int deal[])
    {
    
    }
    
    
    }
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    I would:
    1. Have a flag that says whether you have an ace or not.
    2. Count the aces as 1 to the total.
    3. When you display the total,
    - Check if you have an ace
    - Check if the total is < 11.
    If so, add 10 to the 'displayed' total.

    Comment

    Working...