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.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.Log;
|
|
||||||
import android.view.View;
|
|
||||||
import android.widget.LinearLayout;
|
import android.widget.LinearLayout;
|
||||||
|
|
||||||
import androidx.annotation.Nullable;
|
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.controller.mastermind.MonOnTouchListener;
|
||||||
import com.example.mastermind.vue.mastermind.UnePiece;
|
import com.example.mastermind.vue.mastermind.UnePiece;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class MasterMindActivity extends AppCompatActivity {
|
public class MasterMindActivity extends AppCompatActivity {
|
||||||
|
|
||||||
private int[] code;
|
private int[] code;
|
||||||
@ -24,6 +24,11 @@ public class MasterMindActivity extends AppCompatActivity {
|
|||||||
private LinearLayout correction;
|
private LinearLayout correction;
|
||||||
|
|
||||||
private boolean vide;
|
private boolean vide;
|
||||||
|
|
||||||
|
private ArrayList<ArrayList<Integer>> combinaisons;
|
||||||
|
|
||||||
|
private ArrayList<ArrayList<Integer>> corrections;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
@ -33,16 +38,34 @@ public class MasterMindActivity extends AppCompatActivity {
|
|||||||
this.vide=data.getBooleanExtra("vide", false);
|
this.vide=data.getBooleanExtra("vide", false);
|
||||||
this.jeu=this.findViewById(R.id.jeu);
|
this.jeu=this.findViewById(R.id.jeu);
|
||||||
this.correction=this.findViewById(R.id.correction);
|
this.correction=this.findViewById(R.id.correction);
|
||||||
this.tour=0;
|
|
||||||
this.findViewById(R.id.tour).setOnTouchListener(new MonNextTurnTouch(this));
|
boolean firstInit = true;
|
||||||
|
if (savedInstanceState != null) {
|
||||||
//on récupere le LinearLayout des pieces du tour
|
if (savedInstanceState.containsKey("master_mind")) {
|
||||||
LinearLayout pieces =(LinearLayout) this.jeu.getChildAt(this.tour);
|
firstInit = false;
|
||||||
//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));
|
|
||||||
}
|
}
|
||||||
|
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
|
@Override
|
||||||
@ -56,10 +79,15 @@ public class MasterMindActivity extends AppCompatActivity {
|
|||||||
if(this.tour<9){
|
if(this.tour<9){
|
||||||
//on affiche la correction
|
//on affiche la correction
|
||||||
boolean gagne = this.afficherCorrection((LinearLayout) this.jeu.getChildAt(this.tour));
|
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);
|
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){
|
if (gagne){
|
||||||
this.finDePartie(true);
|
this.finDePartie(true);
|
||||||
@ -94,6 +122,7 @@ public class MasterMindActivity extends AppCompatActivity {
|
|||||||
colorpiece[i]=((UnePiece)pieces.getChildAt(i)).getColor();
|
colorpiece[i]=((UnePiece)pieces.getChildAt(i)).getColor();
|
||||||
}
|
}
|
||||||
LinearLayout correctionsPieces=(LinearLayout) this.correction.getChildAt(this.tour);
|
LinearLayout correctionsPieces=(LinearLayout) this.correction.getChildAt(this.tour);
|
||||||
|
ArrayList<Integer> correction = new ArrayList<>();
|
||||||
//si on a gagner
|
//si on a gagner
|
||||||
boolean gagner=true;
|
boolean gagner=true;
|
||||||
|
|
||||||
@ -101,6 +130,7 @@ public class MasterMindActivity extends AppCompatActivity {
|
|||||||
if(colorpiece[i] == this.code[i]){
|
if(colorpiece[i] == this.code[i]){
|
||||||
//bien placer bon endroit
|
//bien placer bon endroit
|
||||||
((UnePiece)correctionsPieces.getChildAt(i)).setColor(4);
|
((UnePiece)correctionsPieces.getChildAt(i)).setColor(4);
|
||||||
|
correction.add(4);
|
||||||
}else{
|
}else{
|
||||||
gagner=false;
|
gagner=false;
|
||||||
boolean nombre=false;
|
boolean nombre=false;
|
||||||
@ -112,14 +142,18 @@ public class MasterMindActivity extends AppCompatActivity {
|
|||||||
if(nombre){
|
if(nombre){
|
||||||
//mal placer
|
//mal placer
|
||||||
((UnePiece)correctionsPieces.getChildAt(i)).setColor(5);
|
((UnePiece)correctionsPieces.getChildAt(i)).setColor(5);
|
||||||
|
correction.add(5);
|
||||||
|
}else {
|
||||||
|
//mauvais
|
||||||
|
correction.add(6);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.corrections.add(correction);
|
||||||
return gagner;
|
return gagner;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void finDePartie(boolean trouve){
|
public void finDePartie(boolean trouve){
|
||||||
Log.d("finDePartie", " finDePartie: "+trouve);
|
|
||||||
Intent fin=new Intent(this, FinDePartieActivity.class);
|
Intent fin=new Intent(this, FinDePartieActivity.class);
|
||||||
fin.putExtra("trouve", trouve);
|
fin.putExtra("trouve", trouve);
|
||||||
fin.putExtra("tour", this.tour);
|
fin.putExtra("tour", this.tour);
|
||||||
@ -127,4 +161,59 @@ public class MasterMindActivity extends AppCompatActivity {
|
|||||||
this.startActivity(fin);
|
this.startActivity(fin);
|
||||||
this.finish();
|
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