我需要存储一个数组(2d数组)的元素坐标,这是一个java迷宫项目。我的程序使用int plyrX和plyrY的变量来存储位置,即0 1,当玩家在迷宫中移动时,x和y的值会变为玩家决定移动的位置。
我的问题是我似乎无法将位置存储到数组中,例如,如果玩家从0开始并移动到10 1我需要能够将这些值存储到数组中以便我可以重新使用它们来创建最后一个游戏的重放,问题是存储在数组元素中的值似乎总是x和y的最后一个值,在这种情况下为24 37。
以下是移动方法和空间方法的代码的一部分,玩家可以选择一个字符并输入需要移动的空间量,因为您可以看到变量plyrx或plyry被赋予plyrx plyry +的值或者 - 空间数量,这就是让玩家在迷宫中移动的原因。我似乎无法弄清楚的是在每次移动后存储x和y的值。
public void getMoves(){
int npX = 0;
int npY = 0;
storeposX = new int[30];
storeposY = new int[30];
String invalidM = "Invalid move, try again. Can't move into or through a wall.";
String vgood = "Very Good";
String notbad = "Not bad";
String ppoor = "Pretty poor";
getDirection();
//UP
if (dir =='U' || dir == 'u'){
npX = plyrX;
npY = plyrY;
c_plyrX = npX;
c_plyrY = npY;
for(int i = 0; i < spaces+1; i++){
if (maze[npX][npY] == '0'){
movesTaken++;
TextIO.putln(invalidM);
getMoves();
}
else if (i != spaces){
npX = npX - 1;
}
else {
plyrX = plyrX-spaces;
for (int k =0; k<storeposX.length; k++){
storeposX[k]=plyrX;
}
c_plyrX = plyrX;
}
}
}//end UP if
//DOWN
if (dir == 'D' || dir == 'd'){
npX = plyrX;
npY = plyrY;
c_plyrX = npX;
c_plyrY = npY;
for (int i = 0; i < spaces + 1; i++){
if (maze[npX][npY] == '0'){
movesTaken++;
TextIO.putln(invalidM);
getMoves();
}
else if (i != spaces){
npX = npX+1;
}
else{
plyrX = plyrX+spaces;
for (int k=0; k<storeposX.length; k++){
storeposX[k]=plyrX;
}
c_plyrX = plyrX;
}
}
} //end DOWN if
//LEFT
if (dir == 'L' || dir == 'l'){
npX = plyrX;
npY = plyrY;
c_plyrX = npX;
c_plyrY = npY;
for (int i = 0; i < spaces + 1; i++){
if (maze[npX][npY] == ('0')){
movesTaken++;
TextIO.putln(invalidM);
getMoves();
}
else if (i != spaces){
npY = npY - 1;
}
else{
plyrY = plyrY-spaces;
for (int k=0; k<storeposY.length; k++){
storeposY[k]=plyrY;
}
c_plyrY = plyrY;
}
}
} //end LEFT if
//RIGHT
if (dir == 'R' || dir == 'r'){
npX = plyrX;
npY = plyrY;
c_plyrX = npX;
c_plyrY = npY;
for (int i = 0; i < spaces + 1; i++){
if (maze[npX][npY] == '0'){
movesTaken++;
TextIO.putln(invalidM);
getMoves();
}
else if (i != spaces){
npY = npY + 1;
}
else{
plyrY = plyrY+spaces;
for (int k=0; k<storeposY.length; k++){
storeposY[k]=plyrY;
}
c_plyrY = plyrY;
}
}
} //end RIGHT if
//prints message if player escapes from the maze if the values currently held
//in plyrX and plyrY variables match the position of '3'.
if (maze[plyrX][plyrY] == '3'){
gamesWon++;
TextIO.putln("****Congratulations****");
TextIO.putln();
TextIO.putln("You have escaped from the maze.");
TextIO.putln();
TextIO.put("It took you " + movesTaken + " moves to escape ");
if (movesTaken <= 10){
TextIO.put("That is " + vgood);
TextIO.putln();
}
else if (movesTaken <=15){
TextIO.put("That is " + notbad);
TextIO.putln();
}
else{
TextIO.put("That is " + ppoor);
TextIO.putln();
}
TextIO.putln("You have won " + gamesWon + " games so far.");
TextIO.putln();
userMenu();
}
else{
movesTaken++;
redrawMaze();
getMoves();
}
} //end of getMoves method
/**direction, method. Gets the direction character typed by the user
* if the input matches one of the allowed directions method then calls the
* get spaces method to get the number of spaces player wishes to move.
*
*/
public void getDirection(){
TextIO.putln("Enter the direction you wish to move in and the distance");
TextIO.putln("i.e D3 = move down 3 spaces");
TextIO.putln("U - Up, D - Down, L - Left, R - Right: ");
dir = TextIO.getChar();
if (dir == 'U' || dir == 'u' || dir == 'D' || dir == 'd'
|| dir == 'L' || dir == 'l' || dir == 'R' || dir == 'r'){
getSpaces();
}
else{
TextIO.putln("Invalid direction!");
TextIO.putln("Direction must be one of U, D, L or R");
}
} //end direction method
/**spaces method, gets the amount of spaces the user wants to move
*
*/
public void getSpaces(){
TextIO.putln(" ");
spaces = TextIO.getInt();
if (spaces <= 0){
TextIO.put("Invalid amount of spaces, please type the amount of spaces again");
getSpaces();
}
} //end spacesMoved method
答案 0 :(得分:0)
将storeposX和storeposY转换为List(例如ArrayList),只需在每次移动时添加新坐标,这将更容易
您的静态数组将您分成30个元素。
顺便说一句:
在代码中: plyrX = plyrX-spaces; for(int k = 0; k
你这样做:
这就是为什么你总是只有最后的位置。