2023-04-28 23:24:33 +02:00
|
|
|
public class TheseeController {
|
|
|
|
private final Thesee model;
|
2023-04-27 17:57:02 +02:00
|
|
|
private GridView gridView;
|
|
|
|
|
|
|
|
public TheseeController(Thesee model, GridView gridView) {
|
|
|
|
this.model = model;
|
|
|
|
this.gridView = gridView;
|
|
|
|
}
|
|
|
|
|
2023-04-28 23:24:33 +02:00
|
|
|
public TheseeController(Thesee model) {
|
|
|
|
this.model = model;
|
2023-04-27 17:57:02 +02:00
|
|
|
}
|
|
|
|
|
2023-04-28 23:24:33 +02:00
|
|
|
/**
|
|
|
|
* Move Thésée in the given direction
|
|
|
|
* @param direction The direction to move Thésée to
|
|
|
|
* @param simulation The simulation model
|
|
|
|
* @return true if the move was successful, false otherwise.
|
|
|
|
*/
|
|
|
|
public boolean move(Direction direction, Simulation simulation) {
|
|
|
|
simulation.addMove();
|
2023-04-27 17:57:02 +02:00
|
|
|
try {
|
2023-04-28 23:24:33 +02:00
|
|
|
Square newSquare = this.model.getSquare(direction);
|
|
|
|
newSquare.setVisited(true);
|
|
|
|
// If the new square is a wall, add a move to simulate the rollback of the move
|
|
|
|
if (newSquare.isWall()) {
|
|
|
|
simulation.addMove();
|
|
|
|
if (this.gridView != null) this.gridView.repaint();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (newSquare.isExit()) simulation.setEnded();
|
|
|
|
else this.model.setSquare(newSquare);
|
|
|
|
if (this.gridView != null) this.gridView.repaint();
|
2023-04-27 17:57:02 +02:00
|
|
|
return true;
|
|
|
|
} catch (Exception e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-28 23:24:33 +02:00
|
|
|
/**
|
|
|
|
* Get the available directions for the next move.
|
|
|
|
* It helps prevent Thésée from going out of the grid.
|
|
|
|
* @return The available directions
|
|
|
|
*/
|
|
|
|
public Direction[] getAvailableDirections() {
|
|
|
|
Direction[] availableDirections = new Direction[4];
|
|
|
|
int availableDirectionsCount = 0;
|
|
|
|
|
2023-04-27 17:57:02 +02:00
|
|
|
try {
|
2023-04-28 23:24:33 +02:00
|
|
|
this.model.getSquare(Direction.UP);
|
|
|
|
availableDirections[availableDirectionsCount] = Direction.UP;
|
|
|
|
availableDirectionsCount++;
|
|
|
|
} catch (Exception ignored) {}
|
2023-04-27 17:57:02 +02:00
|
|
|
|
|
|
|
try {
|
2023-04-28 23:24:33 +02:00
|
|
|
this.model.getSquare(Direction.DOWN);
|
|
|
|
availableDirections[availableDirectionsCount] = Direction.DOWN;
|
|
|
|
availableDirectionsCount++;
|
|
|
|
} catch (Exception ignored) {}
|
2023-04-27 17:57:02 +02:00
|
|
|
|
2023-04-28 23:24:33 +02:00
|
|
|
try {
|
|
|
|
this.model.getSquare(Direction.LEFT);
|
|
|
|
availableDirections[availableDirectionsCount] = Direction.LEFT;
|
|
|
|
availableDirectionsCount++;
|
|
|
|
} catch (Exception ignored) {}
|
2023-04-27 17:57:02 +02:00
|
|
|
|
2023-04-28 23:24:33 +02:00
|
|
|
try {
|
|
|
|
this.model.getSquare(Direction.RIGHT);
|
|
|
|
availableDirections[availableDirectionsCount] = Direction.RIGHT;
|
|
|
|
availableDirectionsCount++;
|
|
|
|
} catch (Exception ignored) {}
|
2023-04-27 17:57:02 +02:00
|
|
|
|
2023-04-28 23:24:33 +02:00
|
|
|
Direction[] availableDirectionsTrimmed = new Direction[availableDirectionsCount];
|
|
|
|
System.arraycopy(availableDirections, 0, availableDirectionsTrimmed, 0, availableDirectionsCount);
|
|
|
|
return availableDirectionsTrimmed;
|
2023-04-27 17:57:02 +02:00
|
|
|
}
|
|
|
|
}
|