From 4845a40c55242699785d4e9e7711b68d3067b1db Mon Sep 17 00:00:00 2001 From: Justine Yannis Date: Thu, 20 Oct 2022 22:22:40 +0200 Subject: [PATCH] =?UTF-8?q?Am=C3=A9lioration=20de=20l'algorithme,=20plusie?= =?UTF-8?q?urs=20tests=20mais=20utilisaiton=20CPU=20=C3=A9lev=C3=A9=20?= =?UTF-8?q?=C3=A0=20cause=20du=20MouseMotionListener=20et=20pas=20l'algo?= =?UTF-8?q?=20en=20lui=20m=C3=AAme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fr/iutfbleau/projetAgile/View/Grille.java | 23 +++++++++---------- .../fr/iutfbleau/projetAgile/View/Pion.java | 17 +++++--------- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/View/Grille.java b/projetAgile/src/fr/iutfbleau/projetAgile/View/Grille.java index 17cf0e1..cc4218d 100644 --- a/projetAgile/src/fr/iutfbleau/projetAgile/View/Grille.java +++ b/projetAgile/src/fr/iutfbleau/projetAgile/View/Grille.java @@ -63,21 +63,20 @@ public class Grille extends JPanel{ } public void hover(int column, int player) { + Pion.setHighlightColor(player == Constants.PLAYER_ONE ? Constants.PLAYER_ONE_COLOR : Constants.PLAYER_TWO_COLOR ); + boolean found = false; for(int x = 0; x < this.column; x++) { - for(int y = 0; y < this.row; y++) { + for(int y = this.row - 1; y >= 0; y--) { this.grille[x][y].setHover(false); - this.grille[x][y].setPturn(player); + this.grille[x][y].repaint(); //On utilise un peu moins de CPU à redessiner uniquement chaque pion + if(!found && x == column && this.grille[column][y].getPlayer() == Constants.EMPTY_PLAYER) { + this.grille[column][y].setHover(true); + this.grille[x][y].repaint(); //On utilise un peu moins de CPU à redessiner uniquement chaque pion + found = true; + } + if(this.grille[x][y].getPlayer() == Constants.EMPTY_PLAYER) //Si on est sur un pion vide on passe à la colonne suivante (économise des calculs lorsque grille quasiment vide) + break; } } - if(column != -1) { - for (int row = this.row - 1; row >= 0; row--) { - if (this.grille[column][row].getPlayer() == Constants.EMPTY_PLAYER) { - this.grille[column][row].setHover(true); - this.grille[column][row].setPturn(player); - break; - } - } - } - this.repaint(); } } \ No newline at end of file diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/View/Pion.java b/projetAgile/src/fr/iutfbleau/projetAgile/View/Pion.java index a28a59c..b563418 100644 --- a/projetAgile/src/fr/iutfbleau/projetAgile/View/Pion.java +++ b/projetAgile/src/fr/iutfbleau/projetAgile/View/Pion.java @@ -6,9 +6,9 @@ import fr.iutfbleau.projetAgile.Utils.Constants; public class Pion extends JComponent{ + private static Color HIGHLIGHT_COLOR = null; private int player; private boolean hover = false; - private int pturn; public static Dimension getPionMinimumSize() { return new Dimension(80,80); @@ -27,7 +27,6 @@ public class Pion extends JComponent{ this.setMaximumSize(new Dimension(150,150)); this.setPreferredSize(new Dimension(120,120)); this.player = player; - this.pturn=0; } public void setPlayer(int player) { @@ -37,14 +36,15 @@ public class Pion extends JComponent{ public void setHover(boolean b) { this.hover = b; } - public void setPturn(int n){ - this.pturn=n; - } public int getPlayer() { return this.player; } + public static void setHighlightColor(Color c) { + Pion.HIGHLIGHT_COLOR = c; + } + @Override protected void paintComponent(Graphics g) { Graphics g2 = g.create(); @@ -61,12 +61,7 @@ public class Pion extends JComponent{ break; } if(this.hover) { - if(this.pturn==Constants.PLAYER_ONE){ - g2.setColor(Color.RED); - } - if(this.pturn==Constants.PLAYER_TWO){ - g2.setColor(Color.YELLOW); - } + g2.setColor(Pion.HIGHLIGHT_COLOR); g2.fillRect(0, 0, this.getWidth(), this.getHeight()); } g2.setColor(c);