Vérification de victoire faite par le controlleur

This commit is contained in:
Justine Yannis 2022-10-14 10:37:50 +02:00
parent 3c39feaacc
commit a52b67c704
7 changed files with 133 additions and 41 deletions

View File

@ -7,7 +7,9 @@ JAR_OPTION = cvfe projetAgile.jar $(PACKAGE).main.Main -C build fr -C res
#------- Dossiers ------
View : build/$(PACKAGE_PATH)/View/Pion.class \
build/$(PACKAGE_PATH)/View/Grille.class
build/$(PACKAGE_PATH)/View/Grille.class \
build/$(PACKAGE_PATH)/View/Puissance4Panel.class \
build/$(PACKAGE_PATH)/View/TestGrille.class
Utils : build/$(PACKAGE_PATH)/Utils/Constants.class
@ -17,48 +19,50 @@ Events : build/$(PACKAGE_PATH)/Controller/EventListener/GridChangedListener.clas
Model : build/$(PACKAGE_PATH)/Model/GrilleModel.class
Controller : build/$(PACKAGE_PATH)/Controller/GrilleMouseListener.class
#------- Events ------
build/$(PACKAGE_PATH)/Controller/EventListener/PlayerEvent.class : src/$(PACKAGE_PATH)/Controller/EventListener/PlayerEvent.java
javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/Controller/EventListener/PlayerEvent.java
javac $(JAVAC_OPT) $<
build/$(PACKAGE_PATH)/Controller/EventListener/GridEvent.class : src/$(PACKAGE_PATH)/Controller/EventListener/GridEvent.java
javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/Controller/EventListener/GridEvent.java
javac $(JAVAC_OPT) $<
build/$(PACKAGE_PATH)/Controller/EventListener/GridChangedListener.class : src/$(PACKAGE_PATH)/Controller/EventListener/GridChangedListener.java \
build/$(PACKAGE_PATH)/Controller/EventListener/GridEvent.class \
build/$(PACKAGE_PATH)/Controller/EventListener/PlayerEvent.class
javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/Controller/EventListener/GridChangedListener.java
javac $(JAVAC_OPT) $<
build/$(PACKAGE_PATH)/Controller/EventListener/AbstractGridInitiater.class : src/$(PACKAGE_PATH)/Controller/EventListener/AbstractGridInitiater.java \
build/$(PACKAGE_PATH)/Controller/EventListener/GridChangedListener.class \
build/$(PACKAGE_PATH)/Controller/EventListener/GridEvent.class \
build/$(PACKAGE_PATH)/Controller/EventListener/PlayerEvent.class
javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/Controller/EventListener/AbstractGridInitiater.java
javac $(JAVAC_OPT) $<
#------- Controleur ------
build/$(PACKAGE_PATH)/Controller/GrilleMouseListener.class : src/$(PACKAGE_PATH)/Controller/GrilleMouseListener.java \
build/$(PACKAGE_PATH)/View/Grille.class \
build/$(PACKAGE_PATH)/Model/GrilleModel.class
javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/Controller/GrilleMouseListener.java
javac $(JAVAC_OPT) $<
#------- Modele ------
build/$(PACKAGE_PATH)/Model/GrilleModel.class : src/$(PACKAGE_PATH)/Model/GrilleModel.java \
build/$(PACKAGE_PATH)/Controller/EventListener/AbstractGridInitiater.class
javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/Model/GrilleModel.java
javac $(JAVAC_OPT) $<
#------- Utils ------
build/$(PACKAGE_PATH)/Utils/Constants.class : src/$(PACKAGE_PATH)/Utils/Constants.java
javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/Utils/Constants.java
javac $(JAVAC_OPT) $<
#------- Vue ------
build/$(PACKAGE_PATH)/View/Pion.class : src/$(PACKAGE_PATH)/View/Pion.java \
build/$(PACKAGE_PATH)/Utils/Constants.class
javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/View/Pion.java
javac $(JAVAC_OPT) $<
build/$(PACKAGE_PATH)/View/Grille.class : src/$(PACKAGE_PATH)/View/Grille.java \
build/$(PACKAGE_PATH)/View/Pion.class \
@ -66,22 +70,22 @@ build/$(PACKAGE_PATH)/View/Grille.class : src/$(PACKAGE_PATH)/View/Grille.java \
build/$(PACKAGE_PATH)/Controller/EventListener/GridEvent.class \
build/$(PACKAGE_PATH)/Controller/EventListener/PlayerEvent.class \
build/$(PACKAGE_PATH)/Utils/Constants.class
javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/View/Grille.java
javac $(JAVAC_OPT) $<
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
javac $(JAVAC_OPT) $<
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)/Controller/GrilleMouseListener.class
javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/View/TestGrille.java
javac $(JAVAC_OPT) $<
testGrille : Model View Utils Events
testGrille : Controller Model View Utils Events
java -cp build $(PACKAGE).View.TestGrille
clear :

