本学期,我已在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>
任何帮助,将不胜感激。不想让您替我做家庭作业,但我认为这是一个简单的解决方案,我只是想得太远/太过疏忽,并且希望在我提交之前能够正常工作
谢谢!!
连续波
最佳答案
首先,在您的onClick
函数中,您要检查p1turn是否为true,但永远不要将p1turn更改为false,因此,这总是轮到他们了。其次,您在winConditions()
内部唯一调用winConditions()
的位置,因此永远不会使用该函数。