2023-04-11 09:45:48 +02:00
public class Square {
2023-04-27 17:57:02 +02:00
public final int row ;
public final int column ;
public int type = 0 ;
private Grid gridModel ;
2023-04-11 09:45:48 +02:00
2023-04-27 17:57:02 +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 ;
}
2023-04-27 17:57:02 +02:00
/ * *
* 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 ;
}
2023-04-27 17:57:02 +02:00
/ * *
* 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 ;
}
2023-04-27 17:57:02 +02:00
/ * *
* 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 ;
}
2023-04-28 01:19:43 +02:00
public boolean isThesee ( ) {
return this . gridModel . getThesee ( ) . getSquare ( ) = = this ;
}
2023-04-27 17:57:02 +02:00
/ * *
* Sets the current square as a wall
* /
2023-04-28 02:51:23 +02:00
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 ;
}
2023-04-27 17:57:02 +02:00
/ * *
* Removes the existing exit from the grid ( if it exists ) and sets the current square as an exit
* /
2023-04-28 02:51:23 +02:00
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. " ) ;
2023-04-27 17:57:02 +02:00
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 ;
}
2023-04-27 17:57:02 +02:00
/ * *
* Sets the current square as empty ( removes wall or exit )
* /
2023-04-11 09:45:48 +02:00
public void setEmpty ( ) {
this . type = 0 ;
}
2023-04-27 17:57:02 +02:00
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
}