Ajout commentaire et simplication de l'algo de survol des pions en sauvegardant le dernier pion survolé
This commit is contained in:
parent
1408f8e39e
commit
88d00f4f50
@ -57,7 +57,7 @@ build/$(PACKAGE_PATH)/Puissance4/Controller/ResetGame.class : src/$(PACKAGE_PATH
|
||||
build/$(PACKAGE_PATH)/Puissance4/Controller/GrilleMouseListener.class : src/$(PACKAGE_PATH)/Puissance4/Controller/GrilleMouseListener.java
|
||||
@javac $(@JAVAC_OPT) $<
|
||||
|
||||
build/$(PACKAGE_PATH)/Menu/Controller/ModelEventListener.class : src/$(PACKAGE_PATH)/Menu/Controller/ModelEventListener.java \
|
||||
build/$(PACKAGE_PATH)/Puissance4/Controller/ModelEventListener.class : src/$(PACKAGE_PATH)/Puissance4/Controller/ModelEventListener.java \
|
||||
build/$(PACKAGE_PATH)/Puissance4/Event/GridChangedListener.class \
|
||||
build/$(PACKAGE_PATH)/Puissance4/View/Grille.class \
|
||||
$(Event)
|
||||
@ -123,11 +123,7 @@ build/$(PACKAGE_PATH)/Menu/View/Menu.class : src/$(PACKAGE_PATH)/Menu/View/Menu.
|
||||
|
||||
|
||||
build/$(PACKAGE_PATH)/Main.class : src/$(PACKAGE_PATH)/Main.java \
|
||||
build/$(PACKAGE_PATH)/Menu/View/Menu.class \
|
||||
build/$(PACKAGE_PATH)/Puissance4/Controller/Puissance4Controller.class \
|
||||
build/$(PACKAGE_PATH)/Puissance4/View/Grille.class \
|
||||
build/$(PACKAGE_PATH)/Puissance4/Model/GrilleModel.class \
|
||||
build/$(PACKAGE_PATH)/Puissance4/LancementPartie.class
|
||||
build/$(PACKAGE_PATH)/Menu/View/Menu.class
|
||||
@echo "Classes compiled"
|
||||
@javac $(@JAVAC_OPT) $<
|
||||
|
||||
|
@ -19,6 +19,7 @@ public class ModelEventListener implements GridChangedListener {
|
||||
@Override
|
||||
public void playerChanged(StatusEvent e) {
|
||||
this.panel.changeLabel(e.getPlayerTurn());
|
||||
this.panel.changeHoverColor(e.getPlayerTurn());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -40,6 +40,7 @@ public class Puissance4Controller {
|
||||
|
||||
public void initPanel() {
|
||||
this.panel.init();
|
||||
this.panel.changeHoverColor(this.modele.getPlayerTurn());
|
||||
this.panel.changeLabel(this.modele.getPlayerTurn());
|
||||
}
|
||||
|
||||
@ -50,6 +51,7 @@ public class Puissance4Controller {
|
||||
}
|
||||
this.grille.reset();
|
||||
this.modele.reset();
|
||||
this.panel.changeHoverColor(this.modele.getPlayerTurn());
|
||||
this.panel.changeLabel(this.modele.getPlayerTurn());
|
||||
}
|
||||
|
||||
@ -63,11 +65,11 @@ public class Puissance4Controller {
|
||||
public void hoverGrille(int x) {
|
||||
if(this.modele.getGameStatus() == GameStatus.PLAYING) {
|
||||
if(x == -1) {
|
||||
this.grille.hover(-1, this.modele.getPlayerTurn()); //Clean
|
||||
this.grille.hover(-1); //Clean
|
||||
return;
|
||||
}
|
||||
int column = (x * this.modele.getColumn() / grille.getWidth());
|
||||
this.grille.hover(column, this.modele.getPlayerTurn());
|
||||
this.grille.hover(column);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4,20 +4,32 @@ import javax.swing.*;
|
||||
import fr.iutfbleau.projetAgile.Puissance4.Utils.Constants;
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* Classe qui représente uniquement la grille du puissance 4
|
||||
*/
|
||||
public class Grille extends JPanel{
|
||||
|
||||
private Pion grille[][];
|
||||
private int column;
|
||||
private int row;
|
||||
private Pion lastHoveredPion;
|
||||
|
||||
/**
|
||||
* Crée un grille vide
|
||||
*/
|
||||
public Grille() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise la grille avec le panneau donné en argument
|
||||
* @param tab le tableau contenant la position des pions de chaque joueur
|
||||
*/
|
||||
public void init(int[][] tab) {
|
||||
this.column = Constants.COLUMN_COUNT;
|
||||
this.row = Constants.ROW_COUNT;
|
||||
this.grille = new Pion[this.column][this.row];
|
||||
this.lastHoveredPion = null;
|
||||
this.setBackground(new Color(31,31,31));
|
||||
/*
|
||||
* On peut remplacer GridBagLayout par un GridLayout
|
||||
@ -45,13 +57,20 @@ public class Grille extends JPanel{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Change le propriétaire d'un pion et le redessine
|
||||
* @param column la colonne du pion a changer
|
||||
* @param row la ligne du pion a changer
|
||||
* @param player le joueur
|
||||
*/
|
||||
public void addPlayerPiece(int column, int row, int player) {
|
||||
this.grille[column][row].setPlayer(player);
|
||||
this.grille[column][row].repaint();
|
||||
}
|
||||
|
||||
//Fonction de test (pourra être modifié)
|
||||
/**
|
||||
* Réinitialise la grille et la rempli de pion "vide"
|
||||
*/
|
||||
public void reset() {
|
||||
for (int x = 0; x < this.column; x++) {
|
||||
for (int y = 0; y < this.row; y++) {
|
||||
@ -62,20 +81,53 @@ public class Grille extends JPanel{
|
||||
this.repaint();
|
||||
}
|
||||
|
||||
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 = this.row - 1; y >= 0; y--) {
|
||||
this.grille[x][y].setHover(false);
|
||||
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;
|
||||
/**
|
||||
* Change la couleur de survol des pions selon le tour du joueur
|
||||
* @param player le joueur qui joue
|
||||
*/
|
||||
public void changeHoverColor(int player) {
|
||||
Color c;
|
||||
switch(player) {
|
||||
case Constants.PLAYER_ONE :
|
||||
c = new Color(Constants.PLAYER_ONE_COLOR.getRed(), Constants.PLAYER_ONE_COLOR.getGreen(), Constants.PLAYER_ONE_COLOR.getBlue(), 100);
|
||||
break;
|
||||
case Constants.PLAYER_TWO :
|
||||
c = new Color(Constants.PLAYER_TWO_COLOR.getRed(), Constants.PLAYER_TWO_COLOR.getGreen(), Constants.PLAYER_TWO_COLOR.getBlue(), 100);
|
||||
break;
|
||||
default :
|
||||
c = Color.BLACK;
|
||||
}
|
||||
Pion.setHighlightColor(c);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sélectionne le pion qui doit être afficher comme survolé selon la colonne
|
||||
* @param column la colonne survolé
|
||||
*/
|
||||
public void hover(int column) {
|
||||
if(column == -1) {
|
||||
if(this.lastHoveredPion != null) {
|
||||
this.lastHoveredPion.setHover(false);
|
||||
this.lastHoveredPion.repaint();
|
||||
this.lastHoveredPion = null;
|
||||
}
|
||||
}
|
||||
else {
|
||||
for(int y = row - 1; y > 0; y--) {
|
||||
if(this.grille[column][y].getPlayer() == Constants.EMPTY_PLAYER) {
|
||||
if(this.grille[column][y] != this.lastHoveredPion) {
|
||||
if(this.lastHoveredPion != null) {
|
||||
this.lastHoveredPion.setHover(false);
|
||||
this.lastHoveredPion.repaint();
|
||||
}
|
||||
this.lastHoveredPion = this.grille[column][y];
|
||||
this.lastHoveredPion.setHover(true);
|
||||
this.lastHoveredPion.repaint();
|
||||
break;
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,9 @@ import java.awt.*;
|
||||
import javax.swing.*;
|
||||
import fr.iutfbleau.projetAgile.Puissance4.Utils.Constants;
|
||||
|
||||
|
||||
/**
|
||||
* Classe qui représente un pion
|
||||
*/
|
||||
public class Pion extends JComponent{
|
||||
|
||||
private static Color HIGHLIGHT_COLOR = null;
|
||||
@ -23,25 +25,41 @@ public class Pion extends JComponent{
|
||||
return new Dimension(120,120);
|
||||
}
|
||||
|
||||
/**
|
||||
* Crée un pion
|
||||
* @param player a qui appartient le pion
|
||||
*/
|
||||
public Pion(int player) {
|
||||
this.setMinimumSize(new Dimension(80,80));
|
||||
this.setMaximumSize(new Dimension(150,150));
|
||||
this.setPreferredSize(new Dimension(120,120));
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change le "propriétaire" du pion
|
||||
* @param player
|
||||
*/
|
||||
public void setPlayer(int player) {
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indique le pion est survolé
|
||||
* @param b un booléen
|
||||
*/
|
||||
public void setHover(boolean b) {
|
||||
this.hover = b;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retourne le "priopriétaire" du pion
|
||||
* @return le joueur propriétaire du pion
|
||||
*/
|
||||
public int getPlayer() {
|
||||
return this.player;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set la couleur de survol d'un pion
|
||||
* @param c
|
||||
*/
|
||||
public static void setHighlightColor(Color c) {
|
||||
Pion.HIGHLIGHT_COLOR = c;
|
||||
}
|
||||
@ -63,7 +81,7 @@ public class Pion extends JComponent{
|
||||
break;
|
||||
}
|
||||
if(this.hover) {
|
||||
g2.setColor(new Color(Pion.HIGHLIGHT_COLOR.getRed(),Pion.HIGHLIGHT_COLOR.getGreen(),Pion.HIGHLIGHT_COLOR.getBlue(), 100));
|
||||
g2.setColor(Pion.HIGHLIGHT_COLOR);
|
||||
g2.fillOval(Constants.PIECE_MARGIN, Constants.PIECE_MARGIN, this.getWidth() - 2 * Constants.PIECE_MARGIN, this.getHeight() - 2 * Constants.PIECE_MARGIN);
|
||||
}
|
||||
else {
|
||||
|
@ -8,6 +8,9 @@ import fr.iutfbleau.projetAgile.Puissance4.Utils.GameStatus;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
/**
|
||||
* Classe qui représente le panneau du puissance 4, avec la grille, le tour du joueur, les boutons, et le score.
|
||||
*/
|
||||
public class Puissance4Panel extends JPanel{
|
||||
|
||||
private JButton reset;
|
||||
@ -17,6 +20,10 @@ public class Puissance4Panel extends JPanel{
|
||||
private String playerOneName;
|
||||
private String playerTwoName;
|
||||
|
||||
/**
|
||||
* Crée un panneau avec la grille
|
||||
* @param grille la grille à afficher
|
||||
*/
|
||||
public Puissance4Panel(Grille grille) {
|
||||
super();
|
||||
this.reset = new JButton("RECOMMENCER");
|
||||
@ -119,6 +126,10 @@ public class Puissance4Panel extends JPanel{
|
||||
this.menu.addActionListener(l);
|
||||
}
|
||||
|
||||
public void changeHoverColor(int player) {
|
||||
this.grille.changeHoverColor(player);
|
||||
}
|
||||
|
||||
public void changeLabel(int joueur) {
|
||||
switch(joueur){
|
||||
case Constants.PLAYER_ONE:
|
||||
@ -140,7 +151,7 @@ public class Puissance4Panel extends JPanel{
|
||||
}
|
||||
|
||||
public void setGameStatus(GameStatus status, int playerTurn, int playerOneScore, int playerTwoScore) {
|
||||
this.grille.hover(-1, playerTurn);
|
||||
this.grille.hover(-1);
|
||||
switch(status){
|
||||
case WIN:
|
||||
this.label.setText("Victoire de : " + (playerTurn == Constants.PLAYER_ONE ? this.playerOneName : this.playerTwoName));
|
||||
|
Loading…
Reference in New Issue
Block a user