multidimensional array 'advancing'?
hi,
when you have this for example:
Code Java:
String[][] table = new String[4][4];
table[0][0] = ". ";
table[0][1] = ". ";
table[0][2] = ". ";
table[0][3] = ". ";
table[1][0] = ". ";
table[1][1] = ". ";
table[1][2] = ". ";
table[1][3] = ". ";
table[2][0] = ". ";
table[2][1] = ". ";
table[2][2] = ". ";
table[2][3] = ". ";
table[3][0] = ". ";
table[3][1] = ". ";
table[3][2] = ". ";
table[3][3] = ". ";
which is printed like this:
. . . .
. . . .
. . . .
. . . .
is there a way to move around the board?
kinda like this:
Code Java:
table[1][1] = snake;
//move is input
switch (move) {
case "a":
table[]([]--)
so when you input "a", the snake (which is an o instead of a .) stays in the same primary table but decreases in the secondary table.
e.g. original location table[1][1]
input a: table[1][0]
it would look like this:
. . . .
o. . .
. . . .
any ideas?
iam pretty new to Java and programming at all, so i thought to making a command prompt snake game would be fun, iam planning to add random apple generation, eating them etc..
and, is there a way to determine the current table the snake is in?
so i can say current snake location becomes a . again once the snake moves?
Re: multidimensional array 'advancing'?
Think about using 2 variables, say x and y, each access the appropriate portion of the array. You can then use these to access the array elements, and increment/decrement/set them to 'move' through the array
Re: multidimensional array 'advancing'?
you mean like this:
Code Java:
switch (move){
case "a": x--;
break;
case "w": y++
break;
case "d": x++
break;
case "s": y--
if (x == 0 & y==0) {
table[0][0] = o
}
if (x == 0 & y==1) {
table[0][1] = o
}
etc...
?
oh and, it says:
Cannot switch on a value of type String. Only convertible int values or enum constants are permitted,
is there a way via switch to compare an input (in this case called "move") with different cases and then let them be carried out if true?
real problem, the input (the reader (buffered reader thingy)) cante be an int... but i cant switch a string.
Re: multidimensional array 'advancing'?
i did this now:
Code Java:
int y = 0;
int x = 0;
if (move == '8'){
y++;
}
if (move == '5'){
y--;
}
if (move == '4'){
x--;
}
if (move == '6'){
x++;
}
System.out.println ("\r" + move);
System.out.println ("\r" + x);
System.out.println ("\r" + y);
that kinda works, but to check if it works, i let it print out the x and y after each use, and they never changed, they didnt decrease or increase, they always stayed the same.
any ideas?
Re: multidimensional array 'advancing'?
Quote:
Originally Posted by
kitube
you mean like this...
The scope of the program you provide is limited, but I meant something more like (pseudo-code)
Code :
int x, y;
...
conditional:
x--;
conditional:
y++;
...
table[x][y] = somevalue;
Quote:
Originally Posted by
kitube
Cannot switch on a value of type String. Only convertible int values or enum constants are permitted,
is there a way via switch to compare an input (in this case called "move") with different cases and then let them be carried out if true?
You cannot switch on a string: use a primitive or enum. Without knowing exactly what you wish to switch and the full scope of your program on its hard to throw out advice along these lines.
Re: multidimensional array 'advancing'?
works now,
my snake moves, the old gets deleted, it can eat apples, they get randomly generated ect...