-1

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
DevOpsSauce
  • 1,319
  • 1
  • 20
  • 52
  • Are you sure `theWorld.length` > 2 ? – kiwixz Apr 25 '15 at 15:58
  • Yes. I'm using 5 for height and width right now. Just a small number to work through the program. It increments once, then I type "down" again, and it reprints the array with the 'P' still at [1][0], no matter how many times I do it. – DevOpsSauce Apr 25 '15 at 16:16
  • Have you tried to print it in `moveDown`, just to double check ? – kiwixz Apr 25 '15 at 16:19
  • That's why I have the displayWorld(height, width, x, y) method after the increment. Perhaps my logic is incorrect. It reprints the first time just fine, just not after any subsequent actions. – DevOpsSauce Apr 25 '15 at 16:28
  • Why in the world was this question given a down vote? I see where beginners stand when they are struggling. I figured it out anyway, so I'll post my answer. – DevOpsSauce Apr 25 '15 at 18:49

1 Answers1

0

I added a setPlayerX method, and changed the assignment in moveDown to what you see here. It works now, so I figured it out.

   public void setPlayerX(int x) {
    x = 1;
    playerX = x;
}
    public void moveDown(int height, int width, int x, int y) {
    x++;
    playerX = playerX + x;
    displayWorld(height, width, x, y);

}
DevOpsSauce
  • 1,319
  • 1
  • 20
  • 52