Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By jashburn

Thread: Need help with Blackjack Program

  1. #1
    phyxfire is offline Member
    Join Date
    May 2014
    Posts
    2
    Rep Power
    0

    Exclamation Need help with Blackjack Program

    I've been trying to finish this blackjack program I have been making for my programming class, however, there are a few bugs I can't seem to iron out. It's a pretty short program, it's just that I'm an idiot and can't figure basic stuff like this out. Specifically, when I try and run it, I get this error:

    Error occurred during initialization of VM
    java.lang.OutOfMemoryError: unable to create new native thread

    Java Code:
    import static java.lang.System.*;
    import java.applet.Applet;
    import java.util.Scanner;
    import java.io.*;
    import java.util.Random;
    import java.util.Date;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Color;
    import java.util.ArrayList;
    import javax.swing.*;
    
    public class BlackJack
    {
    
    public static void main (String[] args) throws IOException
    {
    Scanner kb = new Scanner (System.in);
    Scanner in = new Scanner("c:\\highscores.txt");
    PrintWriter outln = new PrintWriter("c:\\highscores.txt");
    int yourAmount = 0;
    int wins = 0;
    int losses = 0;
    int dealerAmount = 0;
    Random r = new Random();
    out.println("Type 'hit' to hit, and type 'end' to end.");
    String input = kb.nextLine();
    wins = Integer.parseInt(in.nextLine());
    losses = Integer.parseInt(in.nextLine());
    if("hit".equals(input))
    {
    } else {
    int num = r.nextInt(10) + 1;
    yourAmount += num;
    out.println("Your Total: " + yourAmount);
    out.println("Dealer Total: " + dealerAmount);
    while(dealerAmount <= 16)
    {
    num = r.nextInt(10) + 1;
    dealerAmount += num;
    }
    }
    if(dealerAmount == 21)
    {
    out.println("Dealer Wins!");
    losses += 1;
    }
    if(yourAmount == 21)
    {
    out.println("You Win!");
    wins += 1;
    }
    if("end".equals(input))
    {
    if(dealerAmount == 21 && yourAmount == 21)
    {
    out.println("It's a tie!");
    }
    if(yourAmount > 21 && dealerAmount < 21)
    {
    out.println("Dealer Wins!");
    losses+=1;
    }
    if(dealerAmount > 21 && yourAmount < 21)
    {
    out.println("You Win!");
    wins+=1;
    }
    }
    
    outln.write("Wins: " + wins);
    outln.write("Losses: " + losses);
    
    
    
    }
    }
    pastebin link to not clutter the first post, because god knows I find that annoying.

    So, obviously I'm doing something horribly wrong. Any help would be appreciated, as it is due two days from now. Also, if you notice any other terrible errors, it would benefit me greatly if you could suggest improvements, particularly if you understand the rules of blackjack better than I do.
    Last edited by phyxfire; 05-21-2014 at 08:54 PM.

  2. #2
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Need help with Blackjack Program

    Please edit your post and paste your code into your post (not as a link to another web site). Wrap your code with [code] tags so that it is easier to read, e.g.,
    [code=java]
    // your code here
    [/code]

    It preserves formatting and provides syntax highlighting, e.g.,
    Java Code:
    // your code here
    I'm not able to reproduce the problem you described using your code. There can be a number of causes to the problem. Some questions:
    • How do you run the program?
    • Do you include additional startup/command line arguments when you run the program?

    If you have not done anything "special", then try rebooting your machine. (See OutOfMemoryError: Unable to Create New Native Thread – Problem Demystified | Javalobby for an in-depth explanation on this error.)

    As for the program itself, it wouldn't work properly even if the problem you described does not occur. The 2 biggest issues are:

    1. Scanner in = new Scanner("c:\\highscores.txt");

    This creates a Scanner object to produce values by scanning the specified string. See Scanner (Java Platform SE 7 ) for the right constructor to use in order to create a Scanner to scan values in a file.

    2. PrintWriter outln = new PrintWriter("c:\\highscores.txt");

    This creates a PrintWriter object for the file indicated by the specified string. Quoting from PrintWriter (Java Platform SE 7 ), "If the file exists then it will be truncated to zero size; otherwise, a new file will be created."

    Assuming you have fixed issue #1 above so that the Scanner points to a proper file (which is the same file as the one you provided to the PrintWriter), creating the PrintWriter object as in the above then interferes with the following line of code:
    Java Code:
    wins = Integer.parseInt(in.nextLine());
    This is because the file highscores.txt will be empty (due to the PrintWriter instantiation code above), and so in.nextLine() will throw an exception.
    gimbal2 likes this.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Need help with Blackjack Program

    Here are a few suggestions. I don't know what your assignment is, but its fun to take it as far as you can regardless.

    - the dealer starts with two cards, so you should already be randomly generating two of his/her cards BEFORE you get to play

    - when you play, you always see those first two cards of the dealer; this helps you to decide if you should hit or not. So you should tell to the user that the dealer has '16' for example

    - There is also the concept of the "instant blackjack", as in you have 21 right away when the cards are dealt. This means either you or the dealer automatically wins without even doing any further playing. You could implement that as it is not too difficult. But then also consider the third option that will be rare but not impossible: that you both get instant blackjack.

    - Say the dealer hits 17 initially, then there is no reason at all for the player to stop hitting as long as his/her own number is 16 or below that or its a guaranteed loss. So make your program such that the user doesn't have the choice to stop until his/her own number is equal to or higher than the hand of the dealer.

    - don't track only wins, also track how many times you had blackjack!

    - you should actually have a higher chance to draw the number '10' as there are multiple cards with that value. That increases the chance of getting blackjack. This SOUNDS hard to accomplish, but there are tricks to very easily do this. Consider it a challenge to try and find one; the code does not have to be pretty or clever.


    Fun-factor additions:

    - add the concept of money to the game in stead of only having wins and losses. Losing all your money is a game over condition. Say you start with 100 GP and every game you lose 5 GP to the dealer or win 5 GP back from him.

    - having money, you might consider there to be a maximum amount the dealer has so you can "break the bank"; the best possible victory condition in the game

    - getting blackjack wins you quadruple GP, or if the dealer gets blackjack you lose quadruple GP. High stakes but it gives you more of an incentive to try and get it


    Version 2.0 suggestions:

    And finally this is way too much to build at this point, but blackjack also knows the concepts of insurance, splitting and doubling down. Those would be something for a 2.0 version of the game.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    mbschultz97 is offline Super OP Noob
    Join Date
    May 2014
    Location
    Virginia
    Posts
    65
    Rep Power
    0

    Default Re: Need help with Blackjack Program

    Quote Originally Posted by phyxfire View Post
    particularly if you understand the rules of blackjack better than I do.
    I could help you with that... any questions about the rules? :)

  5. #5
    phyxfire is offline Member
    Join Date
    May 2014
    Posts
    2
    Rep Power
    0

    Default Re: Need help with Blackjack Program

    Thanks for all the replies!
    OK, so I worked a little with it today, and managed to get it to run, but I can't seem to figure out how to read a write to the same file without screwing it up horribly. The other thing I'm having trouble with is getting the computer to record that you have typed hit more than once. In other words, I tried using a while loop to make it continue the program until the user types end, but it just created an infinite loop. Could this be because the variable 'input' only records it once? Can I do anything to prevent this?
    Gimbal, I would really like to implement all the stuff you suggested, but it is due tomorrow and I am freaking the hell out. I did try to fix the conditional statements that kept you from winning if no one reached 21 or went over, but I can't even check if it works yet.
    Jashburn, I made the PrintWriter into a BufferedWriter and made it so it writes before it reads, but how can I make them work in conjunction?
    I could help you with that... any questions about the rules? :)
    No specific questions, just need to make sure I am implementing the rules right. Turns out Blackjack is a lot more complicated (particularly in code form) than I thought.

    Oh, and here's the updated code:

    Java Code:
    import static java.lang.System.*;
    import java.applet.Applet;
    import java.util.Scanner;
    import java.io.*;
    import java.util.Random;
    import java.util.Date;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Color;
    import java.util.ArrayList;
    import javax.swing.*;
    
    public class BlackJack
    {
    
    public static void main (String[] args) throws IOException
    {
    	Scanner kb = new Scanner (System.in);
    	BufferedWriter outln = new BufferedWriter(new FileWriter("H:\\Programming I\\winsandlosses.txt"));
    	Scanner in = new Scanner(new File("H:\\Programming I\\winsandlosses.txt"));
    	int yourAmount = 0;
    	int wins = 0;
    	int losses = 0;
    	outln.write("0");
    	outln.newLine();
    	outln.write("0");
    	outln.flush();
    	int dealerAmount = 0;
    	Random r = new Random();
    	out.println("Type 'hit' to hit, and type 'end' to end.");
    	String input = kb.nextLine();
    	wins = Integer.parseInt(in.nextLine());
    	losses = Integer.parseInt(in.nextLine());
    
    	if(kb.nextLine() == "hit")
    	{
    		int num = r.nextInt(10) + 1;
    		yourAmount += num;
    		out.println("Your Total: " + yourAmount);
    		out.println("Dealer Total: " + dealerAmount);
    		while(dealerAmount <= 16)
    		{
    			num = r.nextInt(10) + 1;
    			dealerAmount += num;
    		}
    	}
    
    if(dealerAmount == 21)
    {
    	out.println("Dealer Wins!");
    	losses += 1;
    }
    if(yourAmount == 21)
    {
    	out.println("You Win!");
    	wins += 1;
    }
    if("end".equals(input))
    {
    	if(yourAmount < dealerAmount && dealerAmount <21)
    	{
    		out.println("Dealer Wins!");
    		losses+=1;
    	}
    	if(yourAmount > dealerAmount && yourAmount <21)
    	{
    		out.println("You Win!");
    		wins+=1;
    	}
    	if(dealerAmount == 21 && yourAmount == 21)
    	{
    		out.println("It's a tie!");
    	}
    	if(yourAmount > 21 && dealerAmount < 21)
    	{
    		out.println("Dealer Wins!");
    		losses+=1;
    	}
    	if(dealerAmount > 21 && yourAmount < 21)
    	{
    		out.println("You Win!");
    		wins+=1;
    	}
    }
    
    outln.write(wins);
    outln.write(losses);
    
    
    
    }
    }

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default Re: Need help with Blackjack Program

    I didn't actually read this thread (I was spammer killing), but I read line #35: you don't compare Strings for equality with the == operator; use the .equals( ... ) method instead.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Need help with Blackjack Program

    The problem that Jashburn describes is still in there.

    Java Code:
    BufferedWriter outln = new BufferedWriter(new FileWriter("H:\\Programming I\\winsandlosses.txt")); // creates an empty file winsandlosses.txt
    Scanner in = new Scanner(new File("H:\\Programming I\\winsandlosses.txt")); // will read from an empty file winsandlosses.txt
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Need help with Blackjack Program

    To handle reading from and writing to the file, the easiest thing to do is to do one thing at a time. E.g., you can start the program by first reading from the file using Scanner to get the number of wins and losses so far. Close the Scanner once you're done reading. Once the game is over, you can write the updated number of wins and losses to the file using BufferedWriter, and close the BufferedWriter once you're done writing. (Note that this assumes the file already exists when you start the program, and that it contains numbers for wins and losses. Get this working first. You can later add code to handle the case where you need to first create and pre-populate the file - if the file is missing.)

    I'd suggest putting your file-reading and file-writing code into their own static methods so as not to clutter the main method. You can also use static variables to keep track of the wins and losses, for your file-reading code to update the variables, as well as for your file-writing code to get the values of the variables to be written.

    Using a while loop to handle multiple hits is easy. E.g.,
    Java Code:
    Scanner kb = new Scanner(System.in);
    
    // Read keyboard input first before the loop.
    String decision = kb.nextLine();
    
    while (!decision.equals("end")) {
        // You can later put an if statement here to handle if decision is neither "hit" nor "end".
    
        // ... your code to handle a "hit" as well as if the player busts after hitting ...
    
        // Read keyboard input again for the next iteration of this while loop.
        decision = kb.nextLine();
    }
    At this stage it's fine to work mainly in the main method. Later on you should learn to create a BlackJack object, and work with instance (not static) variables and methods.

Similar Threads

  1. Need help with BlackJack Program
    By SilverSoul in forum New To Java
    Replies: 3
    Last Post: 04-25-2012, 01:52 PM
  2. Simple BlackJack Program
    By Cookmeup272 in forum New To Java
    Replies: 2
    Last Post: 12-09-2011, 02:09 AM
  3. Blackjack Program Not Calculating Totals Correctly
    By skaterboy987 in forum New To Java
    Replies: 1
    Last Post: 10-19-2011, 08:59 PM
  4. Need quick help with a Java blackjack program!
    By Enigma in forum New To Java
    Replies: 11
    Last Post: 06-01-2011, 09:59 AM
  5. Help with simple Blackjack Program?
    By meeper3000 in forum New To Java
    Replies: 2
    Last Post: 04-26-2009, 09:46 AM

Tags for this Thread

Posting Permissions

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