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."); }
}
}
You can return an array of integer (or better an object). This the simplest way (in my opinion)
Main class:
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);
System.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.";
System.out.println(northD);
} else {
System.out.println("You have entered the home");
}
}
public static void choosePath() {
//Method return an array of integer
int[] newPos = actionClass.cmdCenter(cmdIF, x, y);
//GETX
System.out.println(newPos[0]);
//GETY
System.out.println(newPos[1]);
//set X
x = newPos[0];
//set Y
y = newPos[1];
}
}
Activity class:
public class actionClass {
public static int[] 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 { System.out.println("You can't do that."); }
//New array, first position x, second position y
int[] res = {x,y};
//Return it
return res;
}
}
Output:
Welcome to the world! Which way do you want to go?
EAST
0
1
You have entered the home
More output:
Welcome to the world! Which way do you want to go?
NORTH
1
0
You have entered the woods.
With an object
A more complex way is to use a custom object. So new class:
public class xyObj {
public int x;
public int y;
//Set x and y
public xyObj(int x,int y){
this.x=x;
this.y=y;
}
//get x
public int getX(){
return x;
}
//get y
public int getY(){
return y;
}
}
Now activity class return this object:
public class actionClass {
public static xyObj 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 { System.out.println("You can't do that."); }
//new xyObj setting x and y
xyObj ret = new xyObj(x, y);
//return it
return ret;
}
}
We have to modify also the choosePath method
public static void choosePath() {
xyObj xyPos = actionClass.cmdCenter(cmdIF, x, y);
System.out.println(xyPos.getX());
System.out.println(xyPos.getY());
x = xyPos.getX();
y = xyPos.getX();
}
Same output! Good luck!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句