Blackjack program in Java

Discussion in 'Programming & Scripting' started by confused5, Jan 7, 2009 with 10 replies and 381 views.

  1. confused5

    confused5 Enthusiast

    Messages:
    766
    Ratings:
    75
    228 line blackjack program i wrote in Java. It's pretty simple and well commented, so you should be able to figure out what's going on easily. Great for learning if statements, if-else chains, loops and nested loops, text I/O, working with strings, random number generating, and some strategies for designing computer players. I'm translating it to C++ later so I'll post it if anyone wants me to.

     
  2. Ibdc

    Ibdc Retired Retired

    Messages:
    5,100
    Ratings:
    2,072
    Looks good. Only mistakes I saw were "All choices are y/n"... except when you get an ace lol and your conditional statement to check if the input it y doesn't rule out other characters, instead it counts anything but y as n. And on play again anything but n counts as a y. Other than that it looks good.
     
  3. OP
    confused5

    confused5 Enthusiast

    Messages:
    766
    Ratings:
    75
    Thanks, I hammered out a few errors in school today so i edited with an updated version. I might write a few more if statements to catch that
     
  4. Ibdc

    Ibdc Retired Retired

    Messages:
    5,100
    Ratings:
    2,072
    Hate to do this to you, but the new version is a little buggier than the original. When it asks you if you want to hit and you don't hit y or n, it doesn't hit for you and the dealer's score stays 0, and then one time it said I had a score of 9 and busted. Also

    Card Values: 1, 8. Total Score: 9. Take a hit? y
    You have an Ace. Score as 1 or 11?y
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextInt(Scanner.java:2091)
    at java.util.Scanner.nextInt(Scanner.java:2050)
    at Blackjack.main(blackjack.java:72)

    One more little thing too, when it asks to hit again there's no space between after the ? so it looks like
    Hit Again?y instead of Hit again? y
    and
    You have an Ace. Score as 1 or 11?11 instead of 1 or 11? 11
    (not important, just thought you should know)
     
  5. OP
    confused5

    confused5 Enthusiast

    Messages:
    766
    Ratings:
    75
    Is it? Damn i spent ages on it lol
    Yeah it's looking for either 1 or 11, i'll try throwing an if (ace != 1 || ace != 11) in there to get it to loop back. Damn i wish Java had goto statements for small things like this. Nvm I'll convert everything to strings so nextInt isn't used

    Thanks, fixed. Upload a 3rd version when i change these

    The continue and break statements prevent me from checking (at least in Java) because they will affect the loop checking to see if the choice is valid instead of the hitting loop. I'll change it if i redo it in C++
     
  6. Amurka

    Amurka Contributor

    Messages:
    1,630
    Ratings:
    343
    I dont really know much about blackjack but...

    How could it be both?
     
  7. OP
    confused5

    confused5 Enthusiast

    Messages:
    766
    Ratings:
    75
    to be honest, im not quite sure why that happens lol. just restart the program and it should be fine
     
  8. Amurka

    Amurka Contributor

    Messages:
    1,630
    Ratings:
    343
    You should try to find the run time error and fix it eh? :thumbup:
     
  9. OP
    confused5

    confused5 Enthusiast

    Messages:
    766
    Ratings:
    75
    Yeah I'll look into it more. Something goes wrong in the if-else chain, but it's going to take some time to find the logic error. I've already run through my code and i can't find what causes it
     
  10. Amurka

    Amurka Contributor

    Messages:
    1,630
    Ratings:
    343
    If I knew exactly how to play blackjack I'd look at it for you but i dont really know how
     
  11. OP
    confused5

    confused5 Enthusiast

    Messages:
    766
    Ratings:
    75
    Yeah no problem, i'll work on it when i have some time. Here's an overview of blackjack if you want to tweak it though: [Click here to view the link]