CODE FINAL

CODE SOURCE mastermind.pde
// Need G4P library
import g4p_controls.*;
import java.awt.Font;
// Variables
int nbBallsByTurn; // Nombre de balles maximum par tour
int turnBallsCount; // Nombre de balles dans le tour actuel
int turnMax; // Nombre de tours maximum
int turnId; // Numéro du tour actuel
TurnInfos infos; // infos contenant le nombre de balles de bonne couleur bien/mal placées
boolean restart; // Permet de vérifier si le jeu doit recommencer
color[] colors; // Liste des couleurs possible
Combination combination; // Combinaison du joueur (liste de couleurs)
Combination combinationWin; // Combinaison gagnante (liste de couleurs)
/*
** Fonction renvoyant vrai si la combinaison du joueur est gagnante, faux sinon
*/
boolean isVictory() {
return (combination.equals(combinationWin));
}
public void setup(){
 
size(900, 900, JAVA2D);
background(backgroundColor);
createGUI();
customGUI();
// Règles
nbBallsByTurn = 4; // Nombre de balles a trouver
turnMax = 10; // Nombre de tours
colors = new color[]{blue, yellow, cyan, green, red, gold, violet, orange};
// Initialisation
// Balle
x = 50;
y = 150;
radius = 30;
// Jeu
turnId = 1;
turnBallsCount = 0;
infos = new TurnInfos();
restart = false;
combinationWin = new Combination(4);
combination = new Combination(4);
combinationWin.computeRandomCombination();
drawNewTurn(); // Affiche 4 premieres cases vides à remplir
}
 
public void draw(){
}
// Use this method to add additional statements
// to customise the GUI controls
public void customGUI(){
} ball.pde
// Couleurs
color blue = color(51,0,221);
color yellow = color(255,255,51);
color cyan = color(0,255,255);
color green = color(2,187,6);
color red = color(255,51,0);
color gold = color(239,216,7);
color violet = color(153,51,204);
color orange = color(255,153,51);
color backgroundColor = color(245, 242, 255);
// Propriétés
int x; // Position en x
int y; // Position en y
int radius; // Taille du rayon combination.pde
class Combination {
color[] combination; // Combinaison: liste de couleurs
Combination(int length) {
combination = new color[length];
}
 
color[] getCombination() {
return combination;
}
void setCombination(color[] combination) {
this.combination = combination;
}
/*
** Calcule une combinaison aléatoire
*/
void computeRandomCombination() {
for (int i = 0; i < combination.length; ++i) {
combination[i] = colors[(int) random(0, colors.length - 1)]; //Ajoute à l'ième élément de la combinaison la nième couleur de la liste de couleurs (n étant un entier aléatoire entre 0 et la taille de cette liste - 1)
}
}
/*
** Fonction de comparaison de le combinaison actuelle avec la combinaison c
*/
boolean equals(Combination c) {
for (int i = 0; i < nbBallsByTurn; ++i) {
if (combination[i] != c.getCombination()[i]) //Couleur différente
return false;
}
return true;
}
 
/*
** Affiche la combinaison (couleurs RGB)
*/
void print() {
for (int i = 0; i < combination.length; ++i)
println("Red:" + red(combination[i]) +" Green:" + green(combination[i]) + " Blue:" + blue(combination[i]));
}
} turn_infos.pde
class TurnInfos {
int nbGoodColors; // Nombre de balles de bonne couleur bien placées
int nbWrongColors; // Nombre de balles de bonne couleur mal placées
TurnInfos() {
nbGoodColors = 0;
nbWrongColors = 0;
}
int getNbGoodColors() {
return nbGoodColors;
}
int getNbWrongColors() {
return nbWrongColors;
}
/*
 
** Fonction indiquant si la combinaison contient la couleur c qui ne se situe pas à l'index i (bonne couleur mal placée)
*/
boolean isMisplaced(int index, color c) {
for (int i = 0; i < combinationWin.getCombination().length; ++i)
if (i != index && combinationWin.getCombination()[i] == c)
return true;
return false;
}
/*
** Fonction mettant à jour le nombre de bonnes couleurs bien/mal placées à la fin d'un tour
*/
void computeTurnInfos() {
nbGoodColors = 0;
nbWrongColors = 0;
for (int i = 0; i < combination.getCombination().length; ++i) {
if (combination.getCombination()[i] == combinationWin.getCombination()[i]) //Bonne couleur bien placée
++nbGoodColors;
else if (isMisplaced(i, combination.getCombination()[i])) //Bonne couleur mal placée
++nbWrongColors;
}
}
} gui.pde
void myBtnEvents(GButton button) { //_CODE_:button1:12356:
// It is safe to enter your event code here
} //_CODE_:button1:12356:
 
