From b14bd72bfc7a2f1b84ea816e05d8a4d50bc08cce Mon Sep 17 00:00:00 2001 From: Nico Date: Mon, 8 May 2017 02:00:16 +0200 Subject: [PATCH] Renamed things in French; fixed bugs --- src/diaballik/Diaballik.java | 87 +++++----- .../ActionsControleur.java} | 34 ++-- .../controleur/AffichageControleur.java | 26 +++ .../controleur/TerrainControleur.java | 120 +++++++++++++ .../controller/AffichageController.java | 26 --- .../controller/TerrainController.java | 96 ----------- src/diaballik/model/Case.java | 10 -- src/diaballik/model/ConfigurationPartie.java | 18 +- src/diaballik/model/Jeu.java | 157 ++++++++++-------- src/diaballik/model/Pion.java | 33 +--- src/diaballik/model/Terrain.java | 8 +- src/diaballik/view/CaseView.java | 102 ------------ src/diaballik/view/PionView.java | 99 ----------- src/diaballik/view/TerrainView.java | 78 --------- .../ActionsView.java => vue/ActionsVue.java} | 28 ++-- .../AffichageVue.java} | 14 +- src/diaballik/vue/CaseVue.java | 119 +++++++++++++ src/diaballik/{view => vue}/Dialogs.java | 43 +++-- src/diaballik/vue/PionVue.java | 138 +++++++++++++++ src/diaballik/vue/TerrainVue.java | 88 ++++++++++ 20 files changed, 708 insertions(+), 616 deletions(-) rename src/diaballik/{controller/ActionsController.java => controleur/ActionsControleur.java} (56%) create mode 100644 src/diaballik/controleur/AffichageControleur.java create mode 100644 src/diaballik/controleur/TerrainControleur.java delete mode 100644 src/diaballik/controller/AffichageController.java delete mode 100644 src/diaballik/controller/TerrainController.java delete mode 100644 src/diaballik/view/CaseView.java delete mode 100644 src/diaballik/view/PionView.java delete mode 100644 src/diaballik/view/TerrainView.java rename src/diaballik/{view/ActionsView.java => vue/ActionsVue.java} (78%) rename src/diaballik/{view/AffichageView.java => vue/AffichageVue.java} (69%) create mode 100644 src/diaballik/vue/CaseVue.java rename src/diaballik/{view => vue}/Dialogs.java (85%) create mode 100644 src/diaballik/vue/PionVue.java create mode 100644 src/diaballik/vue/TerrainVue.java diff --git a/src/diaballik/Diaballik.java b/src/diaballik/Diaballik.java index 22c1238..8fdf175 100644 --- a/src/diaballik/Diaballik.java +++ b/src/diaballik/Diaballik.java @@ -1,18 +1,19 @@ package diaballik; -import diaballik.controller.ActionsController; -import diaballik.controller.AffichageController; -import diaballik.controller.TerrainController; +import diaballik.controleur.ActionsControleur; +import diaballik.controleur.AffichageControleur; +import diaballik.controleur.TerrainControleur; import diaballik.model.ConfigurationPartie; import diaballik.model.Jeu; import diaballik.model.Joueur; import diaballik.model.Terrain; -import diaballik.view.CaseView; -import diaballik.view.Dialogs; +import diaballik.vue.CaseVue; +import diaballik.vue.Dialogs; import javafx.application.Application; import javafx.application.Platform; import javafx.geometry.Insets; import javafx.geometry.Pos; +import javafx.scene.Cursor; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; @@ -28,14 +29,17 @@ public class Diaballik extends Application { public Stage stage; - public final static String CSS_MENU_FILE = "DiaballikMenu.css"; - public final static String CSS_JEU_FILE = "DiaballikJeu.css"; - public final static String CSS_DIALOG_FILE = "DiaballikDialogs.css"; + private final static String CSS_MENU = "DiaballikMenu.css"; + private final static String CSS_JEU = "DiaballikJeu.css"; + public final static String CSS_DIALOG = "DiaballikDialogs.css"; - public final static String SAVES_DIRECTORY = "saves"; + public final static String DOSSIER_SAUVEGARDES = "saves"; + public final static String DOSSIER_TERRAINS = "defaultTerrains"; - private final KeyCombination ctrlS = new KeyCodeCombination(KeyCode.S, KeyCombination.CONTROL_DOWN); // save - private final KeyCombination ctrlZ = new KeyCodeCombination(KeyCode.Z, KeyCombination.CONTROL_DOWN); // rollback + public final static String NOM_JEU = "Diaballik"; + + private final KeyCombination ctrlS = new KeyCodeCombination(KeyCode.S, KeyCombination.CONTROL_DOWN); // sauvegarde + private final KeyCombination ctrlZ = new KeyCodeCombination(KeyCode.Z, KeyCombination.CONTROL_DOWN); // actionAnnuler private Scene sceneJeu; private Scene sceneMenu; @@ -50,69 +54,68 @@ public void showSceneMenu() { stage.setScene(sceneMenu); } - public void newGame() { - Optional cp = Dialogs.showNewGameDialog(); + private void nouveauJeu() { + Optional cp = Dialogs.montrerDialogNouvellePartie(); if (cp.isPresent()) { initSceneJeu(cp.get()); showSceneJeu(); } } - public void newGame(String path, boolean isSave) { + private void nouveauJeu(String path, boolean isSave) { ConfigurationPartie cp = new ConfigurationPartie(path, isSave); initSceneJeu(cp); showSceneJeu(); } - public void endGame(Joueur gagnant, int victoireType) { - Dialogs.showEndGame(gagnant, victoireType); + public void finJeu(Joueur gagnant, int victoireType) { + Dialogs.montrerFinJeu(gagnant, victoireType); showSceneMenu(); } - public void exit() { + public void terminer() { Platform.exit(); } private void initSceneJeu(ConfigurationPartie cp) { jeu = new Jeu(cp, this); - TerrainController terrainController = new TerrainController(this); - ActionsController actionsController = new ActionsController(this); - AffichageController affichageController = new AffichageController(this); + TerrainControleur terrainControleur = new TerrainControleur(this); + ActionsControleur actionsControleur = new ActionsControleur(this); + AffichageControleur affichageControleur = new AffichageControleur(this); BorderPane root = new BorderPane(); - root.setCenter(terrainController.getTerrainView()); - root.setRight(actionsController.getActionsView()); - root.setTop(affichageController.getAffichageView()); + root.setCenter(terrainControleur.getTerrainVue()); + root.setRight(actionsControleur.getActionsVue()); + root.setTop(affichageControleur.getAffichageVue()); - sceneJeu = new Scene(root, CaseView.LARGEUR * Terrain.LARGEUR + 225, CaseView.HAUTEUR * Terrain.HAUTEUR + 75); + sceneJeu = new Scene(root, CaseVue.LARGEUR * Terrain.LARGEUR + 225, CaseVue.HAUTEUR * Terrain.HAUTEUR + 75); sceneJeu.setOnKeyPressed(k -> { if (ctrlS.match(k)) { - actionsController.saveGame(SAVES_DIRECTORY); + actionsControleur.actionSauvegarderJeu(DOSSIER_SAUVEGARDES); } else if (ctrlZ.match(k)) { - actionsController.rollback(); + actionsControleur.actionAnnuler(); } }); - sceneJeu.getStylesheets().add(getClass().getResource(CSS_JEU_FILE).toExternalForm()); + sceneJeu.getStylesheets().add(getClass().getResource(CSS_JEU).toExternalForm()); } private void initSceneMenu() { BorderPane root = new BorderPane(); VBox vBox = new VBox(10); vBox.setAlignment(Pos.CENTER); - // TODO: séparer menuScene dans une autre classe - - Label titre = new Label("Diaballik"); + Label titre = new Label(NOM_JEU); titre.setPadding(new Insets(0, 0, 25, 0)); Button newGame = new Button("Nouvelle partie"); - newGame.setOnAction(e -> newGame()); + newGame.setOnAction(e -> nouveauJeu()); Button loadGame = new Button("Charger une partie"); loadGame.setOnAction(e -> { - Optional ofilename = Dialogs.showLoadName(SAVES_DIRECTORY); - ofilename.ifPresent(s -> newGame(SAVES_DIRECTORY + "/" + s, true)); + Optional ofilename = Dialogs.montrerDialogChoisirFichier(DOSSIER_SAUVEGARDES); + if (ofilename != null) + ofilename.ifPresent(s -> nouveauJeu(DOSSIER_SAUVEGARDES + "/" + s, true)); // Méthode avec Filechooser // Plus souple mais complexifie le choix pour l'utilisateur @@ -121,7 +124,7 @@ private void initSceneMenu() { fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Diaballik sauvegarde", "*.txt")); File file = fileChooser.showOpenDialog(stage); if (file != null) { - newGame(file.getAbsolutePath(), true); + nouveauJeu(file.getAbsolutePath(), true); }*/ }); @@ -129,10 +132,10 @@ private void initSceneMenu() { regles.setOnAction(e -> getHostServices().showDocument("http://inf362.forge.imag.fr/Projet/Regles/diaballik/")); Button credits = new Button("Crédits"); - credits.setOnAction(e -> Dialogs.showCredits()); + credits.setOnAction(e -> Dialogs.montrerCredits()); Button quitter = new Button("Quitter"); - quitter.setOnAction(e -> exit()); + quitter.setOnAction(e -> terminer()); vBox.getChildren().add(titre); vBox.getChildren().add(newGame); @@ -146,7 +149,7 @@ private void initSceneMenu() { Platform.runLater(root::requestFocus); sceneMenu = new Scene(root, 600, 400); - sceneMenu.getStylesheets().add(getClass().getResource(CSS_MENU_FILE).toExternalForm()); + sceneMenu.getStylesheets().add(getClass().getResource(CSS_MENU).toExternalForm()); } @Override @@ -155,7 +158,7 @@ public void start(Stage primaryStage) throws Exception { initSceneMenu(); - stage.setTitle("Diaballik"); + stage.setTitle(NOM_JEU); stage.setResizable(false); stage.setScene(sceneMenu); stage.show(); @@ -172,4 +175,12 @@ public Jeu getJeu() { public Scene getSceneJeu() { return sceneJeu; } + + public void setCurseurSelection(Scene scene) { + scene.setCursor(Cursor.HAND); + } + + public void setCurseurNormal(Scene scene) { + scene.setCursor(Cursor.DEFAULT); + } } diff --git a/src/diaballik/controller/ActionsController.java b/src/diaballik/controleur/ActionsControleur.java similarity index 56% rename from src/diaballik/controller/ActionsController.java rename to src/diaballik/controleur/ActionsControleur.java index 437a5e9..67b6e34 100644 --- a/src/diaballik/controller/ActionsController.java +++ b/src/diaballik/controleur/ActionsControleur.java @@ -1,39 +1,39 @@ -package diaballik.controller; +package diaballik.controleur; import diaballik.Diaballik; import diaballik.model.Jeu; -import diaballik.view.ActionsView; -import diaballik.view.Dialogs; +import diaballik.vue.ActionsVue; +import diaballik.vue.Dialogs; import javafx.stage.FileChooser; import java.io.File; -public class ActionsController { +public class ActionsControleur { private final Jeu jeu; - private final ActionsView actionsView; + private final ActionsVue actionsVue; private final Diaballik diaballik; - public ActionsController(Diaballik diaballik) { + public ActionsControleur(Diaballik diaballik) { this.diaballik = diaballik; this.jeu = diaballik.getJeu(); - this.actionsView = new ActionsView(this); + this.actionsVue = new ActionsVue(this); } public Jeu getJeu() { return jeu; } - public ActionsView getActionsView() { - return actionsView; + public ActionsVue getActionsVue() { + return actionsVue; } - public void menu() { - if (Dialogs.confirmByDialog("Vous allez quitter le jeu. La partie sera perdue. Voulez-vous continuer?")) { + public void actionMenu() { + if (Dialogs.dialogConfirmation("Vous allez quitter le jeu. La partie sera perdue. Voulez-vous continuer?")) { diaballik.showSceneMenu(); } } - public void saveGame(String directory) { + public void actionSauvegarderJeu(String directory) { String filename; final FileChooser fileChooser = new FileChooser(); fileChooser.setInitialDirectory(new File(directory)); @@ -45,16 +45,16 @@ public void saveGame(String directory) { if (!filename.endsWith(".txt")) filename += ".txt"; - System.out.println("Save to " + filename); - this.jeu.save(filename); + System.out.println("Sauvegarde vers " + filename); + this.jeu.sauvegarde(filename); } } - public void antijeu() { + public void actionAntijeu() { System.out.println("Antijeu : " + jeu.antijeu()); } - public void rollback() { - jeu.rollback(); + public void actionAnnuler() { + jeu.annuler(); } } diff --git a/src/diaballik/controleur/AffichageControleur.java b/src/diaballik/controleur/AffichageControleur.java new file mode 100644 index 0000000..eee2f96 --- /dev/null +++ b/src/diaballik/controleur/AffichageControleur.java @@ -0,0 +1,26 @@ +package diaballik.controleur; + +import diaballik.Diaballik; +import diaballik.model.Jeu; +import diaballik.vue.AffichageVue; + +public class AffichageControleur { + private final Diaballik diaballik; + + private final Jeu jeu; + private final AffichageVue affichageVue; + + public AffichageControleur(Diaballik diaballik) { + this.diaballik = diaballik; + this.jeu = diaballik.getJeu(); + this.affichageVue = new AffichageVue(this); + } + + public Jeu getJeu() { + return jeu; + } + + public AffichageVue getAffichageVue() { + return affichageVue; + } +} diff --git a/src/diaballik/controleur/TerrainControleur.java b/src/diaballik/controleur/TerrainControleur.java new file mode 100644 index 0000000..1ee34c5 --- /dev/null +++ b/src/diaballik/controleur/TerrainControleur.java @@ -0,0 +1,120 @@ +package diaballik.controleur; + +import diaballik.Diaballik; +import diaballik.model.Case; +import diaballik.model.Jeu; +import diaballik.model.Pion; +import diaballik.vue.CaseVue; +import diaballik.vue.PionVue; +import diaballik.vue.TerrainVue; + +import java.util.ArrayList; + +public class TerrainControleur { + public final Diaballik diaballik; + + private final Jeu jeu; + private final TerrainVue terrainVue; + + private PionVue pionSelectionne; + private boolean modeActionDeplacement = false; // false si le mode est un deplacement, true si c'est une passe + + private ArrayList casesMarquees = new ArrayList<>(); + private ArrayList pionsMarques = new ArrayList<>(); + + public TerrainControleur(Diaballik diaballik) { + this.diaballik = diaballik; + this.jeu = diaballik.getJeu(); + this.terrainVue = new TerrainVue(this); + } + + public boolean caseEstMarquee(CaseVue c) { + return casesMarquees.contains(c); + } + + public Jeu getJeu() { + return jeu; + } + + public TerrainVue getTerrainVue() { + return terrainVue; + } + + public void clicSouris(CaseVue caseCliquee) { + PionVue pionVueCorrespondant = caseCliquee.getPionVue(); + + if (pionSelectionne == null) { // Si on avait pas mémorisé de pion déjà sélectionnée + if (pionVueCorrespondant != null) { // il y a un pion sur la case + if (pionVueCorrespondant.getPion().getCouleur() == this.jeu.getJoueurActuel().getCouleur()) { // pion de la bonne couleur + modeActionDeplacement = pionVueCorrespondant.getPion().aLaBalle(); // on set le bon mode + selectionPion(pionVueCorrespondant); // on le sélectionne + } + } + } else { // sinon + if (caseCliquee == this.pionSelectionne.getCaseVue()) { // l'utilisateur a cliqué sur la même case + finSelection(); + } else if (modeActionDeplacement) { // si on attend une passe, il faut un pion + if (pionVueCorrespondant != null) { + if (pionVueCorrespondant.getPion().getCouleur() == this.jeu.getJoueurActuel().getCouleur()) { // de la bonne couleur + this.jeu.passe(pionSelectionne.getPion(), pionVueCorrespondant.getPion()); + } + } + + finSelection(); + } else { // on attend un déplacement, donc vers une case + if (pionVueCorrespondant != null) { + if (pionVueCorrespondant.getPion().getCouleur() == this.jeu.getJoueurActuel().getCouleur()) { + finSelection(); + modeActionDeplacement = pionVueCorrespondant.getPion().aLaBalle(); // on set le bon mode + selectionPion(pionVueCorrespondant); // on le sélectionne + } + } else { + pionVueCorrespondant = this.pionSelectionne; + if (this.jeu.deplacement(pionSelectionne.getPion(), caseCliquee.getCase())) { // on tente le déplacement + // si on a réussi + if (jeu.getJoueurActuel().getDeplacementsRestants() > 0) { + finSelection(); + selectionPion(pionVueCorrespondant); + } else { + finSelection(); + } + } else { + finSelection(); + } + } + } + } + } + + private void selectionPion(PionVue pionVue) { + pionVue.setSelectionne(true); + this.pionSelectionne = pionVue; + calculActionsPossibles(pionVue.getPion()); + } + + private void calculActionsPossibles(Pion pionClique) { + if (!modeActionDeplacement) { + ArrayList casesPossibles = jeu.getDeplacementsPossibles(pionClique); + + for (Case c : casesPossibles) casesMarquees.add(getTerrainVue().getCaseSur(c.getPoint())); + for (CaseVue c : casesMarquees) c.setMarque(true); + } else { + ArrayList pionsPossibles = jeu.getPassesPossibles(pionClique); + + for (Pion p : pionsPossibles) pionsMarques.add(getTerrainVue().getCaseSur(p.getPosition().getPoint()).getPionVue()); + for (PionVue p : pionsMarques) p.setMarque(true); + } + } + + public void finSelection() { + if (this.pionSelectionne != null) { + this.pionSelectionne.setSelectionne(false); + this.pionSelectionne = null; + } + + for (CaseVue c : casesMarquees) c.setMarque(false); + for (PionVue p : pionsMarques) p.setMarque(false); + casesMarquees.clear(); + pionsMarques.clear(); + } +} diff --git a/src/diaballik/controller/AffichageController.java b/src/diaballik/controller/AffichageController.java deleted file mode 100644 index 10b384b..0000000 --- a/src/diaballik/controller/AffichageController.java +++ /dev/null @@ -1,26 +0,0 @@ -package diaballik.controller; - -import diaballik.Diaballik; -import diaballik.model.Jeu; -import diaballik.view.AffichageView; - -public class AffichageController { - private final Diaballik diaballik; - - private final Jeu jeu; - private final AffichageView affichageView; - - public AffichageController(Diaballik diaballik) { - this.diaballik = diaballik; - this.jeu = diaballik.getJeu(); - this.affichageView = new AffichageView(this); - } - - public Jeu getJeu() { - return jeu; - } - - public AffichageView getAffichageView() { - return affichageView; - } -} diff --git a/src/diaballik/controller/TerrainController.java b/src/diaballik/controller/TerrainController.java deleted file mode 100644 index f48a954..0000000 --- a/src/diaballik/controller/TerrainController.java +++ /dev/null @@ -1,96 +0,0 @@ -package diaballik.controller; - -import diaballik.Diaballik; -import diaballik.model.Case; -import diaballik.model.Jeu; -import diaballik.model.Pion; -import diaballik.model.Point; -import diaballik.view.TerrainView; - -import java.util.ArrayList; - -public class TerrainController { - public final Diaballik diaballik; - - private final Jeu jeu; - private final TerrainView terrainView; - private Case caseSelectionne; - private ArrayList casesMarquees = new ArrayList<>(); - private ArrayList pionsMarques = new ArrayList<>(); - - public TerrainController(Diaballik diaballik) { - this.diaballik = diaballik; - this.jeu = diaballik.getJeu(); - this.terrainView = new TerrainView(this); - } - - public boolean caseEstMarquee(Case c) { - return casesMarquees.contains(c); - } - - public Jeu getJeu() { - return jeu; - } - - public TerrainView getTerrainView() { - return terrainView; - } - - // TODO: rework ça - public void mouseClicked(Point point) { - Case caseCliquee = this.getJeu().getTerrain().getCaseAt(point); - if (this.caseSelectionne == null) { - if (caseCliquee.getPion() != null && caseCliquee.getPion().getCouleur() == this.jeu.getJoueurActuel().getCouleur()) { - this.caseSelectionne = caseCliquee; - this.caseSelectionne.getPion().setSelectionne(true); - marquerAll(caseCliquee); - } - } else { - if (caseCliquee.getPion() != null) { - if (caseCliquee.getPion().getCouleur() == this.jeu.getJoueurActuel().getCouleur()) { - if (caseCliquee == this.caseSelectionne) { - this.caseSelectionne.getPion().setSelectionne(false); - finSelection(); - } else if (this.caseSelectionne.getPion().aLaBalle()) { - this.caseSelectionne.getPion().setSelectionne(false); - this.jeu.passe(this.caseSelectionne.getPion(), caseCliquee); - finSelection(); - } else { - this.caseSelectionne.getPion().setSelectionne(false); - finSelection(); - this.caseSelectionne = caseCliquee; - this.caseSelectionne.getPion().setSelectionne(true); - marquerAll(caseCliquee); - } - } - } else { - this.caseSelectionne.getPion().setSelectionne(false); - this.jeu.deplacement(this.caseSelectionne.getPion(), caseCliquee); - finSelection(); - if (jeu.getJoueurActuel().getDeplacementsRestants() > 0) { - this.caseSelectionne = caseCliquee; - this.caseSelectionne.getPion().setSelectionne(true); - marquerAll(caseCliquee); - } - } - } - } - - private void marquerAll(Case caseCliquee) { - if (!caseCliquee.getPion().aLaBalle()) { - casesMarquees = jeu.getDeplacementsPossibles(caseCliquee.getPion()); - for (Case c : casesMarquees) c.setMarque(true); - } else { - pionsMarques = jeu.getPassesPossibles(caseCliquee.getPion()); - for (Pion p : pionsMarques) p.setMarque(true); - } - } - - private void finSelection() { - this.caseSelectionne = null; - for (Case c : casesMarquees) c.setMarque(false); - for (Pion p : pionsMarques) p.setMarque(false); - casesMarquees.clear(); - pionsMarques.clear(); - } -} diff --git a/src/diaballik/model/Case.java b/src/diaballik/model/Case.java index cd76409..a1c4752 100644 --- a/src/diaballik/model/Case.java +++ b/src/diaballik/model/Case.java @@ -6,8 +6,6 @@ public class Case extends Observable { private final Point point; private Pion pion; - private boolean marque; - public Case(Point point) { this(point, null); } @@ -15,21 +13,13 @@ public Case(Point point) { public Case(Point point, Pion pion) { this.point = point; this.pion = pion; - - this.marque = false; } public void setMarque(boolean marque) { - this.marque = marque; - setChanged(); notifyObservers(); } - public boolean isMarque() { - return marque; - } - public void setPion(Pion pion) { this.pion = pion; } diff --git a/src/diaballik/model/ConfigurationPartie.java b/src/diaballik/model/ConfigurationPartie.java index 81007e7..87f33a9 100644 --- a/src/diaballik/model/ConfigurationPartie.java +++ b/src/diaballik/model/ConfigurationPartie.java @@ -1,15 +1,17 @@ package diaballik.model; +import diaballik.Diaballik; + public class ConfigurationPartie { - String path; - boolean isSave; + String cheminFichier; // chemin vers le fichier terrain ou sauvegarde + boolean estUneSauvegarde; // vrai si cheminFichier est un fichier de sauvegarde, faux si simple fichier représentant un terrain String nomJoueur1; String nomJoueur2; int joueur1ia; int joueur2ia; - public final static String DEFAULT_TERRAIN_PATH = "defaultTerrains/defaultTerrain.txt"; + public final static String CHEMIN_TERRAIN_DEFAUT = Diaballik.DOSSIER_TERRAINS + "/defaultTerrain.txt"; public ConfigurationPartie(String nomJoueur1, String nomJoueur2, int joueur1ia, int joueur2ia) { this.nomJoueur1 = nomJoueur1; @@ -17,12 +19,12 @@ public ConfigurationPartie(String nomJoueur1, String nomJoueur2, int joueur1ia, this.joueur1ia = joueur1ia; this.joueur2ia = joueur2ia; - this.isSave = false; - this.path = DEFAULT_TERRAIN_PATH; + this.estUneSauvegarde = false; + this.cheminFichier = CHEMIN_TERRAIN_DEFAUT; } - public ConfigurationPartie(String path, boolean isSave) { - this.path = path; - this.isSave = isSave; + public ConfigurationPartie(String cheminFichier, boolean estUneSauvegarde) { + this.cheminFichier = cheminFichier; + this.estUneSauvegarde = estUneSauvegarde; } } diff --git a/src/diaballik/model/Jeu.java b/src/diaballik/model/Jeu.java index f60842d..8122de2 100644 --- a/src/diaballik/model/Jeu.java +++ b/src/diaballik/model/Jeu.java @@ -21,10 +21,10 @@ public Action(Pion pion, int action, Case caseAvant) { public Action(Jeu jeu, String s) { String[] parts = s.split(":"); Point pointPion = new Point(parts[0]); - this.pion = jeu.getTerrain().getCaseAt(pointPion).getPion(); + this.pion = jeu.getTerrain().getCaseSur(pointPion).getPion(); this.action = Integer.parseInt(parts[1]); Point pointCaseAvant = new Point(parts[2]); - this.caseAvant = jeu.getTerrain().getCaseAt(pointCaseAvant); + this.caseAvant = jeu.getTerrain().getCaseSur(pointCaseAvant); } public String getSaveString() { @@ -68,16 +68,16 @@ public String toString() { private int tour; private int joueurActuel; - private final static String nomsDisponiblesPath = "nomsDisponibles.txt"; + private final static String CHEMIN_NOM_DISPONIBLES = "nomsDisponibles.txt"; private final ArrayList nomsDisponibles = new ArrayList<>(); private final Diaballik diaballik; public final static int NOMBRE_JOUEURS = 2; - public final static int CHANGED_INFOS = 1; - public final static int CHANGED_TOUR = 2; - public final static int CHANGED_ALL = 3; + public final static int CHANGEMENT_INFOS = 1; + public final static int CHANGEMENT_TOUR = 2; + public final static int CHANGEMENT_GLOBAL = 3; public final static int VICTOIRE_NORMALE = 1; public final static int VICTOIRE_ANTIJEU = 2; @@ -92,11 +92,11 @@ public Jeu(ConfigurationPartie cp, Diaballik diaballik) { this.joueurs[0] = new Joueur(this, Joueur.JOUEUR_VERT); this.joueurs[1] = new Joueur(this, Joueur.JOUEUR_ROUGE); - load(cp); + charger(cp); this.joueurActuel = this.getTour() - 1; - updateListeners(CHANGED_ALL); + updateListeners(CHANGEMENT_GLOBAL); } private void initArrivee() { @@ -107,7 +107,7 @@ private void initArrivee() { } private void initNomsDisponibles() { - try (BufferedReader br = new BufferedReader(new FileReader(nomsDisponiblesPath))) { + try (BufferedReader br = new BufferedReader(new FileReader(CHEMIN_NOM_DISPONIBLES))) { String sCurrentLine; while ((sCurrentLine = br.readLine()) != null) { nomsDisponibles.add(sCurrentLine); @@ -122,13 +122,13 @@ public String getNomAleatoire() { return nomChoisi; } - public void load(ConfigurationPartie cp) { - try (BufferedReader br = new BufferedReader(new FileReader(cp.path))) { + public void charger(ConfigurationPartie cp) { + try (BufferedReader br = new BufferedReader(new FileReader(cp.cheminFichier))) { String sCurrentLine; String parts[]; Joueur joueur; - if (cp.isSave) { + if (cp.estUneSauvegarde) { // tour actuel if ((sCurrentLine = br.readLine()) != null) { this.tour = Integer.parseInt(sCurrentLine); @@ -183,14 +183,14 @@ public Terrain getTerrain() { return this.terrain; } - // Save the game state into path - public void save(String path) { + // Save the game state into cheminFichier + public void sauvegarde(String chemin) { // On écrit les infos suivantes (qui caractérisent l'état du jeu) // tour // nomJ1:depl:pass // nomJ2:depl:pass // terrain... - try (BufferedWriter bw = new BufferedWriter(new FileWriter(path))) { + try (BufferedWriter bw = new BufferedWriter(new FileWriter(chemin))) { bw.write(this.getTour() + "\n"); bw.write(this.joueurs[0].getSaveString()); bw.write(this.joueurs[1].getSaveString()); @@ -206,7 +206,7 @@ public boolean antijeu() { Pion[] tab = new Pion[7]; // on se souvient de la ligne des pions for (Pion p : getTerrain().getPions()[getJoueurAdverse().getCouleur()]) { // pour chaque pion du joueur adversaire if (tab[p.getPosition().getPoint().getX()] != null) { - System.out.println("Antijeu: etape 1 non valide"); + System.out.println("Antijeu: étape 1 non valide (pions adverses ne prennent pas la largeur du terrain)"); return false; } else { tab[p.getPosition().getPoint().getX()] = p; @@ -218,7 +218,7 @@ public boolean antijeu() { Pion p1 = tab[0], p2 = tab[i]; while (++i < 7) { if (Math.abs(p1.getPosition().getPoint().getY() - p2.getPosition().getPoint().getY()) > 1) { - System.out.println("Antijeu: etape 2 non valide"); + System.out.println("Antijeu: étape 2 non valide (pions adverses non collés)"); return false; } @@ -226,7 +226,7 @@ public boolean antijeu() { p2 = tab[i]; } - // 3: on vérifie que le joueur qui a appelé antijeu a bien 3 pions collés au mur + // 3: on vérifie que le joueur qui a appelé actionAntijeu a bien 3 pions collés au mur int n = 0; Point po; Case c; @@ -236,44 +236,44 @@ public boolean antijeu() { if (n >= 3) break; // vérif case de gauche - c = getTerrain().getCaseAt(new Point(po.getX() - 1, po.getY())); + c = getTerrain().getCaseSur(new Point(po.getX() - 1, po.getY())); if (c != null && c.getPion() != null && c.getPion().getCouleur() == getJoueurAdverse().getCouleur()) { n++; continue; } // vérif case du haut - c = getTerrain().getCaseAt(new Point(po.getX(), po.getY() + 1)); + c = getTerrain().getCaseSur(new Point(po.getX(), po.getY() + 1)); if (c != null && c.getPion() != null && c.getPion().getCouleur() == getJoueurAdverse().getCouleur()) { n++; continue; } // vérif case de droite - c = getTerrain().getCaseAt(new Point(po.getX() + 1, po.getY())); + c = getTerrain().getCaseSur(new Point(po.getX() + 1, po.getY())); if (c != null && c.getPion() != null && c.getPion().getCouleur() == getJoueurAdverse().getCouleur()) { n++; continue; } // vérif case du bas - c = getTerrain().getCaseAt(new Point(po.getX(), po.getY() - 1)); + c = getTerrain().getCaseSur(new Point(po.getX(), po.getY() - 1)); if (c != null && c.getPion() != null && c.getPion().getCouleur() == getJoueurAdverse().getCouleur()) { n++; } } if (n >= 3) { - diaballik.endGame(getJoueurActuel(), VICTOIRE_ANTIJEU); + diaballik.finJeu(getJoueurActuel(), VICTOIRE_ANTIJEU); return true; } else { - System.out.println("Antijeu: etape 3 non valide (n = " + n + ")"); + System.out.println("Antijeu: étape 3 non valide (pions alliés collés à la ligne adverse = " + n + "/3)"); return false; } } // Effectue un déplacement (le pion p se déplace sur la case c) - public void deplacement(Pion p, Case c) { + public boolean deplacement(Pion p, Case c) { if (!p.aLaBalle() && deplacementPossible(p.getPosition(), c) && this.getJoueurActuel().actionPossible(Joueur.ACTION_DEPLACEMENT)) { System.out.println("Déplacement!"); @@ -283,11 +283,16 @@ public void deplacement(Pion p, Case c) { historique.add(new Action(p, Joueur.ACTION_DEPLACEMENT, cv)); - if (!this.getJoueurActuel().moinsAction(Joueur.ACTION_DEPLACEMENT)) { - changerTour(); - } + this.getJoueurActuel().moinsAction(Joueur.ACTION_DEPLACEMENT); + //if (!this.getJoueurActuel().moinsAction(Joueur.ACTION_DEPLACEMENT)) { + // changerTour(); + //} + + updateListeners(CHANGEMENT_INFOS); - updateListeners(CHANGED_INFOS); + return true; + } else { + return false; } } @@ -319,13 +324,13 @@ public ArrayList getDeplacementsPossibles(Pion pion) { Point pbase = pion.getPosition().getPoint(); Case ca; - ca = getTerrain().getCaseAt(new Point(pbase.getX() + 1, pbase.getY())); + ca = getTerrain().getCaseSur(new Point(pbase.getX() + 1, pbase.getY())); if (ca != null && ca.getPion() == null) c.add(ca); - ca = getTerrain().getCaseAt(new Point(pbase.getX(), pbase.getY() + 1)); + ca = getTerrain().getCaseSur(new Point(pbase.getX(), pbase.getY() + 1)); if (ca != null && ca.getPion() == null) c.add(ca); - ca = getTerrain().getCaseAt(new Point(pbase.getX() - 1, pbase.getY())); + ca = getTerrain().getCaseSur(new Point(pbase.getX() - 1, pbase.getY())); if (ca != null && ca.getPion() == null) c.add(ca); - ca = getTerrain().getCaseAt(new Point(pbase.getX(), pbase.getY() - 1)); + ca = getTerrain().getCaseSur(new Point(pbase.getX(), pbase.getY() - 1)); if (ca != null && ca.getPion() == null) c.add(ca); return c; @@ -341,49 +346,49 @@ public ArrayList getPassesPossibles(Pion pion) { Point p = pion.getPosition().getPoint(); int i = 0; - while ((c = getTerrain().getCaseAt(new Point(p.getX() - ++i, p.getY()))) != null && (c.getPion() == null || pionAllie(c.getPion()))) { + while ((c = getTerrain().getCaseSur(new Point(p.getX() - ++i, p.getY()))) != null && (c.getPion() == null || pionAllie(c.getPion()))) { if (c.getPion() != null && pionAllie(c.getPion())) pions.add(c.getPion()); } i = 0; - while ((c = getTerrain().getCaseAt(new Point(p.getX() + ++i, p.getY()))) != null && (c.getPion() == null || pionAllie(c.getPion()))) { + while ((c = getTerrain().getCaseSur(new Point(p.getX() + ++i, p.getY()))) != null && (c.getPion() == null || pionAllie(c.getPion()))) { if (c.getPion() != null && pionAllie(c.getPion())) pions.add(c.getPion()); } i = 0; - while ((c = getTerrain().getCaseAt(new Point(p.getX(), p.getY() + --i))) != null && (c.getPion() == null || pionAllie(c.getPion()))) { + while ((c = getTerrain().getCaseSur(new Point(p.getX(), p.getY() + --i))) != null && (c.getPion() == null || pionAllie(c.getPion()))) { if (c.getPion() != null && pionAllie(c.getPion())) pions.add(c.getPion()); } i = 0; - while ((c = getTerrain().getCaseAt(new Point(p.getX(), p.getY() + ++i))) != null && (c.getPion() == null || pionAllie(c.getPion()))) { + while ((c = getTerrain().getCaseSur(new Point(p.getX(), p.getY() + ++i))) != null && (c.getPion() == null || pionAllie(c.getPion()))) { if (c.getPion() != null && pionAllie(c.getPion())) pions.add(c.getPion()); } i = 0; - while ((c = getTerrain().getCaseAt(new Point(p.getX() + ++i, p.getY() + i))) != null && (c.getPion() == null || pionAllie(c.getPion()))) { + while ((c = getTerrain().getCaseSur(new Point(p.getX() + ++i, p.getY() + i))) != null && (c.getPion() == null || pionAllie(c.getPion()))) { if (c.getPion() != null && pionAllie(c.getPion())) pions.add(c.getPion()); } i = 0; - while ((c = getTerrain().getCaseAt(new Point(p.getX() + ++i, p.getY() - i))) != null && (c.getPion() == null || pionAllie(c.getPion()))) { + while ((c = getTerrain().getCaseSur(new Point(p.getX() + ++i, p.getY() - i))) != null && (c.getPion() == null || pionAllie(c.getPion()))) { if (c.getPion() != null && pionAllie(c.getPion())) pions.add(c.getPion()); } i = 0; - while ((c = getTerrain().getCaseAt(new Point(p.getX() + --i, p.getY() + i))) != null && (c.getPion() == null || pionAllie(c.getPion()))) { + while ((c = getTerrain().getCaseSur(new Point(p.getX() + --i, p.getY() + i))) != null && (c.getPion() == null || pionAllie(c.getPion()))) { if (c.getPion() != null && pionAllie(c.getPion())) pions.add(c.getPion()); } i = 0; - while ((c = getTerrain().getCaseAt(new Point(p.getX() + --i, p.getY() - i))) != null && (c.getPion() == null || pionAllie(c.getPion()))) { + while ((c = getTerrain().getCaseSur(new Point(p.getX() + --i, p.getY() - i))) != null && (c.getPion() == null || pionAllie(c.getPion()))) { if (c.getPion() != null && pionAllie(c.getPion())) pions.add(c.getPion()); } @@ -392,40 +397,40 @@ public ArrayList getPassesPossibles(Pion pion) { } // Vérifie la possibilité d'une passe (uniquement dans les axes, la couleur n'est pas vérifiée, ...) - private boolean passePossible(Case c1, Case c2) { - Point p = c1.getPoint(); - Point p2 = c2.getPoint(); + private boolean passePossible(Pion envoyeur, Pion receptionneur) { + Point pEnvoyeur = envoyeur.getPosition().getPoint(); + Point pReceptionneur = receptionneur.getPosition().getPoint(); // check alignement - if (p.getX() == p2.getX()) { // en ligne - int yMax = Math.max(p.getY(), p2.getY()); - int yMin = Math.min(p.getY(), p2.getY()); + if (pEnvoyeur.getX() == pReceptionneur.getX()) { // en ligne + int yMax = Math.max(pEnvoyeur.getY(), pReceptionneur.getY()); + int yMin = Math.min(pEnvoyeur.getY(), pReceptionneur.getY()); for (int y = yMax - 1; y > yMin; y--) { - Pion pionPresent = getTerrain().getCaseAt(new Point(p.getX(), y)).getPion(); + Pion pionPresent = getTerrain().getCaseSur(new Point(pEnvoyeur.getX(), y)).getPion(); if (pionPresent != null && pionPresent.getCouleur() != getJoueurActuel().getCouleur()) { return false; } } - } else if (p.getY() == p2.getY()) { // colonne - int xMax = Math.max(p.getX(), p2.getX()); - int xMin = Math.min(p.getX(), p2.getX()); + } else if (pEnvoyeur.getY() == pReceptionneur.getY()) { // colonne + int xMax = Math.max(pEnvoyeur.getX(), pReceptionneur.getX()); + int xMin = Math.min(pEnvoyeur.getX(), pReceptionneur.getX()); for (int x = xMax - 1; x > xMin; x--) { - Pion pionPresent = getTerrain().getCaseAt(new Point(x, p.getY())).getPion(); + Pion pionPresent = getTerrain().getCaseSur(new Point(x, pEnvoyeur.getY())).getPion(); if (pionPresent != null && pionPresent.getCouleur() != getJoueurActuel().getCouleur()) { return false; } } - } else if (Math.abs(p.getX() - p2.getX()) == Math.abs(p.getY() - p2.getY())) { // diagonale - int xMax = Math.max(p.getX(), p2.getX()); - int xMin = Math.min(p.getX(), p2.getX()); - int yMax = Math.max(p.getY(), p2.getY()); + } else if (Math.abs(pEnvoyeur.getX() - pReceptionneur.getX()) == Math.abs(pEnvoyeur.getY() - pReceptionneur.getY())) { // diagonale + int xMax = Math.max(pEnvoyeur.getX(), pReceptionneur.getX()); + int xMin = Math.min(pEnvoyeur.getX(), pReceptionneur.getX()); + int yMax = Math.max(pEnvoyeur.getY(), pReceptionneur.getY()); int y = yMax; for (int x = xMax - 1; x > xMin; x--) { y--; - Pion pionPresent = getTerrain().getCaseAt(new Point(x, y)).getPion(); + Pion pionPresent = getTerrain().getCaseSur(new Point(x, y)).getPion(); if (pionPresent != null && pionPresent.getCouleur() != getJoueurActuel().getCouleur()) { return false; } @@ -438,26 +443,34 @@ private boolean passePossible(Case c1, Case c2) { } // Effectue une passe d'un pion p vers un pion positionné sur une case c - public void passe(Pion p, Case c) { - if (p.aLaBalle() && passePossible(p.getPosition(), c) && this.getJoueurActuel().actionPossible(Joueur.ACTION_PASSE)) { + public boolean passe(Pion envoyeur, Pion receptionneur) { + if (envoyeur.aLaBalle() + && passePossible(envoyeur, receptionneur) + && this.getJoueurActuel().actionPossible(Joueur.ACTION_PASSE)) { + System.out.println("Passe!"); - Case cv = p.getPosition(); + Case cv = envoyeur.getPosition(); - p.passe(c); + envoyeur.passe(receptionneur); - historique.add(new Action(c.getPion(), Joueur.ACTION_PASSE, cv)); + historique.add(new Action(receptionneur, Joueur.ACTION_PASSE, cv)); - if (partieTerminee(c.getPion())) { + if (partieTerminee(receptionneur)) { // la partie est terminée (le vainqueur est joueurActuel()) - diaballik.endGame(getJoueurActuel(), VICTOIRE_NORMALE); + diaballik.finJeu(getJoueurActuel(), VICTOIRE_NORMALE); } - if (!this.getJoueurActuel().moinsAction(Joueur.ACTION_PASSE)) { - changerTour(); - } + this.getJoueurActuel().moinsAction(Joueur.ACTION_PASSE); + //if (!this.getJoueurActuel().moinsAction(Joueur.ACTION_PASSE)) { + // changerTour(); + //} + + updateListeners(CHANGEMENT_INFOS); - updateListeners(CHANGED_INFOS); + return true; + } else { + return false; } } @@ -490,7 +503,7 @@ public void changerTour() { joueurActuel = ++joueurActuel % Jeu.NOMBRE_JOUEURS; //joueurActuel = (joueurActuel + 1 >= Jeu.NOMBRE_JOUEURS ? 0 : joueurActuel + 1); - updateListeners(CHANGED_TOUR); + updateListeners(CHANGEMENT_TOUR); } public Joueur getJoueurActuel() { @@ -514,7 +527,7 @@ public int getTour() { return tour; } - public void rollback() { + public void annuler() { if (historique.size() == 0) return; Action a = historique.get(historique.size() - 1); @@ -526,7 +539,7 @@ public void rollback() { getJoueurActuel().plusAction(Joueur.ACTION_DEPLACEMENT); break; case Joueur.ACTION_PASSE: - a.getPion().passe(a.getCaseAvant()); + a.getPion().passe(a.getCaseAvant().getPion()); getJoueurActuel().plusAction(Joueur.ACTION_PASSE); break; @@ -534,7 +547,7 @@ public void rollback() { System.err.println("Action non reconnue"); } - updateListeners(CHANGED_ALL); + updateListeners(CHANGEMENT_GLOBAL); } public boolean pionAllie(Pion pion) { diff --git a/src/diaballik/model/Pion.java b/src/diaballik/model/Pion.java index 02fad4f..afeebf6 100644 --- a/src/diaballik/model/Pion.java +++ b/src/diaballik/model/Pion.java @@ -7,30 +7,12 @@ public class Pion extends Observable { private int couleur; private int numero; private Case position; - private boolean selectionne; - private boolean marque; public Pion(int couleur, int numero, Case position) { this.aLaBalle = false; this.couleur = couleur; this.numero = numero; this.position = position; - this.selectionne = false; - this.marque = false; - } - - public void setSelectionne(boolean b) { - this.selectionne = b; - updateListeneners(); - } - - public void setMarque(boolean b) { - this.marque = b; - updateListeneners(); - } - - public boolean isSelectionne() { - return selectionne; } public void setaLaBalle(boolean aLaBalle) { @@ -66,15 +48,10 @@ public void deplacer(Case nouvellePosition) { updateListeneners(); } - public void passe(Case caseAlliee) { - if (caseAlliee.getPion() == null) { // prérequis: la case où on pass doit être occupée - System.err.println("Pion (passe): Case destination libre"); - return; - } - + public void passe(Pion receptionneur) { this.setaLaBalle(false); // on n'a plus la balle - caseAlliee.getPion().setaLaBalle(true); // la balle est au pion de la case alliée - caseAlliee.getPion().updateListeneners(); + receptionneur.setaLaBalle(true); // la balle est au pion de la case alliée + receptionneur.updateListeneners(); updateListeneners(); } @@ -84,10 +61,6 @@ public void updateListeneners() { this.notifyObservers(); } - public boolean isMarque() { - return marque; - } - @Override public String toString() { return this.getPosition().getPoint().toString(); diff --git a/src/diaballik/model/Terrain.java b/src/diaballik/model/Terrain.java index 053b8b5..05d915b 100644 --- a/src/diaballik/model/Terrain.java +++ b/src/diaballik/model/Terrain.java @@ -81,11 +81,11 @@ public Terrain(String terrainString) { } } - public Pion getPionOf(int joueur, int num) { + public Pion getPionDe(int joueur, int num) { return this.pions[joueur][num]; } - public Case getCaseAt(Point point) { + public Case getCaseSur(Point point) { if (estValide(point)) return this.cases[point.getY()][point.getX()]; else return null; } @@ -94,7 +94,7 @@ private boolean estValide(Point point) { return point.getX() >= 0 && point.getX() < LARGEUR && point.getY() >= 0 && point.getY() < HAUTEUR; } - public String getPionRepr(Pion pion) { + public String getPionRepresentation(Pion pion) { String res = ""; if (pion == null) @@ -121,7 +121,7 @@ public String getSaveString() { sb.append(";"); else skip = false; - sb.append(getPionRepr(c.getPion())); + sb.append(getPionRepresentation(c.getPion())); } skip = true; sb.append("\n"); diff --git a/src/diaballik/view/CaseView.java b/src/diaballik/view/CaseView.java deleted file mode 100644 index 4a681de..0000000 --- a/src/diaballik/view/CaseView.java +++ /dev/null @@ -1,102 +0,0 @@ -package diaballik.view; - -import diaballik.model.Case; -import diaballik.model.Point; -import javafx.scene.Cursor; -import javafx.scene.layout.StackPane; - -import java.util.Observable; -import java.util.Observer; - -public class CaseView extends StackPane implements Observer { - private final TerrainView terrainView; - private final Case c; - private final Point point; - private PionView pionView; - - private final boolean pair; - private boolean hover = false; - - public final static int HAUTEUR = 80; - public final static int LARGEUR = 80; - - public CaseView(TerrainView terrainView, Case c) { - super(); - - this.terrainView = terrainView; - this.c = c; - this.point = c.getPoint(); - c.addObserver(this); - this.pionView = null; - - pair = Math.abs(point.getX() - point.getY()) % 2 == 0; - - this.setMaxWidth(LARGEUR); - this.setMaxHeight(HAUTEUR); - this.setWidth(LARGEUR); - this.setHeight(HAUTEUR); - this.setMinSize(LARGEUR, HAUTEUR); - - this.setOnMouseClicked(e -> terrainView.getTerrainController().mouseClicked(point)); - this.setOnMouseEntered(e -> hover(true)); - this.setOnMouseExited(e -> hover(false)); - - update(null, null); - } - - public void setPionView(PionView pionView) { - this.pionView = pionView; - if (pionView != null) { - this.getChildren().add(pionView); - if (hover) - hover(true); - } else { - this.getChildren().clear(); - } - } - - public void hover(boolean enter) { - // selection pion alliés, puis cases dispos - - if (this.pionView != null && terrainView.getTerrainController().getJeu().pionAllie(pionView.getPion())) { - this.pionView.hover(enter); - if (enter) - terrainView.getTerrainController().diaballik.getSceneJeu().setCursor(Cursor.HAND); - else - terrainView.getTerrainController().diaballik.getSceneJeu().setCursor(Cursor.DEFAULT); - hover = false; - } else if (terrainView.getTerrainController().caseEstMarquee(this.c)) { - hover = enter; - if (enter) - terrainView.getTerrainController().diaballik.getSceneJeu().setCursor(Cursor.HAND); - else - terrainView.getTerrainController().diaballik.getSceneJeu().setCursor(Cursor.DEFAULT); - } else { - hover = false; - } - - updateStyleClass(); - } - - public PionView getPionView() { - return pionView; - } - - @Override - public void update(Observable o, Object arg) { - updateStyleClass(); - } - - private void updateStyleClass() { - this.getStyleClass().clear(); - - if (hover) - this.getStyleClass().add("couleurSurvol"); - else if (c.isMarque()) - this.getStyleClass().add("couleurMarquage"); - else if (pair) - this.getStyleClass().add("couleurCasePair"); - else - this.getStyleClass().add("couleurCaseImpair"); - } -} \ No newline at end of file diff --git a/src/diaballik/view/PionView.java b/src/diaballik/view/PionView.java deleted file mode 100644 index 11d3944..0000000 --- a/src/diaballik/view/PionView.java +++ /dev/null @@ -1,99 +0,0 @@ -package diaballik.view; - -import diaballik.model.Joueur; -import diaballik.model.Pion; -import diaballik.model.Point; -import javafx.scene.paint.Color; -import javafx.scene.shape.Circle; - -import java.util.Observable; -import java.util.Observer; - -public class PionView extends Circle implements Observer { - private final Pion pion; - private CaseView caseView; - private TerrainView terrainView; - - private boolean enabled = true; - private boolean hover = false; - - private final static double RAYON = CaseView.HAUTEUR / 4; - private final static double RAYON_BALLE = RAYON * 1.5; - - public PionView(TerrainView terrainView, Pion pion) { - super(RAYON); - - this.pion = pion; - this.caseView = terrainView.getCaseAt(pion.getPosition().getPoint()); - this.terrainView = terrainView; - pion.addObserver(this); - - this.setStroke(Color.BLACK); - - this.update(); - } - - public void update() { - this.update(null, null); - } - - public void hover(boolean enter) { - hover = enter; - updateStyleClass(); - } - - public void resetState() { - this.getPion().setMarque(false); - this.hover = false; - } - - @Override - public void update(Observable o, Object arg) { - Point p = this.pion.getPosition().getPoint(); - - this.caseView.setPionView(null); - this.caseView = this.terrainView.getCaseAt(p); - this.caseView.setPionView(this); - - this.updateStyleClass(); - this.setRadius(getRayon()); - } - - private double getRayon() { - return (this.pion.aLaBalle() ? RAYON_BALLE : RAYON); - } - - private void updateStyleClass() { - this.getStyleClass().clear(); - - if (!enabled) { - this.getStyleClass().add("couleurAdversaire"); - } else if (hover) { - this.getStyleClass().add("couleurSurvol"); - } else if (pion.isSelectionne()) { - this.getStyleClass().add("couleurSelection"); - } else if (pion.isMarque()) { - this.getStyleClass().add("couleurMarquage"); - } else { - if (pion.getCouleur() == Joueur.JOUEUR_VERT) - this.getStyleClass().add("couleurJoueurVert"); - else - this.getStyleClass().add("couleurJoueurRouge"); - } - } - - public void disable() { - this.enabled = false; - resetState(); - update(); - } - - public void enable() { - this.enabled = true; - update(); - } - - public Pion getPion() { - return pion; - } -} diff --git a/src/diaballik/view/TerrainView.java b/src/diaballik/view/TerrainView.java deleted file mode 100644 index 3b0f938..0000000 --- a/src/diaballik/view/TerrainView.java +++ /dev/null @@ -1,78 +0,0 @@ -package diaballik.view; - -import diaballik.controller.TerrainController; -import diaballik.model.Jeu; -import diaballik.model.Joueur; -import diaballik.model.Point; -import diaballik.model.Terrain; -import javafx.scene.Cursor; -import javafx.scene.layout.GridPane; - -import java.util.Observable; -import java.util.Observer; - -public class TerrainView extends GridPane implements Observer { - private final TerrainController terrainController; - private final Terrain terrain; - - private final CaseView[][] cases; // Représentation visuelle du terrain (l'UI) - private final PionView[][] pions; - - public TerrainView(TerrainController terrainController) { - super(); - - this.terrainController = terrainController; - this.terrain = terrainController.getJeu().getTerrain(); - terrainController.getJeu().addObserver(this); - this.setId("terrainView"); - - this.setMaxSize(CaseView.LARGEUR * Terrain.LARGEUR, CaseView.HAUTEUR * Terrain.HAUTEUR); - - this.cases = new CaseView[Terrain.HAUTEUR][Terrain.LARGEUR]; - this.pions = new PionView[Jeu.NOMBRE_JOUEURS][Joueur.NOMBRE_PIONS]; - - int a = 0; - for (int i = 0; i < Terrain.HAUTEUR; i++) { - for (int j = 0; j < Terrain.LARGEUR; j++) { - CaseView cv = new CaseView(this, terrain.getCaseAt(new Point(j, i))); - - this.cases[i][j] = cv; - - this.add(cv, j, i); - } - } - - PionView pv; - for (int i = 0; i < Jeu.NOMBRE_JOUEURS; i++) { - for (int j = 0; j < Joueur.NOMBRE_PIONS; j++) { - pv = new PionView(this, this.terrain.getPionOf(i, j)); - this.pions[i][j] = pv; - } - } - - update(null, null); - } - - public CaseView getCaseAt(Point p) { - return cases[p.getY()][p.getX()]; - } - - public TerrainController getTerrainController() { - return terrainController; - } - - @Override - public void update(Observable o, Object arg) { - if (terrainController.getJeu().getJoueurActuel().getCouleur() == Joueur.JOUEUR_VERT) { - for (PionView p : pions[Joueur.JOUEUR_ROUGE]) p.disable(); - for (PionView p : pions[Joueur.JOUEUR_VERT]) p.enable(); - } else { - for (PionView p : pions[Joueur.JOUEUR_ROUGE]) p.enable(); - for (PionView p : pions[Joueur.JOUEUR_VERT]) p.disable(); - } - - int changed_type = (arg != null ? (int)arg : 0); - if (getTerrainController().diaballik.getSceneJeu() != null && changed_type == Jeu.CHANGED_TOUR) - getTerrainController().diaballik.getSceneJeu().setCursor(Cursor.DEFAULT); - } -} diff --git a/src/diaballik/view/ActionsView.java b/src/diaballik/vue/ActionsVue.java similarity index 78% rename from src/diaballik/view/ActionsView.java rename to src/diaballik/vue/ActionsVue.java index cc459e0..9a12c77 100644 --- a/src/diaballik/view/ActionsView.java +++ b/src/diaballik/vue/ActionsVue.java @@ -1,7 +1,7 @@ -package diaballik.view; +package diaballik.vue; import diaballik.Diaballik; -import diaballik.controller.ActionsController; +import diaballik.controleur.ActionsControleur; import diaballik.model.Jeu; import javafx.geometry.Insets; import javafx.geometry.Pos; @@ -9,12 +9,13 @@ import javafx.scene.control.Label; import javafx.scene.layout.BorderPane; import javafx.scene.layout.VBox; +import javafx.scene.paint.Color; import java.util.Observable; import java.util.Observer; -public class ActionsView extends BorderPane implements Observer { - private final ActionsController actionsController; +public class ActionsVue extends BorderPane implements Observer { + private final ActionsControleur actionsControleur; private final Jeu jeu; private final Label depl; @@ -23,7 +24,7 @@ public class ActionsView extends BorderPane implements Observer { private final Label deplInd; private final Label passInd; - public ActionsView(ActionsController actionsController) { + public ActionsVue(ActionsControleur actionsControleur) { super(); // Infos @@ -59,8 +60,8 @@ public ActionsView(ActionsController actionsController) { this.setId("actionsView"); - this.actionsController = actionsController; - this.jeu = actionsController.getJeu(); + this.actionsControleur = actionsControleur; + this.jeu = actionsControleur.getJeu(); this.jeu.addObserver(this); @@ -70,19 +71,19 @@ public ActionsView(ActionsController actionsController) { passerTour.setId("passerTour"); Button rollback = new Button("Rollback"); - rollback.setOnAction(e -> actionsController.rollback()); + rollback.setOnAction(e -> actionsControleur.actionAnnuler()); rollback.setMaxWidth(Double.MAX_VALUE); Button antijeu = new Button("Antijeu"); - antijeu.setOnAction(e -> actionsController.antijeu()); + antijeu.setOnAction(e -> actionsControleur.actionAntijeu()); antijeu.setMaxWidth(Double.MAX_VALUE); Button save = new Button("Sauvegarder"); - save.setOnAction(e -> actionsController.saveGame(Diaballik.SAVES_DIRECTORY)); + save.setOnAction(e -> actionsControleur.actionSauvegarderJeu(Diaballik.DOSSIER_SAUVEGARDES)); save.setMaxWidth(Double.MAX_VALUE); Button menu = new Button("Menu"); - menu.setOnAction(e -> actionsController.menu()); + menu.setOnAction(e -> actionsControleur.actionMenu()); menu.setMaxWidth(Double.MAX_VALUE); vBoxActions.getChildren().add(passerTour); @@ -109,6 +110,11 @@ public void update(Observable o, Object arg) { else passInd.setText("passe"); depl.setText(deplRest + ""); + if (deplRest < 1) depl.setTextFill(Color.RED); + else depl.setTextFill(Color.BLACK); + pass.setText(passRest + ""); + if (passRest < 1) pass.setTextFill(Color.RED); + else pass.setTextFill(Color.BLACK); } } diff --git a/src/diaballik/view/AffichageView.java b/src/diaballik/vue/AffichageVue.java similarity index 69% rename from src/diaballik/view/AffichageView.java rename to src/diaballik/vue/AffichageVue.java index ef53a0b..8c12930 100644 --- a/src/diaballik/view/AffichageView.java +++ b/src/diaballik/vue/AffichageVue.java @@ -1,6 +1,6 @@ -package diaballik.view; +package diaballik.vue; -import diaballik.controller.AffichageController; +import diaballik.controleur.AffichageControleur; import diaballik.model.Jeu; import diaballik.model.Joueur; import javafx.scene.control.Label; @@ -9,16 +9,16 @@ import java.util.Observable; import java.util.Observer; -public class AffichageView extends BorderPane implements Observer { - private final AffichageController affichageController; +public class AffichageVue extends BorderPane implements Observer { + private final AffichageControleur affichageControleur; private final Jeu jeu; private final Label joueurActuel; - public AffichageView(AffichageController affichageController) { + public AffichageVue(AffichageControleur affichageControleur) { super(); - this.affichageController = affichageController; - this.jeu = affichageController.getJeu(); + this.affichageControleur = affichageControleur; + this.jeu = affichageControleur.getJeu(); this.jeu.addObserver(this); this.setId("affichageView"); diff --git a/src/diaballik/vue/CaseVue.java b/src/diaballik/vue/CaseVue.java new file mode 100644 index 0000000..95a618a --- /dev/null +++ b/src/diaballik/vue/CaseVue.java @@ -0,0 +1,119 @@ +package diaballik.vue; + +import diaballik.Diaballik; +import diaballik.model.Case; +import diaballik.model.Point; +import javafx.scene.layout.StackPane; + +import java.util.Observable; +import java.util.Observer; + +public class CaseVue extends StackPane implements Observer { + private final TerrainVue terrainVue; + private final Case c; + private PionVue pionVue; + + private final boolean pair; + + private boolean marque = false; + private boolean survol = false; + + public final static int HAUTEUR = 80; + public final static int LARGEUR = 80; + + public CaseVue(TerrainVue terrainVue, Case c) { + super(); + + this.terrainVue = terrainVue; + this.c = c; + c.addObserver(this); + this.pionVue = null; + + Point point = c.getPoint(); + pair = Math.abs(point.getX() - point.getY()) % 2 == 0; + + this.setMaxWidth(LARGEUR); + this.setMaxHeight(HAUTEUR); + this.setWidth(LARGEUR); + this.setHeight(HAUTEUR); + this.setMinSize(LARGEUR, HAUTEUR); + + this.setOnMouseClicked(e -> terrainVue.getTerrainControleur().clicSouris(this)); + this.setOnMouseEntered(e -> survoler(true)); + this.setOnMouseExited(e -> survoler(false)); + + update(null, null); + } + + public void setPionVue(PionVue pionVue) { + this.pionVue = pionVue; + if (pionVue != null) { + this.getChildren().add(pionVue); + if (survol) + survoler(true); + } else { + this.getChildren().clear(); + } + } + + private void survoler(boolean enter) { + // selection pion alliés, puis cases dispos + Diaballik d = terrainVue.getTerrainControleur().diaballik; + + if (this.pionVue != null && terrainVue.getTerrainControleur().getJeu().pionAllie(pionVue.getPion())) { + this.pionVue.survoler(enter); + if (enter) + d.setCurseurSelection(d.getSceneJeu()); + else + d.setCurseurNormal(d.getSceneJeu()); + survol = false; + } else if (terrainVue.getTerrainControleur().caseEstMarquee(this)) { + survol = enter; + if (enter) + d.setCurseurSelection(d.getSceneJeu()); + else + d.setCurseurNormal(d.getSceneJeu()); + } else { + survol = false; + } + + updateStyleClass(); + } + + public PionVue getPionVue() { + return pionVue; + } + + private boolean isMarque() { + return marque; + } + + @Override + public void update(Observable o, Object arg) { + updateStyleClass(); + } + + private void updateStyleClass() { + this.getStyleClass().clear(); + + if (survol) + this.getStyleClass().add("couleurSurvol"); + else if (isMarque()) + this.getStyleClass().add("couleurMarquage"); + else if (pair) + this.getStyleClass().add("couleurCasePair"); + else + this.getStyleClass().add("couleurCaseImpair"); + } + + public Case getCase() { + return c; + } + + public void setMarque(boolean marque) { + if (marque != this.marque) { + this.marque = marque; + updateStyleClass(); + } + } +} \ No newline at end of file diff --git a/src/diaballik/view/Dialogs.java b/src/diaballik/vue/Dialogs.java similarity index 85% rename from src/diaballik/view/Dialogs.java rename to src/diaballik/vue/Dialogs.java index f3a43ad..27a3eec 100644 --- a/src/diaballik/view/Dialogs.java +++ b/src/diaballik/vue/Dialogs.java @@ -1,4 +1,4 @@ -package diaballik.view; +package diaballik.vue; import diaballik.Diaballik; import diaballik.model.ConfigurationPartie; @@ -24,7 +24,7 @@ import java.util.Optional; public class Dialogs { - public static boolean confirmByDialog(String message) { + public static boolean dialogConfirmation(String message) { Alert alert = new Alert(Alert.AlertType.CONFIRMATION); alert.setTitle("Confirmer l'action"); alert.setContentText(message); @@ -33,7 +33,7 @@ public static boolean confirmByDialog(String message) { return result.get() == ButtonType.OK; } - public static void showCredits() { + public static void montrerCredits() { int i = 0; Dialog credits = new Dialog<>(); @@ -84,19 +84,19 @@ public static void showCredits() { corps.setBottom(infos); credits.getDialogPane().setContent(corps); - credits.getDialogPane().getStylesheets().add(Diaballik.class.getResource(Diaballik.CSS_DIALOG_FILE).toExternalForm()); + credits.getDialogPane().getStylesheets().add(Diaballik.class.getResource(Diaballik.CSS_DIALOG).toExternalForm()); credits.getDialogPane().getButtonTypes().add(ButtonType.CANCEL); credits.getDialogPane().setPrefSize(400, 250); credits.showAndWait(); } - public static Optional showNewGameDialog() { + public static Optional montrerDialogNouvellePartie() { Dialogs d = new Dialogs(); - return d.getNewGameDialog(); + return d.getDialogNouvellePartie(); } - private Optional getNewGameDialog() { + private Optional getDialogNouvellePartie() { ObservableList iaDifficultes = FXCollections.observableArrayList( "Non", "Facile", @@ -149,14 +149,14 @@ private Optional getNewGameDialog() { content.add(iaJoueur2, 2, 2); config.getDialogPane().setContent(content); - config.getDialogPane().getStylesheets().add(Diaballik.class.getResource(Diaballik.CSS_DIALOG_FILE).toExternalForm()); + config.getDialogPane().getStylesheets().add(Diaballik.class.getResource(Diaballik.CSS_DIALOG).toExternalForm()); config.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL); config.getDialogPane().setPrefSize(400, 150); config.setResultConverter(b -> { if (b == ButtonType.OK) { - int ia1 = convertDifficulte(iaJoueur1.getValue()); - int ia2 = convertDifficulte(iaJoueur2.getValue()); + int ia1 = convertirDifficulte(iaJoueur1.getValue()); + int ia2 = convertirDifficulte(iaJoueur2.getValue()); return new ConfigurationPartie(nomJoueur1.getText(), nomJoueur2.getText(), ia1, ia2); } @@ -168,7 +168,7 @@ private Optional getNewGameDialog() { return config.showAndWait(); } - private static int convertDifficulte(String difficulte) { + private static int convertirDifficulte(String difficulte) { switch (difficulte) { case "Facile": return IA.DIFFICULTE_FACILE; @@ -181,13 +181,19 @@ private static int convertDifficulte(String difficulte) { } } - public static Optional showLoadName(String directory) { + public static Optional montrerDialogChoisirFichier(String directory) { Dialogs d = new Dialogs(); - return d.getLoadName(directory); + return d.getDialogChoisirFichier(directory); } - private Optional getLoadName(String directory) { - ObservableList obs = FXCollections.observableArrayList(getFilesInDir(directory)); + private Optional getDialogChoisirFichier(String directory) { + List fichiersDispo = getFichiersDansDossier(directory); + ObservableList obs; + + if (fichiersDispo != null) + obs = FXCollections.observableArrayList(fichiersDispo); + else + return null; Dialog dialog = new Dialog<>(); dialog.setTitle("Charger une partie"); @@ -210,15 +216,16 @@ private Optional getLoadName(String directory) { }); dialog.getDialogPane().setContent(content); - dialog.getDialogPane().getStylesheets().add(Diaballik.class.getResource(Diaballik.CSS_DIALOG_FILE).toExternalForm()); + dialog.getDialogPane().getStylesheets().add(Diaballik.class.getResource(Diaballik.CSS_DIALOG).toExternalForm()); dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL); dialog.getDialogPane().setPrefSize(400, 400); return dialog.showAndWait(); } - private List getFilesInDir(String directory) { + private List getFichiersDansDossier(String directory) { List results = new ArrayList<>(); + System.out.println("ff " + new File(directory).getAbsolutePath()); File[] files = new File(directory).listFiles((dir, name) -> name.endsWith(".txt")); if (files != null) { @@ -234,7 +241,7 @@ private List getFilesInDir(String directory) { return results; } - public static void showEndGame(Joueur gagnant, int victoireType) { + public static void montrerFinJeu(Joueur gagnant, int victoireType) { String victoireMessage = "Type de victoire: "; switch (victoireType) { case Jeu.VICTOIRE_NORMALE: diff --git a/src/diaballik/vue/PionVue.java b/src/diaballik/vue/PionVue.java new file mode 100644 index 0000000..0d7f3fa --- /dev/null +++ b/src/diaballik/vue/PionVue.java @@ -0,0 +1,138 @@ +package diaballik.vue; + +import diaballik.model.Joueur; +import diaballik.model.Pion; +import diaballik.model.Point; +import javafx.scene.paint.Color; +import javafx.scene.shape.Circle; + +import java.util.Observable; +import java.util.Observer; + +public class PionVue extends Circle implements Observer { + private final static double RAYON = CaseVue.HAUTEUR / 4; + private final static double RAYON_BALLE = RAYON * 1.5; + + private final TerrainVue terrainVue; + private final Pion pion; + + private CaseVue caseVue; + + private boolean actif = true; + private boolean selectionne = false; + private boolean marque = false; + private boolean survol = false; + + public PionVue(TerrainVue terrainVue, Pion pion) { + super(RAYON); + + this.pion = pion; + this.caseVue = terrainVue.getCaseSur(pion.getPosition().getPoint()); + this.terrainVue = terrainVue; + pion.addObserver(this); + + this.setStroke(Color.BLACK); + + this.update(); + } + + public void update() { + this.update(null, null); + } + + public void survoler(boolean enter) { + survol = enter; + updateStyleClass(); + } + + public void reinitialiserStatut() { + setMarque(false); + this.survol = false; + } + + @Override + public void update(Observable o, Object arg) { + Point p = this.pion.getPosition().getPoint(); + + this.caseVue.setPionVue(null); + this.caseVue = this.terrainVue.getCaseSur(p); + this.caseVue.setPionVue(this); + + this.updateStyleClass(); + this.setRadius(getRayon()); + } + + private void updateStyleClass() { + this.getStyleClass().clear(); + + if (!actif) { + this.getStyleClass().add("couleurAdversaire"); + } else if (survol) { + this.getStyleClass().add("couleurSurvol"); + } else if (isSelectionne()) { + this.getStyleClass().add("couleurSelection"); + } else if (isMarque()) { + this.getStyleClass().add("couleurMarquage"); + } else { + if (pion.getCouleur() == Joueur.JOUEUR_VERT) + this.getStyleClass().add("couleurJoueurVert"); + else + this.getStyleClass().add("couleurJoueurRouge"); + } + } + + public void desactiver() { + this.actif = false; + reinitialiserStatut(); + update(); + } + + public void activer() { + this.actif = true; + update(); + } + + // GETTERS objets + public Pion getPion() { + return pion; + } + + public CaseVue getCaseVue() { + return caseVue; + } + + private double getRayon() { + return (this.pion.aLaBalle() ? RAYON_BALLE : RAYON); + } + + // GETTERS booleens + public boolean isMarque() { + return marque; + } + + public boolean isSelectionne() { + return selectionne; + } + + // SETTERS + public void setActif(boolean actif) { + if (actif != this.actif) { + this.actif = actif; + updateStyleClass(); + } + } + + public void setSelectionne(boolean selectionne) { + if (selectionne != this.selectionne) { + this.selectionne = selectionne; + updateStyleClass(); + } + } + + public void setMarque(boolean marque) { + if (marque != this.marque) { + this.marque = marque; + updateStyleClass(); + } + } +} diff --git a/src/diaballik/vue/TerrainVue.java b/src/diaballik/vue/TerrainVue.java new file mode 100644 index 0000000..1a01783 --- /dev/null +++ b/src/diaballik/vue/TerrainVue.java @@ -0,0 +1,88 @@ +package diaballik.vue; + +import diaballik.Diaballik; +import diaballik.controleur.TerrainControleur; +import diaballik.model.Jeu; +import diaballik.model.Joueur; +import diaballik.model.Point; +import diaballik.model.Terrain; +import javafx.scene.layout.GridPane; + +import java.util.Observable; +import java.util.Observer; + +public class TerrainVue extends GridPane implements Observer { + private final TerrainControleur terrainControleur; + private final Terrain terrain; + + private final CaseVue[][] cases; // Représentation visuelle du terrain (l'UI) + private final PionVue[][] pions; + + public TerrainVue(TerrainControleur terrainControleur) { + super(); + + this.terrainControleur = terrainControleur; + this.terrain = terrainControleur.getJeu().getTerrain(); + terrainControleur.getJeu().addObserver(this); + this.setId("terrainView"); + + this.setMaxSize(CaseVue.LARGEUR * Terrain.LARGEUR, CaseVue.HAUTEUR * Terrain.HAUTEUR); + + this.cases = new CaseVue[Terrain.HAUTEUR][Terrain.LARGEUR]; + this.pions = new PionVue[Jeu.NOMBRE_JOUEURS][Joueur.NOMBRE_PIONS]; + + for (int i = 0; i < Terrain.HAUTEUR; i++) { + for (int j = 0; j < Terrain.LARGEUR; j++) { + CaseVue cv = new CaseVue(this, terrain.getCaseSur(new Point(j, i))); + + this.cases[i][j] = cv; + this.add(cv, j, i); + } + } + + PionVue pv; + for (int i = 0; i < Jeu.NOMBRE_JOUEURS; i++) { + for (int j = 0; j < Joueur.NOMBRE_PIONS; j++) { + pv = new PionVue(this, this.terrain.getPionDe(i, j)); + this.pions[i][j] = pv; + } + } + + update(null, null); + } + + public CaseVue getCaseSur(Point p) { + return cases[p.getY()][p.getX()]; + } + + public TerrainControleur getTerrainControleur() { + return terrainControleur; + } + + @Override + public void update(Observable o, Object arg) { + if (terrainControleur.getJeu().getJoueurActuel().getCouleur() == Joueur.JOUEUR_VERT) { + for (PionVue p : pions[Joueur.JOUEUR_ROUGE]) p.desactiver(); + for (PionVue p : pions[Joueur.JOUEUR_VERT]) p.activer(); + } else { + for (PionVue p : pions[Joueur.JOUEUR_ROUGE]) p.activer(); + for (PionVue p : pions[Joueur.JOUEUR_VERT]) p.desactiver(); + } + + int changed_type = (arg != null ? (int)arg : 0); + Diaballik d = getTerrainControleur().diaballik; + if (d.getSceneJeu() != null && changed_type == Jeu.CHANGEMENT_TOUR) { + switch (changed_type) { + case Jeu.CHANGEMENT_TOUR: + d.setCurseurNormal(d.getSceneJeu()); + terrainControleur.finSelection(); + break; + case Jeu.CHANGEMENT_GLOBAL: + terrainControleur.finSelection(); + break; + default: + break; + } + } + } +}