SAE21_2022/src/Square.java

89 lines
2.6 KiB
Java
Raw Normal View History

2023-04-11 09:45:48 +02:00
public class Square {
public final int row;
public final int column;
public int type = 0;
private Grid gridModel;
2023-04-11 09:45:48 +02:00
public Square(Grid gridModel, int row, int column) {
this.gridModel = gridModel;
2023-04-11 09:45:48 +02:00
this.row = row;
this.column = column;
}
/**
* Checks if the current square is a wall
* @return true if the current square is a wall, false otherwise
*/
2023-04-11 09:45:48 +02:00
public boolean isWall() {
return this.type == 1;
}
/**
* Checks if the current square is an exit
* @return true if the current square is an exit, false otherwise
*/
2023-04-11 09:45:48 +02:00
public boolean isExit() {
return this.type == 2;
}
/**
* Checks if the current square is empty (not a wall and not an exit)
* @return true if the current square is empty, false otherwise
*/
2023-04-11 09:45:48 +02:00
public boolean isEmpty() {
return this.type == 0;
}
public boolean isThesee() {
return this.gridModel.getThesee().getSquare() == this;
}
/**
* Sets the current square as a wall
*/
public void setWall() throws Exception {
if (this.gridModel.getThesee().getSquare() == this) throw new Exception("Vous ne pouvez pas placer un mur sur la même case que Thésée. Déplacez d'abord Thésée puis réessayez.");
if (this.isExit()) throw new Exception("Vous ne pouvez pas placer un mur sur la même case que la sortie. Déplacez d'abord la sortie puis réessayez.");
2023-04-11 09:45:48 +02:00
this.type = 1;
}
/**
* Removes the existing exit from the grid (if it exists) and sets the current square as an exit
*/
public void setExit() throws Exception {
if (this.gridModel.getThesee().getSquare() == this) throw new Exception("Vous ne pouvez pas placer la sortie sur la même case que Thésée. Déplacez d'abord Thésée puis réessayez.");
for (int i = 0; i < this.gridModel.getSize(); i++) {
for (int j = 0; j < this.gridModel.getSize(); j++) {
try {
if (this.gridModel.getSquare(i, j).isExit()) {
this.gridModel.getSquare(i, j).setEmpty();
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
2023-04-11 09:45:48 +02:00
this.type = 2;
}
/**
* Sets the current square as empty (removes wall or exit)
*/
2023-04-11 09:45:48 +02:00
public void setEmpty() {
this.type = 0;
}
public int getRow() {
return this.row;
}
public int getColumn() {
return this.column;
}
public Grid getGrid() {
return this.gridModel;
}
2023-04-11 09:45:48 +02:00
}