diff --git a/code/app/src/main/java/projet/iut/jeu_de_la_vie/model/Manager.java b/code/app/src/main/java/projet/iut/jeu_de_la_vie/model/Manager.java index 6e8892a..59f212c 100644 --- a/code/app/src/main/java/projet/iut/jeu_de_la_vie/model/Manager.java +++ b/code/app/src/main/java/projet/iut/jeu_de_la_vie/model/Manager.java @@ -40,6 +40,10 @@ public class Manager implements ObserverBDJ { getActualiseurCellule().inverserCellule(x, y); } + public IBoucleDeJeu getBoucleDeJeu() { + return boucleDeJeu; + } + /** * Change l'actualiseur de cellule en fonction des règles */ diff --git a/code/app/src/main/java/projet/iut/jeu_de_la_vie/model/cellulesVivantes/CellulesVivantes.java b/code/app/src/main/java/projet/iut/jeu_de_la_vie/model/cellulesVivantes/CellulesVivantes.java index 16eb162..e92c35f 100644 --- a/code/app/src/main/java/projet/iut/jeu_de_la_vie/model/cellulesVivantes/CellulesVivantes.java +++ b/code/app/src/main/java/projet/iut/jeu_de_la_vie/model/cellulesVivantes/CellulesVivantes.java @@ -7,6 +7,7 @@ import projet.iut.jeu_de_la_vie.model.cellulesVivantes.observer.ObservableCV; import java.util.HashMap; +import java.util.concurrent.ConcurrentHashMap; /** * Représentation des cellules vivantes sur le plateau. Elle se met à jours automatiquement. @@ -18,13 +19,13 @@ public class CellulesVivantes extends ObservableCV implements ObserverCellule { /** * Dictionaire contenant toutes les cellules vivantes */ - private HashMap cellVivantes; + private ConcurrentHashMap cellVivantes; public CellulesVivantes(){ - this(new HashMap<>()); + this(new ConcurrentHashMap<>()); } - private CellulesVivantes(HashMap cellVivantes){ + private CellulesVivantes(ConcurrentHashMap cellVivantes){ this.cellVivantes = cellVivantes; } @@ -80,13 +81,17 @@ public class CellulesVivantes extends ObservableCV implements ObserverCellule { * @return Le meme objet CellulesVivantes avec une référence diférente */ public CellulesVivantes clone(){ - return new CellulesVivantes(new HashMap<>(cellVivantes)); + return new CellulesVivantes(new ConcurrentHashMap<>(cellVivantes)); } /** * Nétoie le dictionaire contenant les cellules vivantes */ public void reset(){ - cellVivantes = new HashMap<>(); + cellVivantes = new ConcurrentHashMap<>(); + } + + public ConcurrentHashMap getCells(){ + return cellVivantes; } } diff --git a/code/app/src/main/java/projet/iut/jeu_de_la_vie/model/plateau/Plateau.java b/code/app/src/main/java/projet/iut/jeu_de_la_vie/model/plateau/Plateau.java index a4b8664..298db9a 100644 --- a/code/app/src/main/java/projet/iut/jeu_de_la_vie/model/plateau/Plateau.java +++ b/code/app/src/main/java/projet/iut/jeu_de_la_vie/model/plateau/Plateau.java @@ -4,8 +4,10 @@ import projet.iut.jeu_de_la_vie.model.cellulesVivantes.CellulesVivantes; import projet.iut.jeu_de_la_vie.model.cellule.Cellule; import projet.iut.jeu_de_la_vie.model.cellule.créateur.CreateurCellule; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.concurrent.ConcurrentHashMap; /** * Représentation du plateau de jeu diff --git a/code/app/src/main/java/projet/iut/jeu_de_la_vie/view/PlateauView.java b/code/app/src/main/java/projet/iut/jeu_de_la_vie/view/PlateauView.java index 25b9869..024cfdb 100644 --- a/code/app/src/main/java/projet/iut/jeu_de_la_vie/view/PlateauView.java +++ b/code/app/src/main/java/projet/iut/jeu_de_la_vie/view/PlateauView.java @@ -116,6 +116,7 @@ public class PlateauView extends View implements ObserverCV { public void delPosCell(Cellule cellule){ colors.remove(cellule.getPosition()); } + public void resetMap(){ colors.clear(); } public void setColones(int value) throws IllegalArgumentException{ if(value < 0){ diff --git a/code/app/src/main/java/projet/iut/jeu_de_la_vie/view/fragment/FragmentPlateau.java b/code/app/src/main/java/projet/iut/jeu_de_la_vie/view/fragment/FragmentPlateau.java index 41f3ca8..79d00c9 100644 --- a/code/app/src/main/java/projet/iut/jeu_de_la_vie/view/fragment/FragmentPlateau.java +++ b/code/app/src/main/java/projet/iut/jeu_de_la_vie/view/fragment/FragmentPlateau.java @@ -14,17 +14,19 @@ import androidx.fragment.app.Fragment; import projet.iut.jeu_de_la_vie.R; import projet.iut.jeu_de_la_vie.model.Manager; +import projet.iut.jeu_de_la_vie.model.boucleDeJeu.observer.ObservableBDJ; +import projet.iut.jeu_de_la_vie.model.boucleDeJeu.observer.ObserverBDJ; import projet.iut.jeu_de_la_vie.model.cellule.Cellule; import projet.iut.jeu_de_la_vie.model.cellulesVivantes.observer.ObserverCV; import projet.iut.jeu_de_la_vie.view.PlateauView; -public class FragmentPlateau extends Fragment implements ObserverCV { +public class FragmentPlateau extends Fragment implements ObserverBDJ { private Manager manager; public FragmentPlateau(){ super(R.layout.fragment_plateau); //<-- lui passer la future vue du jeu manager = new Manager(); - manager.getActualiseurCellule().getArbitre().getPlateau().getCellulesVivantes().attacher(this); + ((ObservableBDJ) manager.getBoucleDeJeu()).attacher(this); } @Override @@ -41,7 +43,7 @@ public class FragmentPlateau extends Fragment implements ObserverCV { PlateauView plateauView = view.findViewById(R.id.plateauView); plateauView.setLignes(manager.getNumberOfLines()); plateauView.setColones(manager.getNomberOfColumns()); - plateauView.setOnTouchListener(this::loadRect); + plateauView.setOnTouchListener(this::tapRect); Button button = view.findViewById(R.id.startnstopButton); button.setOnClickListener(new View.OnClickListener() { @Override @@ -58,12 +60,12 @@ public class FragmentPlateau extends Fragment implements ObserverCV { }); } - private boolean loadRect(View view1, MotionEvent motionEvent){ + private boolean tapRect(View view1, MotionEvent motionEvent){ int x = (int) (( (float) manager.getNomberOfColumns() / view1.getWidth()) * motionEvent.getX()-5/100); int y = (int) (( (float) manager.getNomberOfColumns() / view1.getWidth()) * motionEvent.getY()-5/100); - return loadRect(view1, x, y); + return tapRect(view1, x, y); } - private boolean loadRect(View view, int x, int y){ + private boolean tapRect(View view, int x, int y){ Log.d("D", "x=" + x); Log.d("D", "y=" + y); try { @@ -74,7 +76,6 @@ public class FragmentPlateau extends Fragment implements ObserverCV { }else { ((PlateauView) view).delPosCell(cellule); } - view.postInvalidate(); }catch (IllegalArgumentException e){ Log.w("WARNING", e.getMessage()); } @@ -87,15 +88,15 @@ public class FragmentPlateau extends Fragment implements ObserverCV { } @Override - public void update(Cellule changingCell) { - View plateauView = getView().findViewById(R.id.plateauView); - if(plateauView != null) { - plateauView.post(new Runnable() { - @Override - public void run() { - loadRect(plateauView, changingCell.getPosition().getX(), changingCell.getPosition().getY()); - } - }); + public void update() { + PlateauView view = (PlateauView) getView().findViewById(R.id.plateauView); + if(view == null){ + return; + } + view.resetMap(); + for (Cellule c: manager.getActualiseurCellule().getArbitre().getPlateau().getCellulesVivantes().getCells().values()) { + view.addPosCell(c); } + view.postInvalidate(); } }