parent
f8c99ea655
commit
823af23b93
@ -0,0 +1,32 @@
|
||||
package projet.iut.jeu_de_la_vie.model.cellulesVivantes.observer;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import projet.iut.jeu_de_la_vie.model.cellule.Cellule;
|
||||
|
||||
public abstract class ObservableCV {
|
||||
private List<ObserverCV> observeurs;
|
||||
|
||||
public ObservableCV(){observeurs = new LinkedList<>();}
|
||||
|
||||
public void attacher(ObserverCV o) throws IllegalArgumentException{
|
||||
if(o == null){
|
||||
throw new IllegalArgumentException("L'observer ne doit pas être null");
|
||||
}
|
||||
observeurs.add(o);
|
||||
}
|
||||
|
||||
public void detacher(ObserverCV o) throws IllegalArgumentException{
|
||||
if(o == null){
|
||||
throw new IllegalArgumentException("L'observer ne doit pas être null");
|
||||
}
|
||||
observeurs.remove(o);
|
||||
}
|
||||
public void notifier(Cellule changingCell) {
|
||||
for (ObserverCV observeur : observeurs) {
|
||||
observeur.update(changingCell);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package projet.iut.jeu_de_la_vie.model.cellulesVivantes.observer;
|
||||
|
||||
import projet.iut.jeu_de_la_vie.model.cellule.Cellule;
|
||||
|
||||
public interface ObserverCV {
|
||||
void update(Cellule changingCell);
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package projet.iut.jeu_de_la_vie.view;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import projet.iut.jeu_de_la_vie.R;
|
||||
|
||||
public class GameActivity extends AppCompatActivity {
|
||||
|
||||
private PlateauView plateauView;
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.game_activity);
|
||||
|
||||
plateauView = findViewById(R.id.plateauView);
|
||||
}
|
||||
}
|
@ -0,0 +1,100 @@
|
||||
package projet.iut.jeu_de_la_vie.view;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Rect;
|
||||
import android.os.Build;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.RequiresApi;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Observable;
|
||||
|
||||
import projet.iut.jeu_de_la_vie.model.cellule.Cellule;
|
||||
import projet.iut.jeu_de_la_vie.model.cellule.Position;
|
||||
import projet.iut.jeu_de_la_vie.model.cellule.observer.ObserverCellule;
|
||||
import projet.iut.jeu_de_la_vie.model.cellulesVivantes.CellulesVivantes;
|
||||
import projet.iut.jeu_de_la_vie.model.cellulesVivantes.observer.ObservableCV;
|
||||
import projet.iut.jeu_de_la_vie.model.cellulesVivantes.observer.ObserverCV;
|
||||
|
||||
public class PlateauView extends View implements ObserverCV {
|
||||
private HashMap<Position, Integer> colors;
|
||||
|
||||
public PlateauView(Context context, @Nullable AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
public PlateauView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
||||
public PlateauView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
|
||||
super(context, attrs, defStyleAttr, defStyleRes);
|
||||
}
|
||||
|
||||
private Paint paint;
|
||||
private int colones;
|
||||
private int lignes;
|
||||
private int dethColor;
|
||||
|
||||
public PlateauView(Context context, int colones, int lignes){
|
||||
super(context);
|
||||
colors = new HashMap<>();
|
||||
paint = new Paint();
|
||||
this.colones = colones;
|
||||
this.lignes = lignes;
|
||||
this.dethColor = Color.BLACK;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDraw(Canvas canvas){
|
||||
super.onDraw(canvas);
|
||||
Integer tmp;
|
||||
Rect rect;
|
||||
for(int i=0; i<lignes; ++i){
|
||||
for(int j=0; j<colones; ++j){
|
||||
Position p = new Position(j, i);
|
||||
tmp = colors.get(p);
|
||||
paint.setColor(dethColor);
|
||||
if(tmp instanceof Integer){
|
||||
paint.setColor(tmp);
|
||||
}
|
||||
rect = new Rect(j, i, j+10, i+10);
|
||||
canvas.drawRect(rect, paint);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void update(Cellule changingCell) {
|
||||
if(changingCell.isAlive()){
|
||||
colors.put(changingCell.getPosition(), changingCell.getActiveColor());
|
||||
}
|
||||
else {
|
||||
colors.remove(changingCell.getPosition());
|
||||
}
|
||||
}
|
||||
|
||||
public void setColones(int value) throws IllegalArgumentException{
|
||||
if(value < 0){
|
||||
throw new IllegalArgumentException("La valeur des colones doit être supperieur à 0");
|
||||
}
|
||||
colones = value;
|
||||
}
|
||||
|
||||
public void setLignes(int value) throws IllegalArgumentException{
|
||||
if(value < 0){
|
||||
throw new IllegalArgumentException("La valeur des colones doit être supperieur à 0");
|
||||
}
|
||||
lignes = value;
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package projet.iut.jeu_de_la_vie.view;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
public class VueJeu extends AppCompatActivity {
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(new PlateauView(this, 10, 10));
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
<projet.iut.jeu_de_la_vie.view.PlateauView
|
||||
android:id="@+id/plateauView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"/>
|
||||
</FrameLayout>
|
Loading…
Reference in new issue