mise en place activite choixcombi (startactivityforresult)

This commit is contained in:
Haïssous Kayyissa 2023-04-08 17:38:12 +02:00
parent f9d020a741
commit 30fd4f018e
6 changed files with 69 additions and 21 deletions

View File

@ -28,6 +28,9 @@
<activity <activity
android:name=".RulesActivity" android:name=".RulesActivity"
android:exported="false"/> android:exported="false"/>
<activity
android:name=".ChoiceCombi"
android:exported="false"/>
<activity <activity
android:name=".MainActivity" android:name=".MainActivity"
android:exported="true"> android:exported="true">

View File

@ -1,19 +1,51 @@
package com.example.mastermind; package com.example.mastermind;
import android.app.Activity; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.View;
import com.example.mastermind.game.GameView; import com.example.mastermind.game.GameView;
import com.example.mastermind.game.Grille;
import com.example.mastermind.game.Saisie; import com.example.mastermind.game.Saisie;
public class HotSeatActivity extends Activity { public class ChoiceCombi extends Activity implements SaisieActivity {
private Saisie saisie;
private Integer[] combiGagnante;
private Integer[] pions;
private View view;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
boolean bot = getIntent().getBooleanExtra("bot", false); int[] tab = getIntent().getIntArrayExtra("pions");
if setContentView(new ChoiceCombi(this, new Saisie())); this.pions=new Integer[6];
for (int i=0;i<tab.length-1;i++) {
this.pions[i] = tab[i];
}
this.saisie=new Saisie();
this.saisie.initSelection(this.getResources().getColor(R.color.pionVide));
this.saisie.setChoix(this.pions);
this.view=new GameView(this,this,this.saisie, null);
} }
public void letsplay() public void changeState(){
Intent returnIntent = new Intent();
returnIntent.putExtra("choix",this.saisie.getSelection());
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
public void addChoix(int choix){
this.saisie.addSelection(choix);
this.view.invalidate();
}
public void removePion() {
this.saisie.removeSelection(this.getResources().getColor(R.color.pionVide));
this.view.invalidate();
}
public void clearChoix() {
this.saisie.initSelection(this.getResources().getColor(R.color.pionVide));
this.view.invalidate();
}
} }

View File

