From 521be6a04a9842dca7221ba8727444bb49c0e341 Mon Sep 17 00:00:00 2001 From: Justine Yannis Date: Thu, 13 Oct 2022 13:40:53 +0200 Subject: [PATCH] =?UTF-8?q?D=C3=A9but=20du=20mod=C3=A8le=20et=20du=20liste?= =?UTF-8?q?ner?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- projetAgile/Makefile | 28 +++++++-- .../Controlers/GrilleMouseListener.java | 25 ++++++++ ...souris.java => ObservateurMenuSouris.java} | 6 +- .../projetAgile/Model/GrilleModel.java | 45 ++++++++++++++ .../Utils/{Utils.java => Constants.java} | 3 +- .../{boutons_menu.java => BoutonsMenu.java} | 5 +- .../fr/iutfbleau/projetAgile/View/Grille.java | 20 +++---- .../projetAgile/View/{menu.java => Menu.java} | 58 +++++++++---------- .../fr/iutfbleau/projetAgile/View/Pion.java | 14 ++--- .../projetAgile/View/Puissance4Panel.java | 4 +- .../projetAgile/View/TestGrille.java | 13 ++++- 11 files changed, 160 insertions(+), 61 deletions(-) create mode 100644 projetAgile/src/fr/iutfbleau/projetAgile/Controlers/GrilleMouseListener.java rename projetAgile/src/fr/iutfbleau/projetAgile/Controlers/{obeservateur_menu_souris.java => ObservateurMenuSouris.java} (84%) create mode 100644 projetAgile/src/fr/iutfbleau/projetAgile/Model/GrilleModel.java rename projetAgile/src/fr/iutfbleau/projetAgile/Utils/{Utils.java => Constants.java} (86%) rename projetAgile/src/fr/iutfbleau/projetAgile/View/{boutons_menu.java => BoutonsMenu.java} (92%) rename projetAgile/src/fr/iutfbleau/projetAgile/View/{menu.java => Menu.java} (52%) diff --git a/projetAgile/Makefile b/projetAgile/Makefile index 8afd933..9ab04c5 100644 --- a/projetAgile/Makefile +++ b/projetAgile/Makefile @@ -8,25 +8,41 @@ JAR_OPTION = cvfe projetAgile.jar $(PACKAGE).main.Main -C build fr -C res View : build/$(PACKAGE_PATH)/View/Pion.class \ build/$(PACKAGE_PATH)/View/Grille.class -Utils : build/$(PACKAGE_PATH)/Utils/Utils.class +Utils : build/$(PACKAGE_PATH)/Utils/Constants.class -build/$(PACKAGE_PATH)/Utils/Utils.class : src/$(PACKAGE_PATH)/Utils/Utils.java - javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/Utils/Utils.java +build/$(PACKAGE_PATH)/Controlers/GrilleMouseListener.class : src/$(PACKAGE_PATH)/Controlers/GrilleMouseListener.java \ + build/$(PACKAGE_PATH)/View/Grille.class \ + build/$(PACKAGE_PATH)/Model/GrilleModel.class + javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/Controlers/GrilleMouseListener.java + +build/$(PACKAGE_PATH)/Model/GrilleModel.class : src/$(PACKAGE_PATH)/Model/GrilleModel.java + javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/Model/GrilleModel.java + +build/$(PACKAGE_PATH)/Utils/Constants.class : src/$(PACKAGE_PATH)/Utils/Constants.java + javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/Utils/Constants.java build/$(PACKAGE_PATH)/View/Pion.class : src/$(PACKAGE_PATH)/View/Pion.java \ - build/$(PACKAGE_PATH)/Utils/Utils.class + build/$(PACKAGE_PATH)/Utils/Constants.class javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/View/Pion.java build/$(PACKAGE_PATH)/View/Grille.class : src/$(PACKAGE_PATH)/View/Grille.java \ build/$(PACKAGE_PATH)/View/Pion.class \ - build/$(PACKAGE_PATH)/Utils/Utils.class + build/$(PACKAGE_PATH)/Utils/Constants.class javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/View/Grille.java -build/$(PACKAGE_PATH)/View/TestGrille.class : src/$(PACKAGE_PATH)/View/TestGrille.java \ +build/$(PACKAGE_PATH)/View/Puissance4Panel.class : src/$(PACKAGE_PATH)/View/Puissance4Panel.java \ build/$(PACKAGE_PATH)/View/Pion.class \ build/$(PACKAGE_PATH)/View/Grille.class + javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/View/Puissance4Panel.java + +build/$(PACKAGE_PATH)/View/TestGrille.class : src/$(PACKAGE_PATH)/View/TestGrille.java \ + build/$(PACKAGE_PATH)/View/Puissance4Panel.class \ + build/$(PACKAGE_PATH)/Model/GrilleModel.class \ + build/$(PACKAGE_PATH)/Controlers/GrilleMouseListener.class javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/View/TestGrille.java + + testGrille : build/$(PACKAGE_PATH)/View/TestGrille.class java -cp build $(PACKAGE).View.TestGrille diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/Controlers/GrilleMouseListener.java b/projetAgile/src/fr/iutfbleau/projetAgile/Controlers/GrilleMouseListener.java new file mode 100644 index 0000000..d766d14 --- /dev/null +++ b/projetAgile/src/fr/iutfbleau/projetAgile/Controlers/GrilleMouseListener.java @@ -0,0 +1,25 @@ +package fr.iutfbleau.projetAgile.Controlers; + +import java.awt.event.MouseEvent; +import javax.swing.event.MouseInputAdapter; +import fr.iutfbleau.projetAgile.Model.GrilleModel; +import fr.iutfbleau.projetAgile.View.Grille; +import fr.iutfbleau.projetAgile.Utils.Constants; + +public class GrilleMouseListener extends MouseInputAdapter{ + + private Grille grille; + private GrilleModel modele; + + public GrilleMouseListener(Grille grille, GrilleModel modele) { + this.grille = grille; + this.modele = modele; + this.modele.addObserver(grille); + } + + @Override + public void mouseClicked(MouseEvent e) { + int column = (e.getX() * Constants.COLUMN_COUNT / grille.getWidth()); + this.modele.addPawn(column); + } +} diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/Controlers/obeservateur_menu_souris.java b/projetAgile/src/fr/iutfbleau/projetAgile/Controlers/ObservateurMenuSouris.java similarity index 84% rename from projetAgile/src/fr/iutfbleau/projetAgile/Controlers/obeservateur_menu_souris.java rename to projetAgile/src/fr/iutfbleau/projetAgile/Controlers/ObservateurMenuSouris.java index 63ab4f3..70bd05a 100644 --- a/projetAgile/src/fr/iutfbleau/projetAgile/Controlers/obeservateur_menu_souris.java +++ b/projetAgile/src/fr/iutfbleau/projetAgile/Controlers/ObservateurMenuSouris.java @@ -3,10 +3,10 @@ package fr.iutfbleau.projetAgile.Controlers; import java.awt.event.MouseListener; import java.awt.event.*; import javax.swing.JFrame; -public class obeservateur_menu_souris implements MouseListener{ - private boutons_menu bout; +public class ObservateurMenuSouris implements MouseListener{ + private BoutonsMenu bout; private JFrame fenetre; - public obeservateur_menu_souris(boutons_menu boutons, JFrame fenetre0){ + public ObservateurMenuSouris(BoutonsMenu boutons, JFrame fenetre0){ this.bout=boutons; this.fenetre=fenetre0; } diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/Model/GrilleModel.java b/projetAgile/src/fr/iutfbleau/projetAgile/Model/GrilleModel.java new file mode 100644 index 0000000..59a778f --- /dev/null +++ b/projetAgile/src/fr/iutfbleau/projetAgile/Model/GrilleModel.java @@ -0,0 +1,45 @@ +package fr.iutfbleau.projetAgile.Model; +import java.util.Observable; +import fr.iutfbleau.projetAgile.Utils.Constants; + +public class GrilleModel extends Observable { + private int[][] grille; + private boolean partyEnd; + private int playerTurn; + private int column, row; + + public GrilleModel() { + this.init(); + } + + public void init() { + this.column = Constants.COLUMN_COUNT; + this.row = Constants.ROW_COUNT; + this.grille = new int[this.column][this.row]; + for(int i = 0; i < this.column; i++) { + for(int j = 0; j < this.row; j++) { + this.grille[i][j] = Constants.EMPTY_PLAYER; + } + } + } + + + public boolean addPawn(int column) { + for (int row = this.row - 1; row >= 0; row--) { + if (grille[column][row] == Constants.EMPTY_PLAYER) { + int[] args = {column, row, this.playerTurn}; + grille[column][row] = this.playerTurn; + this.setChanged(); + this.notifyObservers(args); + this.switchPlayer(); + return true; + } + } + return false; + } + + private void switchPlayer() { + this.playerTurn = (this.playerTurn + 1) % 2; + } + +} diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/Utils/Utils.java b/projetAgile/src/fr/iutfbleau/projetAgile/Utils/Constants.java similarity index 86% rename from projetAgile/src/fr/iutfbleau/projetAgile/Utils/Utils.java rename to projetAgile/src/fr/iutfbleau/projetAgile/Utils/Constants.java index 4d659bf..526f9df 100644 --- a/projetAgile/src/fr/iutfbleau/projetAgile/Utils/Utils.java +++ b/projetAgile/src/fr/iutfbleau/projetAgile/Utils/Constants.java @@ -2,10 +2,11 @@ package fr.iutfbleau.projetAgile.Utils; import java.awt.Color; -public class Utils { +public class Constants { public final static Color PLAYER_ONE_COLOR = Color.RED; public final static Color PLAYER_TWO_COLOR = Color.YELLOW; public final static Color EMPTY_COLOR = new Color(42,42,42); + public final static int EMPTY_PLAYER = -1; public final static int PLAYER_ONE = 0; public final static int PLAYER_TWO = 1; public static int PAWN_MARGIN = 10; diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/View/boutons_menu.java b/projetAgile/src/fr/iutfbleau/projetAgile/View/BoutonsMenu.java similarity index 92% rename from projetAgile/src/fr/iutfbleau/projetAgile/View/boutons_menu.java rename to projetAgile/src/fr/iutfbleau/projetAgile/View/BoutonsMenu.java index 7a83dc9..29eb4a0 100644 --- a/projetAgile/src/fr/iutfbleau/projetAgile/View/boutons_menu.java +++ b/projetAgile/src/fr/iutfbleau/projetAgile/View/BoutonsMenu.java @@ -1,14 +1,13 @@ package fr.iutfbleau.projetAgile.View; -import java.lang.*; import javax.swing.JComponent; import java.awt.*; -public class boutons_menu extends JComponent{ +public class BoutonsMenu extends JComponent{ private String path; private boolean survol; private Image img; - public boutons_menu(String path0){ + public BoutonsMenu(String path0){ super(); this.path=path0; ClassLoader cl = Thread.currentThread().getContextClassLoader(); diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/View/Grille.java b/projetAgile/src/fr/iutfbleau/projetAgile/View/Grille.java index 64e0061..65e8704 100644 --- a/projetAgile/src/fr/iutfbleau/projetAgile/View/Grille.java +++ b/projetAgile/src/fr/iutfbleau/projetAgile/View/Grille.java @@ -1,11 +1,10 @@ package fr.iutfbleau.projetAgile.View; -import javax.rmi.CORBA.Util; import javax.swing.*; import java.awt.*; import java.util.Observable; import java.util.Observer; -import fr.iutfbleau.projetAgile.Utils.Utils; +import fr.iutfbleau.projetAgile.Utils.Constants; public class Grille extends JPanel implements Observer{ @@ -28,9 +27,9 @@ public class Grille extends JPanel implements Observer{ GridBagLayout gbl = new GridBagLayout(); this.setLayout(gbl); GridBagConstraints gbc = new GridBagConstraints(); - this.grille = new Pion[Utils.COLUMN_COUNT][Utils.ROW_COUNT]; - for (int x = 0; x < Utils.COLUMN_COUNT; x++) { - for (int y = 0; y < Utils.ROW_COUNT; y++) { + this.grille = new Pion[Constants.COLUMN_COUNT][Constants.ROW_COUNT]; + for (int x = 0; x < Constants.COLUMN_COUNT; x++) { + for (int y = 0; y < Constants.ROW_COUNT; y++) { Pion pion = new Pion(-1); gbc.gridx = x; gbc.gridy = y; @@ -48,10 +47,10 @@ public class Grille extends JPanel implements Observer{ } } - int minimumWidth = Utils.COLUMN_COUNT * Pion.getPionMinimumSize().width; - int minimumHeight = Utils.ROW_COUNT * Pion.getPionMinimumSize().height; - int preferredWidth = Utils.COLUMN_COUNT * Pion.getPionPreferredSize().width; - int preferredHeight = Utils.ROW_COUNT * Pion.getPionPreferredSize().height; + int minimumWidth = Constants.COLUMN_COUNT * Pion.getPionMinimumSize().width; + int minimumHeight = Constants.ROW_COUNT * Pion.getPionMinimumSize().height; + int preferredWidth = Constants.COLUMN_COUNT * Pion.getPionPreferredSize().width; + int preferredHeight = Constants.ROW_COUNT * Pion.getPionPreferredSize().height; this.setMinimumSize(new Dimension(minimumWidth, minimumHeight)); this.setPreferredSize(new Dimension(preferredWidth, preferredHeight)); @@ -61,11 +60,12 @@ public class Grille extends JPanel implements Observer{ public void update(Observable o, Object arg) { int[] param = (int[]) arg; this.addPlayerPawn(param[0], param[1], param[2]); + System.out.println("Appuie sur la colonne : " + param[0]); } protected void addPlayerPawn(int column, int row, int player) { - Color c = player == Utils.PLAYER_ONE ? Utils.PLAYER_ONE_COLOR : Utils.PLAYER_TWO_COLOR; + Color c = player == Constants.PLAYER_ONE ? Constants.PLAYER_ONE_COLOR : Constants.PLAYER_TWO_COLOR; this.grille[column][row] = new Pion(player); } diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/View/menu.java b/projetAgile/src/fr/iutfbleau/projetAgile/View/Menu.java similarity index 52% rename from projetAgile/src/fr/iutfbleau/projetAgile/View/menu.java rename to projetAgile/src/fr/iutfbleau/projetAgile/View/Menu.java index 4ca2820..5318be7 100644 --- a/projetAgile/src/fr/iutfbleau/projetAgile/View/menu.java +++ b/projetAgile/src/fr/iutfbleau/projetAgile/View/Menu.java @@ -5,9 +5,9 @@ import javax.swing.JFrame; import javax.swing.JLabel; import fr.iutfbleau.projetAgile.Controlers.*; -public class menu extends JFrame{ +public class Menu extends JFrame{ - public menu(){ + public Menu(){ super(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocation(0,0); @@ -34,41 +34,41 @@ public class menu extends JFrame{ titre.setPreferredSize(new Dimension(400,200)); this.add(titre, gbc); - boutons_menu puissance_4=new boutons_menu("puissance4.png"); - boutons_menu echecs=new boutons_menu("echecs.png"); - boutons_menu dames=new boutons_menu("dames.png"); + BoutonsMenu puissance_4=new BoutonsMenu("puissance4.png"); + BoutonsMenu echecs=new BoutonsMenu("echecs.png"); + BoutonsMenu dames=new BoutonsMenu("dames.png"); - boutons_menu demineur=new boutons_menu("demineur.png"); - boutons_menu taquin=new boutons_menu("taquin.png"); - boutons_menu morpion=new boutons_menu("morpion.png"); + BoutonsMenu demineur=new BoutonsMenu("demineur.png"); + BoutonsMenu taquin=new BoutonsMenu("taquin.png"); + BoutonsMenu morpion=new BoutonsMenu("morpion.png"); - boutons_menu CoD=new boutons_menu("CoD.png"); - boutons_menu minecraft=new boutons_menu("minecraft.png"); - boutons_menu puzzle=new boutons_menu("puzzle.png"); + BoutonsMenu CoD=new BoutonsMenu("CoD.png"); + BoutonsMenu minecraft=new BoutonsMenu("minecraft.png"); + BoutonsMenu puzzle=new BoutonsMenu("puzzle.png"); - puissance_4.addMouseListener(new obeservateur_menu_souris(puissance_4, this)); - echecs.addMouseListener(new obeservateur_menu_souris(echecs, this)); - dames.addMouseListener(new obeservateur_menu_souris(dames, this)); + puissance_4.addMouseListener(new ObservateurMenuSouris(puissance_4, this)); + echecs.addMouseListener(new ObservateurMenuSouris(echecs, this)); + dames.addMouseListener(new ObservateurMenuSouris(dames, this)); - demineur.addMouseListener(new obeservateur_menu_souris(demineur, this)); - taquin.addMouseListener(new obeservateur_menu_souris(taquin, this)); - morpion.addMouseListener(new obeservateur_menu_souris(morpion, this)); + demineur.addMouseListener(new ObservateurMenuSouris(demineur, this)); + taquin.addMouseListener(new ObservateurMenuSouris(taquin, this)); + morpion.addMouseListener(new ObservateurMenuSouris(morpion, this)); - CoD.addMouseListener(new obeservateur_menu_souris(CoD, this)); - minecraft.addMouseListener(new obeservateur_menu_souris(minecraft, this)); - puzzle.addMouseListener(new obeservateur_menu_souris(puzzle, this)); + CoD.addMouseListener(new ObservateurMenuSouris(CoD, this)); + minecraft.addMouseListener(new ObservateurMenuSouris(minecraft, this)); + puzzle.addMouseListener(new ObservateurMenuSouris(puzzle, this)); - this.add(puissance_4, menu.GridBagConstraintsRetour(0, 1)); - this.add(echecs, menu.GridBagConstraintsRetour(1, 1)); - this.add(dames, menu.GridBagConstraintsRetour(2, 1)); + this.add(puissance_4, Menu.GridBagConstraintsRetour(0, 1)); + this.add(echecs, Menu.GridBagConstraintsRetour(1, 1)); + this.add(dames, Menu.GridBagConstraintsRetour(2, 1)); - this.add(demineur, menu.GridBagConstraintsRetour(0, 2)); - this.add(taquin, menu.GridBagConstraintsRetour(1, 2)); - this.add(morpion, menu.GridBagConstraintsRetour(2, 2)); + this.add(demineur, Menu.GridBagConstraintsRetour(0, 2)); + this.add(taquin, Menu.GridBagConstraintsRetour(1, 2)); + this.add(morpion, Menu.GridBagConstraintsRetour(2, 2)); - this.add(CoD, menu.GridBagConstraintsRetour(0, 3)); - this.add(minecraft, menu.GridBagConstraintsRetour(1, 3)); - this.add(puzzle, menu.GridBagConstraintsRetour(2, 3)); + this.add(CoD, Menu.GridBagConstraintsRetour(0, 3)); + this.add(minecraft, Menu.GridBagConstraintsRetour(1, 3)); + this.add(puzzle, Menu.GridBagConstraintsRetour(2, 3)); this.setVisible(true); } public static GridBagConstraints GridBagConstraintsRetour(int collonne, int ligne){ diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/View/Pion.java b/projetAgile/src/fr/iutfbleau/projetAgile/View/Pion.java index 283028d..d8ee795 100644 --- a/projetAgile/src/fr/iutfbleau/projetAgile/View/Pion.java +++ b/projetAgile/src/fr/iutfbleau/projetAgile/View/Pion.java @@ -2,7 +2,7 @@ package fr.iutfbleau.projetAgile.View; import java.awt.*; import javax.swing.*; -import fr.iutfbleau.projetAgile.Utils.Utils; +import fr.iutfbleau.projetAgile.Utils.Constants; public class Pion extends JComponent{ @@ -32,17 +32,17 @@ public class Pion extends JComponent{ Graphics g2 = g.create(); Color c; switch(this.player) { - case Utils.PLAYER_ONE : - c = Utils.PLAYER_ONE_COLOR; + case Constants.PLAYER_ONE : + c = Constants.PLAYER_ONE_COLOR; break; - case Utils.PLAYER_TWO : - c = Utils.PLAYER_TWO_COLOR; + case Constants.PLAYER_TWO : + c = Constants.PLAYER_TWO_COLOR; break; default : - c = Utils.EMPTY_COLOR; + c = Constants.EMPTY_COLOR; break; } g2.setColor(c); - g2.fillOval(Utils.PAWN_MARGIN, Utils.PAWN_MARGIN, this.getWidth() - 2 * Utils.PAWN_MARGIN, this.getHeight() - 2 * Utils.PAWN_MARGIN); + g2.fillOval(Constants.PAWN_MARGIN, Constants.PAWN_MARGIN, this.getWidth() - 2 * Constants.PAWN_MARGIN, this.getHeight() - 2 * Constants.PAWN_MARGIN); } } \ No newline at end of file diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/View/Puissance4Panel.java b/projetAgile/src/fr/iutfbleau/projetAgile/View/Puissance4Panel.java index 22c9d55..3f4b8ff 100644 --- a/projetAgile/src/fr/iutfbleau/projetAgile/View/Puissance4Panel.java +++ b/projetAgile/src/fr/iutfbleau/projetAgile/View/Puissance4Panel.java @@ -1,6 +1,5 @@ package fr.iutfbleau.projetAgile.View; -import java.util.Observable; import javax.swing.*; import java.awt.*; @@ -78,4 +77,7 @@ public class Puissance4Panel extends JPanel{ this.add(panneauBas, gbc); } + public Grille getGrille() { + return grille; + } } diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/View/TestGrille.java b/projetAgile/src/fr/iutfbleau/projetAgile/View/TestGrille.java index 32ab727..31dab12 100644 --- a/projetAgile/src/fr/iutfbleau/projetAgile/View/TestGrille.java +++ b/projetAgile/src/fr/iutfbleau/projetAgile/View/TestGrille.java @@ -2,13 +2,24 @@ package fr.iutfbleau.projetAgile.View; import javax.swing.*; import java.awt.*; +import fr.iutfbleau.projetAgile.Controlers.GrilleMouseListener; +import fr.iutfbleau.projetAgile.Model.GrilleModel; + public class TestGrille extends JFrame{ public TestGrille() { super("Puissance 4"); + + Puissance4Panel p = new Puissance4Panel(); + Grille g = p.getGrille(); + GrilleModel gm = new GrilleModel(); + GrilleMouseListener listener = new GrilleMouseListener(g, gm); + g.addMouseListener(listener); + this.add(p); this.setLocation(200, 200); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - this.add(new Puissance4Panel()); + this.setSize(new Dimension(1280,720)); + this.setVisible(true); } public static void main(String[] args) {