2023-03-19 22:36:49 +01:00
|
|
|
package com.example.mastermind;
|
|
|
|
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Bundle;
|
2023-04-08 15:30:51 +02:00
|
|
|
import android.util.Log;
|
2023-04-07 14:29:23 +02:00
|
|
|
import android.view.View;
|
|
|
|
import android.widget.LinearLayout;
|
2023-03-19 22:36:49 +01:00
|
|
|
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
|
2023-04-07 16:08:31 +02:00
|
|
|
import com.example.mastermind.controller.mastermind.MonNextTurnTouch;
|
2023-04-07 14:29:23 +02:00
|
|
|
import com.example.mastermind.controller.mastermind.MonOnTouchListener;
|
|
|
|
import com.example.mastermind.vue.mastermind.UnePiece;
|
|
|
|
|
2023-03-19 22:36:49 +01:00
|
|
|
public class MasterMindActivity extends AppCompatActivity {
|
|
|
|
|
2023-04-07 14:29:23 +02:00
|
|
|
private int[] code;
|
2023-04-07 16:08:31 +02:00
|
|
|
|
|
|
|
private int tour;
|
|
|
|
|
|
|
|
private LinearLayout jeu;
|
|
|
|
|
|
|
|
private LinearLayout correction;
|
|
|
|
|
|
|
|
private boolean vide;
|
2023-03-19 22:36:49 +01:00
|
|
|
@Override
|
|
|
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_master_mind);
|
|
|
|
Intent data=this.getIntent();
|
2023-04-07 14:29:23 +02:00
|
|
|
this.code=data.getIntArrayExtra("code");
|
2023-04-07 16:08:31 +02:00
|
|
|
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));
|
2023-04-07 14:29:23 +02:00
|
|
|
|
2023-04-07 16:08:31 +02:00
|
|
|
//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));
|
2023-04-06 22:51:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBackPressed() {
|
|
|
|
Intent menu=new Intent(this, MenuActivity.class);
|
|
|
|
this.startActivity(menu);
|
|
|
|
this.finish();
|
2023-03-19 22:36:49 +01:00
|
|
|
}
|
2023-04-07 16:08:31 +02:00
|
|
|
|
|
|
|
public void nextTurn(){
|
|
|
|
if(this.tour<9){
|
|
|
|
//on affiche la correction
|
2023-04-08 15:30:51 +02:00
|
|
|
boolean gagne = this.afficherCorrection((LinearLayout) this.jeu.getChildAt(this.tour));
|
2023-04-07 16:08:31 +02:00
|
|
|
//on supprime les listener
|
2023-04-08 15:43:07 +02:00
|
|
|
LinearLayout anciennesPieces =(LinearLayout) this.jeu.getChildAt(this.tour);
|
2023-04-07 16:08:31 +02:00
|
|
|
|
2023-04-08 15:43:07 +02:00
|
|
|
for(int i=0; i<anciennesPieces.getChildCount(); i++){anciennesPieces.getChildAt(i).setOnTouchListener(null);}
|
2023-04-08 15:30:51 +02:00
|
|
|
|
|
|
|
if (gagne){
|
|
|
|
this.finDePartie(true);
|
|
|
|
} else {
|
|
|
|
//on incremente le tour
|
|
|
|
this.tour++;
|
|
|
|
//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));
|
|
|
|
}
|
2023-04-07 16:08:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}else{
|
|
|
|
//on affiche la correction
|
2023-04-08 15:30:51 +02:00
|
|
|
boolean gagne = this.afficherCorrection((LinearLayout) this.jeu.getChildAt(this.tour));
|
2023-04-07 16:08:31 +02:00
|
|
|
//on supprime les listener
|
|
|
|
if(this.tour>0){
|
|
|
|
LinearLayout anciennesPieces =(LinearLayout) this.jeu.getChildAt(this.tour-1);
|
|
|
|
|
|
|
|
for(int i=0; i<anciennesPieces.getChildCount(); i++){anciennesPieces.getChildAt(i).setOnTouchListener(null);}
|
|
|
|
}
|
2023-04-08 15:30:51 +02:00
|
|
|
this.finDePartie(gagne);
|
2023-04-07 16:08:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-08 15:30:51 +02:00
|
|
|
public boolean afficherCorrection(LinearLayout pieces){
|
2023-04-07 16:08:31 +02:00
|
|
|
int[] colorpiece=new int[4];
|
|
|
|
for(int i=0; i<4; i++){
|
|
|
|
colorpiece[i]=((UnePiece)pieces.getChildAt(i)).getColor();
|
|
|
|
}
|
|
|
|
LinearLayout correctionsPieces=(LinearLayout) this.correction.getChildAt(this.tour);
|
|
|
|
//si on a gagner
|
|
|
|
boolean gagner=true;
|
|
|
|
|
|
|
|
for(int i=0; i<4; i++){
|
|
|
|
if(colorpiece[i] == this.code[i]){
|
|
|
|
//bien placer bon endroit
|
|
|
|
((UnePiece)correctionsPieces.getChildAt(i)).setColor(4);
|
|
|
|
}else{
|
|
|
|
gagner=false;
|
|
|
|
boolean nombre=false;
|
|
|
|
for(int j=0; j<4; j++){
|
|
|
|
if(colorpiece[i] == code[j] && colorpiece[j]!=code[j]){
|
|
|
|
nombre=true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(nombre){
|
|
|
|
//mal placer
|
|
|
|
((UnePiece)correctionsPieces.getChildAt(i)).setColor(5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-08 15:30:51 +02:00
|
|
|
return gagner;
|
2023-04-07 16:08:31 +02:00
|
|
|
}
|
|
|
|
|
2023-04-08 15:30:51 +02:00
|
|
|
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);
|
|
|
|
fin.putExtra("code", this.code);
|
|
|
|
this.startActivity(fin);
|
|
|
|
this.finish();
|
2023-04-07 16:08:31 +02:00
|
|
|
}
|
2023-03-19 22:36:49 +01:00
|
|
|
}
|