Ça marche pour de vrai cette foi ^^'

master
Allan POINT 4 years ago
parent d0b01a9de0
commit 72cea8d78f

@ -38,6 +38,10 @@ public class CellulesVivantes implements ObserverCellule {
} }
public CellulesVivantes clone(){ public CellulesVivantes clone(){
return new CellulesVivantes(cellVivantes); return new CellulesVivantes(new HashMap<>(cellVivantes));
}
public void reset(){
cellVivantes = new HashMap<>();
} }
} }

@ -3,7 +3,6 @@ package model;
import model.actualiseur.ActualiseurCellule; import model.actualiseur.ActualiseurCellule;
import model.actualiseur.ActualiseurTour; import model.actualiseur.ActualiseurTour;
import model.actualiseur.ActualiseurTourUnParUn; import model.actualiseur.ActualiseurTourUnParUn;
import model.boucleDeJeu.BoucleDeJeu30FPS;
import model.boucleDeJeu.BoucleDeJeu5FPS; import model.boucleDeJeu.BoucleDeJeu5FPS;
import model.boucleDeJeu.IBoucleDeJeu; import model.boucleDeJeu.IBoucleDeJeu;
import model.boucleDeJeu.observer.ObservableBDJ; import model.boucleDeJeu.observer.ObservableBDJ;
@ -42,11 +41,13 @@ public class Manager implements ObserverBDJ {
} }
private void deleguerChangementCellule() { private void deleguerChangementCellule() {
for (int y=0; y<actualiseurCellule.getArbitre().getPlateau().getLargeur(); ++y){ CellulesVivantes reference = getActualiseurCellule().getArbitre().getPlateau().getCellulesVivantes().clone();
for(int x=0; x<actualiseurCellule.getArbitre().getPlateau().getLongueur(); ++x){ for (int y = 0; y<actualiseurCellule.getArbitre().getPlateau().getLigne(); ++y){
actualiseurCellule.changerCellule(x, y); for(int x = 0; x<actualiseurCellule.getArbitre().getPlateau().getColone(); ++x){
actualiseurCellule.changerCellule(x, y, reference);
} }
} }
} }
public void inverserEtatCellule(Cellule c){ public void inverserEtatCellule(Cellule c){
getActualiseurCellule().getArbitre().getPlateau().getCell(c.getPosition().getX(), c.getPosition().getY()).inverseAlive(); getActualiseurCellule().getArbitre().getPlateau().getCell(c.getPosition().getX(), c.getPosition().getY()).inverseAlive();
@ -63,6 +64,7 @@ public class Manager implements ObserverBDJ {
public void stoperJeu(){ public void stoperJeu(){
actualiseurTour.resetTour(); actualiseurTour.resetTour();
actualiseurCellule.getArbitre().getPlateau().getCellulesVivantes().reset();
jeuLance = false; jeuLance = false;
} }

@ -1,11 +1,12 @@
package model.actualiseur; package model.actualiseur;
import model.CellulesVivantes;
import model.arbitre.Arbitre; import model.arbitre.Arbitre;
import model.plateau.Plateau; import model.plateau.Plateau;
public abstract class ActualiseurCellule { public abstract class ActualiseurCellule {
private Arbitre arbitre; private Arbitre arbitre;
public abstract void changerCellule(int x, int y); public abstract void changerCellule(int x, int y, CellulesVivantes reference);
ActualiseurCellule(Arbitre arbitre) throws IllegalArgumentException{ ActualiseurCellule(Arbitre arbitre) throws IllegalArgumentException{
if(arbitre == null) { if(arbitre == null) {

@ -1,5 +1,6 @@
package model.actualiseur; package model.actualiseur;
import model.CellulesVivantes;
import model.arbitre.Arbitre; import model.arbitre.Arbitre;
public class ActualiseurEtatCellule extends ActualiseurCellule{ public class ActualiseurEtatCellule extends ActualiseurCellule{
@ -7,8 +8,8 @@ public class ActualiseurEtatCellule extends ActualiseurCellule{
super(a); super(a);
} }
@Override @Override
public void changerCellule(int x, int y) { public void changerCellule(int x, int y, CellulesVivantes reference) {
switch(getArbitre().verifierChangementCellules(x, y)) { switch(getArbitre().verifierChangementCellules(x, y, reference)) {
case DIE -> getArbitre().getPlateau().getCell(x, y).setAlive(false); case DIE -> getArbitre().getPlateau().getCell(x, y).setAlive(false);
case LIVE, BIRTH -> getArbitre().getPlateau().getCell(x, y).setAlive(true); case LIVE, BIRTH -> getArbitre().getPlateau().getCell(x, y).setAlive(true);
} }

@ -21,5 +21,5 @@ public abstract class Arbitre {
protected CompteurDeCellule getCompteurCell(){ protected CompteurDeCellule getCompteurCell(){
return compteurCell; return compteurCell;
} }
public abstract CellState verifierChangementCellules(int x, int y); public abstract CellState verifierChangementCellules(int x, int y, CellulesVivantes reference);
} }

@ -12,14 +12,14 @@ public class ArbitreConwayStyle extends Arbitre{
} }
@Override @Override
public CellState verifierChangementCellules(int x, int y) { public CellState verifierChangementCellules(int x, int y, CellulesVivantes reference) {
if(verifierNaissance(x, y, getPlateau().getCellulesVivantes().clone())) { if(verifierNaissance(x, y, reference)) {
return CellState.BIRTH; return CellState.BIRTH;
} }
if(verifierMort(x, y, getPlateau().getCellulesVivantes())) { if(verifierMort(x, y, reference)) {
return CellState.DIE; return CellState.DIE;
} }
return getPlateau().getCell(x, y).isAlive() ? CellState.LIVE : CellState.DIE; return reference.getAt(x, y) != null ? CellState.LIVE : CellState.DIE;
} }
private boolean verifierNaissance(int x, int y, CellulesVivantes reference) { private boolean verifierNaissance(int x, int y, CellulesVivantes reference) {

@ -15,7 +15,7 @@ public class CreateurCellule implements ICreateurCellule {
private int h; private int h;
public CreateurCellule(int w, int h) throws IllegalArgumentException{ public CreateurCellule(int w, int h) throws IllegalArgumentException{
if(w < 0 || h<0){ if(w<0 || h<0){
throw new IllegalArgumentException("La longueur et la largeur doivent être supperieur à 0"); throw new IllegalArgumentException("La longueur et la largeur doivent être supperieur à 0");
} }
this.w = w; this.w = w;
@ -25,14 +25,14 @@ public class CreateurCellule implements ICreateurCellule {
return creerCellules(w, h, observer); return creerCellules(w, h, observer);
} }
public ListProperty<List<Cellule>> creerCellules(int w, int h, CellulesVivantes observer){ public ListProperty<List<Cellule>> creerCellules(int colone, int ligne, CellulesVivantes observer){
ObservableList<List<Cellule>> cellsInit = FXCollections.observableArrayList(); ObservableList<List<Cellule>> cellsInit = FXCollections.observableArrayList();
ListProperty<List<Cellule>> cells = new SimpleListProperty<>(cellsInit); ListProperty<List<Cellule>> cells = new SimpleListProperty<>(cellsInit);
List<Cellule> tmp; List<Cellule> tmp;
Cellule c; Cellule c;
for (int i = 0; i < h; i++) { for (int i = 0; i < ligne; i++) {
tmp = new LinkedList<>(); tmp = new LinkedList<>();
for (int j = 0; j < w; j++) { for (int j = 0; j < colone; j++) {
c = new Cellule(j, i); c = new Cellule(j, i);
c.attacher(observer); c.attacher(observer);
tmp.add(c); tmp.add(c);
@ -41,10 +41,10 @@ public class CreateurCellule implements ICreateurCellule {
} }
return cells; return cells;
} }
public List<Cellule> creerLigneCellule(int longeure){ public List<Cellule> creerLigneCellule(int ligne){
List<Cellule> cells = new LinkedList<>(); List<Cellule> cells = new LinkedList<>();
for(int i=0; i<longeure; ++i){ for(int i=0; i<ligne; ++i){
cells.add(new Cellule(i, longeure)); cells.add(new Cellule(i, ligne));
} }
++h; ++h;
return cells; return cells;

@ -9,15 +9,15 @@ import java.util.List;
public class Plateau implements PrototypePlateau{ public class Plateau implements PrototypePlateau{
private CreateurCellule createurCellule; private CreateurCellule createurCellule;
private IntegerProperty longueur = new SimpleIntegerProperty(); private IntegerProperty colone = new SimpleIntegerProperty();
public int getLongueur() { return longueur.get();} public int getColone() { return colone.get();}
public void setLongueur(int valeur) { longueur.set(valeur); resetGrille(getLargeur(), valeur);} public void setColone(int valeur) { colone.set(valeur); resetGrille(valeur, getLigne());}
public IntegerProperty longueurProperty() { return longueur; } public IntegerProperty coloneProperty() { return colone; }
private IntegerProperty largeur = new SimpleIntegerProperty(); private IntegerProperty ligne = new SimpleIntegerProperty();
public int getLargeur() { return largeur.get(); } public int getLigne() { return ligne.get(); }
public void setLargeur( int valeur ) { largeur.set(valeur); resetGrille(valeur, getLongueur());} public void setLigne(int valeur ) { ligne.set(valeur); resetGrille(getColone(), valeur);}
public IntegerProperty largeurProperty() { return largeur; } public IntegerProperty ligneProperty() { return ligne; }
//private ObservableList<List<Cellule>> grilleObs = FXCollections.observableArrayList(); //private ObservableList<List<Cellule>> grilleObs = FXCollections.observableArrayList();
private ListProperty<List<Cellule>> grille = new SimpleListProperty<>(); private ListProperty<List<Cellule>> grille = new SimpleListProperty<>();
@ -40,10 +40,10 @@ public class Plateau implements PrototypePlateau{
} }
public void resetGrille(){ public void resetGrille(){
resetGrille(getLargeur(), getLongueur()); resetGrille(getColone(), getLigne());
} }
public void resetGrille(int w, int h){ public void resetGrille(int colone, int ligne){
setGrille(createurCellule.creerCellules(w, h, cellulesVivantes)); setGrille(createurCellule.creerCellules(colone, ligne, cellulesVivantes));
} }
public Plateau(){ public Plateau(){
@ -51,26 +51,26 @@ public class Plateau implements PrototypePlateau{
cellulesVivantes = new CellulesVivantes(); cellulesVivantes = new CellulesVivantes();
setGrille(new SimpleListProperty<>()); setGrille(new SimpleListProperty<>());
} }
public Plateau(int longueur, int largeur) { public Plateau(int colone, int ligne) {
this(longueur, largeur, new CellulesVivantes()); this(colone, ligne, new CellulesVivantes());
} }
public Plateau(int longueur, int largeur, CellulesVivantes observer) { public Plateau(int colone, int ligne, CellulesVivantes observer) {
createurCellule = new CreateurCellule(longueur, largeur); createurCellule = new CreateurCellule(colone, ligne);
setLargeur(largeur); setLigne(ligne);
setLongueur(longueur); setColone(colone);
cellulesVivantes = observer; cellulesVivantes = observer;
setGrille(createurCellule.creerCellules(cellulesVivantes)); setGrille(createurCellule.creerCellules(cellulesVivantes));
} }
public Plateau(int longueur, int largeur, ListProperty<List<Cellule>> cellules) public Plateau(int colone, int ligne, ListProperty<List<Cellule>> cellules)
{ {
this(longueur, largeur); this(colone, ligne);
setGrille(cellules); setGrille(cellules);
} }
@Override @Override
public Plateau cloner() { public Plateau cloner() {
return new Plateau(getLongueur(), getLargeur(), getGrille()); return new Plateau(getColone(), getLigne(), getGrille());
} }
public CellulesVivantes getCellulesVivantes() { public CellulesVivantes getCellulesVivantes() {

@ -2,14 +2,11 @@ package views;
import javafx.beans.property.Property; import javafx.beans.property.Property;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.ColorPicker; import javafx.scene.control.ColorPicker;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.control.Spinner; import javafx.scene.control.Spinner;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane; import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle; import javafx.scene.shape.Rectangle;
@ -51,34 +48,34 @@ public class VueJeu {
public void createGrid() { public void createGrid() {
map.getChildren().clear(); map.getChildren().clear();
for(int i=0; i < colGame.getValue().intValue(); ++i) { for(int i=0; i < rowGame.getValue().intValue(); ++i) {
for(int j=0; j < rowGame.getValue().intValue(); ++j) { for(int j=0; j < colGame.getValue().intValue(); ++j) {
Rectangle rect = new Rectangle(15, 15, color.getValue()); Rectangle rect = new Rectangle(15, 15, color.getValue());
Cellule c = manager.getActualiseurCellule().getArbitre().getPlateau().getCell(j, i); Cellule c = manager.getActualiseurCellule().getArbitre().getPlateau().getCell(j, i);
rect.fillProperty().bindBidirectional(c.activeColorProperty()); rect.fillProperty().bindBidirectional(c.activeColorProperty());
rect.setOnMouseClicked((src)->manager.inverserEtatCellule(c)); rect.setOnMouseClicked((src)->manager.inverserEtatCellule(c));
map.add(rect, i, j); map.add(rect, j, i);
} }
} }
} }
public void generateraRandom() { public void generateraRandom() {
resetGrid(); resetGrid();
int largeur = manager.getActualiseurCellule().getArbitre().getPlateau().getLargeur(); int ligne = manager.getActualiseurCellule().getArbitre().getPlateau().getLigne();
int longueur = manager.getActualiseurCellule().getArbitre().getPlateau().getLongueur(); int colone = manager.getActualiseurCellule().getArbitre().getPlateau().getColone();
for(int i=0; i<(longueur+largeur)/2; ++i) { for(int i=0; i<(colone+ligne)/2; ++i) {
Random random = new Random(); Random random = new Random();
manager.getActualiseurCellule().getArbitre().getPlateau().getCell(random.nextInt(longueur), random.nextInt(largeur)).setAlive(true); manager.getActualiseurCellule().getArbitre().getPlateau().getCell(random.nextInt(colone), random.nextInt(ligne)).setAlive(true);
} }
} }
public void initialize() { public void initialize() {
manager = new Manager(); manager = new Manager();
deathColor = Color.BLACK; deathColor = Color.BLACK;
rowGame.getValueFactory().valueProperty().bindBidirectional((Property) manager.getActualiseurCellule().getArbitre().getPlateau().largeurProperty()); rowGame.getValueFactory().valueProperty().bindBidirectional((Property) manager.getActualiseurCellule().getArbitre().getPlateau().ligneProperty());
colGame.getValueFactory().valueProperty().bindBidirectional((Property) manager.getActualiseurCellule().getArbitre().getPlateau().longueurProperty()); colGame.getValueFactory().valueProperty().bindBidirectional((Property) manager.getActualiseurCellule().getArbitre().getPlateau().coloneProperty());
manager.getActualiseurCellule().getArbitre().getPlateau().longueurProperty().addListener((src)->resetGrid()); manager.getActualiseurCellule().getArbitre().getPlateau().coloneProperty().addListener((src)->resetGrid());
manager.getActualiseurCellule().getArbitre().getPlateau().largeurProperty().addListener((src)->resetGrid()); manager.getActualiseurCellule().getArbitre().getPlateau().ligneProperty().addListener((src)->resetGrid());
colGame.getValueFactory().setValue(10); colGame.getValueFactory().setValue(10);
rowGame.getValueFactory().setValue(10); rowGame.getValueFactory().setValue(10);
@ -87,8 +84,8 @@ public class VueJeu {
nbColGame.setText(colGame.getValue().toString()); nbColGame.setText(colGame.getValue().toString());
nbRowGame.setText(rowGame.getValue().toString()); nbRowGame.setText(rowGame.getValue().toString());
numTour.textProperty().bind(((ActualiseurTourUnParUn)manager.getActualiseurTour()).cptTourProperty().asString()); numTour.textProperty().bind(((ActualiseurTourUnParUn)manager.getActualiseurTour()).cptTourProperty().asString());
nbColGame.textProperty().bind(manager.getActualiseurCellule().getArbitre().getPlateau().longueurProperty().asString()); nbColGame.textProperty().bind(manager.getActualiseurCellule().getArbitre().getPlateau().coloneProperty().asString());
nbRowGame.textProperty().bind(manager.getActualiseurCellule().getArbitre().getPlateau().largeurProperty().asString()); nbRowGame.textProperty().bind(manager.getActualiseurCellule().getArbitre().getPlateau().ligneProperty().asString());
} }
public void startGame(ActionEvent actionEvent) { public void startGame(ActionEvent actionEvent) {

Loading…
Cancel
Save