View File

@ -5,6 +5,7 @@ import javax.swing.event.MouseInputAdapter;
import fr.iutfbleau.projetAgile.Model.GrilleModel;
import fr.iutfbleau.projetAgile.View.Grille;
import fr.iutfbleau.projetAgile.Utils.Constants;
import static fr.iutfbleau.projetAgile.Utils.Constants.GameStatus.*;
public class GrilleMouseListener extends MouseInputAdapter{
@ -21,7 +22,72 @@ public class GrilleMouseListener extends MouseInputAdapter{
@Override
public void mouseClicked(MouseEvent e) {
int column = (e.getX() * Constants.COLUMN_COUNT / grille.getWidth());
this.modele.addPawn(column);
if(this.modele.getGameStatus() == PLAYING ) {
int column = (e.getX() * this.modele.getColumn() / grille.getWidth());
this.verifyColumn(column);
}
}
protected void verifyColumn(int column) {
int[][] grille = this.modele.getTab();
for (int row = this.modele.getRow() - 1; row >= 0; row--) {
if (grille[column][row] == Constants.EMPTY_PLAYER) {
this.modele.addPiece(column, row);
if(!this.verifyWin(column, row))
this.modele.switchPlayer();
return;
}
}
}
protected boolean verifyWin(int column, int row) {
int playerColor = this.modele.getPlayerTurn();
int diagonalColumn = column;
int diagonalRow = row;
if(verifyAlignedPiece(column, 0, 0, 1, playerColor) || verifyAlignedPiece(0, row, 1, 0, playerColor)) {
this.modele.setPartyStatus(STOP);
return true;
}
while(diagonalColumn > 0 && diagonalRow > 0) {
diagonalColumn--;
diagonalRow--;
}
if(verifyAlignedPiece(diagonalColumn, diagonalRow, 1, 1, playerColor)) {
this.modele.setPartyStatus(STOP);
return true;
}
diagonalColumn = column;
diagonalRow = row;
while(diagonalColumn < this.modele.getColumn() - 1 && diagonalRow > 0) {
diagonalColumn++;
diagonalRow--;
}
if(verifyAlignedPiece(diagonalColumn, diagonalRow, -1, 1, playerColor)) {
this.modele.setPartyStatus(STOP);
return true;
}
return false;
}
private boolean verifyAlignedPiece(int numColumn, int numRow, int columnOffset, int rowOffset, int playerColor) {
int[][] grille = this.modele.getTab();
int couleur = playerColor;
int compteur = 0;
while ((numColumn >= 0) && (numColumn < this.modele.getColumn()) && (numRow >= 0) && (numRow < this.modele.getRow())) {
if (grille[numColumn][numRow] != couleur)
compteur = 0;
else
compteur++;
if (compteur >= 4)
return true;
numColumn += columnOffset;
numRow += rowOffset;
}
return false;
}
}

View File

@ -2,10 +2,11 @@ package fr.iutfbleau.projetAgile.Model;
import fr.iutfbleau.projetAgile.Controller.EventListener.AbstractGridInitiater;
import fr.iutfbleau.projetAgile.Utils.Constants;
import fr.iutfbleau.projetAgile.Utils.Constants.GameStatus;
public class GrilleModel extends AbstractGridInitiater{
private int[][] grille;
private boolean partyEnd;
private GameStatus gameStatus = GameStatus.PLAYING;
private int playerTurn;
private int column, row;
@ -25,27 +26,39 @@ public class GrilleModel extends AbstractGridInitiater{
}
}
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;
fireGridChanged(column, row, this.playerTurn);
this.switchPlayer();
return true;
}
}
return false;
public void addPiece(int column, int row) {
grille[column][row] = this.playerTurn;
fireGridChanged(column, row, this.playerTurn);
}
private void switchPlayer() {
public void switchPlayer() {
int oldPlayer = this.playerTurn;
this.playerTurn = (this.playerTurn + 1) % 2;
firePlayerChanged(oldPlayer, this.playerTurn);
}
public int getPlayerTurn() {
return this.playerTurn;
}
public int[][] getTab() {
return grille;
return this.grille;
}
public int getColumn() {
return this.column;
}
public int getRow() {
return this.row;
}
public void setPartyStatus(GameStatus gameStatus) {
this.gameStatus = gameStatus;
}
public GameStatus getGameStatus() {
return this.gameStatus;
}
}

