Déplacement de la méthode de vérification dans le modèle + fonction de test de reset de la grille
This commit is contained in:
parent
c1902c184d
commit
3f4f2fa25d
@ -4,7 +4,6 @@ import java.awt.event.MouseEvent;
|
||||
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{
|
||||
@ -22,72 +21,18 @@ public class GrilleMouseListener extends MouseInputAdapter{
|
||||
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
if(this.modele.getGameStatus() == PLAYING ) {
|
||||
int column = (e.getX() * this.modele.getColumn() / grille.getWidth());
|
||||
this.verifyColumn(column);
|
||||
}
|
||||
if(e.getButton() == MouseEvent.BUTTON1) {
|
||||
if(this.modele.getGameStatus() == PLAYING) {
|
||||
int column = (e.getX() * this.modele.getColumn() / grille.getWidth());
|
||||
this.modele.verifyColumn(column);
|
||||
}
|
||||
}
|
||||
//Seulement pour cas de test (Doit être modifié)
|
||||
else {
|
||||
this.modele.reset();
|
||||
this.grille.reset();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import fr.iutfbleau.projetAgile.Utils.Constants.GameStatus;
|
||||
public class GrilleModel extends AbstractGridInitiater{
|
||||
private int[][] grille;
|
||||
private GameStatus gameStatus = GameStatus.PLAYING;
|
||||
private int piecePlayed = 0;
|
||||
private int playerTurn;
|
||||
private int column, row;
|
||||
|
||||
@ -19,18 +20,109 @@ public class GrilleModel extends AbstractGridInitiater{
|
||||
this.column = Constants.COLUMN_COUNT;
|
||||
this.row = Constants.ROW_COUNT;
|
||||
this.grille = new int[this.column][this.row];
|
||||
this.reset();
|
||||
}
|
||||
//Fonction de test (pourra être modifié)
|
||||
public void reset() {
|
||||
for(int i = 0; i < this.column; i++) {
|
||||
for(int j = 0; j < this.row; j++) {
|
||||
this.grille[i][j] = Constants.EMPTY_PLAYER;
|
||||
}
|
||||
}
|
||||
this.playerTurn = Constants.PLAYER_ONE;
|
||||
this.piecePlayed = 0;
|
||||
this.setPartyStatus(GameStatus.PLAYING);
|
||||
}
|
||||
|
||||
public void addPiece(int column, int row) {
|
||||
protected void addPiece(int column, int row) {
|
||||
grille[column][row] = this.playerTurn;
|
||||
fireGridChanged(column, row, this.playerTurn);
|
||||
}
|
||||
|
||||
public void verifyColumn(int column) {
|
||||
for (int row = this.row - 1; row >= 0; row--) {
|
||||
if (grille[column][row] == Constants.EMPTY_PLAYER) {
|
||||
this.addPiece(column, row);
|
||||
if(!this.verifyWin(column, row))
|
||||
this.switchPlayer();
|
||||
this.verifyDraw();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Vérifie si le jeton qui vient d'être joué termine la partie
|
||||
* @param column La colonne du pion qui vient d'être joué
|
||||
* @param row La ligne du pion qui vient d'être joué
|
||||
* @return Retourne true si le jeu est fini
|
||||
*/
|
||||
protected boolean verifyWin(int column, int row) {
|
||||
int playerColor = this.playerTurn;
|
||||
int diagonalColumn = column;
|
||||
int diagonalRow = row;
|
||||
//Vérification horizontal et vertical
|
||||
if(verifyAlignedPiece(column, 0, 0, 1, playerColor) || verifyAlignedPiece(0, row, 1, 0, playerColor)) {
|
||||
this.setPartyStatus(GameStatus.STOP);
|
||||
return true;
|
||||
}
|
||||
|
||||
while(diagonalColumn > 0 && diagonalRow > 0) {
|
||||
diagonalColumn--;
|
||||
diagonalRow--;
|
||||
}
|
||||
//Vérification diagonale haut à gauche -> bas à droite
|
||||
if(verifyAlignedPiece(diagonalColumn, diagonalRow, 1, 1, playerColor)) {
|
||||
this.setPartyStatus(GameStatus.STOP);
|
||||
return true;
|
||||
}
|
||||
|
||||
diagonalColumn = column;
|
||||
diagonalRow = row;
|
||||
while(diagonalColumn < this.column - 1 && diagonalRow > 0) {
|
||||
diagonalColumn++;
|
||||
diagonalRow--;
|
||||
}
|
||||
|
||||
//Vérification diagonale haut à droite -> bas à gauche
|
||||
if(verifyAlignedPiece(diagonalColumn, diagonalRow, -1, 1, playerColor)) {
|
||||
this.setPartyStatus(GameStatus.STOP);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retourne true si 4 jetons de la même couleur ou plus sont alignés
|
||||
* @param numColumn La colonne où commencer
|
||||
* @param numRow La ligne où commencer
|
||||
* @param columnOffset l'offset de déplacement vertical
|
||||
* @param rowOffset l'offset de déplacement horizontal
|
||||
* @param playerColor La couleur du joueur (entier)
|
||||
* @return Retourne true si 4 jetons ou plus sont alignés
|
||||
*/
|
||||
private boolean verifyAlignedPiece(int numColumn, int numRow, int columnOffset, int rowOffset, int playerColor) {
|
||||
int couleur = playerColor;
|
||||
int compteur = 0;
|
||||
while ((numColumn >= 0) && (numColumn < this.column) && (numRow >= 0) && (numRow < this.row)) {
|
||||
if (grille[numColumn][numRow] != couleur)
|
||||
compteur = 0;
|
||||
else
|
||||
compteur++;
|
||||
if (compteur >= 4)
|
||||
return true;
|
||||
numColumn += columnOffset;
|
||||
numRow += rowOffset;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void verifyDraw() {
|
||||
if(this.piecePlayed >= this.column * this.row)
|
||||
this.setPartyStatus(GameStatus.DRAW);
|
||||
}
|
||||
|
||||
public void switchPlayer() {
|
||||
int oldPlayer = this.playerTurn;
|
||||
this.playerTurn = (this.playerTurn + 1) % 2;
|
||||
@ -55,6 +147,7 @@ public class GrilleModel extends AbstractGridInitiater{
|
||||
|
||||
public void setPartyStatus(GameStatus gameStatus) {
|
||||
this.gameStatus = gameStatus;
|
||||
//avertir un observateur
|
||||
}
|
||||
|
||||
public GameStatus getGameStatus() {
|
||||
|
@ -71,4 +71,14 @@ public class Grille extends JPanel implements GridChangedListener{
|
||||
System.out.println("Tour du joueur : " + (e.getNewPlayer() == Constants.PLAYER_ONE ? "Rouge" : "Jaune"));
|
||||
}
|
||||
|
||||
//Fonction de test (pourra être modifié)
|
||||
public void reset() {
|
||||
for (int x = 0; x < this.column; x++) {
|
||||
for (int y = 0; y < this.row; y++) {
|
||||
this.grille[x][y].setPlayer(Constants.EMPTY_PLAYER);
|
||||
}
|
||||
}
|
||||
this.repaint();
|
||||
}
|
||||
|
||||
}
|
@ -9,12 +9,11 @@ import fr.iutfbleau.projetAgile.Model.GrilleModel;
|
||||
public class TestGrille extends JFrame{
|
||||
public TestGrille() {
|
||||
super("Puissance 4");
|
||||
|
||||
Puissance4Panel p = new Puissance4Panel();
|
||||
Grille g = p.getGrille();
|
||||
this.setLayout(new FlowLayout());
|
||||
Grille g = new Grille();
|
||||
GrilleModel gm = new GrilleModel();
|
||||
GrilleMouseListener listener = new GrilleMouseListener(g, gm);
|
||||
this.add(p);
|
||||
this.add(g);
|
||||
this.setLocation(200, 200);
|
||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
this.setSize(new Dimension(1280,720));
|
||||
|
Loading…
Reference in New Issue
Block a user