I felt like a complete geek today, and I loved every minute of it. I’m already halfway through my book, “Beginning Programming with Java for Dummies.” I was actually nervous about reading through the book so quickly until I realized many of the programming basics that are taught in the book are concepts that I have already learned. I took C++ courses in college and a “Basic Programming I” course that I mastered in high school. I already have a grasp on things like:
- variables
- values
- types
- programming nests
- while statements
- switches
- loops
Apparently, these concepts are universal to any programming language. Getting them to work for the language you writing in is just a matter of syntax. As I was reading the chapter regarding “while statements,” I started to type in the sample code that the author provids for a game he refers to as “21.” It wasn’t java code for a blackjack game – apparently the author thought that would be too difficult for beginners who are only halfway through his book.
When I started to read through the sample code, the game uses a “while” loop to continue drawing a ‘card’ (a random number from 1 to 10) until the total of the numbers equals or exceeds 21. I wondered to myself, “What’s the point of that?” I don’t even get to decide if I want to take another card (run through the while loop again) or not. I keep taking cards until I get exactly 21 or until I bust – and that was the moment.
That was the moment when I felt my geeky inspiration and initiative spark. I haven’t felt it in so long, I was beginning to wonder if I still had geeky sparks of inspiration and initiative. Hell, I may only be halfway through the book, but I’m going to at least make this a little more challenging for myself and see what I can do.
The “while loop” in the sample code simple tests to determine if the total of the cards drawn is equal to or greater than 21. I inserted code that the author has already discussed that would ASK the player if they would like to take another card or not. My “while” loop doesn’t depend on the total value of the cards drawn, but to the answer character the player gives when asked if they would like another card. As long as the player answers ‘y’ to the question the “while loop” continues. Any answer other than a ‘y’ or a ‘Y’ breaks the while loop.
I was even inspired to format my output to the screen into columns so that the display of my simple console-based java application would look somewhat professional. I was able to take the sample code from this lesson as well as bits of code from previous lessons to generate a program that more closely resembles “21″ or blackjack – and then it happened again.
I’ve added a phase two to my project. ‘m pretty certain I’ve learned enough to continue this project and use the sample code and a few “if/else” statements to simulate a dealer’s hand. I can use conditions similar to the rules of real blackjack where the dealer would continue to take cards as long as the total is less than 17. In phase three, I can compare the dealer’s total with the player’s total and declare a winner. Phase four, will neaten it up a bit and make it look as close to real blackjack as possible; a player will see the dealer’s “show” card and their own cards before determining if they want to hit or stay – just like real blackjack..
It sounds simple, and I’m sure for those of you who have been programming or scripting as a profession or as a constant hobby, it is relatively simple. I’ll get to where you are eventually. It’s all part of my master plan!
Here’s my code – I’m so proud. Yes, I know it’s simple, but it’s a start! And yes, I know that after reading that long entry about my project you’re probably expecting 100+ lines of code. Nope – just this:
import java.util.Random;
import java.util.Scanner;
public class blackjack {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
Random myRandom = new Random();
int pcard, ptotal = 0;
char hitreply = ‘y’;
while (hitreply == ‘y’ || hitreply == ‘Y’) {
System.out.println(“Card\tTotal”);
pcard = myRandom.nextInt(10) + 1;
ptotal += pcard;
System.out.print(pcard);
System.out.print(“\t”);
System.out.print(ptotal);
System.out.println();
if (ptotal>21){
System.out.print(“You busted!”);
hitreply = ‘n’;
}else{
System.out.println();
System.out.print(“Would you like another card (y/n) “);
hitreply = myScanner.findWithinHorizon(“.”,0).charAt(0);
System.out.println();
}
}
}
}