ajout des tours

This commit is contained in:
martins 2023-04-07 16:08:31 +02:00
parent 17f16b35d7
commit 2ec61de83d
8 changed files with 140 additions and 22 deletions

View File

@ -8,25 +8,39 @@ import android.widget.LinearLayout;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.example.mastermind.controller.mastermind.MonNextTurnTouch;
import com.example.mastermind.controller.mastermind.MonOnTouchListener;
import com.example.mastermind.vue.mastermind.UnePiece;
public class MasterMindActivity extends AppCompatActivity {
private int[] code;
private int tour;
private LinearLayout jeu;
private LinearLayout correction;
private boolean vide;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_master_mind);
Intent data=this.getIntent();
this.code=data.getIntArrayExtra("code");
LinearLayout l=this.findViewById(R.id.jeu);
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));
for(int i=0; i<l.getChildCount(); i++){
LinearLayout fils = (LinearLayout) l.getChildAt(i);
for(int j=0; j<fils.getChildCount(); j++){
fils.getChildAt(j).setOnTouchListener(new MonOnTouchListener((UnePiece) fils.getChildAt(j), data.getBooleanExtra("vide", false)));
}
//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));
}
}
@ -36,4 +50,73 @@ public class MasterMindActivity extends AppCompatActivity {
this.startActivity(menu);
this.finish();
}
public void nextTurn(){
if(this.tour<9){
//on affiche la correction
this.afficherCorrection((LinearLayout) this.jeu.getChildAt(this.tour));
//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);}
}
//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));
}
}else{
//on affiche la correction
this.afficherCorrection((LinearLayout) this.jeu.getChildAt(this.tour));
//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);}
}
this.finDePartie();
}
}
public void afficherCorrection(LinearLayout pieces){
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);
}
}
}
if(gagner){
this.finDePartie();
}
}
public void finDePartie(){
}
}

View File

@ -0,0 +1,22 @@
package com.example.mastermind.controller.mastermind;
import android.view.MotionEvent;
import android.view.View;
import com.example.mastermind.MasterMindActivity;
public class MonNextTurnTouch implements View.OnTouchListener {
private MasterMindActivity master;
public MonNextTurnTouch(MasterMindActivity m){
this.master=m;
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(motionEvent.getActionMasked() == MotionEvent.ACTION_UP)
this.master.nextTurn();
return false;
}
}

View File

@ -35,10 +35,14 @@ public class ObservateurMenuDebutPartie implements View.OnClickListener {
Random r=new Random();
int tab[]=new int[4];
tab[0]=r.nextInt(6);
tab[1]=r.nextInt(6);
tab[2]=r.nextInt(6);
tab[3]=r.nextInt(6);
int max=5;
if(vide.isChecked()){
max++;
}
tab[0]=r.nextInt(max);
tab[1]=r.nextInt(max);
tab[2]=r.nextInt(max);
tab[3]=r.nextInt(max);
mastermind.putExtra("code", tab);
menu.startActivity(mastermind);

View File

@ -92,15 +92,12 @@ public abstract class MonPaint {
return MonPaint.getRouge();
case 1:
return MonPaint.getBleue();
case 2:
return MonPaint.getVerte();
case 3:
return MonPaint.getJaune();
case 4:
return MonPaint.getNoir();
case 5:
return MonPaint.getBlanche();
case 6:

View File

@ -13,6 +13,6 @@ public class PieceCorrection extends UnePiece{
@Override
protected int getRaduis() {
return 20;
return 15;
}
}

View File

@ -16,7 +16,7 @@ public class UnePiece extends View {
public UnePiece(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
this.color=4;
this.color=0;
}
@Override
@ -31,7 +31,7 @@ public class UnePiece extends View {
}
protected int getRaduis() {
return 40;
return 30;
}
public int getColor() {

View File

@ -7,6 +7,19 @@
android:background="@color/marron"
android:gravity="center_vertical"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/correction"
android:layout_alignParentLeft="true"
android:layout_marginLeft="@dimen/titre"
android:layout_marginRight="@dimen/titre"
android:id="@+id/tour"
android:text="Valider"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@ -23,7 +36,7 @@
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="vertical"
android:layout_alignParentBottom="true">
android:layout_above="@+id/tour">
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center">
@ -105,12 +118,12 @@
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="right"
android:orientation="vertical"
android:id="@+id/correction"
android:layout_toRightOf="@+id/jeu"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true">
android:layout_above="@+id/tour">
<LinearLayout android:layout_width="wrap_content" android:layout_height="@dimen/pieceM" android:gravity="center" android:layout_margin="@dimen/margeM">
@ -176,5 +189,4 @@
</LinearLayout>
</RelativeLayout>

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="pieceM">40dp</dimen>
<dimen name="pieceM">30dp</dimen>
<dimen name="margeM">10dp</dimen>
<dimen name="correction">20dp</dimen>
<dimen name="correction">15dp</dimen>
<dimen name="marge_correction">5dp</dimen>
<dimen name="titre">30dp</dimen>
</resources>