I am making a game where a player ('P') starts on a 2d array of dashes at [0][0]. The player is prompted to enter the size of the array, and then enter an action (left, right, up, down, or exit). When that action is entered, 'P' is supposed to move by 1 index. I am working on my moveDown method and nothing else yet. When I type "down", it will increment to theWorld[1][0], but will not increment again (theWorld[2][0]) if I type "down". It just stays at [1][0].
There are two classes to this program, but the only thing my other class has right now is the main method and the world.userInput method, so I won't include that. This is a work in progress, so excuse any sloppy code, extra space, etc. Here is what I have so far:
import java.util.Scanner;
class World {
private int characterRow;
private int characterColumn;
private char[][] theWorld;
char player = 'P';
int playerX = 0;
int playerY = 0;
String userAction;
boolean keepPlaying = true;
boolean outOfBounds;
public World() {
characterRow = 0;
characterColumn = 0;
}
public World(int height, int width) {
this.characterRow = height;
this.characterColumn = width;
}
public void setHeight(int height) {
characterRow = height;
}
public void setWidth(int width) {
characterColumn = width;
}
public void userInput(int height, int width, int x, int y) {
Scanner input = new Scanner(System.in);
System.out.print("Enter World width: ");
width = input.nextInt();
System.out.print("Enter World height: ");
height = input.nextInt();
displayWorld(height, width, x, y);
input.nextLine();
while(keepPlaying)
{
System.out.print("ACTION > ");
String action = input.nextLine();
while(!action.equalsIgnoreCase("exit"))
{
keepPlaying = true;
if(action.equalsIgnoreCase("down") && x > theWorld.length)
{
outOfBounds = true;
System.out.println("You cannot go out of bounds. Try again.");
}
else
break;
}
if(action.equalsIgnoreCase("down"))
{
moveDown(height, width, x, y, action);
}
}
public void moveDown(int height, int width, int x, int y, String action) {
if(x < theWorld.length-1)
{
x += 1;
}
displayWorld(height, width, x, y);
}
public void displayWorld(int height, int width, int x, int y) {
theWorld = new char[height][width];
for(height = 0; height < theWorld.length; height++)
{
for(width = 0; width < theWorld[height].length; width++)
{
theWorld[height][width] = '-';
theWorld[x][y] = player;
System.out.print(theWorld[height][width] + " ");
}
System.out.println();
}
}//end displayWorld
}//end class