gpt4 book ai didi

java - 在 android studio 中让我的 Tic Tac Toe 应用程序正常工作时遇到问题

转载 作者:太空宇宙 更新时间:2023-11-04 10:20:07 25 4
gpt4 key购买 nike

我在 android studio 上为本学期的应用程序开发类(class)设计了一个 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

最佳答案

首先,在您的 onClick 函数中,您将检查 p1turn 是否为 true,但不会将 p1turn 更改为 false,因此总是轮到他们。其次,您在 winConditions() 内部调用 winConditions() 的唯一位置,因此该函数永远不会被使用。

关于java - 在 android studio 中让我的 Tic Tac Toe 应用程序正常工作时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51275637/

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