@ -1,6 +1,7 @@
package com.example.mastermind; package com.example.mastermind;
import android.app.Activity; import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Paint; import android.graphics.Paint;
import android.os.Bundle; import android.os.Bundle;
@ -11,7 +12,7 @@ import com.example.mastermind.game.GameView;
import com.example.mastermind.game.Grille; import com.example.mastermind.game.Grille;
import com.example.mastermind.game.Saisie; import com.example.mastermind.game.Saisie;
public class GameActivity extends Activity { public class GameActivity extends Activity implements SaisieActivity {
private Integer[] pionsAttaquant; private Integer[] pionsAttaquant;
private Integer[] pionsDefenseur; private Integer[] pionsDefenseur;
private Integer pionVide; private Integer pionVide;
@ -33,9 +34,15 @@ public class GameActivity extends Activity {
initpions(); initpions();
this.theBot=new Bot(this.pionsAttaquant, this.pionsDefenseur, this.pionVide); this.theBot=new Bot(this.pionsAttaquant, this.pionsDefenseur, this.pionVide);
if(!this.bot){ if(!this.bot){
//ChoiceCombi Intent choiceCombi = new Intent(this, ChoiceCombi.class);
int[] tabpions=new int[6];
for (int i=0;i<this.pionsAttaquant.length;i++) {
tabpions[i] = this.pionsAttaquant[i];
} }
this.view=new GameView(this,this.saisie, this.grille); choiceCombi.putExtra("pions", tabpions);
startActivityForResult(choiceCombi, 1);
}
this.view=new GameView(this,this,this.saisie, this.grille);
setContentView(view); setContentView(view);
} }
@ -67,10 +74,6 @@ public class GameActivity extends Activity {
} }
} }
public boolean getState() {
return this.state;
}
//ajoute une nouvelle couleur pour la séléction à soumettre //ajoute une nouvelle couleur pour la séléction à soumettre
public void addChoix(int choix){ public void addChoix(int choix){
this.saisie.addSelection(choix); this.saisie.addSelection(choix);

View File

@ -0,0 +1,12 @@
package com.example.mastermind;
public interface SaisieActivity {
public void addChoix(int index);
void changeState();
void removePion();
void clearChoix();
}

View File

@ -1,5 +1,6 @@
package com.example.mastermind.game; package com.example.mastermind.game;
import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Bitmap; import android.graphics.Bitmap;
@ -11,6 +12,7 @@ import android.view.View;
import com.example.mastermind.GameActivity; import com.example.mastermind.GameActivity;
import com.example.mastermind.R; import com.example.mastermind.R;
import com.example.mastermind.SaisieActivity;
import java.util.Collection; import java.util.Collection;
import java.util.LinkedList; import java.util.LinkedList;
@ -23,8 +25,8 @@ public class GameView extends View {
private Bitmap cancelBtn; private Bitmap cancelBtn;
private Bitmap backBtn; private Bitmap backBtn;
private Bitmap validBtn; private Bitmap validBtn;
public GameView(GameActivity context, Saisie saisie, Grille grille) { public GameView(Context con, SaisieActivity context, Saisie saisie, Grille grille) {
super(context); super(con);
this.saisie=saisie; this.saisie=saisie;
this.grille=grille; this.grille=grille;
this.setOnTouchListener(new TouchListener(context)); this.setOnTouchListener(new TouchListener(context));

View File

@ -4,11 +4,11 @@ import android.view.MotionEvent;
import android.view.View; import android.view.View;
import com.example.mastermind.GameActivity; import com.example.mastermind.GameActivity;
import com.example.mastermind.SaisieActivity;
public class TouchListener implements View.OnTouchListener{ public class TouchListener implements View.OnTouchListener{
private GameActivity context; private SaisieActivity context;
private int i = 0; public TouchListener(SaisieActivity context) {
public TouchListener(GameActivity context) {
this.context=context; this.context=context;
} }
@Override @Override
@ -25,9 +25,6 @@ public class TouchListener implements View.OnTouchListener{
this.context.addChoix(0); this.context.addChoix(0);
} else if(v.getWidth()*2/13+v.getWidth()/8-v.getWidth()/15<x && x<v.getWidth()*2/13+v.getWidth()/8+v.getWidth()/15){ } else if(v.getWidth()*2/13+v.getWidth()/8-v.getWidth()/15<x && x<v.getWidth()*2/13+v.getWidth()/8+v.getWidth()/15){
this.context.addChoix(1); this.context.addChoix(1);
if(!this.context.getState()) {
i++;
}
} else if(2*v.getWidth()*2/13+v.getWidth()/8-v.getWidth()/15<x && x<2*v.getWidth()*2/13+v.getWidth()/8+v.getWidth()/15) { } else if(2*v.getWidth()*2/13+v.getWidth()/8-v.getWidth()/15<x && x<2*v.getWidth()*2/13+v.getWidth()/8+v.getWidth()/15) {
this.context.addChoix(2); this.context.addChoix(2);
} else if(3*v.getWidth()*2/13+v.getWidth()/8-v.getWidth()/15<x && x<3*v.getWidth()*2/13+v.getWidth()/8+v.getWidth()/15){ } else if(3*v.getWidth()*2/13+v.getWidth()/8-v.getWidth()/15<x && x<3*v.getWidth()*2/13+v.getWidth()/8+v.getWidth()/15){
@ -41,7 +38,6 @@ public class TouchListener implements View.OnTouchListener{
} else if (v.getHeight()-v.getWidth()/16-v.getWidth()/13<y && y<v.getHeight()-v.getHeight()/16+v.getWidth()/13){ } else if (v.getHeight()-v.getWidth()/16-v.getWidth()/13<y && y<v.getHeight()-v.getHeight()/16+v.getWidth()/13){
if(v.getWidth()/2+(v.getWidth()/11)*2-v.getWidth()/13<x && x<v.getWidth()/2+(v.getWidth()/11)*2+v.getWidth()/13) { // soumettre if(v.getWidth()/2+(v.getWidth()/11)*2-v.getWidth()/13<x && x<v.getWidth()/2+(v.getWidth()/11)*2+v.getWidth()/13) { // soumettre
this.context.changeState(); this.context.changeState();
i = 0;
} else if (v.getWidth()/2-v.getWidth()/13<x && x<v.getWidth()/2+v.getWidth()/13) { // retour } else if (v.getWidth()/2-v.getWidth()/13<x && x<v.getWidth()/2+v.getWidth()/13) { // retour
this.context.removePion(); this.context.removePion();
} else if (v.getWidth()/2-(v.getWidth()/11)*2-v.getWidth()/13<x && x<v.getWidth()/2-(v.getWidth()/11)*2+v.getWidth()/13) { // annuler } else if (v.getWidth()/2-(v.getWidth()/11)*2-v.getWidth()/13<x && x<v.getWidth()/2-(v.getWidth()/11)*2+v.getWidth()/13) { // annuler