Я написал базовый c код, в котором, если игра заканчивается, на всплывающем экране появляются две кнопки (повторное совпадение и главное меню). Он отлично работает для первых двух игр. Но застревает, если мы нажимаем эту кнопку в третий раз. Также в некоторых случаях он зависает при 2-м щелчке
For Eg:
If after game ends if we click on Re-Match button it works.
After playing the game again if we click on Re-match button it works
now the 3rd time when we click on Re-match button it gets stuck.
Same happens fot he Main menu button
Ниже мой код.
public class AI extends AppCompatActivity {
String playName,player;
boolean gameActive = true;
int activePlayer = 0;
int[] gameState = {2, 2 , 2, 2, 2, 2, 2, 2, 2};
int[][] winPositions = {{0,1,2}, {3,4,5}, {6,7,8},
{0,3,6}, {1,4,7}, {2,5,8},
{0,4,8}, {2,4,6}};
Set<Integer> tappedArray = new LinkedHashSet();
Set<Integer> computerArray = new LinkedHashSet();
Set<Integer> mergedSet = new HashSet<Integer>();
ImageView computerImg;
public void playerTap(final View view){
ImageView img = (ImageView) view;
Intent in= getIntent();
playName = in.getStringExtra("playName");
int tappedImage = Integer.parseInt(img.getTag().toString());
if(!gameActive){
gameReset(view);
}
if(gameState[tappedImage] == 2) {
gameState[tappedImage] = activePlayer;
img.setTranslationY(-1000f);
if (activePlayer == 0) {
if("popeyevsbluto".equals(playName)) {
player="Popeye";
img.setImageResource(R.drawable.popeyesolo);
activePlayer = 1;
TextView status = findViewById(R.id.status);
status.setText("Bluto's Turn - Tap to play");
}
else if("dectervsdeedee".equals(playName)) {
player="Dexter";
img.setImageResource(R.drawable.dextersolo);
activePlayer = 1;
TextView status = findViewById(R.id.status);
status.setText("DeeDee's Turn - Tap to play");
}
else if("tomvsjerry".equals(playName)) {
player="Jerry";
img.setImageResource(R.drawable.jerrysolo);
activePlayer = 1;
TextView status = findViewById(R.id.status);
status.setText("Tom's Turn - Tap to play");
}
else if("couragevseustace".equals(playName)) {
player="Courage";
img.setImageResource(R.drawable.couragesolo);
activePlayer = 1;
TextView status = findViewById(R.id.status);
status.setText("Eustace's Turn - Tap to play");
}
else if("flashvszoom".equals(playName)) {
player="Flash";
img.setImageResource(R.drawable.flashsolo);
activePlayer = 1;
TextView status = findViewById(R.id.status);
status.setText("Zoom's Turn - Tap to play");
}
else if("greenvssinestro".equals(playName)) {
player="Green Lantern";
img.setImageResource(R.drawable.greensolo);
activePlayer = 1;
TextView status = findViewById(R.id.status);
status.setText("Sinestro's Turn - Tap to play");
}
else if("supermanvssiterio".equals(playName)) {
player="Superman";
img.setImageResource(R.drawable.supermansolo);
activePlayer = 1;
TextView status = findViewById(R.id.status);
status.setText("Lex Luthor's Turn - Tap to play");
}
else if("batmanvsjoker".equals(playName)) {
player="Batman";
img.setImageResource(R.drawable.batman);
activePlayer = 1;
TextView status = findViewById(R.id.status);
status.setText("Joker's Turn - Tap to play");
}
else if("captainvsred".equals(playName)) {
player="Captain America";
img.setImageResource(R.drawable.captainsolo);
activePlayer = 1;
TextView status = findViewById(R.id.status);
status.setText("Red Skull's Turn - Tap to play");
}
else if("thanosvsironman".equals(playName)) {
player="Thanos";
img.setImageResource(R.drawable.thanossolo);
activePlayer = 1;
TextView status = findViewById(R.id.status);
status.setText("Iron man's Turn - Tap to play");
}
else if("spidermanvsgreen".equals(playName)) {
player="Spider Man";
img.setImageResource(R.drawable.spidermansolo);
activePlayer = 1;
TextView status = findViewById(R.id.status);
status.setText("Green Goblin's Turn - Tap to play");
}
else if("thorvslokee".equals(playName)) {
player="Thor";
img.setImageResource(R.drawable.thorsolo);
activePlayer = 1;
TextView status = findViewById(R.id.status);
status.setText("Loki's Turn - Tap to play");
}
else if("bheemvskirmada".equals(playName)) {
player="Bheem";
img.setImageResource(R.drawable.bheemsolo);
activePlayer = 1;
TextView status = findViewById(R.id.status);
status.setText("Kirmada's Turn - Tap to play");
}
else if("nobitavsgian".equals(playName)) {
player="Nobita";
img.setImageResource(R.drawable.nobitasolo);
activePlayer = 1;
TextView status = findViewById(R.id.status);
status.setText("Gian's Turn - Tap to play");
}
else if("oggyvscock".equals(playName)) {
player="Oggy";
img.setImageResource(R.drawable.oggysolo);
activePlayer = 1;
TextView status = findViewById(R.id.status);
status.setText("Cockroache's Turn - Tap to play");
}
}
img.animate().translationYBy(1000f).setDuration(300);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
computerplay(view);
}
},500);
}
for(int[] winPosition: winPositions){
if(gameState[winPosition[0]] == gameState[winPosition[1]] &&
gameState[winPosition[1]] == gameState[winPosition[2]] &&
gameState[winPosition[0]]!=2){
String winnerStr;
gameActive = false;
if(gameState[winPosition[0]] == 0){
winnerStr = player;
}
else{
winnerStr = player;
}
AlertDialog.Builder builder= new AlertDialog.Builder(AI.this);
builder.setTitle(winnerStr +" Won The Match");
builder.setPositiveButton("Re - Match", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
gameReset(view);
}
});
builder.setNeutralButton("Main Menu", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent in=new Intent(AI.this, MainActivity.class);
startActivity(in);
}
});
AlertDialog alertDialog=builder.create();
alertDialog.show();
}
}
boolean draw=true;
for(int i=0; i < gameState.length;i++){
if(gameState[i] == 2){
draw= false;
}
}
if(draw){
AlertDialog.Builder builder= new AlertDialog.Builder(AI.this);
builder.setTitle("Having Fun.?");
builder.setPositiveButton("Re - Match", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
gameReset(view);
}
});
builder.setNeutralButton("Main Menu", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent in=new Intent(AI.this, MainActivity.class);
startActivity(in);
}
});
AlertDialog alertDialog=builder.create();
alertDialog.show();
}
}
public void computerplay(View view){
int computer;
ImageView img = (ImageView) view;
int tappedImage = Integer.parseInt(img.getTag().toString());
tappedArray.add(tappedImage);
mergedSet.addAll(tappedArray);
mergedSet.addAll(computerArray);
int length = gameState.length;
do {
computer = (int) (Math.random() * length);
}
while(mergedSet.contains(computer));
computerArray.add(computer);
if(computer == 0) {
computerImg = findViewById(R.id.imageView0);
}
else if(computer == 1) {
computerImg = findViewById(R.id.imageView1);
}
else if(computer == 2) {
computerImg = findViewById(R.id.imageView2);
}
else if(computer == 3) {
computerImg = findViewById(R.id.imageView3);
}
else if(computer == 4) {
computerImg = findViewById(R.id.imageView4);
}
else if(computer == 5) {
computerImg = findViewById(R.id.imageView5);
}
else if(computer == 6) {
computerImg = findViewById(R.id.imageView6);
}
else if(computer == 7) {
computerImg = findViewById(R.id.imageView7);
}
else if(computer == 8) {
computerImg = findViewById(R.id.imageView8);
}
if("popeyevsbluto".equals(playName)) {
player = "Bluto";
computerImg.setImageResource(R.drawable.blutosolo);
activePlayer = 0;
TextView status = findViewById(R.id.status);
status.setText("Popeye's Turn - Tap to play");
}
else if("dectervsdeedee".equals(playName)) {
player = "DeeDee";
computerImg.setImageResource(R.drawable.deedeesolo);
activePlayer = 0;
TextView status = findViewById(R.id.status);
status.setText("Dexter's Turn - Tap to play");
}
else if("tomvsjerry".equals(playName)) {
player = "Tom";
computerImg.setImageResource(R.drawable.tomsolo);
activePlayer = 0;
TextView status = findViewById(R.id.status);
status.setText("Jerry's Turn - Tap to play");
}
else if("couragevseustace".equals(playName)) {
player = "Eustace";
computerImg.setImageResource(R.drawable.eustacesolo);
activePlayer = 0;
TextView status = findViewById(R.id.status);
status.setText("Courage's Turn - Tap to play");
}
else if("flashvszoom".equals(playName)) {
player = "Zoom";
computerImg.setImageResource(R.drawable.zoomsolo);
activePlayer = 0;
TextView status = findViewById(R.id.status);
status.setText("Flash's Turn - Tap to play");
}
else if("greenvssinestro".equals(playName)) {
player = "Sinestor";
computerImg.setImageResource(R.drawable.sinestrosolo);
activePlayer = 0;
TextView status = findViewById(R.id.status);
status.setText("Green Lantern's Turn - Tap to play");
}
else if("supermanvssiterio".equals(playName)) {
player = "Lex Luthor";
computerImg.setImageResource(R.drawable.lexluthorsolo);
activePlayer = 0;
TextView status = findViewById(R.id.status);
status.setText("Superman 's Turn - Tap to play");
}
else if("batmanvsjoker".equals(playName)) {
player = "Joker";
computerImg.setImageResource(R.drawable.joker);
activePlayer = 0;
TextView status = findViewById(R.id.status);
status.setText("Batman's Turn - Tap to play");
}
else if("captainvsred".equals(playName)) {
player = "Red Skull";
computerImg.setImageResource(R.drawable.redskullsolo);
activePlayer = 0;
TextView status = findViewById(R.id.status);
status.setText("Captain America's Turn - Tap to play");
}
else if("thanosvsironman".equals(playName)) {
player = "Iron Man";
computerImg.setImageResource(R.drawable.ironmansolo);
activePlayer = 0;
TextView status = findViewById(R.id.status);
status.setText("Thano's Turn - Tap to play");
}
else if("spidermanvsgreen".equals(playName)) {
player = "Green Goblin";
computerImg.setImageResource(R.drawable.greengoblinsolo);
activePlayer = 0;
TextView status = findViewById(R.id.status);
status.setText("Spiderman's Turn - Tap to play");
}
else if("thorvslokee".equals(playName)) {
player = "Loki";
computerImg.setImageResource(R.drawable.lokisolo);
activePlayer = 0;
TextView status = findViewById(R.id.status);
status.setText("Thor's Turn - Tap to play");
}
else if("bheemvskirmada".equals(playName)) {
player = "Kirmada";
computerImg.setImageResource(R.drawable.kirmadasolo);
activePlayer = 0;
TextView status = findViewById(R.id.status);
status.setText("Bheem's Turn - Tap to play");
}
else if("nobitavsgian".equals(playName)) {
player = "Gian";
computerImg.setImageResource(R.drawable.gian);
activePlayer = 0;
TextView status = findViewById(R.id.status);
status.setText("Nobita's Turn - Tap to play");
}
else if("oggyvscock".equals(playName)) {
player = "Coackroaches";
computerImg.setImageResource(R.drawable.cocksolo);
activePlayer = 0;
TextView status = findViewById(R.id.status);
status.setText("Oggy's Turn - Tap to play");
}
}
public void gameReset(View view) {
gameActive = true;
activePlayer = 0;
for(int i=0; i<gameState.length; i++){
gameState[i] = 2;
}
((ImageView)findViewById(R.id.imageView0)).setImageResource(0);
((ImageView)findViewById(R.id.imageView1)).setImageResource(0);
((ImageView)findViewById(R.id.imageView2)).setImageResource(0);
((ImageView)findViewById(R.id.imageView3)).setImageResource(0);
((ImageView)findViewById(R.id.imageView4)).setImageResource(0);
((ImageView)findViewById(R.id.imageView5)).setImageResource(0);
((ImageView)findViewById(R.id.imageView6)).setImageResource(0);
((ImageView)findViewById(R.id.imageView7)).setImageResource(0);
((ImageView)findViewById(R.id.imageView8)).setImageResource(0);
TextView status = findViewById(R.id.status);
status.setText("Tap to play");
mergedSet.clear();
tappedArray.clear();
computerArray.clear();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_page);
}
}
Я пытался отладить и выяснить .. но в журнале ничего нет