Utilisation des fonctions déjà existantes pour éviter la duplication de code

This commit is contained in:
Justine Yannis 2022-11-12 13:09:09 +01:00
parent 7cda7fdbdc
commit a4fb1ee2c6
7 changed files with 19 additions and 37 deletions

View File

@ -31,7 +31,7 @@ public class ModelEventListener implements GridChangedListener {
}
@Override
public void playerRemove(PlayerEvent e){
this.panel.removePlayer(e.getJ(), e.getGrille());
public void playerWin(PlayerEvent e){
this.panel.playerWin(e.getJ());
}
}

View File

@ -65,12 +65,12 @@ public abstract class AbstractGridInitiater {
}
}
protected void firePlayerChanged(int playerRemove, int[][] grille){
protected void firePlayerWin(int playerRemove){
PlayerEvent event=null;
for(GridChangedListener listener : getGridListeners()) {
if(event == null)
event= new PlayerEvent(playerRemove, grille);
listener.playerRemove(event);
event= new PlayerEvent(playerRemove);
listener.playerWin(event);
}
}

View File

@ -19,9 +19,9 @@ public interface GridChangedListener extends EventListener{
void statusChanged(StatusEvent e);
/**
* Méthode invoqué lors de la suppréssion d'un joueur
* Méthode invoqué lorsqu'un joueur gagne
* @param e evenement du joueur a supprimé
*/
void playerRemove(PlayerEvent e);
void playerWin(PlayerEvent e);
}

View File

@ -5,17 +5,11 @@ package fr.iutfbleau.projetAgile.Puissance4.Event;
*/
public class PlayerEvent {
private int J;
private int[][] grille;
public PlayerEvent(int j0, int[][] grille0){
public PlayerEvent(int j0){
this.J=j0;
this.grille=grille0;
}
public int getJ() {
return J;
}
public int[][] getGrille() {
return grille;
}
}

View File

@ -99,7 +99,7 @@ public class GrilleModel extends AbstractGridInitiater{
private void removePlayer(){
this.increaseScore();
this.removePlayerPion();
this.firePlayerChanged(this.playerTurn, this.grille);
this.firePlayerWin(this.playerTurn);
this.playersList.pollLast();
this.switchPlayer();
this.verifyNewWin();
@ -244,21 +244,22 @@ public class GrilleModel extends AbstractGridInitiater{
private void removePlayerPion(){
for(int i=0; i<this.grille.length; i++){
for(int t=0; t<this.grille[i].length; t++){
if(this.grille[i][t] == this.playerTurn ){
this.dropColumn(this.grille[i], t);
if(this.grille[i][t] == this.playerTurn){
this.dropColumn(i, t);
this.piecePlayed--;
}
}
}
}
private void dropColumn(int[] col, int ligne){
private void dropColumn(int col, int ligne){
for(int i=ligne; i>=0; i--){
if(i>0){
col[i]=col[i-1];
this.grille[col][i]=this.grille[col][i-1];
}else{
col[0]=Constants.EMPTY_PLAYER;
this.grille[col][0]=Constants.EMPTY_PLAYER;
}
this.fireGridChanged(col, i, this.grille[col][i]);
}
}

View File

@ -79,19 +79,7 @@ public class Grille extends JPanel{
}
}
this.repaint();
}
public void removePlayer(int Joueur, int[][] j){
for (int x = 0; x < this.column; x++) {
for (int y = 0; y < this.row; y++) {
this.grille[x][y].setPlayer(j[x][y]);
this.grille[x][y].setHover(false);
}
}
this.repaint();
}
}
/**
* Change la couleur de survol des pions selon le tour du joueur

View File

@ -124,6 +124,7 @@ public class Puissance4Panel extends JPanel{
gbc.insets = new Insets(10, 0, 0, 0);
this.add(this.reset, gbc);
reset.setBackground(new Color(255,64,64));
reset.setForeground(Color.WHITE);
@ -153,7 +154,6 @@ public class Puissance4Panel extends JPanel{
case Constants.PLAYER_ONE:
this.playerTurn.setText("Tour de : " + this.playerOneName);
break;
case Constants.PLAYER_TWO:
this.playerTurn.setText("Tour de : " + this.playerTwoName);
break;
@ -230,9 +230,8 @@ public class Puissance4Panel extends JPanel{
this.changeScore(playerOneScore, playerTwoScore, playerThreeScore);
}
public void removePlayer(int player, int[][] grille){
this.grille.removePlayer(player, grille);
JOptionPane.showMessageDialog(this, "Le joeur "+String.valueOf(player)+" a gagné");
public void playerWin(int player){
JOptionPane.showMessageDialog(this, "Le joueur "+String.valueOf(player)+" a gagné");
}
/**