View File

@ -3,13 +3,18 @@ package fr.iutfbleau.projetAgile.Utils;
import java.awt.Color;
public class Constants {
public enum GameStatus {
PLAYING, STOP, DRAW;
}
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;
public static int PIECE_MARGIN = 10;
public static int COLUMN_COUNT = 7;
public static int ROW_COUNT = 6;
}

View File

@ -8,12 +8,17 @@ import fr.iutfbleau.projetAgile.Utils.Constants;
public class Grille extends JPanel implements GridChangedListener{
private Pion grille[][];
private int column;
private int row;
public Grille() {
super();
}
public void init(int[][] tab) {
this.column = Constants.COLUMN_COUNT;
this.row = Constants.ROW_COUNT;
this.grille = new Pion[this.column][this.row];
this.setBackground(new Color(31,31,31));
/*
* On peut remplacer GridBagLayout par un GridLayout
@ -21,9 +26,8 @@ public class Grille extends JPanel implements GridChangedListener{
GridBagLayout gbl = new GridBagLayout();
this.setLayout(gbl);
GridBagConstraints gbc = new GridBagConstraints();
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++) {
for (int x = 0; x < this.column; x++) {
for (int y = 0; y < this.row; y++) {
Pion pion = new Pion(tab[x][y]);
gbc.gridx = x;
gbc.gridy = y;
@ -41,17 +45,17 @@ public class Grille extends JPanel implements GridChangedListener{
}
}
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;
int minimumWidth = this.column * Pion.getPionMinimumSize().width;
int minimumHeight = this.row * Pion.getPionMinimumSize().height;
int preferredWidth = this.column * Pion.getPionPreferredSize().width;
int preferredHeight = this.row * Pion.getPionPreferredSize().height;
this.setMinimumSize(new Dimension(minimumWidth, minimumHeight));
this.setPreferredSize(new Dimension(preferredWidth, preferredHeight));
}
protected void addPlayerPawn(int column, int row, int player) {
protected void addPlayerPiece(int column, int row, int player) {
Color c = player == Constants.PLAYER_ONE ? Constants.PLAYER_ONE_COLOR : Constants.PLAYER_TWO_COLOR;
this.grille[column][row].setPlayer(player);
this.grille[column][row].repaint();
@ -59,7 +63,7 @@ public class Grille extends JPanel implements GridChangedListener{
@Override
public void gridChanged(GridEvent e) {
this.addPlayerPawn(e.getColumn(), e.getRow(), e.getPlayer());
this.addPlayerPiece(e.getColumn(), e.getRow(), e.getPlayer());
}
@Override

View File

@ -47,6 +47,6 @@ public class Pion extends JComponent{
break;
}
g2.setColor(c);
g2.fillOval(Constants.PAWN_MARGIN, Constants.PAWN_MARGIN, this.getWidth() - 2 * Constants.PAWN_MARGIN, this.getHeight() - 2 * Constants.PAWN_MARGIN);
g2.fillOval(Constants.PIECE_MARGIN, Constants.PIECE_MARGIN, this.getWidth() - 2 * Constants.PIECE_MARGIN, this.getHeight() - 2 * Constants.PIECE_MARGIN);
}
}

View File

@ -17,7 +17,7 @@ public class Puissance4Panel extends JPanel{
this.label = new JLabel("??");
this.grille = new Grille();
this.init();
this.grille.requestFocusInWindow();
//this.grille.requestFocusInWindow();
}
public void init() {