Programming Blackjack In Java
A game of blackjack in Java. GitHub Gist: instantly share code, notes, and snippets. With a proper OO design, you should only have to return one thing. You should be able to create a 'card' class that would hold the rank (A-K) and a suit. I would NOT have it return a value, since the value is not a property of the card, but of the rules of the game of blackjack. Blackjack.java is the 'Main' class in this program, which uses the other files (including yours) to run a Blackjack program. This file will only work when you complete the homework and you can run the file to simulate a blackjack game. Review the class material on ArrayLists and class material and textbook on interfaces. I've created a relatively simple Blackjack game in java. The reason why I decided to do this specific project was to improve my object orientated programming in java. I will post my code so feel free to come with criticism etc. I am reading my first course in Java, have that in mind.
Blackjack
Blackjack is a simple java program demonstrating file I/0 and using multiple classes in java. The program deals a blackjack hand, and checks the user's decisions (hit, stand, double or split) against correct strategy. The correct strategy is laid out in the text file 'bjin.txt' which should reside in the same directory as the program.
The program does not keep score in its present form, it simply checks the user's input (hit, stand, split or double) against the correct decision from bjin.txt. When the player chooses to split a pair, only one of the split hands is played. Type 'quit' at the command line to exit the program. Bjin.txt can be modified to change what the 'correct' decision is for a given hand.
The program consists of three java classes, Deck.java, Hand.java, and Blackjack.java.
Deck.java is a 52 card deck. The deal() function returns a string consisting of a numeric value, or J, Q, K, or A, representing a random card. When a card is dealt, deck marks that card as missing. deck will not reshuffle automatically, and will fail if more than 52 cards are dealt. The shuffle() function reshuffles the deck.
Hand.java represents a person's hand (either the player or the dealer). The hit() function takes a string consisting of a numeric value, or J, Q, K, or A, and adds it to it's array of cards. The other functions return different aspects of a hand.
Blackjack.java is where the main() loop is. The only real member variable it has is the array consisting of the correct choices under each situation.
This program can be compiled into a windows executable with gcj in cygwin, using the command:
gcj --main=Blackjack -mno-cygwin Blackjack.java Hand.java Deck.java -o blackjack.exe
This program can be compiled to java class files and run using the following commands:
javac Blackjack.java
javac Hand.java
Javac Deck.java
java Blackjack
Files
Java Program Blackjack Code
Simple Blackjack Java Program
Blackjack.java
Hand.java
Deck.java
bjin.txt This is a sample file, and not correct strategy in all cases
blackjack.exe For win32. Compiled using gcj under cygwin.