本学期,我在Android Studio上为我的应用程序开发班设计了一个Tic Tac Toe应用程序。我首先遇到了由于OnClickListener而导致代码崩溃的问题,该问题通过使用正确的值修复buttonMatrix来解决。我的代码现在可以打开并运行,但是在使游戏前进到游戏的下一回合时遇到问题。基本上我的应用程序会运行,然后我要根据玩家1还是玩家2的回合按下要在其中放置“ X”或“ O”值的按钮。我当前的代码仅放置X,而没有前进到玩家2的回合,并且当我连续获得3 X时,它也不适用我的获胜条件。我当前的代码如下:
MainActivity.Java:
package com.example.corey.tictactoegame;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
class MainActivity extends AppCompatActivity implements View.OnClickListener
{
private Button[][] buttonMatrix = new Button[3][3];
private boolean p1Turn = true;
private int turnAdvance;
private int player1Score;
private int player2Score;
TextView textViewP1;
TextView textViewP2;
TextView gameStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//IDs the TextViews containing player scores//
textViewP1 = findViewById(R.id.p1Score);
textViewP2 = findViewById(R.id.p2Score);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
String buttonID = "button_" + i + j;
int resourceID = getResources().getIdentifier(buttonID, "id",
getPackageName());
buttonMatrix[i][j] = findViewById(resourceID);
buttonMatrix[i][j].setOnClickListener(this);
}
}
// creating the click functionality for the New Game Button//
Button newGameButton = findViewById(R.id.buttonNewGame);
newGameButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
newGameReset();
}
});
}
@Override
//Checks to see if button pressed is an empty button or a
// previously used button and returns a value if
// empty depending on player turn//
public void onClick(View view) {
if (!((Button) view).getText().toString().equals("")) {
return;
}
turnAdvance = 0;
// Creates text values for the buttons with either "X" or "O" depending on player turn//
if (p1Turn) {
((Button) view).setText("X");
turnAdvance++;
}
else {
((Button) view).setText("O");
turnAdvance++;
}
//advances the round after a turn from both players//
turnAdvance++;
}
// creating the win method based on conditions within the matrix as stated below//
private boolean winConditions() {
String[][] matrix = new String[3][3];
if (winConditions()) {
if (p1Turn) {
p1Wins();
} else {
p2Wins();
}
//ends game in a draw if 9 turns are made//
if (turnAdvance == 9) {
gameDraw();
}
//ends the turnAdvance function//
else {
p1Turn = !p1Turn;
}
}
//creating the win conditions for the game covering all
// horizontal, vertical, and diagonal possibilities//
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
matrix[i][j] = buttonMatrix[i][j].getText().toString();
}
}
for (int i = 0; i < 3; i++) {
if (matrix[i][0].equals(matrix[i][1])
&& matrix[i][0].equals(matrix[i][2])
&& !matrix[i][0].equals("")) {
return true;
}
}
for (int i = 0; i < 3; i++) {
if (matrix[0][i].equals(matrix[1][i])
&& matrix[0][i].equals(matrix[1][i])
&& !matrix[0][i].equals("")) {
return true;
}
}
if (matrix[0][0].equals(matrix[1][1])
&& matrix[0][0].equals(matrix[2][2])
&& !matrix[0][0].equals("")) {
return true;
}
if (matrix[0][2].equals(matrix[1][1])
&& matrix[0][2].equals(matrix[2][0])
&& !matrix[0][2].equals("")) {
return true;
}
return false;
}
//creating a method to clear the board when a game ends//
private void boardClear() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
buttonMatrix[i][j].setText("");
}
}
turnAdvance = 0;
p1Turn = true;
}
//creating a method to set the score text for each player//
private void updateScoreText() {
textViewP1.setText("Player One (X): " + player1Score);
textViewP2.setText("Player Two (O): " + player2Score);
}
//creating a method to reset all score values and clear board//
private void newGameReset() {
player1Score = 0;
player2Score = 0;
updateScoreText();
boardClear();
}
//creating a method for player one winning//
private void p1Wins() {
player1Score++;
gameStatus.setText("Player One (X) Wins!");
updateScoreText();
boardClear();
}
//creating a method for player two winning//
private void p2Wins() {
player2Score++;
gameStatus.setText("Player Two (O) Wins!");
updateScoreText();
boardClear();
}
//creating a method for the game ending in a draw//
private void gameDraw() {
gameStatus.setText("Game Draw!");
boardClear();
}
protected void savedState(Bundle currentState) {
super.onSaveInstanceState(currentState);
currentState.putInt("turnAdvance", turnAdvance);
currentState.putInt("player1Score", player1Score);
currentState.putInt("player2Score", player2Score);
currentState.putBoolean("p1Turn", p1Turn);
}
protected void restoredState(Bundle newState) {
super.onRestoreInstanceState(newState);
turnAdvance = newState.getInt("turnAdvance");
player1Score = newState.getInt("player1Score");
player2Score = newState.getInt("player2Score");
p1Turn = newState.getBoolean("p1Turn");
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="136dp">
<TextView
android:id="@+id/p1Score"
android:layout_width="187dp"
android:layout_height="wrap_content"
android:text="Player One (X): 0"
android:textSize="24sp"
android:freezesText="true"/>
<TextView
android:id="@+id/p2Score"
android:layout_width="187dp"
android:layout_height="wrap_content"
android:layout_below="@id/p1Score"
android:text="Player Two (O): 0"
android:textSize="24sp"
android:freezesText="true"/>
<TextView
android:id="@+id/gameOutcome"
android:layout_width="329dp"
android:layout_height="28dp"
android:layout_below="@+id/buttonNewGame"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text=""
android:textAlignment="center"
android:textSize="28sp" />
<Button
android:id="@+id/buttonNewGame"
android:layout_width="141dp"
android:layout_height="82dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:text="New Game"
android:textSize="24sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="@+id/button_00"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="50sp"
android:freezesText="true"/>
<Button
android:id="@+id/button_01"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="50sp"
android:freezesText="true"/>
<Button
android:id="@+id/button_02"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="50sp"
android:freezesText="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="@+id/button_10"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="50sp"
android:freezesText="true"/>
<Button
android:id="@+id/button_11"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="50sp"
android:freezesText="true"/>
<Button
android:id="@+id/button_12"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="50sp"
android:freezesText="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="@+id/button_20"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="50sp"
android:freezesText="true"/>
<Button
android:id="@+id/button_21"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="50sp"
android:freezesText="true"/>
<Button
android:id="@+id/button_22"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="50sp"
android:freezesText="true"/>
</LinearLayout>
</LinearLayout>
在此方面的任何帮助将不胜感激。不想让您替我做家庭作业,但我认为这是一个简单的解决方法,我只是想得太远/太过疏忽,并且希望在我提交之前能正常工作
谢谢!
CW
答案 0 :(得分:0)
首先,在您的onClick
函数中,您要检查p1turn是否为true,但不要将p1turn更改为false,因此始终轮到他们了。其次,您在winConditions()
内部唯一调用winConditions()
的地方,因此永远不会使用该函数。
答案 1 :(得分:0)
这是我更新的代码。此时我唯一遇到的问题是在实际满足winConditions之前就已满足winConditions。 IE 2在水平和垂直方向上连续获胜。对角线的胜利似乎与三连胜。
MainActivity.java:
package com.example.corey.tictactoegame;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
//implementing OnClickListener for Buttons//
class MainActivity extends AppCompatActivity implements View.OnClickListener {
//I decided to use a matrix instead of just calling all buttons
// individually and creating a long list of winConditions.
// I did do a little more in depth research on using 2D arrays
// and it took a while to figure this out but I'm glad I did//
private Button[][] buttonMatrix = new Button[3][3];
private boolean p1Turn = true;
private int turnAdvance;
private int player1Score;
private int player2Score;
TextView textViewP1;
TextView textViewP2;
TextView gameStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//IDs the TextViews containing player scores and the game outcomes//
textViewP1 = findViewById(R.id.p1Score);
textViewP2 = findViewById(R.id.p2Score);
gameStatus = findViewById(R.id.gameOutcome);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
String buttonID = "button_" + i + j;
int resourceID = getResources().getIdentifier(buttonID, "id", getPackageName());
buttonMatrix[i][j] = findViewById(resourceID);
buttonMatrix[i][j].setOnClickListener(this);
}
}
// creating the click functionality for the New Game Button//
Button newGameButton = findViewById(R.id.buttonNewGame);
newGameButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
newGameReset();
}
});
}
@Override
//Checks to see if button pressed is an empty button or a
// previously used button and returns a value if
// empty depending on player turn//
public void onClick(View view) {
if (!((Button) view).getText().toString().equals("")) {
return;
}
// Creates text values for the buttons with either "X" or "O" depending on player turn//
if (p1Turn) {
((Button) view).setText("X");
}
else {
((Button) view).setText("O");
}
//advances turns//
turnAdvance++;
//ends game in a draw if 9 turns are made//
if (turnAdvance == 9) {
gameDraw();
}
else {
p1Turn = !p1Turn;
}
if (winConditions()) {
if (p1Turn) {
p2Wins();
} else {
p1Wins();
}
}
}
// creating the win method based on conditions within the matrix as stated below//
private boolean winConditions() {
String[][] matrix = new String[3][3];
//creating the win conditions for the game covering all
// horizontal, vertical, and diagonal possibilities//
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
matrix[i][j] = buttonMatrix[i][j].getText().toString();
}
}
for (int i = 0; i < 3; i++) {
if (matrix[i][0].equals(matrix[i][1])
&& matrix[i][0].equals(matrix[i][2])
&& !matrix[i][0].equals("")) {
return true;
}
}
for (int i = 0; i < 3; i++) {
if (matrix[0][i].equals(matrix[1][i])
&& matrix[0][i].equals(matrix[1][i])
&& !matrix[0][i].equals("")) {
return true;
}
}
if (matrix[0][0].equals(matrix[1][1])
&& matrix[0][0].equals(matrix[2][2])
&& !matrix[0][0].equals("")) {
return true;
}
if (matrix[0][2].equals(matrix[1][1])
&& matrix[0][2].equals(matrix[2][0])
&& !matrix[0][2].equals("")) {
return true;
}
return false;
}
//creating a method to clear the board when a game ends//
private void boardClear() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
buttonMatrix[i][j].setText("");
}
}
//starts game at 0 turns and player 1's turn//
turnAdvance = 0;
p1Turn = true;
}
//creating a method to set the score text for each player//
private void updateScoreText() {
textViewP1.setText("Player One (X): " + player1Score);
textViewP2.setText("Player Two (O): " + player2Score);
}
//creating a method to reset all score values and clear
// board when the New Game button is pressed//
private void newGameReset() {
player1Score = 0;
player2Score = 0;
updateScoreText();
boardClear();
}
//creating a method for player one winning//
private void p1Wins() {
player1Score++;
gameStatus.setText("Player One (X) Wins!");
updateScoreText();
boardClear();
}
//creating a method for player two winning//
private void p2Wins() {
player2Score++;
gameStatus.setText("Player Two (O) Wins!");
updateScoreText();
boardClear();
}
//creating a method for the game ending in a draw//
private void gameDraw() {
gameStatus.setText("Game Draw!");
boardClear();
}
protected void onSaveInstanceState(Bundle currentState) {
super.onSaveInstanceState(currentState);
currentState.putInt("turnAdvance", turnAdvance);
currentState.putInt("player1Score", player1Score);
currentState.putInt("player2Score", player2Score);
currentState.putBoolean("p1Turn", p1Turn);
}
protected void onRestoreInstanceState(Bundle newState) {
super.onRestoreInstanceState(newState);
turnAdvance = newState.getInt("turnAdvance");
player1Score = newState.getInt("player1Score");
player2Score = newState.getInt("player2Score");
p1Turn = newState.getBoolean("p1Turn");
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="136dp">
<TextView
android:id="@+id/p1Score"
android:layout_width="187dp"
android:layout_height="wrap_content"
android:text="Player One (X): 0"
android:textSize="24sp"
android:freezesText="true"/>
<TextView
android:id="@+id/p2Score"
android:layout_width="187dp"
android:layout_height="wrap_content"
android:layout_below="@id/p1Score"
android:text="Player Two (O): 0"
android:textSize="24sp"
android:freezesText="true"/>
<TextView
android:id="@+id/gameOutcome"
android:layout_width="329dp"
android:layout_height="28dp"
android:layout_below="@+id/buttonNewGame"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text=""
android:textAlignment="center"
android:textSize="20sp" />
<Button
android:id="@+id/buttonNewGame"
android:layout_width="141dp"
android:layout_height="82dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:text="New Game by Corey Whitlow"
android:textSize="16sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="@+id/button_00"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="40sp"
android:freezesText="true"/>
<Button
android:id="@+id/button_01"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="40sp"
android:freezesText="true"/>
<Button
android:id="@+id/button_02"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="40sp"
android:freezesText="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="@+id/button_10"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="40sp"
android:freezesText="true"/>
<Button
android:id="@+id/button_11"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="40sp"
android:freezesText="true"/>
<Button
android:id="@+id/button_12"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="40sp"
android:freezesText="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="@+id/button_20"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="40sp"
android:freezesText="true"/>
<Button
android:id="@+id/button_21"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="40sp"
android:freezesText="true"/>
<Button
android:id="@+id/button_22"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="40sp"
android:freezesText="true"/>
</LinearLayout>
</LinearLayout>
我还必须更改winConditions,内容如下:
if (winConditions()) {
if (p1Turn) {
p2Wins();
} else {
p1Wins();
}
}
从本质上讲,我觉得当你读这篇文章时,它是说轮到玩家2赢了,而大声朗读没有意义吗?不知道为什么我要交换那个。再次感谢您提供如此巨大的帮助!我爱android,这是我在本学期开始学习的第一本哈哈! :D