I am building an Interactive Fiction game that uses a second class to deal with commands. When I type a command it is sent to the second class to be processed. The problem is when I run the code my values (int x
and int y
) are not being passed back into the main class. How do I pass these values back into the main class so that northD
will print?
Main class:
import java.util.Scanner;
import static java.lang.System.out;
public class Main {
static String cmdIF;
static int x = 0;
static int y = 0;
static String northD;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
out.println("Welcome to the world! Which way do you want to go?");
cmdIF = input.nextLine();
choosePath();
if(x==1 || y == 0) {
northD = "You have entered the woods.";
out.print(northD);
}
}
public static void choosePath() {
actionClass.cmdCenter(cmdIF, x, y);
}
}
Second class:
import static java.lang.System.out;
public class actionClass {
public static void cmdCenter(String cmdIF, int x, int y) {
if(cmdIF.equalsIgnoreCase("NORTH") || cmdIF.equalsIgnoreCase("GO NORTH")){
x++;
}
else if(cmdIF.equalsIgnoreCase("EAST") || cmdIF.equalsIgnoreCase("GO EAST")) {
y++;
}
else if(cmdIF.equalsIgnoreCase("SOUTH") || cmdIF.equalsIgnoreCase("GO SOUTH")) {
x--;
}
else if(cmdIF.equalsIgnoreCase("WEST") || cmdIF.equalsIgnoreCase("GO WEST")) {
y--;
}
else { out.println("You can't do that."); }
}
}