Need to somehow keep track of co ordinates visited in an array
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;
}
开发者_如何学C }
} //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
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 :
- get the difference on X between EndPosition and StartPosition
- store current position on all StoredPositions
that's why you always only have the last position.
精彩评论