gpt4 book ai didi

java - Android Tic tac toe - 电脑获胜后,玩家仍然可以使用按钮,并获胜

转载 作者:行者123 更新时间:2023-11-30 00:33:53 25 4
gpt4 key购买 nike

我是一个新的 android 程序员,我需要你的帮助..正如我所说的问题是在计算机获胜后,玩家仍然可以使用按钮并获胜..

代码如下:

package com.example.qwert.graox;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

int c[][];
int i, j = 0;
Button b[][];
TextView textView;
AI ai;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


setBoard();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuItem item = menu.add("New Game");
return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
setBoard();
return true;
}


// Set up the game board.
private void setBoard() {
ai = new AI();
b = new Button[4][4];
c = new int[4][4];


textView = (TextView) findViewById(R.id.dialogi);


b[1][3] = (Button) findViewById(R.id.button1);
b[1][2] = (Button) findViewById(R.id.button2);
b[1][1] = (Button) findViewById(R.id.button3);


b[2][3] = (Button) findViewById(R.id.button4);
b[2][2] = (Button) findViewById(R.id.button5);
b[2][1] = (Button) findViewById(R.id.button6);


b[3][3] = (Button) findViewById(R.id.button7);
b[3][2] = (Button) findViewById(R.id.button8);
b[3][1] = (Button) findViewById(R.id.button9);

for (i = 1; i <= 3; i++) {
for (j = 1; j <= 3; j++)
c[i][j] = 2;
}


textView.setText("Press any key to start");
textView.setTextColor(0xFFFF0000);


// add the click listeners for each button
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 3; j++) {
b[i][j].setOnClickListener(new MyClickListener(i, j));
if(!b[i][j].isEnabled()) {
b[i][j].setText("-");
b[i][j].setTextColor(0xff000000);
b[i][j].setEnabled(true);
}
}
}
}


class MyClickListener implements View.OnClickListener {
int x;
int y;


public MyClickListener(int x, int y) {
this.x = x;
this.y = y;
}

public void onClick(View view) {

if (b[x][y].isEnabled()); {
b[x][y].setEnabled(false);
b[x][y].setText("O");
b[x][y].setTextColor(0xFFFFFFFF );
c[x][y] = 0;
textView.setText("");
if (!checkBoard()) {
ai.takeTurn();
}
}
}
}


private class AI {
public void takeTurn() {
if(c[1][1]==2 &&
((c[1][2]==0 && c[1][3]==0) ||
(c[2][2]==0 && c[3][3]==0) ||
(c[2][1]==0 && c[3][1]==0))) {
markSquare(1,1);
} else if (c[1][2]==2 &&
((c[2][2]==0 && c[3][2]==0) ||
(c[1][1]==0 && c[1][3]==0))) {
markSquare(1,2);
} else if(c[1][3]==2 &&
((c[1][1]==0 && c[1][2]==0) ||
(c[3][1]==0 && c[2][2]==0) ||
(c[2][3]==0 && c[3][3]==0))) {
markSquare(1,3);
} else if(c[2][1]==2 &&
((c[2][2]==0 && c[2][3]==0) ||
(c[1][1]==0 && c[3][1]==0))){
markSquare(2,1);
} else if(c[2][2]==2 &&
((c[1][1]==0 && c[3][3]==0) ||
(c[1][2]==0 && c[3][2]==0) ||
(c[3][1]==0 && c[1][3]==0) ||
(c[2][1]==0 && c[2][3]==0))) {
markSquare(2,2);
} else if(c[2][3]==2 &&
((c[2][1]==0 && c[2][2]==0) ||
(c[1][3]==0 && c[3][3]==0))) {
markSquare(2,3);
} else if(c[3][1]==2 &&
((c[1][1]==0 && c[2][1]==0) ||
(c[3][2]==0 && c[3][3]==0) ||
(c[2][2]==0 && c[1][3]==0))){
markSquare(3,1);
} else if(c[3][2]==2 &&
((c[1][2]==0 && c[2][2]==0) ||
(c[3][1]==0 && c[3][3]==0))) {
markSquare(3,2);
}else if( c[3][3]==2 &&
((c[1][1]==0 && c[2][2]==0) ||
(c[1][3]==0 && c[2][3]==0) ||
(c[3][1]==0 && c[3][2]==0))) {
markSquare(3,3);
} else {
Random rand = new Random();

int a = rand.nextInt(4);
int b = rand.nextInt(4);
while(a==0 || b==0 || c[a][b]!=2) {
a = rand.nextInt(4);
b = rand.nextInt(4);
}
markSquare(a,b);
}
}


private void markSquare(int x, int y) {
b[x][y].setEnabled(false);
b[x][y].setText("X");
b[x][y].setTextColor(0xFF00FF00);
c[x][y] = 1;
checkBoard();
}
}

