coordination joueur qui gagne

This commit is contained in:
martins 2022-11-10 13:00:34 +01:00
parent a809d51803
commit 4dd025d47b
2 changed files with 67 additions and 31 deletions

View File

@ -73,7 +73,7 @@ public class Puissance4Controller {
* @see fr.iutfbleau.projetAgile.Puissance4.Model.GrilleModel#verifyColumn(int)
*/
public void verifyColumn(int x) {
if(this.modele.getGameStatus() == GameStatus.PLAYING) {
if(this.modele.getGameStatus() == GameStatus.PLAYING || this.modele.getGameStatus() == GameStatus.FIRST) {
int column = (x * this.modele.getColumn() / grille.getWidth());
this.modele.verifyColumn(column);
}
@ -85,7 +85,7 @@ public class Puissance4Controller {
* Si x est égal à -1 efface le survol
*/
public void hoverGrille(int x) {
if(this.modele.getGameStatus() == GameStatus.PLAYING) {
if(this.modele.getGameStatus() == GameStatus.PLAYING || this.modele.getGameStatus() == GameStatus.FIRST) {
if(x == -1) {
this.grille.hover(-1); //Clean
return;

View File

@ -1,5 +1,7 @@
package fr.iutfbleau.projetAgile.Puissance4.Model;
import java.util.LinkedList;
import fr.iutfbleau.projetAgile.Puissance4.Event.AbstractGridInitiater;
import fr.iutfbleau.projetAgile.Puissance4.Utils.Constants;
import fr.iutfbleau.projetAgile.Puissance4.Utils.GameStatus;
@ -19,8 +21,8 @@ public class GrilleModel extends AbstractGridInitiater{
private int playerOneScore;
private int playerTwoScore;
private int playerThreeScore;
private LinkedList<Integer> tour;
private int playerNumber;
/**
* Crée le modèle et l'initialise avec des cases vides
*/
@ -32,7 +34,13 @@ public class GrilleModel extends AbstractGridInitiater{
this.playerOneScore = 0;
this.playerTwoScore = 0;
this.playerThreeScore = 0;
this.playerNumber = playerNumber;
this.tour = new LinkedList<Integer>();
this.tour.add(Constants.PLAYER_ONE);
this.tour.add(Constants.PLAYER_TWO);
if(playerNumber==3){
this.tour.add(Constants.PLAYER_THREE);
}
this.playerNumber=playerNumber;
this.reset();
}
@ -48,7 +56,13 @@ public class GrilleModel extends AbstractGridInitiater{
this.playerOneScore = 0;
this.playerTwoScore = 0;
this.playerThreeScore = 0;
this.playerNumber = playerNumber;
this.tour=new LinkedList<Integer>();
this.tour.add(Constants.PLAYER_ONE);
this.tour.add(Constants.PLAYER_TWO);
if(playerNumber==3){
this.tour.add(Constants.PLAYER_THREE);
}
this.playerNumber=playerNumber;
this.reset();
}
@ -61,7 +75,13 @@ public class GrilleModel extends AbstractGridInitiater{
this.grille[i][j] = Constants.EMPTY_PLAYER;
}
}
this.playerTurn = Constants.PLAYER_ONE;
this.tour.clear();
this.tour.add(Constants.PLAYER_ONE);
this.tour.add(Constants.PLAYER_TWO);
if(this.playerNumber==3){
this.tour.add(Constants.PLAYER_THREE);
}
this.switchPlayer();
this.piecePlayed = 0;
this.setPartyStatus(GameStatus.PLAYING);
}
@ -101,7 +121,11 @@ 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);
if(this.tour.size()==3){
this.Player3();
}else{
this.setPartyStatus(GameStatus.WIN);
}
return true;
}
@ -111,7 +135,11 @@ public class GrilleModel extends AbstractGridInitiater{
}
//Vérification diagonale haut à gauche -> bas à droite
if(verifyAlignedPiece(diagonalColumn, diagonalRow, 1, 1, playerColor)) {
this.setPartyStatus(GameStatus.WIN);
if(this.tour.size()==3){
this.Player3();
}else{
this.setPartyStatus(GameStatus.WIN);
}
return true;
}
@ -124,13 +152,37 @@ public class GrilleModel extends AbstractGridInitiater{
//Vérification diagonale haut à droite -> bas à gauche
if(verifyAlignedPiece(diagonalColumn, diagonalRow, -1, 1, playerColor)) {
this.setPartyStatus(GameStatus.WIN);
if(this.tour.size()==3){
this.Player3();
}else{
this.setPartyStatus(GameStatus.WIN);
}
return true;
}
return false;
}
private void Player3(){
switch(this.playerTurn){
case Constants.PLAYER_ONE:
this.playerOneScore++;
break;
case Constants.PLAYER_TWO:
this.playerTwoScore++;
break;
case Constants.PLAYER_THREE:
this.playerThreeScore++;
break;
}
this.fireStatusChanged(GameStatus.FIRST, playerTurn, playerOneScore, playerTwoScore, playerThreeScore);
this.removePlayerPion();
this.firePlayerChanged(this.playerTurn, this.grille);
this.tour.pollLast();
this.switchPlayer();
}
/**
* Retourne true si 4 jetons de la même couleur ou plus sont alignés
* @param numColumn La colonne commencer
@ -145,7 +197,7 @@ public class GrilleModel extends AbstractGridInitiater{
int max = 0;
int compteur = 0;
if(this.playerNumber == 2)
if(this.tour.size() == 2)
max = Constants.ALIGNED_NUMBER_TWO;
else
max = Constants.ALIGNED_NUMBER_THREE;
@ -171,18 +223,8 @@ public class GrilleModel extends AbstractGridInitiater{
* Change le tour du joueur et notifie tous les listeners
*/
public void switchPlayer() {
switch(this.playerNumber) {
case 3 :
if(this.playerTurn == Constants.PLAYER_ONE)
this.playerTurn = Constants.PLAYER_TWO;
else if(this.playerTurn == Constants.PLAYER_TWO)
this.playerTurn = Constants.PLAYER_THREE;
else
this.playerTurn = Constants.PLAYER_ONE;
break;
default :
this.playerTurn = this.playerTurn == Constants.PLAYER_ONE ? Constants.PLAYER_TWO : Constants.PLAYER_ONE;
}
this.playerTurn=(int)this.tour.pollFirst();
this.tour.add((Integer)this.playerTurn);
fireStatusChanged(this.gameStatus, this.playerTurn, this.playerOneScore, this.playerTwoScore, this.playerThreeScore);
}
@ -193,7 +235,7 @@ public class GrilleModel extends AbstractGridInitiater{
*/
public void setPartyStatus(GameStatus gameStatus) {
this.gameStatus = gameStatus;
if(gameStatus == GameStatus.WIN || gameStatus == GameStatus.FIRST) {
if(gameStatus == GameStatus.WIN) {
if(this.playerTurn == Constants.PLAYER_ONE)
playerOneScore++;
else if(this.playerTurn == Constants.PLAYER_TWO)
@ -203,12 +245,6 @@ public class GrilleModel extends AbstractGridInitiater{
}
fireStatusChanged(this.gameStatus, this.playerTurn, this.playerOneScore, this.playerTwoScore, this.playerThreeScore);
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, this.playerThreeScore);
}
}
/**
@ -325,7 +361,7 @@ public class GrilleModel extends AbstractGridInitiater{
}
public int getPlayerNumber() {
return this.playerNumber;
return this.tour.size();
}
/**