From 686eb2c3e325d38ae23651799706f786c6c3a7b1 Mon Sep 17 00:00:00 2001 From: Lyanis Souidi Date: Fri, 28 Apr 2023 02:51:23 +0200 Subject: [PATCH] Ajout de la prise en charge des clics sur la grille --- src/GridController.java | 18 +++++++++++++++--- src/GridView.java | 11 ++++++++++- src/Square.java | 7 +++++-- src/Thesee.java | 3 ++- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/GridController.java b/src/GridController.java index 5689ceb..2f0b046 100644 --- a/src/GridController.java +++ b/src/GridController.java @@ -35,14 +35,26 @@ public class GridController { if (square.isWall()) { square.setEmpty(); } else { - square.setWall(); + try { + square.setWall(); + } catch (Exception ex) { + JOptionPane.showMessageDialog(view, ex.getMessage(), "Erreur", JOptionPane.ERROR_MESSAGE); + } } view.repaint(); } else if (editMode == Mode.THESEE) { - model.getThesee().setSquare(square); + try { + model.getThesee().setSquare(square); + } catch (Exception ex) { + JOptionPane.showMessageDialog(view, ex.getMessage(), "Erreur", JOptionPane.ERROR_MESSAGE); + } view.repaint(); } else if (editMode == Mode.EXIT) { - square.setExit(); + try { + square.setExit(); + } catch (Exception ex) { + JOptionPane.showMessageDialog(view, ex.getMessage(), "Erreur", JOptionPane.ERROR_MESSAGE); + } view.repaint(); } } diff --git a/src/GridView.java b/src/GridView.java index bcefb17..f49651b 100644 --- a/src/GridView.java +++ b/src/GridView.java @@ -26,7 +26,16 @@ public class GridView extends JPanel { } public Square click(MouseEvent e) { - // TODO: Détection du clic sur une case + if ((e.getX() < this.gridStartX) || (e.getX() > (this.gridStartX + this.gridSize)) || (e.getY() < this.gridStartY) || (e.getY() > (this.gridStartY + this.gridSize))) return null; + int x = (e.getX() - this.gridStartX) / this.squareSize; + int y = (e.getY() - this.gridStartY) / this.squareSize; + if ((x >= 0) && (x < this.model.getSize()) && (y >= 0) && (y < this.model.getSize())) { + try { + return this.model.getSquare(y, x); + } catch (Exception ex) { + return null; + } + } return null; } diff --git a/src/Square.java b/src/Square.java index 5e7de4c..fc4d9eb 100644 --- a/src/Square.java +++ b/src/Square.java @@ -37,14 +37,17 @@ public class Square { /** * Sets the current square as a wall */ - public void setWall() { + 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."); this.type = 1; } /** * Removes the existing exit from the grid (if it exists) and sets the current square as an exit */ - public void setExit() { + 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 { diff --git a/src/Thesee.java b/src/Thesee.java index 7a42617..3b382ea 100644 --- a/src/Thesee.java +++ b/src/Thesee.java @@ -2,7 +2,8 @@ public class Thesee { private Square square; private Square intialSquare; - public void setSquare(Square square) { + public void setSquare(Square square) throws Exception { + if (square.isExit()) throw new Exception("Vous ne pouvez pas placer Thesee sur la même case que la sortie. Déplacez d'abord la sortie puis réessayez."); square.setEmpty(); this.square = square; if (this.intialSquare == null) this.intialSquare = this.square;