Ajout sauvegarde état Partie Master Mind
This commit is contained in:
parent
b2a18b3fcb
commit
2ef6863791
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -2,8 +2,6 @@ package com.example.mastermind;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
@ -13,6 +11,8 @@ import com.example.mastermind.controller.mastermind.MonNextTurnTouch;
|
||||
import com.example.mastermind.controller.mastermind.MonOnTouchListener;
|
||||
import com.example.mastermind.vue.mastermind.UnePiece;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class MasterMindActivity extends AppCompatActivity {
|
||||
|
||||
private int[] code;
|
||||
@ -24,6 +24,11 @@ public class MasterMindActivity extends AppCompatActivity {
|
||||
private LinearLayout correction;
|
||||
|
||||
private boolean vide;
|
||||
|
||||
private ArrayList<ArrayList<Integer>> combinaisons;
|
||||
|
||||
private ArrayList<ArrayList<Integer>> corrections;
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
@ -33,17 +38,35 @@ public class MasterMindActivity extends AppCompatActivity {
|
||||
this.vide=data.getBooleanExtra("vide", false);
|
||||
this.jeu=this.findViewById(R.id.jeu);
|
||||
this.correction=this.findViewById(R.id.correction);
|
||||
this.tour=0;
|
||||
this.findViewById(R.id.tour).setOnTouchListener(new MonNextTurnTouch(this));
|
||||
|
||||
//on récupere le LinearLayout des pieces du tour
|
||||
LinearLayout pieces =(LinearLayout) this.jeu.getChildAt(this.tour);
|
||||
//on ajoute le listener
|
||||
for(int i=0; i<pieces.getChildCount(); i++){
|
||||
UnePiece p=(UnePiece) pieces.getChildAt(i);
|
||||
p.setOnTouchListener(new MonOnTouchListener(p, this.vide));
|
||||
boolean firstInit = true;
|
||||
if (savedInstanceState != null) {
|
||||
if (savedInstanceState.containsKey("master_mind")) {
|
||||
firstInit = false;
|
||||
}
|
||||
}
|
||||
if (firstInit){
|
||||
Bundle b = new Bundle();
|
||||
|
||||
b.putInt("tour", 0);
|
||||
|
||||
this.combinaisons = new ArrayList<>();
|
||||
combinaisons.add(new ArrayList<>(0));
|
||||
for (int i=0; i<4; i++)
|
||||
combinaisons.get(0).add(6);
|
||||
b.putSerializable("combinaisons", combinaisons);
|
||||
|
||||
this.corrections = new ArrayList<>();
|
||||
corrections.add(new ArrayList<>(0));
|
||||
for (int i=0; i<4; i++)
|
||||
corrections.get(0).add(6);
|
||||
b.putSerializable("corrections", corrections);
|
||||
|
||||
this.init(b);
|
||||
}
|
||||
|
||||
this.findViewById(R.id.tour).setOnTouchListener(new MonNextTurnTouch(this));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
@ -56,10 +79,15 @@ public class MasterMindActivity extends AppCompatActivity {
|
||||
if(this.tour<9){
|
||||
//on affiche la correction
|
||||
boolean gagne = this.afficherCorrection((LinearLayout) this.jeu.getChildAt(this.tour));
|
||||
//on supprime les listener
|
||||
//on supprime les listener et sauvegarde la combinaison
|
||||
LinearLayout anciennesPieces =(LinearLayout) this.jeu.getChildAt(this.tour);
|
||||
|
||||
for(int i=0; i<anciennesPieces.getChildCount(); i++){anciennesPieces.getChildAt(i).setOnTouchListener(null);}
|
||||
ArrayList<Integer> combinaison = new ArrayList<>();
|
||||
for(int i=0; i<anciennesPieces.getChildCount(); i++){
|
||||
anciennesPieces.getChildAt(i).setOnTouchListener(null);
|
||||
combinaison.add(((UnePiece)anciennesPieces.getChildAt(i)).getColor());
|
||||
}
|
||||
this.combinaisons.add(combinaison);
|
||||
|
||||
if (gagne){
|
||||
this.finDePartie(true);
|
||||
@ -94,6 +122,7 @@ public class MasterMindActivity extends AppCompatActivity {
|
||||
colorpiece[i]=((UnePiece)pieces.getChildAt(i)).getColor();
|
||||
}
|
||||
LinearLayout correctionsPieces=(LinearLayout) this.correction.getChildAt(this.tour);
|
||||
ArrayList<Integer> correction = new ArrayList<>();
|
||||
//si on a gagner
|
||||
boolean gagner=true;
|
||||
|
||||
@ -101,6 +130,7 @@ public class MasterMindActivity extends AppCompatActivity {
|
||||
if(colorpiece[i] == this.code[i]){
|
||||
//bien placer bon endroit
|
||||
((UnePiece)correctionsPieces.getChildAt(i)).setColor(4);
|
||||
correction.add(4);
|
||||
}else{
|
||||
gagner=false;
|
||||
boolean nombre=false;
|
||||
@ -112,14 +142,18 @@ public class MasterMindActivity extends AppCompatActivity {
|
||||
if(nombre){
|
||||
//mal placer
|
||||
((UnePiece)correctionsPieces.getChildAt(i)).setColor(5);
|
||||
correction.add(5);
|
||||
}else {
|
||||
//mauvais
|
||||
correction.add(6);
|
||||
}
|
||||
}
|
||||
}
|
||||
this.corrections.add(correction);
|
||||
return gagner;
|
||||
}
|
||||
|
||||
public void finDePartie(boolean trouve){
|
||||
Log.d("finDePartie", " finDePartie: "+trouve);
|
||||
Intent fin=new Intent(this, FinDePartieActivity.class);
|
||||
fin.putExtra("trouve", trouve);
|
||||
fin.putExtra("tour", this.tour);
|
||||
@ -127,4 +161,59 @@ public class MasterMindActivity extends AppCompatActivity {
|
||||
this.startActivity(fin);
|
||||
this.finish();
|
||||
}
|
||||
|
||||
public void init(Bundle state){
|
||||
this.tour= state.getInt("tour");
|
||||
ArrayList<ArrayList<Integer>> combinaisons = (ArrayList<ArrayList<Integer>>) state.getSerializable("combinaisons");
|
||||
ArrayList<ArrayList<Integer>> corrections = (ArrayList<ArrayList<Integer>>) state.getSerializable("corrections");
|
||||
|
||||
for (int i=0; i<=this.tour; i++){
|
||||
ArrayList<Integer> combinaison = combinaisons.get(i);
|
||||
ArrayList<Integer> correction = corrections.get(i);
|
||||
LinearLayout pieces = (LinearLayout) this.jeu.getChildAt(i);
|
||||
LinearLayout correctionsPieces = (LinearLayout) this.correction.getChildAt(i);
|
||||
for (int j=0; j<4; j++){
|
||||
UnePiece p = (UnePiece) pieces.getChildAt(j);
|
||||
p.setColor(combinaison.get(j));
|
||||
UnePiece c = (UnePiece) correctionsPieces.getChildAt(j);
|
||||
c.setColor(correction.get(j));
|
||||
if (i==this.tour){
|
||||
p.setOnTouchListener(new MonOnTouchListener(p, this.vide));
|
||||
}
|
||||
}
|
||||
}
|
||||
combinaisons.remove(this.tour);
|
||||
corrections.remove(this.tour);
|
||||
this.combinaisons = combinaisons;
|
||||
this.corrections = corrections;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
if (outState == null)
|
||||
outState = new Bundle();
|
||||
Bundle b = new Bundle();
|
||||
b.putInt("tour", this.tour);
|
||||
|
||||
ArrayList<Integer> combinaison = new ArrayList<>();
|
||||
LinearLayout anciennesPieces =(LinearLayout) this.jeu.getChildAt(this.tour);
|
||||
for (int i=0; i<4; i++)
|
||||
combinaison.add(((UnePiece)anciennesPieces.getChildAt(i)).getColor());
|
||||
this.combinaisons.add(combinaison);
|
||||
b.putSerializable("combinaisons", this.combinaisons);
|
||||
|
||||
ArrayList<Integer> correction = new ArrayList<>();
|
||||
for (int i=0; i<4; i++)
|
||||
correction.add(6);
|
||||
this.corrections.add(correction);
|
||||
b.putSerializable("corrections", this.corrections);
|
||||
outState.putBundle("master_mind", b);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onRestoreInstanceState(Bundle savedInstanceState) {
|
||||
super.onRestoreInstanceState(savedInstanceState);
|
||||
this.init(savedInstanceState.getBundle("master_mind"));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user