This commit is contained in:
Justine Yannis 2022-11-10 18:55:09 +01:00
commit 7a1d8aadac
19 changed files with 468 additions and 42 deletions

View File

@ -1,5 +1,5 @@
@JAVAC_OPT = -implicit:none \
-d build -classpath build -sourcepath src
-d build -classpath "build:res/mariadb-client.jar" -sourcepath src
PACKAGE = fr.iutfbleau.projetAgile
PACKAGE_PATH = fr/iutfbleau/projetAgile
@ -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
@ -23,9 +24,10 @@ Controller = build/$(PACKAGE_PATH)/Puissance4/Controller/GrilleMouseListener.cla
build/$(PACKAGE_PATH)/Puissance4/Controller/ResetGame.class
#BUT PAR DEFAUT
ArgileGames.jar : build/$(PACKAGE_PATH)/Main.class
ArgileGames.jar : build \
build/$(PACKAGE_PATH)/Main.class
@echo "Creating jar..."
@jar cfe ArgileGames.jar $(PACKAGE_PATH).Main -C build fr -C res images
@jar cfme ArgileGames.jar Manifest.txt $(PACKAGE_PATH).Main res/ -C build fr
@echo "Finished"
#------- Events ------
@ -45,6 +47,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
@ -127,7 +132,6 @@ build/$(PACKAGE_PATH)/Menu/View/BoutonsMenu.class : src/$(PACKAGE_PATH)/Menu/Vie
build/$(PACKAGE_PATH)/Menu/View/Menu.class : src/$(PACKAGE_PATH)/Menu/View/Menu.java \
build/$(PACKAGE_PATH)/Menu/Controller/ObservateurMenuSouris.class \
build/$(PACKAGE_PATH)/Menu/View/BoutonsMenu.class \
build/$(PACKAGE_PATH)/Menu/Controleur/ObservateurAccueil.class \
build/$(PACKAGE_PATH)/Menu/Controller/ObservateurFenetre.class \
build/$(PACKAGE_PATH)/Puissance4/LancementPuissance4.class
@javac $(@JAVAC_OPT) $<
@ -144,11 +148,20 @@ build/$(PACKAGE_PATH)/Puissance4/LancementPuissance4.class : src/$(PACKAGE_PATH)
build/$(PACKAGE_PATH)/Puissance4/View/ColorChooserDialog.class \
build/$(PACKAGE_PATH)/Puissance4/View/Grille.class \
build/$(PACKAGE_PATH)/Puissance4/Model/GrilleModel.class
@javac $(@JAVAC_OPT) $<
@javac -implicit:none -d build -classpath "build:res/mariadb-client.jar" -sourcepath src $<
build/$(PACKAGE_PATH)/Jeu.class : src/$(PACKAGE_PATH)/Jeu.java
@javac $(@JAVAC_OPT) $<
build :
@mkdir build
@echo "Creating Folder"
# BUT FACTICES:
.PHONY: clear doc run
@ -162,4 +175,3 @@ run: ArgileGames.jar
clean :
@rm -rf build/*

3
projetAgile/Manifest.txt Normal file
View File

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Class-Path: res/mariadb-client.jar
Created-By: 1.7.0_06 (Oracle Corporation)

Binary file not shown.

View File

@ -20,7 +20,7 @@ public class BoutonsMenu extends JComponent{
super();
this.path=path0;
ClassLoader cl = Thread.currentThread().getContextClassLoader();
this.img = Toolkit.getDefaultToolkit().getImage(cl.getResource("images/"+this.path+".png"));
this.img = Toolkit.getDefaultToolkit().getImage(cl.getResource("res/images/"+this.path+".png"));
this.survol=false;
this.disponibiliter=disponibiliter0;
}
@ -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

@ -26,8 +26,10 @@ public class ColorChooserListener implements ChangeListener {
ColorSelectionModel chooser = (ColorSelectionModel) e.getSource();
if(this.player == Constants.PLAYER_ONE)
Constants.PLAYER_ONE_COLOR = chooser.getSelectedColor();
else
else if(this.player == Constants.PLAYER_TWO)
Constants.PLAYER_TWO_COLOR = chooser.getSelectedColor();
else
Constants.PLAYER_THREE_COLOR = chooser.getSelectedColor();
composant.repaint();
}
}

View File

@ -7,10 +7,17 @@ public class ModelEventListener implements GridChangedListener {
private Puissance4Panel panel;
/**
* Constructeur
* @param p
*/
public ModelEventListener(Puissance4Panel p) {
this.panel = p;
}
/**
* Ajoute un pion à l'emplacement modifié dans la grille
*/
@Override
public void gridChanged(GridEvent e) {
this.panel.addPlayerPiece(e.getColumn(), e.getRow(), e.getPlayer());
@ -20,6 +27,11 @@ public class ModelEventListener implements GridChangedListener {
public void statusChanged(StatusEvent e) {
this.panel.changeLabel(e.getPlayerTurn());
this.panel.changeHoverColor(e.getPlayerTurn());
this.panel.setGameStatus(e.getStatus(), e.getPlayerTurn(), e.getPlayerOneScore(), e.getPlayerTwoScore());
this.panel.setGameStatus(e.getStatus(), e.getPlayerTurn(), e.getPlayerOneScore(), e.getPlayerTwoScore(), e.getPlayerThreeScore());
}
@Override
public void playerRemove(PlayerEvent e){
this.panel.removePlayer(e.getJ(), e.getGrille());
}
}

View File

@ -44,9 +44,10 @@ public class Puissance4Controller {
*/
public void initPanel() {
this.panel.init();
this.panel.setPlayerNumber(this.modele.getPlayerNumber());
this.panel.changeHoverColor(this.modele.getPlayerTurn());
this.panel.changeLabel(this.modele.getPlayerTurn());
this.panel.changeScore(this.modele.getPlayerOneScore(), this.modele.getPlayerTwoScore());
this.panel.changeScore(this.modele.getPlayerOneScore(), this.modele.getPlayerTwoScore(), this.modele.getPlayerThreeScore());
}
/**
@ -62,6 +63,10 @@ public class Puissance4Controller {
}
this.grille.reset();
this.modele.reset();
if(this.modele.getPlayerThreeName() != null) {
this.modele.setPlayerNumber(3);
this.panel.setPlayerNumber(3);
}
this.panel.changeHoverColor(this.modele.getPlayerTurn());
this.panel.changeLabel(this.modele.getPlayerTurn());
}
@ -122,4 +127,18 @@ public class Puissance4Controller {
this.panel.setPlayerOneName(this.modele.getPlayerOneName());
this.panel.setPlayerTwoName(this.modele.getPlayerTwoName());
}
/**
* Change le nom des joueurs dans le modèle et la vue
* @param J1 le nom du joueur 1
* @param J2 le nom du joueur 2
*/
public void changeName(String J1, String J2, String J3) {
this.modele.setPlayerOneName(J1);
this.modele.setPlayerTwoName(J2);
this.modele.setPlayerThreeName(J3);
this.panel.setPlayerOneName(this.modele.getPlayerOneName());
this.panel.setPlayerTwoName(this.modele.getPlayerTwoName());
this.panel.setPlayerThreeName(this.modele.getPlayerThreeName());
}
}

View File

@ -56,12 +56,22 @@ public abstract class AbstractGridInitiater {
* @param playerOneScore Le score du joueur 1
* @param playerTwoScore Le score du joueur 2
*/
protected void fireStatusChanged(GameStatus status, int playerTurn, int playerOneScore, int playerTwoScore) {
protected void fireStatusChanged(GameStatus status, int playerTurn, int playerOneScore, int playerTwoScore, int playerThreeScore) {
StatusEvent event = null;
for(GridChangedListener listener : getGridListeners()) {
if(event == null)
event = new StatusEvent(status, playerTurn, playerOneScore, playerTwoScore);
event = new StatusEvent(status, playerTurn, playerOneScore, playerTwoScore, playerThreeScore);
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

@ -10,6 +10,7 @@ public class StatusEvent {
private int playerTurn;
private int playerOneScore;
private int playerTwoScore;
private int playerThreeScore;
/**
* Crée un évènement
@ -17,12 +18,14 @@ public class StatusEvent {
* @param playerTurn le tour du joueur
* @param playerOneScore le score du joueur 1
* @param playerTwoScore le score du joueur 2
* @param playerThreeScore le score du joueur 2
*/
public StatusEvent(GameStatus status, int playerTurn, int playerOneScore, int playerTwoScore) {
public StatusEvent(GameStatus status, int playerTurn, int playerOneScore, int playerTwoScore, int playerThreeScore) {
this.status = status;
this.playerTurn = playerTurn;
this.playerOneScore = playerOneScore;
this.playerTwoScore = playerTwoScore;
this.playerThreeScore = playerThreeScore;
}
/**
@ -56,4 +59,12 @@ public class StatusEvent {
public int getPlayerTwoScore() {
return playerTwoScore;
}
/**
* Retourne le score du joueur 3
* @return Le score du joueur 3
*/
public int getPlayerThreeScore() {
return playerThreeScore;
}
}

View File

@ -6,9 +6,17 @@ import fr.iutfbleau.projetAgile.Puissance4.View.Grille;
import fr.iutfbleau.projetAgile.Puissance4.View.ColorChooserDialog;
import fr.iutfbleau.projetAgile.Puissance4.Model.GrilleModel;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import org.mariadb.jdbc.*;
import java.sql.Statement;
import java.util.concurrent.CancellationException;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
/**
* Classe qui initialise une partie de Puissance 4
@ -24,17 +32,44 @@ public class LancementPuissance4 implements Jeu{
public LancementPuissance4(JFrame fenetre) throws CancellationException{
String J1 = null;
String J2 = null;
ColorChooserDialog chooser = new ColorChooserDialog(fenetre, true);
String J3 = null;
int playerNumber;
JRadioButton twoPlayer = new JRadioButton("Deux joueurs");
JRadioButton threePlayer = new JRadioButton("Trois joueurs");
ButtonGroup buttons = new ButtonGroup();
buttons.add(twoPlayer);
buttons.add(threePlayer);
Object[] message = {
"Combien de joueurs ?", twoPlayer, threePlayer
};
int choix = JOptionPane.showConfirmDialog(fenetre, message, "Entrez le nombre de joueur", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if(choix != JOptionPane.OK_OPTION) {
throw new CancellationException("User cancelled");
}
if(buttons.getSelection() == twoPlayer.getModel())
playerNumber = 2;
else
playerNumber = 3;
ColorChooserDialog chooser = new ColorChooserDialog(fenetre, true, playerNumber);
if (chooser.getChoice()) {
J1 = chooser.getPlayerOneName();
J2 = chooser.getPlayerTwoName();
if(playerNumber == 3)
J3 = chooser.getPlayerThreeName();
} else {
throw new CancellationException("User cancelled");
}
this.sendToDatabase(playerNumber);
Grille g=new Grille();
GrilleModel m=new GrilleModel();
GrilleModel m=new GrilleModel(playerNumber);
this.P4 = new Puissance4Controller(g, m);
this.P4.changeName(J1, J2);
if(playerNumber == 2)
this.P4.changeName(J1, J2);
else
this.P4.changeName(J1, J2, J3);
this.P4.initPanel();
}
@ -55,4 +90,32 @@ public class LancementPuissance4 implements Jeu{
public JPanel getPanel(){
return this.P4.getPanel();
}
public void sendToDatabase(int playerNumber) {
try {
Class.forName("org.mariadb.jdbc.Driver"); //Verification du pilote
Connection conn = DriverManager.getConnection("jdbc:mariadb://dwarves.iut-fbleau.fr/justiney", "justiney", "motdepasseJustiney");
try {
Statement req = conn.createStatement();
//------ Requete preparee --------
if(playerNumber == 2)
req.executeUpdate("UPDATE SondageMarchePuissance4 SET twoPlayer = twoPlayer + 1");
else
req.executeUpdate("UPDATE SondageMarchePuissance4 SET threePlayer = threePlayer + 1");
req.close();
} catch (Exception e) {
System.err.println("Probleme requete");
e.printStackTrace();
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

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;
@ -15,19 +17,52 @@ public class GrilleModel extends AbstractGridInitiater{
private int column, row;
private String playerOneName;
private String playerTwoName;
private String playerThreeName;
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
*/
public GrilleModel() {
public GrilleModel(int playerNumber) {
super();
this.column = Constants.COLUMN_COUNT;
this.row = Constants.ROW_COUNT;
this.grille = new int[this.column][this.row];
this.playerOneScore = 0;
this.playerTwoScore = 0;
this.playerThreeScore = 0;
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();
}
/**
* Crée le modèle et l'initialise avec des les cases du tableau donné en arguments
*/
public GrilleModel(int[][] tab, int playerNumber) {
//Faire un test pour le nombre de colonnes/lignes
super();
this.column = Constants.COLUMN_COUNT;
this.row = Constants.ROW_COUNT;
this.grille = tab;
this.playerOneScore = 0;
this.playerTwoScore = 0;
this.playerThreeScore = 0;
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();
}
@ -40,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);
}
@ -80,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;
}
@ -90,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;
}
@ -103,13 +152,36 @@ 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.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
@ -121,13 +193,20 @@ public class GrilleModel extends AbstractGridInitiater{
*/
private boolean verifyAlignedPiece(int numColumn, int numRow, int columnOffset, int rowOffset, int playerColor) {
int couleur = playerColor;
int max = 0;
int compteur = 0;
if(this.tour.size() == 2)
max = Constants.ALIGNED_NUMBER_TWO;
else
max = Constants.ALIGNED_NUMBER_THREE;
while ((numColumn >= 0) && (numColumn < this.column) && (numRow >= 0) && (numRow < this.row)) {
if (grille[numColumn][numRow] != couleur)
compteur = 0;
else
compteur++;
if (compteur >= 4)
if (compteur >= max)
return true;
numColumn += columnOffset;
numRow += rowOffset;
@ -143,8 +222,10 @@ public class GrilleModel extends AbstractGridInitiater{
* Change le tour du joueur et notifie tous les listeners
*/
public void switchPlayer() {
this.playerTurn = this.playerTurn == Constants.PLAYER_ONE ? Constants.PLAYER_TWO : Constants.PLAYER_ONE;
fireStatusChanged(this.gameStatus, this.playerTurn, this.playerOneScore, this.playerTwoScore);
this.playerTurn=(int)this.tour.pollFirst();
this.tour.add((Integer)this.playerTurn);
fireStatusChanged(this.gameStatus, this.playerTurn, this.playerOneScore, this.playerTwoScore, this.playerThreeScore);
}
/**
@ -156,10 +237,37 @@ public class GrilleModel extends AbstractGridInitiater{
if(gameStatus == GameStatus.WIN) {
if(this.playerTurn == Constants.PLAYER_ONE)
playerOneScore++;
else
else if(this.playerTurn == Constants.PLAYER_TWO)
playerTwoScore++;
else
playerThreeScore++;
}
fireStatusChanged(this.gameStatus, this.playerTurn, this.playerOneScore, this.playerTwoScore);
fireStatusChanged(this.gameStatus, this.playerTurn, this.playerOneScore, this.playerTwoScore, this.playerThreeScore);
}
/**
* 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;
}
}
}
/**
@ -210,6 +318,15 @@ public class GrilleModel extends AbstractGridInitiater{
return this.playerTwoName;
}
/**
* Retourne le nom du joueur 3
* @return Un string du nom du joueur 2
*/
public String getPlayerThreeName() {
return this.playerThreeName;
}
/**
* Retourne le score du joueur 1
* @return Le score du joueur 1
@ -226,6 +343,14 @@ public class GrilleModel extends AbstractGridInitiater{
return playerTwoScore;
}
/**
* Retourne le score du joueur 3
* @return Le score du joueur 3
*/
public int getPlayerThreeScore() {
return playerTwoScore;
}
/**
* Retourne le {@link fr.iutfbleau.projetAgile.Puissance4.Utils.GameStatus status} de la partie
* @return Le status de la partie
@ -234,6 +359,10 @@ public class GrilleModel extends AbstractGridInitiater{
return this.gameStatus;
}
public int getPlayerNumber() {
return this.tour.size();
}
/**
* Définit le nom du joueur 1
* @param playerOneName Le nom du joueur 1
@ -249,4 +378,16 @@ public class GrilleModel extends AbstractGridInitiater{
public void setPlayerTwoName(String playerTwoName) {
this.playerTwoName = playerTwoName;
}
/**
* Définit le nom du joueur 2
* @param playerThreeName Le nom du joueur 2
*/
public void setPlayerThreeName(String playerThreeName) {
this.playerThreeName = playerThreeName;
}
public void setPlayerNumber(int i) {
this.playerNumber = i;
}
}

View File

@ -11,9 +11,13 @@ public class Constants {
*/
public static Color PLAYER_ONE_COLOR = new Color(252,21,21);
/**
* La couleur du joueur 1
* La couleur du joueur 2
*/
public static Color PLAYER_TWO_COLOR = new Color(241,205,15);
/**
* La couleur du joueur 3
*/
public static Color PLAYER_THREE_COLOR = new Color(15,20,241);
/**
* La couleur d'un pion "vide"
*/
@ -34,6 +38,10 @@ public class Constants {
* L'entier qui représente un pion du joueur 2
*/
public final static int PLAYER_TWO = 2;
/**
* L'entier qui représente un pion du joueur 3
*/
public final static int PLAYER_THREE = 3;
/**
* Le survol du pion est activé ?
*/
@ -50,6 +58,15 @@ public class Constants {
* Le nombre de ligne de la grille
*/
public final static int ROW_COUNT = 6;
/**
* Nombre de pion aligné avec 2 joueurs
*/
public final static int ALIGNED_NUMBER_TWO = 4;
/**
* Nombre de pion aligné avec 3 joueurs
*/
public final static int ALIGNED_NUMBER_THREE = 3;
private Constants() {

View File

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

View File

@ -27,6 +27,7 @@ public class ColorChooserDialog extends JDialog{
private JTextField field1;
private JTextField field2;
private JTextField field3;
private boolean confirm = false;
/**
@ -35,7 +36,7 @@ public class ColorChooserDialog extends JDialog{
* @param modal specifie si la fenêtre modale bloque les interactions avec les fenêtres mères
* @see javax.swing.JDialog#JDialog(Frame, boolean)
*/
public ColorChooserDialog(Frame owner, boolean modal) {
public ColorChooserDialog(Frame owner, boolean modal, int playerNumber) {
super(owner, "Choisissez vos noms et couleurs",modal);
this.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
@ -43,8 +44,10 @@ public class ColorChooserDialog extends JDialog{
int length = 12;
JColorChooser playerOneChooser = new JColorChooser(Constants.PLAYER_ONE_COLOR);
JColorChooser playerTwoChooser = new JColorChooser(Constants.PLAYER_TWO_COLOR);
JColorChooser playerThreeChooser = new JColorChooser(Constants.PLAYER_THREE_COLOR);
AbstractColorChooserPanel[] panelsP1 = playerOneChooser.getChooserPanels();
AbstractColorChooserPanel[] panelsP2 = playerTwoChooser.getChooserPanels();
AbstractColorChooserPanel[] panelsP3 = playerThreeChooser.getChooserPanels();
UIManager.put("ColorChooser.swatchesSwatchSize", new Dimension(length, length));
UIManager.put("ColorChooser.swatchesRecentSwatchSize", new Dimension(length, length));
@ -53,37 +56,49 @@ public class ColorChooserDialog extends JDialog{
for(int i = 1; i < panelsP1.length; i++) {
playerOneChooser.removeChooserPanel(panelsP1[i]);
playerTwoChooser.removeChooserPanel(panelsP2[i]);
playerThreeChooser.removeChooserPanel(panelsP3[i]);
}
JButton bouton = new JButton("Confirmer");
Pion pionP1 = new Pion(Constants.PLAYER_ONE);
Pion pionP2 = new Pion(Constants.PLAYER_TWO);
Pion pionP3 = new Pion(Constants.PLAYER_THREE);
bouton.addActionListener(new ColorActionListener(this));
pionP1.setPreferredSize(new Dimension(50,50));
pionP2.setPreferredSize(new Dimension(50,50));
pionP3.setPreferredSize(new Dimension(50,50));
JPanel panelPionP1 = new JPanel();
JPanel panelPionP2 = new JPanel();
JPanel panelPionP3 = new JPanel();
panelPionP1.add(pionP1);
panelPionP1.setBackground(Constants.BACKGROUND_COLOR);
panelPionP2.add(pionP2);
panelPionP2.setBackground(Constants.BACKGROUND_COLOR);
panelPionP3.add(pionP3);
panelPionP3.setBackground(Constants.BACKGROUND_COLOR);
playerOneChooser.setPreviewPanel(panelPionP1);
playerTwoChooser.setPreviewPanel(panelPionP2);
playerThreeChooser.setPreviewPanel(panelPionP3);
playerOneChooser.getSelectionModel().addChangeListener(new ColorChooserListener(pionP1, Constants.PLAYER_ONE));
playerTwoChooser.getSelectionModel().addChangeListener(new ColorChooserListener(pionP2, Constants.PLAYER_TWO));
playerThreeChooser.getSelectionModel().addChangeListener(new ColorChooserListener(pionP3, Constants.PLAYER_THREE));
JPanel panelP1 = new JPanel(new GridBagLayout());
JPanel panelP2 = new JPanel(new GridBagLayout());
JPanel panelP3 = new JPanel(new GridBagLayout());
JLabel labelP1 = new JLabel("Nom et couleur du joueur 1");
JLabel labelP2 = new JLabel("Nom et couleur du joueur 2");
JLabel labelP3 = new JLabel("Nom et couleur du joueur 2");
this.field1 = new JTextField("Joueur 1");
this.field2 = new JTextField("Joueur 2");
this.field3 = new JTextField("Joueur 3");
gbc.gridx = 0;
gbc.gridy = 0;
@ -127,8 +142,33 @@ public class ColorChooserDialog extends JDialog{
gbc.gridy = 0;
this.add(panelP2,gbc);
if(playerNumber == 3) {
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weighty = 0;
panelP3.add(labelP3, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
panelP3.add(field3, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.weighty = 0.5;
panelP3.add(playerThreeChooser, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 2;
gbc.insets = new Insets(5, 5, 5, 5);
this.add(panelP3,gbc);
}
gbc.gridx = 1;
gbc.gridy = 1;
if(playerNumber == 3)
gbc.gridy = 2;
else
gbc.gridy = 1;
gbc.weighty = 0;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.EAST;
@ -209,6 +249,14 @@ public class ColorChooserDialog extends JDialog{
return this.field2.getText();
}
/**
* Permet de récupérer le nom inscrit pour le joueur 3
* @return un {@code String} correspondant au nom du joueur 3
*/
public String getPlayerThreeName() {
return this.field3.getText();
}
/**
* Permet de confirmer la choix de l'utilisateur

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
@ -94,8 +106,11 @@ public class Grille extends JPanel{
case Constants.PLAYER_TWO :
c = new Color(Constants.PLAYER_TWO_COLOR.getRed(), Constants.PLAYER_TWO_COLOR.getGreen(), Constants.PLAYER_TWO_COLOR.getBlue(), 100);
break;
case Constants.PLAYER_THREE :
c = new Color(Constants.PLAYER_THREE_COLOR.getRed(), Constants.PLAYER_THREE_COLOR.getGreen(), Constants.PLAYER_THREE_COLOR.getBlue(), 100);
break;
default :
c = Color.BLACK;
c = Color.BLACK;
}
Pion.setHighlightColor(c);
}
@ -134,4 +149,8 @@ public class Grille extends JPanel{
}
}
}
public void removePlayerPion(int Joueur){
}
}

View File

@ -88,6 +88,9 @@ public class Pion extends JComponent{
case Constants.PLAYER_TWO :
c = Constants.PLAYER_TWO_COLOR;
break;
case Constants.PLAYER_THREE :
c = Constants.PLAYER_THREE_COLOR;
break;
default :
c = Constants.EMPTY_COLOR;
break;

View File

@ -20,6 +20,8 @@ public class Puissance4Panel extends JPanel{
private Grille grille;
private String playerOneName;
private String playerTwoName;
private String playerThreeName;
private int playerNumber;
/**
* Crée un panneau avec la grille
@ -156,7 +158,9 @@ public class Puissance4Panel extends JPanel{
case Constants.PLAYER_TWO:
this.playerTurn.setText("Tour de : " + this.playerTwoName);
break;
case Constants.PLAYER_THREE:
this.playerTurn.setText("Tour de : " + this.playerThreeName);
break;
default:
this.playerTurn.setText("JOUEUR INCONNU");
break;
@ -169,7 +173,17 @@ public class Puissance4Panel extends JPanel{
* @param playerTwoScore le score du joueur 2
*/
public void changeScore(int playerOneScore, int playerTwoScore) {
this.score.setText(this.playerOneName + " : " + playerOneScore + " - " + playerTwoScore + " : " + this.playerTwoName);
this.score.setText(this.playerOneName + " : " + playerOneScore + " | " + this.playerTwoName + " : " + playerTwoScore);
}
/**
* Change le texte affichant le score des joueurs
* @param playerOneScore le score du joueur 1
* @param playerTwoScore le score du joueur 2
* @param playerThreeScore le score du joueur 3
*/
public void changeScore(int playerOneScore, int playerTwoScore, int playerThreeScore) {
this.score.setText(this.playerOneName + " : " + playerOneScore + " | " + this.playerTwoName + " : " + playerTwoScore + " | " + this.playerThreeName + " : " + playerThreeScore);
}
/**
@ -189,11 +203,18 @@ public class Puissance4Panel extends JPanel{
* @param playerOneScore le score du joueur 1
* @param playerTwoScore le score du joueur 2
*/
public void setGameStatus(GameStatus status, int playerTurn, int playerOneScore, int playerTwoScore) {
public void setGameStatus(GameStatus status, int playerTurn, int playerOneScore, int playerTwoScore, int playerThreeScore) {
this.grille.hover(-1);
switch(status){
case WIN:
this.playerTurn.setText("Victoire de : " + (playerTurn == Constants.PLAYER_ONE ? this.playerOneName : this.playerTwoName));
case WIN:
String gagnant = null;
if(playerTurn == Constants.PLAYER_ONE)
gagnant = this.playerOneName;
else if(playerTurn == Constants.PLAYER_TWO)
gagnant = this.playerTwoName;
else
gagnant = this.playerThreeName;
this.playerTurn.setText("Victoire de : " + gagnant);
break;
case DRAW:
@ -204,7 +225,15 @@ public class Puissance4Panel extends JPanel{
this.changeLabel(playerTurn);
break;
}
this.changeScore(playerOneScore, playerTwoScore);
if(this.playerNumber == 2)
this.changeScore(playerOneScore, playerTwoScore);
else
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é");
}
/**
@ -223,6 +252,18 @@ public class Puissance4Panel extends JPanel{
this.playerTwoName = playerTwoName;
}
/**
* Permet de définir le nom du joueur 3
* @param playerTwoName le nom du joueur
*/
public void setPlayerThreeName(String playerThreeName) {
this.playerThreeName = playerThreeName;
}
public void setPlayerNumber(int playerNumber) {
this.playerNumber = playerNumber;
}
/**
* Retourne la grille du Puissance 4
* @return La grille du Puissance 4