class Command:
    def execute(self):
        raise NotImplementedError

    def undo(self):
        raise NotImplementedError


class Player:
    def __init__(self, position):
        self.position = position


class MoveCommand(Command):
    DIRS = {
        "W": (0, -1),
        "S": (0, 1),
        "A": (-1, 0),
        "D": (1, 0),
    }

    def __init__(self, maze, player, direction):
        self.maze = maze
        self.player = player
        self.direction = direction.upper()
        self.prev_position = None

    def execute(self):
        if self.direction not in self.DIRS:
            return False
        dx, dy = self.DIRS[self.direction]
        current = self.player.position
        nxt = self.maze.getCell(current.x + dx, current.y + dy)
        if nxt is None or not nxt.isPassable():
            return False
        self.prev_position = current
        self.player.position = nxt
        return True

    def undo(self):
        if self.prev_position is not None:
            self.player.position = self.prev_position
            return True
        return False