retour au menu en fin de partie et presque la textview

This commit is contained in:
Haïssous Kayyissa 2023-04-09 13:42:44 +02:00
parent 03f89a8f32
commit e44c528a73
6 changed files with 144 additions and 6 deletions

View File

@ -4,6 +4,8 @@ import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.example.mastermind.game.GameView;
import com.example.mastermind.game.Saisie;
@ -25,7 +27,10 @@ public class ChoiceCombi extends Activity implements SaisieActivity {
this.saisie.initSelection(this.getResources().getColor(R.color.pionVide));
this.saisie.setChoix(this.pions);
this.view=new GameView(this,this,this.saisie, null);
setContentView(view);
this.view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
setContentView(R.layout.activity_game);
LinearLayout linearLayout = findViewById(R.id.layout);
linearLayout.addView(this.view);
}
public void changeState(){

View File

@ -5,12 +5,18 @@ import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.mastermind.end.EndView;
import com.example.mastermind.game.Bot;
import com.example.mastermind.game.GameView;
import com.example.mastermind.game.Grille;
import com.example.mastermind.game.Saisie;
import com.example.mastermind.game.TapListener;
public class GameActivity extends Activity implements SaisieActivity {
private Integer[] pionsAttaquant;
@ -23,6 +29,7 @@ public class GameActivity extends Activity implements SaisieActivity {
private Bot theBot;
private boolean state;
private View view;
private LinearLayout rootView;
@Override
@ -46,7 +53,11 @@ public class GameActivity extends Activity implements SaisieActivity {
startActivityForResult(choiceCombi, 1);
}
this.view=new GameView(this,this,this.saisie, this.grille);
setContentView(view);
this.view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
setContentView(R.layout.activity_game);
this.rootView = findViewById(R.id.layout);
this.rootView.addView(this.view);
this.rootView.setOnTouchListener(new TapListener(this));
}
@Override
@ -70,7 +81,7 @@ public class GameActivity extends Activity implements SaisieActivity {
if (!this.state) {
this.saisie.setChoix(this.pionsAttaquant);
this.grille.addNotation(this.saisie.getSelection());
end();
if(end()){return;}
this.saisie.initSelection(this.getResources().getColor(R.color.pionVide));
this.view.invalidate();
this.state = !this.state;
@ -87,7 +98,7 @@ public class GameActivity extends Activity implements SaisieActivity {
this.saisie.initSelection(this.getResources().getColor(R.color.pionVide));
//On fait noter la combinaison au Bot
this.grille.addNotation(this.theBot.notation((combi)));
end();
if(end()){return;}
this.view.invalidate();
}
}
@ -108,7 +119,7 @@ public class GameActivity extends Activity implements SaisieActivity {
this.view.invalidate();
}
public void end (){
public boolean end (){
Integer[] lastNotation = this.grille.getLastNotation();
int nbWin=0;
for (int i=0;i<4;i++){
@ -119,9 +130,13 @@ public class GameActivity extends Activity implements SaisieActivity {
if(nbWin==4) {
System.out.println("WIN");
victoire(true);
return true;
} else if (this.grille.getSizeSubs()==10){
System.out.println("LOSE");
victoire(false);
return true;
} else {
return false;
}
}
@ -129,6 +144,22 @@ public class GameActivity extends Activity implements SaisieActivity {
for (int i=0;i<4;i++){
this.saisie.addSelection(this.combiGagnante[i]);
}
EndView lastview=new EndView(this, this.grille, this.combiGagnante);
this.view=lastview;
this.rootView.removeAllViews();
this.view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
this.rootView.addView(this.view);
TextView textView = new TextView(this);
textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setGravity(Gravity.CENTER);
if (gagne){
textView.setText("Victoire de l'attaquand en test coups");
} else {
textView.setText("Victoire du défenseur");
}
this.rootView.addView(textView);
System.out.println(textView.getText());
}
//initialise les collections de pions et remplie saisie et grille de pions vides

View File

@ -0,0 +1,68 @@
package com.example.mastermind.end;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
import com.example.mastermind.GameActivity;
import com.example.mastermind.R;
import com.example.mastermind.SaisieActivity;
import com.example.mastermind.game.Grille;
import com.example.mastermind.game.Saisie;
import java.util.Collection;
import java.util.LinkedList;
public class EndView extends View {
private Integer[] combiWin;
private Grille grille;
private Paint circle;
private int nbcoups;
public EndView(Context context, Grille grille, Integer[] combiWin) {
super(context);
this.combiWin=combiWin;
this.grille=grille;
this.circle = new Paint();
}
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
this.setBackgroundColor(this.getResources().getColor(R.color.grey));
//affichage des anciennes soumissions
//copie des soumissions
LinkedList<Integer> grille = new LinkedList<Integer>();
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<Integer> notation = new LinkedList<Integer>();
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
//copie de la zone de saisie
for (int i=0;i<4;i++){
this.circle.setColor(this.combiWin[i]);
canvas.drawCircle((i*this.getWidth()/5+this.getWidth()/5),this.getHeight()-this.getHeight()*2/9, this.getWidth()/14, this.circle);
}
}
}

View File

@ -38,7 +38,6 @@ public class GameView extends View {
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
this.setBackgroundColor(this.getResources().getColor(R.color.grey));
if(this.grille!=null) {
//affichage des anciennes soumissions
//copie des soumissions

View File

@ -0,0 +1,24 @@
package com.example.mastermind.game;
import android.app.Activity;
import android.view.MotionEvent;
import android.view.View;
import com.example.mastermind.GameActivity;
import com.example.mastermind.SaisieActivity;
public class TapListener implements View.OnTouchListener{
private Activity context;
public TapListener(Activity context) {
this.context=context;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getActionMasked();
if (action == MotionEvent.ACTION_UP) {
this.context.finish();
}
return true;
}
}

View File

@ -0,0 +1,11 @@
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey"
tools:context=".GameActivity"
android:orientation="vertical">
</LinearLayout>