21 Sticks - Programming A Fun Java Game

Introduction

We're going to make a fun Java game that you can play yourself against the computer!

The game is called 21 Sticks (known also as 21 game), and we will use everything we learned in Java for Beginners in order to make this Java game.

The rules of our game are simple. You start with 21 sticks, and two players take turns either taking one or two sticks. The player who takes the last stick loses. Simple right?

Step One - Define Variables

The first thing we want to do to start our fun Java game example is figure out all of the things we need to keep track of. This is always a good idea before you start programming because it helps to create a programming plan. Imagine what would happen if no one planned before building a bridge!

Perhaps the most important variable to keep track of is the number of sticks. Let's make sure we have a variable at the beginning of our program that is the number of sticks, and let's make sure it starts at 21.

We also need to figure out how we will get the user input. A Java Scanner will do just fine for us. We need to ask the user two things: whether or not he/she would like to go first, and how many sticks he/she wants to pick up. Because mixing Scanners can be tricky, we'll create two scanners instead. We'll also need to keep track of the choices the player picks. For going first, we'll store that as a String. For the number of sticks, we'll use an int (integer).

Again, it's best to define all of your variables at the beginning of the program, so we'll keep all of these in mind. In the end this will better help us to code up our short but fun Java game!

Step Two - Program Logic

How is the 21 game played? Well, in this case players take turns taking sticks until there are no more sticks. Notice the word until. This usually means we are dealing with a while loop. The taking turns depends on who goes first, the player or the computer. Since it depends on who is first, we'll probably need an if statement to figure out who's first.

How does a player lose? Only if the player takes the last stick. Oh wait, that means we need more if's to take care of that.

Step Three - Code... One Step At A Time

Now it's time to start programming our fun Java game example. Create a new Java file called TwentyOneSticks and create the main method. If you've forgotten how to do that, take a look at Making Your First Program to see how it was done with Hello World.

Let's go back to the list of variables we absolutely needed to have. First, we needed to keep track of the number of sticks. Let's write that:

Let's also create the two Scanners we'll need in order to get both who goes first and the number of sticks from the user:

Here is also where we should ask the player if they'd like to go first or not. We'll make them have to type either 'Y' or 'y' in order to go first. If they don't type either of those, we can assume they will go second. For now, ask the player if they'd like to go first and store their option inside of a String variable.

That should be good for now. Let's figure out what happens once the player has decided to go first or not. We know that our Java game now has to keep going until the last stick has been taken. This means the rest of our code is going to be inside of a loop, specifically a while loop.

Now, either the computer will make a move first or the player will, depending on who was chosen to go first by the player. So, we'll need an if-else statement. If the player goes first, we'll ask the player to pick sticks, and then the computer, otherwise it will be the other way around. So now, our code should look like this:

You can test to see if this part of our Java game works by adding in a System.out.println("Player goes first"); inside of the if, and System.out.println("Computer goes first"); inside the else. Note that your code will not ever stop running! Why? Because remember the while loop we added; it will only stop looping when the number of sticks is equal to or less than zero!

Let's create the 21 game for when the player goes first. We won't worry about the other way around yet, since if we can do it this way, making the game go the other way is easy!

Since the player is going first, we need to let him or her know how many sticks there are left. Then, we want to ask how many sticks they'd like to take, either one or two sticks. Then, get the input and subtract that many sticks away:

From here, two things can happen. Either the player took the last stick, or it's the computer's turn to take sticks. That means we once again need an if statement. Remember that any time there are two or more possible ways your program can go, you need to use an if statement. So, how do we check if the player took the last stick?

Inside the else part, we want to make the computer take its turn. Here we'll use some simple artificial intelligence in order to make the computer decide how many sticks to take. Our computer's logic will be simple: if taking two sticks makes it lose the game, or if taking two sticks leaves some multiple of three left, then only take one, otherwise take two. It's not so important to understand this logic, but we'd like the computer not to play too poorly else our small but fun Java game may not be as much well, fun. You can try out the game for yourself later to see if you think the computer is too easy.

Last, we want to tell the player how many sticks the computer took, and then we also want to subtract the number of sticks it took. If the computer took the last stick, the player wins!

You can now try out your fun Java game! Make sure to type in y or Y so that you take your turn first. We haven't coded the game to work the other way yet!

One last thing we should do before working on the computer going first: we need to make sure the player can only take away one or two sticks. If you look at the code, what will happen if they decide they want 20 sticks? Nothing is stopping the player from doing so! This will ruin our Java game to no end! To prevent this, we'll do some simple error checking and handling. If the player types a number less than one, we'll assume he or she meant one. If he or she types in a number greater than two, we'll assume it to be two.

Now, to make your fun Java game play the other way, we only have to flip around the code so that the computer plays first. What part of the code do we need to take from the first half of the code? Every part that makes the computer play, of course!

In the first half of the code, you checked if the player lost. If not, the computer took its turn. We need to do the same thing here. If the computer loses, the player wins. Otherwise, the player takes its turn.

Your little, fun Java game is now complete! 21 Sticks is a great logic game to play, and I hope that the logic for programming it proved fun as well! Here is the completed code for you to check your code with. You'll notice that the game logic is not very long. It really is a quick little game to make!

I hope you enjoyed this short but fun Java game example. There will be many more games like the 21 Game and other examples for you to practice your Java programming skills, so stay tuned!

If you have any questions, comments, or concerns, feel free to contact us.

Custom Search