/*
** Dessine une balle de la couleur c
** Met à jour la position pour la balle suivante
** Met à jour la combinaison du joueur
*/
void drawBall(color c) {
fill(c);
ellipse(x,y,radius,radius);
x = x + 50;
combination.getCombination()[turnBallsCount++] = c;
}
/*
** Supprime la derniere balle placée
** Met à jour la position à la balle précedente
*/
void removeBall() {
x -= 50;
fill(backgroundColor); // On remplit avec la couleur du fond
ellipse(x,y,radius,radius);
--turnBallsCount;
}
/*
** Affiche 4 cercles vides, qui seront remplies par le joueur
*/
void drawNewTurn() {
noFill();
 
for (int i = 0; i < nbBallsByTurn; ++i)
ellipse(x + i*50, y, radius,radius);
}
/*
** Affiche le nombre de balles bien/mal placées
** Met à jour le numéro du tour et la position de la balle à la ligne suivante
** Remise à zero de la combinaison du joueur
*/
void endTurn() {
textSize(15);
fill(0);
text("Blanches: " + infos.getNbGoodColors() + ", Noires: " + infos.getNbWrongColors(), x, y + 7);
y=y+50;
x=50;
++turnId;
drawNewTurn();
// Remise à zéro
turnBallsCount = 0;
combination = new Combination(4);
}
public void buttonValidate_click1(GButton source, GEvent event) { //_CODE_:button1:967742:
if (restart) { // On a cliqué sur le bouton recommencer
setup(); // Remise à zéro du jeu
return;
 
}
if (turnBallsCount < nbBallsByTurn) // Empeche le joueur de valider tant que la combinaison n'est pas complete
return;
if (isVictory()) { // Cas d'un victoire du joueur
textSize(30);
fill(green);
text("Victoire !", 600, 450);
restart = true;
buttonValidate.setText("Recommencer");
return;
}
else if (turnId == turnMax) { // Cas d'une défaite du joueur
textSize(30);
fill(red);
text("Défaite !", 600, 450);
restart = true;
buttonValidate.setText("Recommencer");
return;
}
// On calcule le nombre de bien/mal placées
infos.computeTurnInfos();
// Mises à jour à la fin du tour
endTurn();
} //_CODE_:button1:967742:
 
