配列 (2 次元配列) の要素座標を格納する必要があります。これは Java 迷路プロジェクト用です。私のプログラムは int plyrX と plyrY の変数を使用して、位置、つまり 0 1 を格納します。プレイヤーが迷路の周りを移動すると、x と y の値は、プレイヤーが移動することを決定した場所に変化します。
私の問題は、たとえば、プレーヤーが 0 1 から開始して 10 1 に移動した場合、位置を配列に格納できないように見えることです。これらの値を配列に格納できるようにして、再利用できるようにする必要があります。最後のゲームのリプレイを作成すると、問題は、配列要素に格納されている値が常に x と y の最後の値 (この場合は 24 37) であるように見えることです。
これは、移動メソッドとスペース メソッドのコードの一部です。プレイヤーは、移動する必要がある文字とスペースの量を選択できます。スペースであり、これがプレーヤーを迷路の周りに移動させるものです。私が理解できないのは、個々の移動の後に 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