Text Based Blackjack Java Code

The best tool for Java programmers as they begin writing their first programs is a debatable topic. Their goal has to be learning the basics of the Java language. It's also important that the programming should be fun. Fun for me is writing and running programs with the least amount of hassle. The question then becomes not so much how to learn Java as where. The programs have to be written somewhere and choosing between using a type of text editor or an integrated development environment can determine just how much fun programming can be.

What Is a Text Editor?

One direction where we can take our programming skills is game development. Here, we'll build a text based Blackjack engine that allows us to play against a dealer, who follows conventional house rules. The logic of blackjack is simple, but is sufficiently complex that we can gain valuable experience for making more complicated games later on. BlackJack is a situation in which a player gets an addition of 2 cards that equals 21. For example, and Ace and a Jack. A BlackJack hand beats all hands except another BlackJack hand, which it will push. In the event that a player hits on his cards and the addition of the cards is more than 21, he/she is out of the game and is a bust. Large Collection of JavaScript source code. Choose from thousands of free scripts. JavaScript tutorials with example code. Excellent reference material for JavaScript. If you need help with JavaScript. JavaScript Made Easy is the place to find it.

There isn't a way to spruce up what a text editor does. It creates and edits files that contain nothing more than plain text. Some won't even offer you a range of fonts or formatting options.

Using a text editor is the most simplistic way to write Java programs. Once the Java code is written it can be compiled and run by using command-line tools in a terminal window.

Example Text Editors: Notepad (Windows), TextEdit (Mac OS X), GEdit (Ubuntu)

What Is a Programming Text Editor?

There are text editors that are made specifically for writing programming languages. We're calling them programming text editors to highlight the difference, but they are generally known simply as text editors. They still only deal with plain text files but they also have some handy features for programmers:

  • Syntax Highlighting: Colors are assigned to highlight different parts of a Java program. It makes code easier to read and debug. For example, you could set up syntax highlighting so that Java keywords are blue, comments are green, string literals are orange, and so on.
  • Automatic Editing: Java programmers format their programs so that blocks of code are indented together. This indentation can be done automatically by the editor.
  • Compilation and Execution Commands: To save the programmer having to switch from the text editor to a terminal window these editors have the ability to compile and execute Java programs. Therefore, debugging can be done all in one place.

Example Programming Text Editors: TextPad (Windows), JEdit (Windows, Mac OS X, Ubuntu)

What Is an IDE?

IDE stands for Integrated Development Environment. They are powerful tools for programmers that offer all the features of a programming text editor and much more. The idea behind an IDE is to encompass everything a Java programmer could want to do in one application. Theoretically, it should allow them to develop Java programs faster.

There are so many features an IDE can contain that the following list contains only a selected few. It should highlight how useful they can be to programmers:

  • Automatic Code Completion: Whilst typing in Java code the IDE can help by showing a list of possible options. For example, when using a String object a programmer might want to use one of its methods. As they type, a list of methods they can choose from will appear in a popup menu.
  • Access Databases: To help connect Java applications to databases IDEs can access different databases and query data contained within them.
  • GUI Builder:Graphical user interfaces can be created by dragging and dropping Swing components onto a canvas. The IDE automatically writes the Java code that creates the GUI.
  • Optimization: As Java applications become more complex, speed and efficiency become more important. Profilers built into the IDE can highlight areas where the Java code could be improved.
  • Version Control: Previous versions of source code files can be kept. It's a useful feature because a working version of a Java class can be stored. If in the future it is modified, a new version can be created. If the modifications cause problems the file can be rolled back to the previous working version.

Example IDEs: Eclipse (Windows, Mac OS X, Ubuntu), NetBeans (Windows, Mac OS X, Ubuntu)

What Should Beginner Java Programmers Use?

For a beginner to learn the Java language they don't need all the tools contained within an IDE. In fact, having to learn a complex piece of software can be as daunting as learning a new programming language. At the same time, it's not much fun to continually switch between a text editor and a terminal window in order to compile and run Java programs.

Our best advice tends to favor using NetBeans under the strict instructions that beginners ignore almost all of its functionality at the beginning. Focus purely on how to create a new project and how to run a Java program. The rest of the functionality will become clear when it's needed.

Blackjack Source Code Java

#BlackjackA text-based Blackjack game with a dealer and one player (user)

Blackjack is a simple Java application that I wrote for an AP Computer Science class, taken in high school. It currently lacks a GUI, but contains all the correct logic and fun of an actual game of Blackjack (Five Card Charlie is even a way to win!)

Note: A .zip download is also availble on my site.

##How to Play

BlackJack is a card game in which the object of the game is to get closer to a hand value of 21 than the dealer. The value of the cards is as follows:

  • 2-10 are face value
  • Jack-King are valued at 10
  • Ace is valued as either 11 or 1 (based on hand)

Both the dealer and the player are originally dealt 2 cards each, but the dealer only shows one, while you show both. To take another card, hit, if you are satisfied with your hand, stay, if you'd like to double your bet, you must take one card and one card only by doubling down.

Java Blackjack Game Code

The most basic way to win is by getting closer to 21 than the dealer without exceeding 21, but you may also win by:

  • BlackJack: hand contains an ace and a card of value 10
  • Five Card Charlie: obtaining five cards with a value that is <= 21
  • Dealer bust: dealer's hand exceeds 21

To play the game, simply follow the in-game instructions.

##Table of Contents

src/card contains source code for:

Program
  • Card
  • CardTester
  • Deck
  • DeckTester
  • Shoe
  • ShoeTester
  • Hand
  • HandTester
  • Dealer
  • Player
  • GUI

dist/javadoc contains JavaDocs HTML

##ContributingIf you're interested in pulling this project, I'd love to accept your request! Make a GUI for it and push it through!

##Example Output

Welcome to Jack Kasbeer's BlackJack!
What is your name?
Jack
Welcome to BlackJack, Jack!
How much money would you like to start with?
10000
Please place a bet:
500

Dealer is showing:
Rank: King
Suit: Clubs
Numeric Value: 10

Your Cards:
Rank: Ten
Suit: Spades
Numeric Value: 10

Rank: Nine
Suit: Spades
Numeric Value: 9

Would you like to hit(1), stay(2), or double down(3)?
2

YOUR HAND:
Rank: Ten
Suit: Spades
Numeric Value: 10

Rank: Nine
Suit: Spades
Numeric Value: 9

Text Based Blackjack Java Code

DEALER'S HAND:
Rank: King
Suit: Clubs
Numeric Value: 10

Rank: Ten
Suit: Diamonds
Numeric Value: 10

Dealer wins!

Your current bank roll: 9500.0
Would you like to play again? 1 for YES, 2 for NO.
2

Thanks for playing!

Blackjack On Java

##Side notesIn the event of a tie, dealer wins.You earn 1.5 times your bet if you have Blackjack or Double Down and win

Java Program Blackjack Code

##Known BugsMy BlackJack is fairly solid, but you cannot enter in a String for starting amount of money.There is also an issue with the Ace: every now and then it doesn't change value when you bust.

##Future additions

  • Add 'insurance' options
  • Allow the user to 'split' when they have a pair
  • Graphic User Interface
  • Multiple players in one game
  • Play against your friends without an AI

##HighlightsNote:This section was part of my assignment for the class, and isn't really useful to someone interested in downloading the game.

I still plan on finishing my GUI so that I can truly be proud of my game, but as of now I am proud of my use of try/catch methods because it solidifies the game. I am also proud of my use of recursion because it is more efficient for try/catches and is more complex than what we've learned.