首页 > 解决方案 > 无法让我在 android studio 中的井字游戏应用程序正常工作

问题描述

本学期我在 android studio 上为我的应用开发课程设计了一个井字游戏应用程序。我首先遇到了由于 OnClickListener 导致代码崩溃的问题,我通过使用正确的值修复 buttonMatrix 来解决该问题。我的代码现在打开并运行,但在让我的游戏进入游戏的下一个回合时遇到问题。基本上我的应用程序会运行,然后我按下想要放置“X”或“O”值的按钮,具体取决于轮到玩家 1 还是玩家 2。我当前的代码只放置 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");
    }

活动主.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>

对此的任何帮助将不胜感激。不是在找你为我做作业,但我认为这是一个简单的解决方法,我只是想太多/忽略了,希望在我提交之前能正常工作

谢谢!!

连续波

标签: javaandroidandroid-studio

解决方案


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


推荐阅读