arrays - Why does variable only increment once in Java code? -
i making game player ('p') starts on 2d array of dashes @ [0][0]. player prompted enter size of array, , enter action (left, right, up, down, or exit). when action entered, 'p' supposed move 1 index. working on movedown method , nothing else yet. when type "down", increment theworld[1][0], not increment again (theworld[2][0]) if type "down". stays @ [1][0].
there 2 classes program, thing other class has right main method , world.userinput method, won't include that. work in progress, excuse sloppy code, space, etc. here have 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
i added setplayerx method, , changed assignment in movedown see here. works now, figured 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); }
Comments
Post a Comment