ajout fonction supprimer les pions

This commit is contained in:
martins 2022-11-10 11:35:05 +01:00
parent 41ec67a568
commit 669ba0f74f
10 changed files with 107 additions and 3 deletions

View File

@ -14,7 +14,8 @@ Utils = build/$(PACKAGE_PATH)/Puissance4/Utils/Constants.class \
Events = build/$(PACKAGE_PATH)/Puissance4/Event/GridChangedListener.class \
build/$(PACKAGE_PATH)/Puissance4/Event/GridEvent.class \
build/$(PACKAGE_PATH)/Puissance4/Event/StatusEvent.class
build/$(PACKAGE_PATH)/Puissance4/Event/StatusEvent.class \
build/$(PACKAGE_PATH)/Puissance4/Event/PlayerEvent.class
Model = build/$(PACKAGE_PATH)/Puissance4/Model/GrilleModel.class
@ -45,6 +46,9 @@ build/$(PACKAGE_PATH)/Puissance4/Event/AbstractGridInitiater.class : src/$(PACKA
$(Events)
@javac $(@JAVAC_OPT) $<
build/$(PACKAGE_PATH)/Puissance4/Event/PlayerEvent.class : src/$(PACKAGE_PATH)/Puissance4/Event/PlayerEvent.java
@javac $(@JAVAC_OPT) $<
#------- Controleur ------
build/$(PACKAGE_PATH)/Menu/Controleur/ObservateurAccueil.class : src/$(PACKAGE_PATH)/Menu/Controller/ObservateurAccueil.java

View File

@ -66,6 +66,7 @@ public class BoutonsMenu extends JComponent{
}else{
secondPinceau.drawImage(this.img, 0, 0, this.getWidth(), this.getHeight(), this);
secondPinceau.fillRoundRect(0, 0, this.getWidth(), this.getHeight(), this.getWidth()/100*9, this.getHeight()/100*29);
}
secondPinceau.setColor(new Color(255, 255, 255));
secondPinceau.setFont(new Font(Font.SANS_SERIF, Font.BOLD, (this.getWidth()/10)));

View File

@ -22,4 +22,9 @@ public class ModelEventListener implements GridChangedListener {
this.panel.changeHoverColor(e.getPlayerTurn());
this.panel.setGameStatus(e.getStatus(), e.getPlayerTurn(), e.getPlayerOneScore(), e.getPlayerTwoScore());
}
@Override
public void playerRemove(PlayerEvent e){
this.panel.RemovePlayer(e.getJ(), e.getGrille());
}
}

View File

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

View File

@ -17,5 +17,11 @@ public interface GridChangedListener extends EventListener{
* @param e Evenement contenant les informations sur le status du jeu
*/
void statusChanged(StatusEvent e);
/**
* Méthode invoqué lors de la suppréssion d'un joueur
* @param e evenement du joueur a supprimé
*/
void playerRemove(PlayerEvent e);
}

View File

@ -0,0 +1,21 @@
package fr.iutfbleau.projetAgile.Puissance4.Event;
/**
* evénement lors de la supprésion d'un joueur
*/
public class PlayerEvent {
private int J;
private int[][] grille;
public PlayerEvent(int j0, int[][] grille0){
this.J=j0;
this.grille=grille0;
}
public int getJ() {
return J;
}
public int[][] getGrille() {
return grille;
}
}

View File

@ -80,7 +80,7 @@ public class GrilleModel extends AbstractGridInitiater{
int diagonalRow = row;
//Vérification horizontal et vertical
if(verifyAlignedPiece(column, 0, 0, 1, playerColor) || verifyAlignedPiece(0, row, 1, 0, playerColor)) {
this.setPartyStatus(GameStatus.WIN);
this.setPartyStatus(GameStatus.FIRST);
return true;
}
@ -153,13 +153,43 @@ public class GrilleModel extends AbstractGridInitiater{
*/
public void setPartyStatus(GameStatus gameStatus) {
this.gameStatus = gameStatus;
if(gameStatus == GameStatus.WIN) {
if(gameStatus == GameStatus.WIN || gameStatus == GameStatus.FIRST) {
if(this.playerTurn == Constants.PLAYER_ONE)
playerOneScore++;
else
playerTwoScore++;
}
fireStatusChanged(this.gameStatus, this.playerTurn, this.playerOneScore, this.playerTwoScore);
if(this.gameStatus==GameStatus.FIRST){
this.removePlayerPion();
firePlayerChanged(this.playerTurn, this.grille);
this.gameStatus=GameStatus.PLAYING;
fireStatusChanged(this.gameStatus, this.playerTurn, this.playerOneScore, this.playerTwoScore);
}
}
/**
* retire les pions du joueur qui viens de gagner
*/
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);
this.piecePlayed--;
}
}
}
}
private void dropColumn(int[] col, int ligne){
for(int i=ligne; i>=0; i--){
if(i>0){
col[i]=col[i-1];
}else{
col[0]=Constants.EMPTY_PLAYER;
}
}
}
/**

View File

@ -4,6 +4,7 @@ package fr.iutfbleau.projetAgile.Puissance4.Utils;
* Enumeration du status d'une partie (En cours, gagné, égalité)
*/
public enum GameStatus {
FIRST,
/**
* Status d'une partie en cours
*/

View File

@ -81,6 +81,18 @@ 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
* @param player le joueur qui joue
@ -134,4 +146,8 @@ public class Grille extends JPanel{
}
}
}
public void removePlayerPion(int Joueur){
}
}

View File

@ -200,6 +200,10 @@ public class Puissance4Panel extends JPanel{
this.playerTurn.setText("Egalité");
break;
case FIRST:
JOptionPane.showMessageDialog(this, "Victoire de : " + (playerTurn == Constants.PLAYER_ONE ? this.playerOneName : this.playerTwoName), "Victoire", JOptionPane.DEFAULT_OPTION);
break;
default:
this.changeLabel(playerTurn);
break;
@ -207,6 +211,12 @@ public class Puissance4Panel extends JPanel{
this.changeScore(playerOneScore, playerTwoScore);
}
public void RemovePlayer(int Player, int[][] grille){
this.grille.RemovePlayer(Player, grille);
this.changeHoverColor(Player);
this.changeLabel(Player);
}
/**
* Permet de définir le nom du joueur 1
* @param playerOneName le nom du joueur