From f9d020a7414d747917f2bb22ab1e7d45e347dd3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kayyissa=20HA=C3=8FSSOUS?= Date: Sat, 8 Apr 2023 16:15:01 +0200 Subject: [PATCH] MVC --- app/src/main/AndroidManifest.xml | 2 +- ...{HotSeatActivity.java => ChoiceCombi.java} | 5 +- .../com/example/mastermind/GameActivity.java | 134 +++++++++++++++ .../com/example/mastermind/MainActivity.java | 4 +- .../com/example/mastermind/game/GameView.java | 161 +++--------------- .../mastermind/game/TouchListener.java | 28 +-- app/src/main/res/layout/activity_main.xml | 3 +- 7 files changed, 182 insertions(+), 155 deletions(-) rename app/src/main/java/com/example/mastermind/{HotSeatActivity.java => ChoiceCombi.java} (83%) create mode 100644 app/src/main/java/com/example/mastermind/GameActivity.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 8e95ca6..6c75b04 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -14,7 +14,7 @@ android:theme="@style/Theme.MasterMind" tools:targetApi="31"> grille = new LinkedList(); - grille.addAll(this.grille.getSoumissions()); - for (int y=0; y<10;y++) { - for (int x=0;x<4;x++) { - this.circle.setColor(grille.pop()); - //TODO: coordonnées propres - canvas.drawCircle(( x*this.getWidth()/8+(this.getWidth()*21/68)),(y*this.getHeight()/14+this.getHeight()/21), this.getWidth()/17, this.circle); + if(this.grille!=null) { + //affichage des anciennes soumissions + //copie des soumissions + LinkedList grille = new LinkedList(); + grille.addAll(this.grille.getSoumissions()); + for (int y = 0; y < 10; y++) { + for (int x = 0; x < 4; x++) { + this.circle.setColor(grille.pop()); + canvas.drawCircle((x * this.getWidth() / 8 + (this.getWidth() * 21 / 68)), (y * this.getHeight() / 14 + this.getHeight() / 21), this.getWidth() / 17, this.circle); + } + } + LinkedList notation = new LinkedList(); + notation.addAll(this.grille.getNotations()); + for(int y=0; y<10; y++) { + for(int x=0; x<2; x++) { // colonne gauche + this.circle.setColor(notation.pop()); + canvas.drawCircle((x*this.getWidth()/11+(this.getWidth()/11)),(y*this.getHeight()/14+getHeight()/21), this.getWidth()/26, this.circle); + } + for(int x=0; x<2; x++) { // colonne droite + this.circle.setColor(notation.pop()); + canvas.drawCircle((x*this.getWidth()/11+(this.getWidth()*4/5)),(y*this.getHeight()/14+getHeight()/21), this.getWidth()/26, this.circle); + } } } // affichage de la zone de saisie @@ -62,7 +65,6 @@ public class GameView extends View { Integer[] saisie = this.saisie.getSelection(); for (int i=0;i<4;i++){ this.circle.setColor(saisie[i]); - //TODO: coordonnées propres (encore) canvas.drawCircle((i*this.getWidth()/5+this.getWidth()/5),this.getHeight()-this.getHeight()*2/9, this.getWidth()/14, this.circle); } @@ -72,36 +74,16 @@ public class GameView extends View { couleurs.addAll(this.saisie.getChoix()); for (int i=0;i notation = new LinkedList(); - notation.addAll(this.grille.getNotations()); - for(int y=0; y<10; y++) { - for(int x=0; x<2; x++) { // colonne gauche - this.circle.setColor(notation.pop()); - canvas.drawCircle((x*this.getWidth()/11+(this.getWidth()/11)),(y*this.getHeight()/14+getHeight()/21), this.getWidth()/26, this.circle); - } - for(int x=0; x<2; x++) { // colonne droite - this.circle.setColor(notation.pop()); - canvas.drawCircle((x*this.getWidth()/11+(this.getWidth()*4/5)),(y*this.getHeight()/14+getHeight()/21), this.getWidth()/26, this.circle); - } - } // bouton valider - /*this.circle.setColor(this.getResources().getColor(R.color.green)); // version dessin - canvas.drawCircle((this.getWidth()/2)+(getWidth()/11)*2,this.getHeight()-this.getHeight()/16, this.getWidth()/13, this.circle);*/ validBtn = decodeSampledBitmapFromResource(getResources(), R.drawable.valid_button, getWidth()/13, getWidth()/13); // version img canvas.drawBitmap(validBtn, this.getWidth()/2+(this.getWidth()/11)*2-getWidth()/13, this.getHeight()/2+this.getHeight()*2/5, null); // bouton retour - /* this.circle.setColor(this.getResources().getColor(R.color.blue)); - canvas.drawCircle((this.getWidth()/2), this.getHeight()-this.getHeight()/16, this.getWidth()/13, this.circle);*/ backBtn = decodeSampledBitmapFromResource(getResources(), R.drawable.back_button, getWidth()/13, getWidth()/13); canvas.drawBitmap(backBtn, this.getWidth()/2-getWidth()/13, this.getHeight()/2+this.getHeight()*2/5, null); // bouton annuler - /*this.circle.setColor(this.getResources().getColor(R.color.red)); - canvas.drawCircle((this.getWidth()/2)-(getWidth()/11)*2, this.getHeight()-this.getHeight()/16, this.getWidth()/13, this.circle);*/ cancelBtn = decodeSampledBitmapFromResource(getResources(), R.drawable.cancel_button, getWidth()/13, getWidth()/13); canvas.drawBitmap(cancelBtn, this.getWidth()/2-(getWidth()/11)*2-getWidth()/13, this.getHeight()/2+this.getHeight()*2/5, null); } @@ -130,97 +112,4 @@ public class GameView extends View { options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(res, resId, options); } - - //Change l'état de soumission à notation après qu'une combinaision ai été soumise puis inversement - public void changeState() { - if(!this.bot) { - if (!this.state) { - this.saisie.setChoix(this.pionsAttaquant); - this.grille.addNotation(this.saisie.getSelection()); - victoire(); - this.saisie.initSelection(this.getResources().getColor(R.color.pionVide)); - this.invalidate(); - this.state = !this.state; - } else if (this.state && this.saisie.getSizeSelection() == 4) { - this.saisie.setChoix(this.pionsDefenseur); - this.grille.addSoumission(this.saisie.getSelection()); - this.saisie.initSelection(this.getResources().getColor(R.color.pionVide)); - invalidate(); - this.state = !this.state; - } - } else if (this.state && this.saisie.getSizeSelection() == 4) { - Integer[] combi = this.saisie.getSelection(); - this.grille.addSoumission(combi); - this.saisie.initSelection(this.getResources().getColor(R.color.pionVide)); - //On fait noter la combinaison au Bot - this.grille.addNotation(this.theBot.notation((combi))); - victoire(); - invalidate(); - } - } - - public boolean getState() { - return this.state; - } - - //ajoute une nouvelle couleur pour la séléction à soumettre - public void addChoix(int choix){ - this.saisie.addSelection(choix); - this.invalidate(); - } - - public void removePion() { - this.saisie.removeSelection(this.getResources().getColor(R.color.pionVide)); - this.invalidate(); - } - - public void clearChoix() { - this.saisie.initSelection(this.getResources().getColor(R.color.pionVide)); - this.invalidate(); - } - - public boolean victoire (){ - Integer[] lastNotation = this.grille.getLastNotation(); - int nbWin=0; - for (int i=0;i<4;i++){ - if (lastNotation[i]==this.pionsDefenseur[1]){ - nbWin++; - } - } - if(nbWin==4) { - System.out.println("WIN"); - return true; - } else { - System.out.println("LOSE"); - return false; - } - } - - //initialise les collections de pions et remplie saisie et grille de pions vides - public void initpions(){ - //on initialise les pions - //on créer une ligne de 4 pions gris représentants une ligne de pions vides - this.pionVide = this.getResources().getColor(R.color.grey); - - //Le défenseur a des pions noirs et blancs - this.pionsDefenseur = new Integer[2]; - this.pionsDefenseur[0]=this.getResources().getColor(R.color.white); - this.pionsDefenseur[1]=this.getResources().getColor(R.color.black); - - //L'attaquant a des pions de couleurs - this.pionsAttaquant = new Integer[6]; - this.pionsAttaquant[0]=this.getResources().getColor(R.color.pink); - this.pionsAttaquant[1]=this.getResources().getColor(R.color.purple); - this.pionsAttaquant[2]=this.getResources().getColor(R.color.blue); - this.pionsAttaquant[3]=this.getResources().getColor(R.color.green); - this.pionsAttaquant[4]=this.getResources().getColor(R.color.yellow); - this.pionsAttaquant[5]=this.getResources().getColor(R.color.white); - - // on inisialise la saisie - saisie.setChoix(this.pionsAttaquant); - saisie.initSelection(this.getResources().getColor(R.color.pionVide)); - - // on rempli la grille de cases grises - grille.initGrille(this.pionVide); - } } diff --git a/app/src/main/java/com/example/mastermind/game/TouchListener.java b/app/src/main/java/com/example/mastermind/game/TouchListener.java index 03fea90..b8a0c5b 100644 --- a/app/src/main/java/com/example/mastermind/game/TouchListener.java +++ b/app/src/main/java/com/example/mastermind/game/TouchListener.java @@ -3,11 +3,13 @@ package com.example.mastermind.game; import android.view.MotionEvent; import android.view.View; +import com.example.mastermind.GameActivity; + public class TouchListener implements View.OnTouchListener{ - private GameView view; + private GameActivity context; private int i = 0; - public TouchListener(GameView view) { - this.view=view; + public TouchListener(GameActivity context) { + this.context=context; } @Override public boolean onTouch(View v, MotionEvent event) { @@ -20,30 +22,30 @@ public class TouchListener implements View.OnTouchListener{ //Surveille quel bouton de couleur est choisi if (v.getHeight()-v.getHeight()/7-v.getWidth()/16