Classwork 7: Craps N. Duong & R. Rodriguez, Java Crash Course January 6, 2015 For this classwork, you will be writing code for the game Craps. For those of you who do not know, Craps is a dice-rolling game. How this game is played is by having a single player roll two dice. If the sum of the numbers on the dice is either 7 or 11 and this is the player s first roll of the game, the players automatically wins. If the sum of the numbers on the dice is either 2, 3, or 12 and this is the player s first roll of the game, the player loses. Otherwise, the sum of the numbers on the dice is the new winning number and the player continues to roll the dice until he wins the game. If the player is not on his first roll of the game and he rolls a 7, the player loses. However, if the player is not on his first roll of the game and he rolls the new winning number (the sum of the numbers on the dice on the first roll of the game), he wins. Otherwise, he will continue to roll until he either wins or loses the game. When the player wins or loses the game, his new winning number is no longer applicable meaning he will not win if he rolls on the "new winning number" (this is put in quotes because it s technically no longer a new winning number). To download the game files, please CLICK ON HERE! 1 The Program Explained You may feel a bit overwhelmed by the amount of files we need in order to create and run this simple dice-rolling game, and that s fine! However you should also realize that this is the amount of work that is required to be done in order to have a simple and properly functioning game. And with more complex games, there will be more files! This is just to give you a little bit of taste into actually programming something bigger than what we have done previously in this class. For this classwork, the only file you will be editing in will be CrapsGame.java. While you are welcome to look into the other files, please do not make any changes in the other files because it may break the program. In addition to this, it is OK if you do not understand a lot of the code in the other files. A lot of the ideas, and concepts written in the other files are well beyond this simple crash course. The biggest idea you need to understand is that CrapsGame.java does not require a main() method! Instead, we will be trying to write code for the method inside of CrapsGame.java abstractly. And in order for you to be able to write a method abstractly, you must know how Classwork 7 Page 1
a method is constructed well. Below this will be a short explanation on understanding what a method is suppose to do. 1 public static int iseven(int num) { 2... 3 } 4 Listing 1: Understanding Methods 5 / ** Everything below this is considered a comment -- which will not 6 * be read by the program. 7 * 8 * You may notice that the method inside of CrapsGame.java may not 9 * have static -- this is fine, static has to do with the way objects 10 * work. It is OK if static does not exist in the method declaration. 11 * 12 * What does it return? "int" -- how do we know this? Because after 13 * public and before the method s name is the type "int." Now we know 14 * what TYPE to return. 15 * 16 * Now what kind of argument(s) -- if any does it take in? You 17 * figure this out by looking between the parenthesis (...). If 18 * there are nothing inside of it, the program has no arguments, 19 * meaning it does not take in anything. However, in this specific 20 * case, we see that it takes in "int num". What this means, the 21 * program will take in "a variable called num with the type int". 22 * Now we know what arguments this method takes in. 23 * / At the moment, all you really need to know is that somewhere in the other files, there is a line of code that uses the method processroll(...), which you will be required to write the logic for. And since the current processroll(...) does not have actual logic to it, it does not return the correct value. Therefore, currently, our program is not working as it is supposed to. Now in order to fix this, we will need to write the logic out for the method processroll(...). 2 Understanding processroll(...) First thing you need to understand is that methods use the idea of abstraction. Meaning processroll(...) uses the idea of abstraction too! Therefore do not think of the game; instead, think of what processroll(...) is supposed to do. So here s the big question, what is processroll(...) suppose to do? Let s look at its declaration to figure it out. Listing 2: processroll() 1 public int processroll(int sumofroll) { Classwork 7 Page 2
2 // FIXME -- write the appropriate algorithm 3 return 0; // dummy return -- need to return an actual value later 4 } At the moment, we see that nothing is written inside the class besides a comment telling us to fix it with an appropriate algorithm. There is also a dummy return because the method demands something to be returned. So this gives us the first hint. This method needs to return something, but what? Using what we learned above, the type that comes after public but before the methodname is the type that gets returned. In this situation, it is an int. Now continue reading through the declaration, we see that something exists between the parenthesis. As mentioned above, this means that the method has an arguments (or in other words, the method takes something in). Now how do we figure out what type does it take in? By reading the variable created, which in this case is "int sumofroll". Translating this into English, it means "a variable sumofroll with the type int." Putting it all together: this method we are supposed to write will return an int. This method takes in an int called sumofroll for us to use in any manner we wish for the method to work properly. 3 What processroll(...) Does Now that you know the little details of the what processroll(...) expects from its declaration, we will move onto what is the "appropriate algorithm" for processroll(...) is. And what I mean by this is what the method processroll(...) is suppose to do. We know what it returns and takes in, but what does it do? processroll(...) is suppose to take its given argument, sumofroll, to return -1, 0, or 1. In this situation, -1, 0, 1 is lose, continue-playing, and win respectively. You will have to write an algorithm that will appropriate return either -1, 0, or 1 depending on whether or not the sumofroll is a winning number, losing number, or otherwise a number that does not affect the game and will let the player keep rolling the dice. You will be expected to figure out this algorithm by the description of the game given above and/or using the pseudocode we will be providing you in the next section. For those of you who do not know what pseudocode is, it is a guide/reference for an algorithm based on code to guide you on which paths/ideas/directions you should be taking in order to code the specific algorithm. Classwork 7 Page 3
4 processroll(...) Pseudocode The following is only a guide or reference on paths/ideas/directions you can take in order to create a functioning algorithm for the method processroll(...). You will need to translate this into actual Java code/syntax. While the following code may look like actual Java code, it is using the idea of abstraction such that some of the statements/commands we call inside the pseudocode may not actually exist inside of Java but when a human person read the pseudocode, they get understand what the pseudocode is trying to do and then the human person will translate it to the Java language. Again, do not copy this into your file. It will not work! Listing 3: processroll(...) pseudocode 1 processroll(int sumofroll) { 2 if (firstround) { 3 if (sumofroll is 7 or 11) { 4 WIN(); 5 } else if (sumofroll is 2 or 3 or 12) { 6 LOSE(); 7 } else { 8 SAVE_CURRENT_ROLL_SUM(); 9 CONTINUE_GAME(); 10 } 11 } else { // if it s not firstround, it s any other rounds else! 12 if (sumofroll is 7) { 13 RESET_WINNING_NUMBER(); 14 LOSE(); 15 } else if (sumofroll is winningnumber) { 16 RESET_WINNING_NUMBER(); 17 WIN(); 18 } else { 19 CONTINUE_GAME(); 20 } 21 } 22 } To see the pseudocode s commands defined, look onto the next page. Classwork 7 Page 4
firstround sumofroll winningnumber WIN() LOSE() CONTINUE_GAME() SAVE_CURRENT_ROLL_SUM() RESET_WINNING_NUMBER() The boolean representing whether or not this is the Player s first round in rolling the dice The variable representing the sum of the numbers on the dice after rolling. The variable representing the sum of the numbers on the dice that will be considered as the "winning number" after the first roll. Because this variable is to be remembered even after processroll(...) is called again, its declaration or creation should be outside of the processroll(...) scope the Player has won the game the Player has lost the game the Player has lost the game This will record/save/store the current sumofroll into winningnumber This will method will change the variable winningnumber to 0 so that a winning number will not exist anymore since the game will restart after the Player loses or wins 5 Disclaimer This program was originally created by Michael Ferraro for his APCS class. Some revisions were made by R. Rodriguez and N. Duong. We have his permission to use his code for this class assignment. Classwork 7 Page 5