2026-03-11 15:52:10 +01:00
|
|
|
package sae.chuzzle;
|
|
|
|
|
|
|
|
|
|
import android.app.Activity;
|
2026-03-19 10:07:24 +01:00
|
|
|
import android.content.Intent;
|
2026-03-11 15:52:10 +01:00
|
|
|
import android.os.Bundle;
|
2026-03-19 10:13:13 +01:00
|
|
|
import android.os.CountDownTimer;
|
2026-03-11 15:52:10 +01:00
|
|
|
import android.view.View;
|
|
|
|
|
import android.widget.Button;
|
|
|
|
|
import android.widget.RadioButton;
|
|
|
|
|
import android.widget.Spinner;
|
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
|
|
public class MainActivity extends Activity implements View.OnClickListener {
|
|
|
|
|
|
2026-03-19 10:13:13 +01:00
|
|
|
private static final long DUREE_HARD_MS = 60_000L; // 1 minute
|
2026-03-19 10:07:24 +01:00
|
|
|
|
2026-03-11 15:52:10 +01:00
|
|
|
private Controleur controleur;
|
|
|
|
|
private Button btnJouer;
|
2026-03-19 10:07:24 +01:00
|
|
|
private Button btnMenu;
|
2026-03-19 10:13:13 +01:00
|
|
|
private TextView tvTimer;
|
2026-03-19 10:07:24 +01:00
|
|
|
|
2026-03-19 10:13:13 +01:00
|
|
|
private CountDownTimer compteARebours;
|
|
|
|
|
private long tempsRestantMs;
|
2026-03-19 10:07:24 +01:00
|
|
|
private boolean hardMode;
|
|
|
|
|
private EtatJeu etatJeu;
|
2026-03-11 15:52:10 +01:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
setContentView(R.layout.activity_main);
|
|
|
|
|
|
|
|
|
|
// --- Modèle ---
|
2026-03-19 10:07:24 +01:00
|
|
|
long graine = getIntent().getLongExtra("graine", System.currentTimeMillis());
|
|
|
|
|
hardMode = getSharedPreferences("chuzzle_prefs", MODE_PRIVATE)
|
2026-03-11 15:52:10 +01:00
|
|
|
.getBoolean("hard_mode", false);
|
|
|
|
|
boolean daltonien = getSharedPreferences("chuzzle_prefs", MODE_PRIVATE)
|
|
|
|
|
.getBoolean("daltonien", false);
|
|
|
|
|
|
2026-03-19 10:07:24 +01:00
|
|
|
etatJeu = new EtatJeu(graine, hardMode);
|
|
|
|
|
|
|
|
|
|
// --- Restauration si retour de pause ---
|
|
|
|
|
if (savedInstanceState != null) {
|
|
|
|
|
etatJeu.restaurerEtat(savedInstanceState);
|
2026-03-19 10:13:13 +01:00
|
|
|
if (hardMode) {
|
|
|
|
|
tempsRestantMs = savedInstanceState.getLong("tempsRestantMs", DUREE_HARD_MS);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
tempsRestantMs = DUREE_HARD_MS;
|
2026-03-19 10:07:24 +01:00
|
|
|
}
|
2026-03-11 15:52:10 +01:00
|
|
|
|
|
|
|
|
// --- Vue ---
|
|
|
|
|
VueGrille vueGrille = findViewById(R.id.vueGrille);
|
|
|
|
|
vueGrille.definirModeDaltonien(daltonien);
|
|
|
|
|
|
2026-03-19 10:13:13 +01:00
|
|
|
tvTimer = findViewById(R.id.tvTimer);
|
2026-03-19 10:07:24 +01:00
|
|
|
|
2026-03-11 15:52:10 +01:00
|
|
|
// --- Controleur ---
|
|
|
|
|
btnJouer = findViewById(R.id.btnJouer);
|
|
|
|
|
btnJouer.setOnClickListener(this);
|
|
|
|
|
|
2026-03-19 10:07:24 +01:00
|
|
|
btnMenu = findViewById(R.id.btnMenu);
|
|
|
|
|
btnMenu.setOnClickListener(this);
|
|
|
|
|
|
|
|
|
|
|
2026-03-11 15:52:10 +01:00
|
|
|
controleur = new Controleur(
|
|
|
|
|
this,
|
|
|
|
|
etatJeu,
|
|
|
|
|
vueGrille,
|
2026-03-19 10:07:24 +01:00
|
|
|
graine,
|
|
|
|
|
(TextView) findViewById(R.id.tvScore),
|
|
|
|
|
(TextView) findViewById(R.id.tvCoups),
|
2026-03-11 15:52:10 +01:00
|
|
|
(RadioButton) findViewById(R.id.rbLigne),
|
|
|
|
|
(RadioButton) findViewById(R.id.rbDroite),
|
2026-03-19 10:07:24 +01:00
|
|
|
(Spinner) findViewById(R.id.spinnerIndex),
|
2026-03-19 10:13:13 +01:00
|
|
|
btnJouer
|
2026-03-11 15:52:10 +01:00
|
|
|
);
|
2026-03-19 10:07:24 +01:00
|
|
|
|
|
|
|
|
// --- Gestion tactile ---
|
|
|
|
|
GestionnaireTactile gestionnaireTactile =
|
|
|
|
|
new GestionnaireTactile(vueGrille, etatJeu, controleur);
|
|
|
|
|
vueGrille.setOnTouchListener(gestionnaireTactile);
|
|
|
|
|
|
2026-03-19 10:13:13 +01:00
|
|
|
// --- Timer hard mode ---
|
|
|
|
|
if (hardMode && !etatJeu.estTerminee()) {
|
|
|
|
|
demarrerTimer(tempsRestantMs);
|
|
|
|
|
} else if (hardMode && etatJeu.estTerminee()) {
|
|
|
|
|
tvTimer.setVisibility(View.VISIBLE);
|
|
|
|
|
tvTimer.setText("⏱ 0s");
|
2026-03-19 10:07:24 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -
|
2026-03-19 10:13:13 +01:00
|
|
|
// TIMER
|
|
|
|
|
|
|
|
|
|
private void demarrerTimer(long dureeMs) {
|
|
|
|
|
tvTimer.setVisibility(View.VISIBLE);
|
|
|
|
|
|
|
|
|
|
compteARebours = new CountDownTimer(dureeMs, 1000) {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onTick(long millisRestants) {
|
|
|
|
|
tempsRestantMs = millisRestants;
|
|
|
|
|
long secondes = millisRestants / 1000;
|
|
|
|
|
tvTimer.setText("⏱ " + secondes + "s");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onFinish() {
|
|
|
|
|
tvTimer.setText("⏱ 0s");
|
|
|
|
|
// Forcer la fin de partie par le temps
|
|
|
|
|
etatJeu.forcerFinDePartie();
|
|
|
|
|
controleur.verifierFinDePartie();
|
|
|
|
|
}
|
|
|
|
|
}.start();
|
|
|
|
|
}
|
2026-03-19 10:07:24 +01:00
|
|
|
|
|
|
|
|
// -
|
|
|
|
|
// CYCLE DE VIE
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onSaveInstanceState(Bundle outState) {
|
|
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
|
if (etatJeu != null) {
|
|
|
|
|
etatJeu.sauvegarderEtat(outState);
|
|
|
|
|
}
|
2026-03-19 10:13:13 +01:00
|
|
|
if (hardMode) {
|
|
|
|
|
outState.putLong("tempsRestantMs", tempsRestantMs);
|
2026-03-19 10:07:24 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onDestroy() {
|
|
|
|
|
super.onDestroy();
|
2026-03-19 10:13:13 +01:00
|
|
|
if (compteARebours != null) {
|
|
|
|
|
compteARebours.cancel();
|
|
|
|
|
}
|
2026-03-11 15:52:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
if (v == btnJouer) {
|
|
|
|
|
controleur.gererCoupJoueur();
|
|
|
|
|
}
|
2026-03-19 10:07:24 +01:00
|
|
|
if (v == btnMenu) {
|
|
|
|
|
Intent intent = new Intent(this, MenuActivity.class);
|
|
|
|
|
startActivity(intent);
|
|
|
|
|
}
|
2026-03-11 15:52:10 +01:00
|
|
|
}
|
2026-03-19 10:07:24 +01:00
|
|
|
}
|