public void buttonRemove_click1(GButton source, GEvent event) { //_CODE_:button2:587044:
removeBall();
} //_CODE_:button2:587044:
public void buttonBlue_click1(GButton source, GEvent event) { //_CODE_:button2:587044:
if (turnBallsCount < nbBallsByTurn)
drawBall(blue);
} //_CODE_:button2:587044:
public void buttonYellow_click1(GButton source, GEvent event) { //_CODE_:button3:311723:
if (turnBallsCount < nbBallsByTurn)
drawBall(yellow);
} //_CODE_:button3:311723:
public void buttonCyan_click1(GButton source, GEvent event) { //_CODE_:button4:779461:
if (turnBallsCount < nbBallsByTurn)
drawBall(cyan);
} //_CODE_:button4:779461:
public void buttonGreen_click1(GButton source, GEvent event) { //_CODE_:button5:398508:
if (turnBallsCount < nbBallsByTurn)
drawBall(green);
} //_CODE_:button5:398508:
public void buttonRed_click1(GButton source, GEvent event) { //_CODE_:button6:356184:
if (turnBallsCount < nbBallsByTurn)
drawBall(red);
 
} //_CODE_:button6:356184:
public void buttonGold_click1(GButton source, GEvent event) { //_CODE_:button7:272583:
if (turnBallsCount < nbBallsByTurn)
drawBall(gold);
} //_CODE_:button7:272583:
public void buttonViolet_click1(GButton source, GEvent event) { //_CODE_:button8:445442:
if (turnBallsCount < nbBallsByTurn)
drawBall(violet);
} //_CODE_:button8:445442:
public void buttonOrange_click1(GButton source, GEvent event) { //_CODE_:button9:781116:
if (turnBallsCount < nbBallsByTurn)
drawBall(orange);
} //_CODE_:button9:781116:
// Create all the GUI controls.
// autogenerated do not edit
public void createGUI(){
G4P.messagesEnabled(false);
G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
G4P.setCursor(ARROW);
if(frame != null)
frame.setTitle("Sketch Window");
 
buttonValidate = new GButton(this, 480, 800, 140, 60);
buttonValidate.setText("Valider");
buttonValidate.setTextItalic();
buttonValidate.setLocalColorScheme(GCScheme.PURPLE_SCHEME);
buttonValidate.addEventHandler(this, "buttonValidate_click1");
buttonRemove = new GButton(this, 640, 800, 70, 60);
buttonRemove.setText("Supprimer");
buttonRemove.setTextItalic();
buttonRemove.setLocalColorScheme(GCScheme.RED_SCHEME);
buttonRemove.addEventHandler(this, "buttonRemove_click1");
title = new GLabel(this, 260, 0, 340, 80);
title.setFont(new Font("Mickey", Font.PLAIN, 24));
title.setText("Mastermind");
title.setOpaque(false);
infosBW = new GLabel(this, 580, 650, 200, 80);
infosBW.setText("Blanche: bonne couleur bien placée\nNoire: bonne couleur mal placée");
infosBW.setTextAlign(GAlign.LEFT, GAlign.MIDDLE);
button2 = new GButton(this, 16, 745, 28, 25);
button2.setText("bleu");
button2.addEventHandler(this, "buttonBlue_click1");
button3 = new GButton(this, 58, 744, 28, 25);
button3.setText("jaune");
button3.setLocalColorScheme(GCScheme.YELLOW_SCHEME);
button3.addEventHandler(this, "buttonYellow_click1");
 
button4 = new GButton(this, 102, 744, 28, 25);
button4.setText("cyan");
button4.setLocalColorScheme(GCScheme.CYAN_SCHEME);
button4.addEventHandler(this, "buttonCyan_click1");
button5 = new GButton(this, 144, 745, 28, 25);
button5.setText("vert");
button5.setLocalColorScheme(GCScheme.GREEN_SCHEME);
button5.addEventHandler(this, "buttonGreen_click1");
button6 = new GButton(this, 59, 783, 28, 25);
button6.setText("or");
button6.setLocalColorScheme(GCScheme.GOLD_SCHEME);
button6.addEventHandler(this, "buttonGold_click1");
button7 = new GButton(this, 15, 783, 28, 25);
button7.setText("rouge");
button7.setLocalColorScheme(GCScheme.RED_SCHEME);
button7.addEventHandler(this, "buttonRed_click1");
button8 = new GButton(this, 101, 784, 28, 25);
button8.setText("violet");
button8.setLocalColorScheme(GCScheme.PURPLE_SCHEME);
button8.addEventHandler(this, "buttonViolet_click1");
button9 = new GButton(this, 142, 784, 28, 25);
button9.setText("orange");
button9.setLocalColorScheme(GCScheme.ORANGE_SCHEME);
button9.addEventHandler(this, "buttonOrange_click1");
}
// Variable declarations
// autogenerated do not edit
GButton buttonValidate;
 
GButton buttonRemove;
GLabel title;
GLabel infosBW;
GButton button2;
GButton button3;
GButton button4;
GButton button5;
GButton button6;
GButton button7;
GButton button8;
GButton button9;

Contact

Mastermind-VincianeLilia vincianelilia@gmail.com