Ajout d'un controleur
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
package sae.chuzzle;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.Button;
|
||||
import android.widget.RadioButton;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class Controleur {
|
||||
|
||||
private final Activity activite;
|
||||
private final EtatJeu etatJeu;
|
||||
private final VueGrille vueGrille;
|
||||
|
||||
private final TextView tvScore;
|
||||
private final TextView tvCoups;
|
||||
private final RadioButton rbLigne;
|
||||
private final RadioButton rbDroite;
|
||||
private final Spinner spinnerIndex;
|
||||
private final Button btnJouer;
|
||||
|
||||
// -
|
||||
// CONSTRUCTEUR
|
||||
|
||||
|
||||
public Controleur(Activity activite, EtatJeu etatJeu, VueGrille vueGrille,
|
||||
TextView tvScore, TextView tvCoups,
|
||||
RadioButton rbLigne, RadioButton rbDroite,
|
||||
Spinner spinnerIndex, Button btnJouer) {
|
||||
|
||||
this.activite = activite;
|
||||
this.etatJeu = etatJeu;
|
||||
this.vueGrille = vueGrille;
|
||||
this.tvScore = tvScore;
|
||||
this.tvCoups = tvCoups;
|
||||
this.rbLigne = rbLigne;
|
||||
this.rbDroite = rbDroite;
|
||||
this.spinnerIndex = spinnerIndex;
|
||||
this.btnJouer = btnJouer;
|
||||
|
||||
initialiserSpinner();
|
||||
rafraichirAffichage();
|
||||
}
|
||||
|
||||
// -
|
||||
// INITIALISATION
|
||||
|
||||
|
||||
private void initialiserSpinner() {
|
||||
String[] indices = {"0", "1", "2", "3", "4", "5"};
|
||||
ArrayAdapter<String> adaptateur = new ArrayAdapter<>(
|
||||
activite,
|
||||
android.R.layout.simple_spinner_item,
|
||||
indices
|
||||
);
|
||||
adaptateur.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
||||
spinnerIndex.setAdapter(adaptateur);
|
||||
}
|
||||
|
||||
// -
|
||||
// GESTION DU COUP
|
||||
|
||||
|
||||
public void gererCoupJoueur() {
|
||||
boolean estLigne = rbLigne.isChecked();
|
||||
int index = spinnerIndex.getSelectedItemPosition();
|
||||
|
||||
int sens;
|
||||
if (rbDroite.isChecked()) {
|
||||
sens = +1;
|
||||
} else {
|
||||
sens = -1;
|
||||
}
|
||||
|
||||
boolean accepte = etatJeu.appliquerCoup(estLigne, index, sens);
|
||||
|
||||
if (!accepte) {
|
||||
Toast.makeText(activite, "Coup invalide !", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
if(accepte){
|
||||
Toast.makeText(activite, "Coup valide !", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
rafraichirAffichage();
|
||||
|
||||
if (etatJeu.estTerminee()) {
|
||||
Toast.makeText(
|
||||
activite,
|
||||
"Partie terminee ! Score : " + etatJeu.obtenirScore(),
|
||||
Toast.LENGTH_LONG
|
||||
).show();
|
||||
btnJouer.setEnabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
// -
|
||||
// MISE A JOUR DE LA VUE
|
||||
|
||||
|
||||
public void rafraichirAffichage() {
|
||||
tvScore.setText("Score : " + etatJeu.obtenirScore());
|
||||
tvCoups.setText("Coups : " + etatJeu.obtenirNbCoups());
|
||||
vueGrille.definirGrille(etatJeu.obtenirGrille());
|
||||
vueGrille.definirVerrous(etatJeu.obtenirVerrous());
|
||||
}
|
||||
}
|
||||
@@ -19,9 +19,9 @@ public class EtatJeu {
|
||||
private boolean partieTerminee = false;
|
||||
private boolean hardMode = false;
|
||||
|
||||
// =========================================================
|
||||
//-
|
||||
// CONSTRUCTEURS
|
||||
// =========================================================
|
||||
|
||||
|
||||
public EtatJeu() {
|
||||
aleatoire = new Random();
|
||||
@@ -34,9 +34,9 @@ public class EtatJeu {
|
||||
initialiserGrilleSansTriples();
|
||||
}
|
||||
|
||||
// =========================================================
|
||||
// -
|
||||
// GETTERS
|
||||
// =========================================================
|
||||
|
||||
|
||||
public int obtenirScore() {
|
||||
return score;
|
||||
@@ -74,9 +74,9 @@ public class EtatJeu {
|
||||
return copie;
|
||||
}
|
||||
|
||||
// =========================================================
|
||||
// -
|
||||
// INITIALISATION SANS TRIPLES
|
||||
// =========================================================
|
||||
//
|
||||
|
||||
private void initialiserGrilleSansTriples() {
|
||||
for (int ligne = 0; ligne < NB_LIGNES; ligne++) {
|
||||
@@ -111,9 +111,9 @@ public class EtatJeu {
|
||||
return false;
|
||||
}
|
||||
|
||||
// =========================================================
|
||||
// -
|
||||
// DECALAGE CIRCULAIRE
|
||||
// =========================================================
|
||||
|
||||
|
||||
public void decalerLigne(int ligne, int sens) {
|
||||
int s = ((sens % NB_COLONNES) + NB_COLONNES) % NB_COLONNES;
|
||||
@@ -143,9 +143,9 @@ public class EtatJeu {
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================
|
||||
// -
|
||||
// APPLIQUER UN COUP
|
||||
// =========================================================
|
||||
|
||||
|
||||
public boolean appliquerCoup(boolean estLigne, int index, int sens) {
|
||||
if (partieTerminee) {
|
||||
@@ -183,9 +183,9 @@ public class EtatJeu {
|
||||
return true;
|
||||
}
|
||||
|
||||
// =========================================================
|
||||
// -
|
||||
// TROUVER LES SERIES
|
||||
// =========================================================
|
||||
|
||||
|
||||
public List<int[]> trouverSeries() {
|
||||
boolean[][] aSupprimer = new boolean[NB_LIGNES][NB_COLONNES];
|
||||
@@ -245,9 +245,9 @@ public class EtatJeu {
|
||||
return positions;
|
||||
}
|
||||
|
||||
// =========================================================
|
||||
// -
|
||||
// RESOLUTION AVEC CASCADE
|
||||
// =========================================================
|
||||
|
||||
|
||||
public int resoudreEtRemplir() {
|
||||
int pointsTotal = 0;
|
||||
@@ -362,9 +362,9 @@ public class EtatJeu {
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================
|
||||
// -
|
||||
// VERROUS (hard mode)
|
||||
// =========================================================
|
||||
|
||||
|
||||
private boolean estBloque(boolean estLigne, int index) {
|
||||
if (estLigne) {
|
||||
@@ -419,9 +419,9 @@ public class EtatJeu {
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================
|
||||
// -
|
||||
// DETECTION FIN DE PARTIE
|
||||
// =========================================================
|
||||
|
||||
|
||||
public boolean aUnCoupValide() {
|
||||
for (int i = 0; i < NB_LIGNES; i++) {
|
||||
@@ -461,9 +461,9 @@ public class EtatJeu {
|
||||
return resultat;
|
||||
}
|
||||
|
||||
// =========================================================
|
||||
// -
|
||||
// UTILITAIRES PRIVES
|
||||
// =========================================================
|
||||
|
||||
|
||||
private int[][] copierGrille() {
|
||||
int[][] copie = new int[NB_LIGNES][NB_COLONNES];
|
||||
|
||||
@@ -3,25 +3,14 @@ package sae.chuzzle;
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.Button;
|
||||
import android.widget.RadioButton;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import sae.chuzzle.EtatJeu;
|
||||
import sae.chuzzle.VueGrille;
|
||||
|
||||
public class MainActivity extends Activity implements View.OnClickListener {
|
||||
|
||||
private EtatJeu etatJeu;
|
||||
private VueGrille vueGrille;
|
||||
private TextView tvScore;
|
||||
private TextView tvCoups;
|
||||
private RadioButton rbLigne;
|
||||
private RadioButton rbDroite;
|
||||
private Spinner spinnerIndex;
|
||||
private Controleur controleur;
|
||||
private Button btnJouer;
|
||||
|
||||
@Override
|
||||
@@ -29,96 +18,40 @@ public class MainActivity extends Activity implements View.OnClickListener {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
// - Lire la graine et les options
|
||||
long graine = getIntent().getLongExtra("graine", System.currentTimeMillis());
|
||||
// --- Modèle ---
|
||||
long graine = getIntent().getLongExtra("graine", System.currentTimeMillis());
|
||||
boolean hardMode = getSharedPreferences("chuzzle_prefs", MODE_PRIVATE)
|
||||
.getBoolean("hard_mode", false);
|
||||
boolean daltonien = getSharedPreferences("chuzzle_prefs", MODE_PRIVATE)
|
||||
.getBoolean("daltonien", false);
|
||||
boolean hardMode = getSharedPreferences("chuzzle_prefs", MODE_PRIVATE)
|
||||
.getBoolean("hard_mode", false);
|
||||
|
||||
// - Modèle
|
||||
etatJeu = new EtatJeu(graine, hardMode);
|
||||
EtatJeu etatJeu = new EtatJeu(graine, hardMode);
|
||||
|
||||
// - Récupérer les vues depuis le XML
|
||||
tvScore = findViewById(R.id.tvScore);
|
||||
tvCoups = findViewById(R.id.tvCoups);
|
||||
vueGrille = findViewById(R.id.vueGrille);
|
||||
rbLigne = findViewById(R.id.rbLigne);
|
||||
rbDroite = findViewById(R.id.rbDroite);
|
||||
spinnerIndex = findViewById(R.id.spinnerIndex);
|
||||
btnJouer = findViewById(R.id.btnJouer);
|
||||
|
||||
// - Configurer la vue
|
||||
vueGrille.definirGrille(etatJeu.obtenirGrille());
|
||||
vueGrille.definirVerrous(etatJeu.obtenirVerrous());
|
||||
// --- Vue ---
|
||||
VueGrille vueGrille = findViewById(R.id.vueGrille);
|
||||
vueGrille.definirModeDaltonien(daltonien);
|
||||
|
||||
// - Configurer le spinner
|
||||
String[] indices = {"0", "1", "2", "3", "4", "5"};
|
||||
ArrayAdapter<String> adaptateur = new ArrayAdapter<>(
|
||||
this,
|
||||
android.R.layout.simple_spinner_item,
|
||||
indices
|
||||
);
|
||||
adaptateur.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
||||
spinnerIndex.setAdapter(adaptateur);
|
||||
|
||||
// --- Bouton jouer ---
|
||||
// --- Controleur ---
|
||||
btnJouer = findViewById(R.id.btnJouer);
|
||||
btnJouer.setOnClickListener(this);
|
||||
|
||||
controleur = new Controleur(
|
||||
this,
|
||||
etatJeu,
|
||||
vueGrille,
|
||||
(TextView) findViewById(R.id.tvScore),
|
||||
(TextView) findViewById(R.id.tvCoups),
|
||||
(RadioButton) findViewById(R.id.rbLigne),
|
||||
(RadioButton) findViewById(R.id.rbDroite),
|
||||
(Spinner) findViewById(R.id.spinnerIndex),
|
||||
btnJouer
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
// GESTION DES CLICS
|
||||
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (v == btnJouer) {
|
||||
gererCoupJoueur();
|
||||
controleur.gererCoupJoueur();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// COORDINATION
|
||||
|
||||
|
||||
private void gererCoupJoueur() {
|
||||
boolean estLigne = rbLigne.isChecked();
|
||||
int index = spinnerIndex.getSelectedItemPosition();
|
||||
|
||||
int sens;
|
||||
if (rbDroite.isChecked()) {
|
||||
sens = +1;
|
||||
} else {
|
||||
sens = -1;
|
||||
}
|
||||
|
||||
boolean accepte = etatJeu.appliquerCoup(estLigne, index, sens);
|
||||
|
||||
if (!accepte) {
|
||||
Toast.makeText(
|
||||
this,
|
||||
"Coup invalide : aucune serie creee !",
|
||||
Toast.LENGTH_SHORT
|
||||
).show();
|
||||
}
|
||||
|
||||
rafraichirAffichage();
|
||||
|
||||
if (etatJeu.estTerminee()) {
|
||||
Toast.makeText(
|
||||
this,
|
||||
"Partie terminee ! Score final : " + etatJeu.obtenirScore(),
|
||||
Toast.LENGTH_LONG
|
||||
).show();
|
||||
btnJouer.setEnabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void rafraichirAffichage() {
|
||||
tvScore.setText("Score : " + etatJeu.obtenirScore());
|
||||
tvCoups.setText("Coups : " + etatJeu.obtenirNbCoups());
|
||||
vueGrille.definirGrille(etatJeu.obtenirGrille());
|
||||
vueGrille.definirVerrous(etatJeu.obtenirVerrous());
|
||||
}
|
||||
}
|
||||
@@ -3,15 +3,15 @@ package sae.chuzzle;
|
||||
import android.app.Activity;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.CompoundButton;
|
||||
import android.widget.Switch;
|
||||
|
||||
public class OptionsActivity extends Activity
|
||||
implements CompoundButton.OnCheckedChangeListener {
|
||||
|
||||
private SharedPreferences prefs;
|
||||
private Switch switchDaltonien;
|
||||
private Switch switchHard;
|
||||
private CheckBox checkDaltonien;
|
||||
private CheckBox checkHard;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -20,21 +20,21 @@ public class OptionsActivity extends Activity
|
||||
|
||||
prefs = getSharedPreferences("chuzzle_prefs", MODE_PRIVATE);
|
||||
|
||||
switchDaltonien = findViewById(R.id.switchDaltonien);
|
||||
switchHard = findViewById(R.id.switchHard);
|
||||
checkDaltonien = findViewById(R.id.switchDaltonien);
|
||||
checkHard = findViewById(R.id.switchHard);
|
||||
|
||||
switchDaltonien.setChecked(prefs.getBoolean("daltonien", false));
|
||||
switchHard.setChecked(prefs.getBoolean("hard_mode", false));
|
||||
checkDaltonien.setChecked(prefs.getBoolean("daltonien", false));
|
||||
checkHard.setChecked(prefs.getBoolean("hard_mode", false));
|
||||
|
||||
switchDaltonien.setOnCheckedChangeListener(this);
|
||||
switchHard.setOnCheckedChangeListener(this);
|
||||
checkDaltonien.setOnCheckedChangeListener(this);
|
||||
checkHard.setOnCheckedChangeListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton bouton, boolean estCoche) {
|
||||
if (bouton == switchDaltonien) {
|
||||
if (bouton == checkDaltonien) {
|
||||
prefs.edit().putBoolean("daltonien", estCoche).apply();
|
||||
} else if (bouton == switchHard) {
|
||||
} else if (bouton == checkHard) {
|
||||
prefs.edit().putBoolean("hard_mode", estCoche).apply();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.Toast;
|
||||
|
||||
|
||||
public class SeedActivity extends Activity implements View.OnClickListener {
|
||||
|
||||
private EditText etGraine;
|
||||
|
||||
@@ -3,6 +3,7 @@ package sae.chuzzle;
|
||||
import android.content.Context;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Paint;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
@@ -39,9 +40,20 @@ public class VueGrille extends View {
|
||||
pinceauSymbole.setTextAlign(Paint.Align.CENTER);
|
||||
}
|
||||
|
||||
// =========================================================
|
||||
public VueGrille(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
|
||||
pinceauCase.setAntiAlias(true);
|
||||
pinceauCase.setStyle(Paint.Style.FILL);
|
||||
|
||||
pinceauSymbole.setAntiAlias(true);
|
||||
pinceauSymbole.setColor(0xFF000000);
|
||||
pinceauSymbole.setTextAlign(Paint.Align.CENTER);
|
||||
}
|
||||
|
||||
|
||||
// API PUBLIQUE (appelée par MainActivity uniquement)
|
||||
// =========================================================
|
||||
|
||||
|
||||
/**
|
||||
* Reçoit une copie de la grille depuis MainActivity.
|
||||
@@ -60,9 +72,9 @@ public class VueGrille extends View {
|
||||
invalidate();
|
||||
}
|
||||
|
||||
// =========================================================
|
||||
// -
|
||||
// DESSIN
|
||||
// =========================================================
|
||||
|
||||
|
||||
@Override
|
||||
protected void onDraw(@NonNull Canvas canvas) {
|
||||
@@ -121,9 +133,9 @@ public class VueGrille extends View {
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================
|
||||
// -
|
||||
// UTILITAIRE PRIVE
|
||||
// =========================================================
|
||||
|
||||
|
||||
private void definirCouleur(int type) {
|
||||
switch (type % NB_TYPES) {
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:aapt="http://schemas.android.com/aapt"
|
||||
android:width="108dp"
|
||||
android:height="108dp"
|
||||
android:viewportWidth="108"
|
||||
android:viewportHeight="108">
|
||||
<path android:pathData="M31,63.928c0,0 6.4,-11 12.1,-13.1c7.2,-2.6 26,-1.4 26,-1.4l38.1,38.1L107,108.928l-32,-1L31,63.928z">
|
||||
<aapt:attr name="android:fillColor">
|
||||
<gradient
|
||||
android:endX="85.84757"
|
||||
android:endY="92.4963"
|
||||
android:startX="42.9492"
|
||||
android:startY="49.59793"
|
||||
android:type="linear">
|
||||
<item
|
||||
android:color="#44000000"
|
||||
android:offset="0.0" />
|
||||
<item
|
||||
android:color="#00000000"
|
||||
android:offset="1.0" />
|
||||
</gradient>
|
||||
</aapt:attr>
|
||||
</path>
|
||||
<path
|
||||
android:fillColor="#FFFFFF"
|
||||
android:fillType="nonZero"
|
||||
android:pathData="M65.3,45.828l3.8,-6.6c0.2,-0.4 0.1,-0.9 -0.3,-1.1c-0.4,-0.2 -0.9,-0.1 -1.1,0.3l-3.9,6.7c-6.3,-2.8 -13.4,-2.8 -19.7,0l-3.9,-6.7c-0.2,-0.4 -0.7,-0.5 -1.1,-0.3C38.8,38.328 38.7,38.828 38.9,39.228l3.8,6.6C36.2,49.428 31.7,56.028 31,63.928h46C76.3,56.028 71.8,49.428 65.3,45.828zM43.4,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2c-0.3,-0.7 -0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C45.3,56.528 44.5,57.328 43.4,57.328L43.4,57.328zM64.6,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2s-0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C66.5,56.528 65.6,57.328 64.6,57.328L64.6,57.328z"
|
||||
android:strokeWidth="1"
|
||||
android:strokeColor="#00000000" />
|
||||
</vector>
|
||||
@@ -50,8 +50,8 @@
|
||||
|
||||
<Switch
|
||||
android:id="@+id/switchHard"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"/>
|
||||
android:layout_width="50dp"
|
||||
android:layout_height="50dp"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
<resources>
|
||||
<string name="app_name">Chuzzle</string>
|
||||
<string name="app_name">RealChuzzle</string>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user