0

我需要存储一个数组(二维数组)的元素坐标,这是一个 java 迷宫项目。我的程序使用 int plyrX 和 plyrY 的变量来存储位置,即 0 1,当玩家在迷宫中移动时,x 和 y 的值会改变到玩家决定移动的任何位置。

我的问题是我似乎无法将位置存储到数组中,例如,如果玩家从 0 1 开始并移动到 10 1,我需要能够将这些值存储到数组中,以便我可以重新使用它们创建上一场比赛的重播,问题是存储在数组元素中的值似乎总是 x 和 y 的最后一个值,在本例中为 24 37。

这是moves方法和spaces方法的部分代码,玩家可以选择一个char并输入需要移动的空格数量,如您所见,变量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 
4

1 回答 1

0

将您的 storeposX 和 storeposY 转换为 List(例如 ArrayList)并在每次移动时添加新坐标,这样会更容易

您的静态数组将您限制为 30 个元素。

顺便提一句 :

在代码中: plyrX = plyrX-spaces; 对于 (int k =0; k

你正在这样做:

  1. 获取 EndPosition 和 StartPosition 在 X 上的差异
  2. 在所有 StoredPositions 上存储当前位置

这就是为什么你总是只有最后一个位置。

于 2011-01-07T09:56:53.653 回答