0

I have a map class, which is an Array of 100 Strings

public class map{
    [...]
    public void movePlayer(String entrada){
    if (entrada.equalsIgnoreCase("w")){
        move = -10;
    }
    else if (entrada.equalsIgnoreCase("s")){
        move = 10;
    }
    else if (entrada.equalsIgnoreCase("d")){
        move = 1;
    }
    else if (entrada.equalsIgnoreCase("a")){
        move = -1;
    }
    for(int i = 0; i < mapa.length; i++){
        if (mapa[i].equals("P")){
            int moved = i+move;
            mapa[moved] = "P";
        }
    }
}

The main looks a little like this

String[] mapa = map.getMap();
    mapa[0] = "T";
    mapa[99] = "P";
    for (int j = 0; j<10; j++){
        for(int i = 0; i < map.length; i++){
            if (i == 9 || i == 19 || i == 29 || i == 39 || i == 49 || i == 59 || i == 69 || i == 79 || i == 89 || i == 99){
                System.out.println(mapa[i]);
            }
            else{
                System.out.print(mapa[i]);
            }
        }
        System.out.print("\n");
        System.out.print("Entre o movimento:");
        map.movePlayer(read.nextLine());
        mapa = map.getMap();
    }

It runs like this: A map with random chars are printed, some are normal floors some are traps EXAMPLE

T##_______
^__@__*^@_
@#^^#_#___
_*^^^#^^@^
^^_#_^____
^#_^#___##
*@^_^_____
^@##_^__^#
#_@^##^#**
#@^^_#@_#P
Enter movement:w
T##_______
^__@__*^@_
@#^^#_#___
_*^^^#^^@^
^^_#_^____
^#_^#___##
*@^_^_____
^@##_^__^#
#_@^##^#*P
#@^^_#@_#P
Enter movement: w
T##_______
^__@__*^@_
@#^^#_#___
_*^^^#^^@^
^^_#_^____
^#_^#___##
*@^_^_____
^@##_^__^P
#_@^##^#*P
#@^^_#@_#P

How can I make the program to print the place where the Player originally was with the previous char it had, in this case a blank space in position [99] and a "*" in position [89]? Thanks for the patience!

3 Answers 3

4

Instead of tracking the player inside the map, you can leave the map unchanged and just track the player's position.

int playerpos = 99;
for (int j = 0; j<10; j++){
    for(int i = 0; i < map.length; i++){
        if (playerpos == i)
          System.out.print("P");
        else
          System.out.print(mapa[i]);
        if (i %10 == 9){
            System.out.println();
        }
    }

Then your movePlayer method just changes the playerpos variable.

Sign up to request clarification or add additional context in comments.

2 Comments

Good idea. There's a syntax error in line 7(mapa instead of map)
From OP's code, mapa is an instance of map, so that was deliberate. Doesn't match the map.length in the for loop though, but that matches the original code so maybe it's a static constant he's pulling.
0

before player's movement you need to store data from the target cell, like this

int moved = i+move;
previousChar = mapa[moved]; // store replaced symbol
mapa[moved] = "P";

so, during next move you need to restore original char in old position, it could look like this:

int moved = i+move;
mapa[i] = previousChar; // restore original symbol at current player's location
previousChar = mapa[moved]; // preserve symbol at new player's location
mapa[moved] = "P"; // move player to new location

don't forget to init previousChar before very first move

Comments

0

As Lashane is saying, use a variable to store the overriden char for later assignment:

public class map{
    [...]

    this.init(function() {
        last_squarechar = INIT_SQUARECHAR
        [...]
    });

    public void movePlayer(String entrada) {
        if (entrada.equalsIgnoreCase("w")){
            move = -10;
        }
        else if (entrada.equalsIgnoreCase("s")){
            move = 10;
        }
        else if (entrada.equalsIgnoreCase("d")){
            move = 1;
        }
        else if (entrada.equalsIgnoreCase("a")){
            move = -1;
        }
    }
    for(int i = 0; i < mapa.length; i++){
        if (mapa[i].equals("P")){
            int moved = i+move;
            mapa[i] = last_squarechar
            last_squarechar = mapa[moved]
            mapa[moved] = "P";
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.