Ajout des classes CellulesVivantes, CompteurCellule, ObservableCellule et Position

master
Allan POINT 4 years ago
parent 5376edb37a
commit c098ee634e

@ -2,12 +2,21 @@ package model;
public abstract class Arbitre {
Regles regle;
Plateau plateau;
private Plateau plateau;
private CompteurDeCellule compteurCell;
private CellulesVivantes cellulesVivantes;
public Arbitre(Plateau plateau, Regles regle) {
this.regle = regle;
public Arbitre(Plateau plateau) {
this.plateau = plateau;
cellulesVivantes = new CellulesVivantes();
compteurCell = new CompteurDeCellule();
}
public Plateau getPlateau(){
return plateau;
}
protected CompteurDeCellule getCompteurCell(){
return getCompteurCell();
}
public abstract CellState VerifierChangementCellules(int x, int y);
}

@ -2,8 +2,8 @@ package model;
public class ArbitreConwayStyle extends Arbitre{
public ArbitreConwayStyle(Plateau plateau, Regles regle) {
super(plateau, regle);
public ArbitreConwayStyle(Plateau plateau) {
super(plateau);
}
@Override
@ -14,60 +14,22 @@ public class ArbitreConwayStyle extends Arbitre{
if(verifierMort(x, y)) {
return CellState.DIE;
}
return plateau.getCell(x, y).estAlive() ? CellState.LIVE : CellState.DIE;
return getPlateau().getCell(x, y).estAlive() ? CellState.LIVE : CellState.DIE;
}
private boolean verifierNaissance(int x, int y) {
int cpt = compterNbCellAutour(x, y);
if(cpt == 3 && !plateau.getCell(x, y).estAlive()) {
return true;
int cpt = getCompteurCell().compteNombreCellulesAutour(x, y, getPlateau());
if(cpt == 3 && !getPlateau().getCell(x, y).estAlive()) {
return true;
}
return false;
}
private boolean verifierMort(int x, int y) {
int cpt = compterNbCellAutour(x, y);
if(!(cpt == 2 || cpt == 3) && plateau.getCell(x, y).estAlive()) {
return true;
int cpt = getCompteurCell().compteNombreCellulesAutour(x, y, getPlateau());
if(!(cpt == 2 || cpt == 3) && getPlateau().getCell(x, y).estAlive()) {
return true;
}
return false;
}
private int compterNbCellAutour(int x, int y)
{
int cpt = 0;
if(plateau.getCell(x-1, y-1).estAlive())
{
++cpt;
}
if(plateau.getCell(x, y-1).estAlive())
{
++cpt;
}
if(plateau.getCell(x+1, y-1).estAlive())
{
++cpt;
}
if(plateau.getCell(x+1, y).estAlive())
{
++cpt;
}
if(plateau.getCell(x+1, y+1).estAlive())
{
++cpt;
}
if(plateau.getCell(x, y+1).estAlive())
{
++cpt;
}
if(plateau.getCell(x-1, y+1).estAlive())
{
++cpt;
}
if(plateau.getCell(x-1, y).estAlive())
{
++cpt;
}
return cpt;
}
}

@ -3,7 +3,7 @@ package model;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ReadOnlyBooleanProperty;
public class Cellule {
public class Cellule extends ObservableCellule {
private int x;
private int y;
private BooleanProperty alive;
@ -14,8 +14,11 @@ public class Cellule {
}
public Boolean estAlive() { return alive.get(); }
public void setAlive(Boolean alive) { this.alive.set(alive); }
public ReadOnlyBooleanProperty aliveProperty() { return alive; }
public void setAlive(Boolean alive) {
notifier();
this.alive.set(alive);
}
public int getX() { return x; }
public void setX(int valeur) throws IllegalArgumentException{

@ -0,0 +1,17 @@
package model;
import java.util.HashMap;
public class CellulesVivantes {
private HashMap<Position, Cellule> cellVivantes;
public Cellule getAt(int x, int y){
Position p = new Position(x, y);
return cellVivantes.get(p);
}
public void addPeer(int x, int y, Cellule cell){
Position p = new Position(x, y);
cellVivantes.put(p, cell);
}
}

@ -0,0 +1,59 @@
package model;
public class CompteurDeCellule {
public int compteNombreCellulesAutour(int x, int y, Plateau plateau){
int cpt = 0;
try {
if(plateau.getCell(x-1, y-1).estAlive())
{
++cpt;
}
}catch (IllegalArgumentException e){}
try {
if (plateau.getCell(x, y - 1).estAlive()) {
++cpt;
}
}catch (IllegalArgumentException e){}
try {
if(plateau.getCell(x+1, y-1).estAlive())
{
++cpt;
}
}catch (IllegalArgumentException e){}
try {
if(plateau.getCell(x+1, y).estAlive())
{
++cpt;
}
}catch (IllegalArgumentException e){}
try {
if (plateau.getCell(x + 1, y + 1).estAlive()) {
++cpt;
}
}catch (IllegalArgumentException e){}
try {
if (plateau.getCell(x, y + 1).estAlive()) {
++cpt;
}
}catch (IllegalArgumentException e) {}
try {
if (plateau.getCell(x - 1, y + 1).estAlive()) {
++cpt;
}
}catch (IllegalArgumentException e){}
try {
if (plateau.getCell(x - 1, y).estAlive()) {
++cpt;
}
}catch (IllegalArgumentException e){}
return cpt;
}
}

@ -0,0 +1,30 @@
package model;
import java.util.Collection;
import java.util.TreeSet;
public abstract class ObservableCellule {
Collection<Observer> observeurs;
public ObservableCellule(){
observeurs = new TreeSet<>();
}
public void attacher(Observer o) throws IllegalArgumentException{
if(o == null){
throw new IllegalArgumentException("L'observer ne doit pas être null");
}
observeurs.add(o);
}
public void detacher(Observer o) throws IllegalArgumentException{
if(o == null){
throw new IllegalArgumentException("L'observer ne doit pas être null");
}
observeurs.remove(o);
}
public void notifier() {
for (Observer observeur : observeurs) {
observeur.update();
}
}
}

@ -1,7 +1,11 @@
package model;
import javafx.beans.property.*;
import javafx.collections.ObservableList;
import javafx.collections.FXCollections;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
public class Plateau {
@ -15,13 +19,23 @@ public class Plateau {
public void setLargeur( int valeur ) { largeur.set(valeur); }
public IntegerProperty largeurProperty() { return largeur; }
private ObjectProperty grille = new SimpleObjectProperty();
public List<List<Cellule>> getGrille() { return (List<List<Cellule>>) grille.get(); }
public void setGrille(List<Cellule> cells) { grille.set(cells); }
public ReadOnlyObjectProperty grilleProperty() { return grille; }
private ObservableList grilleObs = FXCollections.observableArrayList();
private ListProperty<List<Cellule>> grille = new SimpleListProperty<>(grilleObs);
public ObservableList<List<Cellule>> getGrille() { return grille.get(); }
public void setGrille(ObservableList<List<Cellule>> cells) { grille.set(cells); }
public ReadOnlyListProperty grilleProperty() { return grille; }
public Cellule getCell(int x, int y) {
return ((List<List<Cellule>>) grille.get()).get(y).get(x);
public Cellule getCell(int x, int y) throws IllegalArgumentException{
if(x < 0 || y < 0) {
throw new IllegalArgumentException("X ou Y est inférieur à 0");
}
if(y >= getGrille().size()){
throw new IllegalArgumentException("Y est trop grand !!!");
}
if(x >= getGrille().get(y).size()){
throw new IllegalArgumentException("X est trop grand !!!");
}
return grille.get().get(y).get(x);
}
}

@ -0,0 +1,19 @@
package model;
public class Position {
private int x;
private int y;
public Position(int x, int y){
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}

@ -1,5 +0,0 @@
package model;
public enum Regles {
ConwaySStyle
}
Loading…
Cancel
Save