The door is the main object in the room, and it is different than the other objects. We need to break down the function into little problems, but we must first identify those problems. The first thing we should do collect input. We then need to check that the input is valid. After we collect all the input we should check if the code is correct.
public static boolean door(int code[]) {
// Create some variables
// Collect digits from the player
// Check if the guessed digits are equal to the door code
// return an incorrect guess as false or a correct guess as true
}
This is a basic outline for the door method. It is often helpful to comment what each section of code should do because it outlines subproblems we need to solve in order to accomplish a task. Lets solve each of the subproblems commented above.
Firstly we should think about what variables we need for this function. We need to collect digits so we will need a way to store those. We also need a way to know if we have collected a digit and we need a way to know if we are collecting a digit.
// Create some variables
boolean doing_action = true;
int[] options = new int[3];
Now we need to collect input from the user. At the top of the file we need to import a scanner from the java library and we will create a scanner object at the start of our main class.
import java.util.Scanner;
public class Main {
static Scanner input = new scanner(Sytstem.in);
Now the code to collect one digit might look something like this
// While doing_action is true
while (doing_action) {
// If there is a number
if (input.hasNextInt()) {
// Set the first option equal to the number
options[0] = input.nextInt();
// Our number must be between 0 and 9
if (options[0] >= 0 && options[0] <= 9) {
// doing_action will be false so the loop will exit
doing_action = false;
}
// The number was not between 0 and 9
else {
System.out.println("Digit one must be a whole number between 0-9:");
}
}
// Input did not find an integer
else {
System.out.println("Digit one must be a whole number between 0-9:");
}
}
This is just the code to collect one digit from the player, but our door has a three digit combination. We will need do this three more times and we will have to reset doing_action between while loops.
Once we have collected all of our digits we then need to check if it is the correct door combination.
We cannot simply return options == code
because options and code are arrays. We want to
know if the numbers in the arrays are equal. We should loop over each number in both arrays and check if
they are the same.
// The variable to keep track of if the codes are equal
boolean is_equal = true;
// Loop over all the digits
for (int i = 0; i < 3; i++) {
// If one of the digits in the code is incorrect then the code is wrong
// and we don't need to look at anymore digits
if (options[i] != code[i]) {
is_equal = false;
break;
}
}
// We want to return if the code is right or not so the door can open
return is_equal;
The final function should look something like this now.
public static boolean door(int code[]) {
boolean doing_action = true;
int[] options = new int[3];
System.out.println("You walk to the door. The rotary padlock contains three digits. You enter a code");
while (doing_action) {
if (input.hasNextInt()) {
options[0] = input.nextInt();
if (options[0] > 9) {
System.out.println("Digit one must be a whole number between 0-9:");
} else {
doing_action = false;
}
} else {
System.out.println("Digit one must be a whole number between 0-9:");
}
}
doing_action = true;
while (doing_action) {
if (input.hasNextInt()) {
options[1] = input.nextInt();
if (options[1] > 9) {
System.out.println("Digit one must be a whole number between 0-9:");
} else {
doing_action = false;
}
} else {
System.out.println("Digit one must be a whole number between 0-9:");
}
}
doing_action = true;
while (doing_action) {
if (input.hasNextInt()) {
options[2] = input.nextInt();
if (options[2] > 9) {
System.out.println("Digit one must be a whole number between 0-9:");
} else {
doing_action = false;
}
} else {
System.out.println("Digit one must be a whole number between 0-9:");
}
}
System.out.println("Chose code: " + options[0] + " " + options[1] + " " + options[2]);
boolean is_equal = true;
for (int i = 0; i < 3; i++) {
if (options[i] != code[i]) {
is_equal = false;
break;
}
}
return correct_code;
}
Although this works, it is very repetative. We use the same code to collect a digit three times. Lets break that section of code into its own function so it can be used by other functions as well. It's not good practice to write the same code twice.
public static void getNumber(int min_num, int max_num) {
boolean doing_action = true;
int choice = -1;
while (doing_action) {
if (input.hasNextInt()) {
choice = input.nextInt();
if (choice >= min_num && choice <= max_num) {
doing_action = false;
} else {
System.out.println("Digit one must be a whole number between" + min_num + "-" + max_num);
}
}
else {
System.out.println("Digit one must be a whole number between" + min_num + "-" + max_num);
}
}
return choice;
}
Now the final door function should look something like this.
public static boolean door(int code[]) {
int[] options = new int[3];
System.out.println("You walk to the door. The rotary padlock contains three digits. You enter a code");
options[0] = getNumber(0, 9);
options[1] = getNumber(0, 9);
options[2] = getNumber(0, 9);
System.out.println("Chose code: " + options[0] + " " + options[1] + " " + options[2]);
boolean is_equal = true;
for (int i = 0; i < 3; i++) {
if (options[i] != code[i]) {
is_equal = false;
break;
}
}
return is_equal;
}
This function can be refactored even more. Right now we are limited to a three digit code. Using for-loops we could have a variable digit code, but that is left for you to you.