// check the board to see if someone has won
private boolean checkBoard() {
boolean gameOver = false;
if ((c[1][1] == 0 && c[2][2] == 0 && c[3][3] == 0)
|| (c[1][3] == 0 && c[2][2] == 0 && c[3][1] == 0)
|| (c[1][2] == 0 && c[2][2] == 0 && c[3][2] == 0)
|| (c[1][3] == 0 && c[2][3] == 0 && c[3][3] == 0)
|| (c[1][1] == 0 && c[1][2] == 0 && c[1][3] == 0)
|| (c[2][1] == 0 && c[2][2] == 0 && c[2][3] == 0)
|| (c[3][1] == 0 && c[3][2] == 0 && c[3][3] == 0)
|| (c[1][1] == 0 && c[2][1] == 0 && c[3][1] == 0)) {
textView.setText("Super - you Win");
gameOver = true;
} else if ((c[1][1] == 1 && c[2][2] == 1 && c[3][3] == 1)
|| (c[1][3] == 1 && c[2][2] == 1 && c[3][1] == 1)
|| (c[1][2] == 1 && c[2][2] == 1 && c[3][2] == 1)
|| (c[1][3] == 1 && c[2][3] == 1 && c[3][3] == 1)
|| (c[1][1] == 1 && c[1][2] == 1 && c[1][3] == 1)
|| (c[2][1] == 1 && c[2][2] == 1 && c[2][3] == 1)
|| (c[3][1] == 1 && c[3][2] == 1 && c[3][3] == 1)
|| (c[1][1] == 1 && c[2][1] == 1 && c[3][1] == 1)) {
textView.setText("GameOver. Computer wins..");
gameOver = true;
} else {
boolean empty = false;
for(i=1; i<=3; i++) {
for(j=1; j<=3; j++) {
if(c[i][j]==2) {
empty = true;
break;
}
}
}
if(!empty) {
gameOver = true;
textView.setText("Game Over. Draft!");
}
}
return gameOver;
}
}

我认为问题出在某处:

// check the board to see if someone has won
private boolean checkBoard() {
boolean gameOver = false;
if ((c[1][1] == 0 && c[2][2] == 0 && c[3][3] == 0)
|| (c[1][3] == 0 && c[2][2] == 0 && c[3][1] == 0)
|| (c[1][2] == 0 && c[2][2] == 0 && c[3][2] == 0)
|| (c[1][3] == 0 && c[2][3] == 0 && c[3][3] == 0)
|| (c[1][1] == 0 && c[1][2] == 0 && c[1][3] == 0)
|| (c[2][1] == 0 && c[2][2] == 0 && c[2][3] == 0)
|| (c[3][1] == 0 && c[3][2] == 0 && c[3][3] == 0)
|| (c[1][1] == 0 && c[2][1] == 0 && c[3][1] == 0)) {
textView.setText("Super - you Win");
gameOver = true;
} else if ((c[1][1] == 1 && c[2][2] == 1 && c[3][3] == 1)
|| (c[1][3] == 1 && c[2][2] == 1 && c[3][1] == 1)
|| (c[1][2] == 1 && c[2][2] == 1 && c[3][2] == 1)
|| (c[1][3] == 1 && c[2][3] == 1 && c[3][3] == 1)
|| (c[1][1] == 1 && c[1][2] == 1 && c[1][3] == 1)
|| (c[2][1] == 1 && c[2][2] == 1 && c[2][3] == 1)
|| (c[3][1] == 1 && c[3][2] == 1 && c[3][3] == 1)
|| (c[1][1] == 1 && c[2][1] == 1 && c[3][1] == 1)) {
textView.setText("GameOver. Computer wins..");
gameOver = true;
} else {
boolean empty = false;
for(i=1; i<=3; i++) {
for(j=1; j<=3; j++) {
if(c[i][j]==2) {
empty = true;
break;
}
}
}
if(!empty) {
gameOver = true;
textView.setText("Game Over. Draft!");
}
}
return gameOver;
}

最佳答案

你的错误在那里

public void onClick(View view) { 
if (b[x][y].isEnabled()) {
b[x][y].setEnabled(false);
b[x][y].setText("O");
b[x][y].setTextColor(0xFFFFFFFF );
c[x][y] = 0;
textView.setText("");
if (!checkBoard()) {
ai.takeTurn();
}
}
}

玩家总是可以触摸按钮

最好的方法是你 gameOver be field 并检查 onClick if 语句

关于java - Android Tic tac toe - 电脑获胜后,玩家仍然可以使用按钮,并获胜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43753774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com