Looking at yesterday's "game" in-depth.
Yesterday, we created our very first game. Now we will dissect the class behind it.
Here's the code:
import java.util.Random;
//The greatest game ever made follows.
class BySimpleIMeanSimple {
static int dieValue;
public static void main(String[] args) {
rollDie();
} // ends main
static void rollDie() {
Random rand = new Random();
// Assign a random number between 1 and 6 as dieValue
dieValue = rand.nextInt(6) + 1;
System.out.println("You rolled a " + dieValue);
// Why add 1? Think about it.
testDieValue(dieValue);
} // ends rollDie()
static void testDieValue(int dieValue) {
if (dieValue <= 2) {
System.out.println("You Lose.");
} // ends if
else if (dieValue == 3 || dieValue == 4 || dieValue == 5) {
System.out.println();
System.out.println("Rerolling.");
System.out.println();
rollDie();
} // ends else if
else if (dieValue == 6) {
System.out.println("You win! Congratulations!");
} // ends else if
} // ends testDieValue()
} // ends BySimpleIMeanSimple Class
2. We then declare the name of the class: BySimpleIMeanSimple
3. Then a static integer called dieValue, which will hold the value of the rolled die is created
4. Next, we create the main method, which just invokes a method called rollDie();
5. The rollDie() method is then created. It has the following parts.
I. Creation of the Random object called rand.
II. Assignment of a random number between 0 and 5 (+1 since numbers range from 1 to 6 on a die) to the dieValue integer we created earlier.
III. It uses System.out.println to display the value of the integer dieValue.
IV. It invokes the method testDieValue(), which is defined below the rollDie() method. This testDieValue() method requires an integer parameter, so we input the new dieValue, which contains the random number between 1 and 6.
6. Next we declare the testDieValue() method. This is where the Control Flow statements happen (we control the way that the program proceeds based on conditional statements).
It takes the value of integer dieValue and then carries out the appropriate if statement.
I. If the value is less than or equal to 2, the player loses, so it displays, "You lose."
II. If the value is exactly 6 (==), it displays "You Win! Congratulations!"
III. If the value is 3 or 4 or 5 (This could've been written in many ways, such as: if (dieValue >= 3 && dieValue <= 5)...), it rerolls, invoking again the rollDie() method, which creates a random number and assigns it as the value of dieValue and then uses this value to invoke the testDieValue();
Someone asked earlier, what is the purpose of the: System.out.println();
Well, it is there to create an empty line in the console (where the output is displayed). It helps organize the way the program "speaks" to you.
NOTE: Make sure you are keeping up with the braces. When you do actually programming, braces won't be color coded to show you where the boundaries of each class or method are!
Lesson #1-13: Switches in Java
Java, much like railroad tracks, incorporates switches to direct the path of execution.
We will look at switches using an example that we have used before: a coin toss.
NOTE: Make sure that the class name in the Package Explorer to the left side of the screen on Eclipse matches that of the one stated in the code. Otherwise, you will get an error. The reason for this is that when you press Play, Eclipse looks for the class that with the name of the file that you are executing, and it will not be able to locate it.
(i.e. You should create a class named CoinTossSwitch.java and copy this code):
import java.util.Random;
class CoinTossSwitch {
public static void main(String[] args) {
Random rand = new Random();
int randomInt = rand.nextInt(2);
System.out.println(randomInt);
switch(randomInt) {
case 0:
System.out.println("Tails!");
break;
case 1:
System.out.println("Heads!");
break;
} // Ends switch
} // Ends main
} // Ends Class
switch (randomInt) {
case 0:
System.out.println("Tails!");
break;
case 1:
System.out.println("Heads!");
break;
} // Ends switch
switch (variable){
case 1: // if variable == 1
doThis();
break;
case 2: // if variable == 2
doThat();
break;
case 3:
doOther();
break;
case 4: case 5: case 6: // if variable == 4, 5 or 6
doSomethingElse();
break;
default:
doDefault();
break;
}
Inside the parentheses (), you write the variable name that you are testing.
If this variable is an integer, you write:
case 1, case 2, and so on (You can combine cases and include a default case for all other cases).
If it is is a boolean, you would write:
case true, case false.
If it is a String, you would write whichever values that the String variable can take.
For example, if your String can contain any color value, you would use:
case "red", case "blue", and so on.
The purpose of the "break;" is to separate the cases. If you forget to put the break, the code will not stop executing after a case is satisfied. It will keep going. Break can also be used to end loops (which we will discuss in the coming lessons).
__________
Applying this to the example code above,
1. We are testing an integer called randomInt.
2. If randomInt is 0, the computer will output: "Heads!" and break the case.
3. If randomInt is 1, the computer will output: "Tails!" and break the case.
What would be a practical application of this code? Let's say that we have a game in which the player can select one of three characters.
Let's name them Mario, Luigi, and Yoshi.
We would then create a String variable that holds the value of the name, and then use a switch to carry out the appropriate response.
Note: The following will only work in Java 1.7. On Java 1.6 and below, we would use integers to represent our characters, like in the example at this link.
//Sample Character Selection Screen
class CharacterSelect {
public static void main(String[] args) {
String currentCharacter = "Mario";
int maxLife;
int maxJump;
int maxSpeed;
switch (currentCharacter) {
case "Mario":
System.out.println("You have selected Mario!");
maxLife = 70;
maxJump = 50;
maxSpeed = 25;
break;
case "Luigi":
System.out.println("You have selected Luigi!");
maxLife = 40;
maxJump = 70;
maxSpeed = 30;
break;
case "Yoshi":
System.out.println("You have selected Yoshi!");
maxLife = 50;
maxJump = 30;
maxSpeed = 40;
break;
}
}
}
Tomorrow we will discuss looping. We are getting closer to developing a game, so hang tight and stay tuned!
If you want to thank me for the guide, you can donate here or download TUMBL +!
Thank you guys for reading and I'm here if you have any questions!
Comment below and Like us on Facebook.