需要以某种方式跟踪数组中访问的坐标
我需要存储数组(二维数组)的元素坐标,这是一个java迷宫项目。我的程序使用 int plyrX 和 plyrY 变量来存储位置,即 0 1,当玩家在迷宫中移动时,x 和 y 的值会更改为玩家决定移动的位置。
我的问题是我似乎无法将位置存储到数组中,例如如果玩家从 0 1 开始并移动到 10 1 我需要能够将这些值存储到数组中,以便我可以重复使用它们创建上一场比赛的重播,问题是存储在数组元素中的值似乎总是 x 和 y 的最后一个值,在本例中为 24 37。
这是 moves 方法和 space 方法的部分代码,玩家可以选择一个字符并输入需要移动的空间量,如您所见,变量 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
I need to store element co ordinates of an array(2d array), this is for a java maze project. My program uses to variables of int plyrX and plyrY to store the positiions i.e 0 1, as the player moves around the maze the values of x and y change to wherever the player decides to move.
My poroblem is I cant seem to be able to store the positions into an array for example if the player starts at 0 1 and moves to 10 1 I need to be able to store these values into an array so I can re-use them for creating a replay of the last game, the problem is the value stored in the array elements always seems to be the last value of x and y, in this case 24 37.
Here is part of the code for a moves method and spaces method, the player can select a char and input amount of spaces needed to be moved, as you can see the variable plyrx or plyry is given the value of plyrx plyry+ or - amount of spaces and this is what moves the player around the maze. What i cant seem to figure out is to store the values of x and y after each individual move.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 storeposX 和 storeposY 转换为 List(例如 ArrayList),然后在每次移动时添加新坐标,这样会更容易。
静态数组将您限制为 30 个元素。
顺便说一句:
在代码中:
plyrX = plyrX-空间;
for (int k =0; k
你正在这样做:
EndPosition 和 StartPosition
StoredPositions
这就是为什么你总是只有最后一个位置。
Transform your storeposX and storeposY to List (ArrayList for example) and just add the new coordinate at each moves it will be easier
Your static arrays blocs you to 30 elements.
Btw :
In the code :
plyrX = plyrX-spaces;
for (int k =0; k
you are doing this :
EndPosition and StartPosition
StoredPositions
that's why you always only have the last position.