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 10 of 10

Thread: Freezing on bust (BlackJack)

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Freezing on bust (BlackJack)

    Here is an SSCE ( Short version of program). Everything works except when the player busts the program freezes. I can't figure out why and It's starting to stress me out.

    I'd be willing to pay anyone to help me out with this one.

    import hsa.*;
     
    public class MyBlackJackProject
    {
    	public static void main(String[] args) 
    	{
    	Console con = new Console();
     
    	//Declare Variables
    	int again = 0;
     
     
    do
    {
    	int money = 1000;
    	double range = 26;
    	int randomGenNum;
    	int yourBet;
    	int userHandValue = 0, userDrawnValue;
    	int dealerHandValue = 0, dealerDrawnValue;
    	String playAgain;
    	String hit = "";
    	String strBetAmount;
    	String userCard, dealerCard;
     
    	int[] arrayCardValues = {0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10};
    	String[] arrayCardSuites = 
    	{
    	"","","Ace/Clubs","Ace/Diamonds","Ace/Hearts","Ace/Spades",
    	"2/Clubs","2/Diamonds","2/Hearts","2/Spades","3/Clubs","3/Diamonds","3/Hearts","3/Spades",
    	"4/Clubs","4/Diamonds","4/Hearts","4/Spades","5/Clubs","5/Diamonds","5/Hearts","5/Spades",
    	"6/Clubs","6/Diamonds","6/Hearts","6/Spades","7/Clubs","7/Diamonds","7/Hearts","7/Spades",
    	"8/Clubs","8/Diamonds","8/Hearts","8/Spades","9/Clubs","9/Diamonds","9/Hearts","9/Spades",
    	"10/Clubs","10/Diamonds","10/Hearts","10/Spades","Jack/Clubs","Jack/Diamonds","Jack/Hearts","Jack/Spades"
    	,"Queen/Clubs","Queen/Diamonds","Queen/Hearts","Queen/Spades","King/Clubs","King/Diamonds","King/Hearts","King/Spades"
    	};
     
    	//ask the user how much they want to bet
    	con.println("You currently have $" + money  + " to play with");
    	con.println("How much do you want to bet?");
    	strBetAmount = con.readLine();
    	yourBet = Integer.parseInt(strBetAmount);	
     
    	//dealers first card
    		randomGenNum = (int)((range * Math.random()) + 1)*2;
    		//assigning the dealer hand value & card, pulling index from array
    		dealerHandValue = arrayCardValues[randomGenNum];
    	//Check if the dealer got an ace
    		if ((randomGenNum >= 2) && (randomGenNum <= 5))
    			{
    			dealerHandValue = dealerHandValue + 10;
    			}
    		int tempDealerHandValue = dealerHandValue;
    		dealerCard = arrayCardSuites[randomGenNum];
    	//displaying values to the screen
    		con.println("The dealer is showing a " + dealerCard);
     
    	//dealers second card
    		randomGenNum = (int)((range * Math.random()) + 1)*2;
    	//assigning the dealer hand value & card, pulling index from array
    		dealerHandValue = arrayCardValues[randomGenNum];
    	//Check if the dealer got an ace
    		if ((dealerHandValue != 11) && (randomGenNum >= 2) && (randomGenNum <= 5))
    			{
    			dealerHandValue = dealerHandValue + 10;
    			}
    		dealerCard = arrayCardSuites[randomGenNum];
     
    	//displaying values to the screen
    		con.println("The dealers second card is " + dealerCard);
    		dealerHandValue = tempDealerHandValue + dealerHandValue;
    		con.println("The dealers total hand value is: " + (dealerHandValue));
    		con.println("");
     
     
    	//users first card
    		randomGenNum = (int)((range * Math.random()) + 1)*2;
    	//assigning the user hand value & card, pulling index from array
    		userHandValue = arrayCardValues[randomGenNum];
    	//Check if the user got an ace
    		if ((randomGenNum >= 2) && (randomGenNum <= 5))
    			{
    			userHandValue = userHandValue + 10;
    			}
    		int tempUserHandValue = userHandValue;
    		userCard = arrayCardSuites[randomGenNum];
    	//displaying values to the screen
    		con.println("Your first card is " + userCard);
    	//users second card
    		randomGenNum = (int)((range * Math.random()) + 1)*2;
    	//assigning the user hand value & card, pulling index from array
    		userHandValue = arrayCardValues[randomGenNum];
    	//Check if the user has an ace
    		if ((userHandValue != 11) && (randomGenNum >= 2) && (randomGenNum <= 5))
    			{
    			userHandValue = userHandValue + 10;
    			}
    		userCard = arrayCardSuites[randomGenNum];
    	//displaying values to the screen
    		con.println("Your second card is " + userCard);
    		userHandValue=tempUserHandValue+userHandValue;
    		con.println("Your total hand value is: " + (userHandValue));
    		con.println("");	
    	//check for the either player having a blackjack(hand value = 21)
    			if ((dealerHandValue == 21) && (userHandValue != 21))
    				{
    				money=money-yourBet;
    				con.println("Dealer has 21. You lose! You have $"+money+" left.");
    				}
     
    			if ((dealerHandValue != 21) && (userHandValue == 21))
    				{
    				money=money+yourBet;
    				con.println("Winner, Winner chicken dinner. You have 21. ! You have $"+money+" left.");
    				}
     
    			if ((dealerHandValue == 21) && (userHandValue == 21))
    				{
    				con.println("You tied with the dealer. Its a push. You have $"+money+" left."); 
    				}
     
    			if ((dealerHandValue != 21) && (userHandValue != 21))
    				{			
    					do
    					{
    					con.println("Would you like to hit? (Y/N)");
    					hit = con.readLine();
    					hit = hit.toUpperCase();
    					if (hit.compareTo("N") == 0 )
    					break;
    					if (hit.compareTo("Y") == 0 )
    				 		{	
    						again = 0;
    						//placeholder for giving user another card.
    						randomGenNum = (int)((range * Math.random()) + 1)*2;
    						//assigning the user hand value & card, pulling index from array
    						userDrawnValue = arrayCardValues[randomGenNum];
    						userHandValue = userDrawnValue + userHandValue;
    						userCard = arrayCardSuites[randomGenNum];
    						con.println("Your hit card is " + userCard);
    						con.println("Your hand value is "+userHandValue+".");
    						}
    					}	
    					while ((userHandValue < 21) && (again == 0));
    					con.println("");
     
    							//If the user exceeds 21
    							if (userHandValue > 21)
    							{
    							money=money-yourBet;
    							con.println("You busted! You have $"+ money +" left."); 
    							}							
     
    						else
    		 				again = 1;
    				}
     
    	//start the dealers loop
    	do
    	{
    	if ((dealerHandValue <= 16) && (again != 0))
    		{	
    				again = 1;
    				//placeholder for giving dealer another card.
    				randomGenNum = (int)((range * Math.random()) + 1)*2;
    				//assigning the dealer hand value & card, pulling index from array
    				dealerDrawnValue = arrayCardValues[randomGenNum];
    				dealerHandValue = dealerDrawnValue+dealerHandValue;
    				dealerCard = arrayCardSuites[randomGenNum];
    				con.println("The dealers hit card is " + dealerCard);
    				con.println("The dealers hand value is " + dealerHandValue + ".");
    		}
    	}
    	while (dealerHandValue <= 16);
    	//If the dealer exceeds 21
    	if (dealerHandValue > 21)
    		{
    		money=money+yourBet;
    		con.println("Dealer busted! You have $"+money+" left."); 
    		}				
     
    	//print final hand values
    	con.println("");
    	con.println("Your final hand value is: " + userHandValue);
    	con.println("The dealers final hand value is: " + dealerHandValue);
     
    	//if noone breaks or has 21 compare the dealer hand to the user hand to see who wins.
    	//If the dealer wins the hand.
    	if ((dealerHandValue < 22) && (dealerHandValue > userHandValue))
    		{
    		money=money-yourBet;
    		con.println("The dealer beat you! You have $"+money+" left.");
    		}
    	//If the player wins the hand.	
    	if ((userHandValue < 22) && (dealerHandValue < userHandValue))
    		{
    		money=money+yourBet;
    		con.println("You beat the dealer, Congratulations!!! You have $"+money+" left.");
    		}
    	//If there is a tie.
    	if (dealerHandValue == userHandValue)
    		{
    		con.println("You tied with the dealer. Its a push. You have $"+money+" left."); 
    		}
     
    	//See if the user wants to play again.
    	con.println("Would you like to play again? (Y/N)");
    	playAgain = con.readLine();
    	playAgain = playAgain.toUpperCase();
    	if (playAgain.compareTo("Y") == 0 )
    		again = 1;
    	else
    		 again = 0;
    	con.println("");	 
     
    }
    while (again == 1);
     
    	}	
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Freezing on bust (BlackJack)

    Quote Originally Posted by DaffyPWNS View Post
    Here is an SSCE
    It isn't short (over 200 lines) and its not compilable (I don't have the hsa lib).

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

    Default Re: Freezing on bust (BlackJack)

    http://staugustinechs.ca/cadawas/hsa.jar

    There are comments in the code. Also it's fairly short it's just if statements.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Freezing on bust (BlackJack)

    Add some println statements to see where the execution flow goes and the value of variables as they change. If there is a loop, it will show in the printed output

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Freezing on bust (BlackJack)

    I don't get what your saying I've tried this and I really just need visual help of some sort. I'm willing to pay.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Freezing on bust (BlackJack)

    I really just need visual help of some sort.
    If you add enough printlns in your code you will get more visual help than you need. It will show you where the loop is and why.

    Add some println statements to see where the execution flow goes and the value of variables.

  7. #7
    Junior Member
    Join Date
    Jun 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Freezing on bust (BlackJack)

    Can you please just figure this out for me ?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Freezing on bust (BlackJack)

    Sorry, I have told you how to find the error. What I suggested is exactly what I did to find the error.
    It can take a few minutes but I'm sure you will find it. You should know how to find bugs in your code if you want to learn programming.
    If you only want to pass the course, good luck.

  9. #9
    Junior Member
    Join Date
    Jun 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Freezing on bust (BlackJack)

    Got it thanks Norm

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Freezing on bust (BlackJack)

    Your very welcome.

    Always make sure there is a way out of a loop. The if without an else left a hole in the logic.

Similar Threads

  1. [Paypal] Blackjack basic java help.
    By DaffyPWNS in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 9th, 2011, 07:46 AM
  2. How to add card images to Blackjack game??
    By Jayc44 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 15th, 2011, 12:43 PM
  3. Blackjack simulation program help
    By senorfletch in forum Java Theory & Questions
    Replies: 2
    Last Post: April 5th, 2011, 09:22 AM
  4. Help with blackjack game
    By santosd1118 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 12th, 2010, 12:55 AM
  5. A little help with my Blackjack code
    By Neophyte in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 3rd, 2010, 01:13 PM