Brandon Finley
Brandon Finley

Reputation: 127

Looking for a more elegant solution to move character

Trying to move my character across the terminal on a checker-style board. I'm sure there is a more elegant way to write this code, but I'm having a brain-fart on how to do so.

// move your character.
public void moveYou(String wasd, You person) {
    if (wasd.equals("w")) {
        // swaps ground in front of you with you.
        ground[person.getX()][person.getY()] = ground[person.getX() - 1][person.getY()];
        // replaces you with ground above you.
        ground[person.getX() - 1][person.getY()] = person;
        person.setXandY(person.getX() - 1, person.getY());
        person.setDir("[ ^ ]");
    } else if (wasd.equals("s")) {
        ground[person.getX()][person.getY()] = ground[person.getX() + 1][person.getY()];
        ground[person.getX() + 1][person.getY()] = person;
        person.setXandY(person.getX() + 1, person.getY());
        person.setDir("[ v ]");
    } else if (wasd.equals("a")) {
        ground[person.getX()][person.getY()] = ground[person.getX()][person.getY() - 1];
        ground[person.getX()][person.getY() - 1] = person;
        person.setXandY(person.getX(), person.getY() - 1);
        person.setDir("[ < ]");
    } else if (wasd.equals("d")) {
        ground[person.getX()][person.getY()] = ground[person.getX()][person.getY() + 1];
        ground[person.getX()][person.getY() + 1] = person;
        person.setXandY(person.getX(), person.getY() + 1);
        person.setDir("[ > ]");
    }
    printMap();
}

Upvotes: 0

Views: 76

Answers (1)

Mạnh Quyết Nguyễn
Mạnh Quyết Nguyễn

Reputation: 18235

Map your value with the intended behaviour via Enum

public enum Move {
  UP("w", "^", new CoordinateChange(1, 0)),
  DOWN(...),
  LEFT(...),
  RIGHT(...);

  private final String key;
  private final String direction;
  private final coordinateChange;

  private CoordinateChange {
    private int x;
    private int y;
    // getter, setter, constructor
  }

  public static Move of(String keyboard) {
    // COnvert key board `w`,`s`, `a`, `d` to specific Move
  }
}

Now convert your "move to" action like this one:

if (wasd.equals("w")) {
    // swaps ground in front of you with you.
    ground[person.getX()][person.getY()] = ground[person.getX() - 1][person.getY()]; 
    // replaces you with ground above you.
    ground[person.getX() - 1][person.getY()] = person;
    person.setXandY(person.getX() - 1, person.getY());
    person.setDir("[ ^ ]");
}

with a call to the following method:

public void changeCoordination(String wasd)
    Move move = Move.of(wasd);
    // ground[person.getX()][person.getY()] = ground[person.getX() - 1][person.getY()];  
    // Don't know what you're trying to do here but hope you got the idea, just place the coordinate change to `Move`
    ground[person.getX() + move.getCoordinateChange().getX()][person.getY() + move.getCoordinateChange().getY()] = person;
    person.setDir(move.getDirection());
}

And you don't have if/else anymore. Just write that function and pass the wasd value.

Upvotes: 4

Related Questions