You can try out the game for yourself later to see if you think the computer is too easy. 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. 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. Here we'll use some simple artificial intelligence in order to make the computer decide how many sticks to take. Inside the else part, we want to make the computer take its turn. So, how do we check if the player took the last stick? Remember that any time there are two or more possible ways your program can go, you need to use an if statement. That means we once again need an if statement. Either the player took the last stick, or it's the computer's turn to take sticks. Then, get the input and subtract that many sticks away:įrom here, two things can happen. Then, we want to ask how many sticks they'd like to take, either one or two sticks. Since the player is going first, we need to let him or her know how many sticks there are left. 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! Let's create the 21 game for when the player goes first. 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! You can test to see if this part of our Java game works by adding in a ("Player goes first") inside of the if, and ("Computer goes first") inside the else. 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. Now, either the computer will make a move first or the player will, depending on who was chosen to go first by the player. This means the rest of our code is going to be inside of a loop, specifically a while loop. We know that our Java game now has to keep going until the last stick has been taken. Let's figure out what happens once the player has decided to go first or not. For now, ask the player if they'd like to go first and store their option inside of a String variable. If they don't type either of those, we can assume they will go second. We'll make them have to type either 'Y' or 'y' in order to go first. Here is also where we should ask the player if they'd like to go first or not. 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: First, we needed to keep track of the number of sticks. Let's go back to the list of variables we absolutely needed to have. 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. Create a new Java file called TwentyOneSticks and create the main method. Now it's time to start programming our fun Java game example. Oh wait, that means we need more if's to take care of that. How does a player lose? Only if the player takes the last stick. Since it depends on who is first, we'll probably need an if statement to figure out who's first. The taking turns depends on who goes first, the player or the computer. This usually means we are dealing with a while loop. How is the 21 game played? Well, in this case players take turns taking sticks until there are no more sticks. In the end this will better help us to code up our short but fun Java game! For the number of sticks, we'll use an int (integer).Īgain, it's best to define all of your variables at the beginning of the program, so we'll keep all of these in mind. For going first, we'll store that as a String. We'll also need to keep track of the choices the player picks. Because mixing Scanners can be tricky, we'll create two scanners instead. 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. We also need to figure out how we will get the user input. 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. Perhaps the most important variable to keep track of is the number of sticks. Imagine what would happen if no one planned before building a bridge! This is always a good idea before you start programming because it helps to create a programming plan. 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. The player who takes the last stick loses. You start with 21 sticks, and two players take turns either taking one or two sticks. 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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |