java - Noughts & Crosses/Tic Tac Toe 获胜条件并在 Java/Android 中清除游戏(长文)

标签 java android tic-tac-toe

我在玩简单的牛轧糖和十字游戏时遇到问题,我试图设置获胜条件,在击中三个位置/ block 时,它会显示一条消息,声明您已赢得游戏并禁用所有其他位置/ block 但是我的行为很奇怪,例如,如果您尝试通过位置 1、4、7 获胜,那么当您单击位置 1 和 4 时,它就会显示该消息并禁用所有按钮。下面是代码:

Activity :

package com.example.noughtsandcrosses;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class NoughtsAndCrossesXActivity extends Activity implements OnClickListener  {
    Button[] buttons = new Button[10];
    int[] squares = new int[10];
    Button newGame;
    Button exitGame;
    TextView WinGame;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.noughts_and_croses);    

        buttons[1] = (Button) findViewById(R.id.one);
        buttons[2] = (Button) findViewById(R.id.two);
        buttons[3] = (Button) findViewById(R.id.three);
        buttons[4] = (Button) findViewById(R.id.four);
        buttons[5] = (Button) findViewById(R.id.five);
        buttons[6] = (Button) findViewById(R.id.six);
        buttons[7] = (Button) findViewById(R.id.seven);
        buttons[8] = (Button) findViewById(R.id.eight);
        buttons[9] = (Button) findViewById(R.id.nine);  
        newGame = (Button) findViewById(R.id.new_game);
        newGame.setOnClickListener(this);
        WinGame = (TextView) findViewById(R.id.win_game);
        exitGame = (Button) findViewById(R.id.exit_game);
        exitGame.setOnClickListener(this);


        for (int i = 1; i <= 9; i++){
            buttons[i].setOnClickListener(this);    
        }   
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.login_page, menu);
        return true;
    }

    public void clearGame(){
        for (int i = 1; i <= 9; i++){
            buttons[i].setText("");
            buttons[i].setEnabled(true);
            squares[i] = 1;
        }
        WinGame.setText("");
    }

    /*public void exitGameToMenu(){
        Intent a = new Intent(getApplicationContext(),MenuActivity.class);
        a.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(a);
    }*/

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.one:
            makeMove(1);
            respond();
            break;
        case R.id.two:
            makeMove(2);
            respond();
            break;
        case R.id.three:
            makeMove(3);
            respond();
            break;
        case R.id.four:
            makeMove(4);
            respond();
            break;
        case R.id.five:
            makeMove(5);
            respond();
            break;
        case R.id.six:
            makeMove(6);
            respond();
            break;
        case R.id.seven:
            makeMove(7);
            respond();
            break;
        case R.id.eight:
            makeMove(8);
            respond();
            break;
        case R.id.nine:
            makeMove(9);
            respond();
            break;
        case R.id.new_game:
            System.out.println("newgame");
            clearGame();
            break;
        case R.id.exit_game:
            System.out.println("exitgame");
            //exitGameToMenu(); 
            Intent a = new Intent (this, MenuActivity.class);
            startActivity(a);
            break;
        }
    }

    public void CheckIfPlayerWon (int i) {
        if (squares[1] == i && squares[2] == i && squares[3] == i){
            WinGame.setText("You have won the game! 1");
            buttons[1].setEnabled(false);
            buttons[2].setEnabled(false);
            buttons[3].setEnabled(false);
            buttons[4].setEnabled(false);
            buttons[5].setEnabled(false);
            buttons[6].setEnabled(false);
            buttons[7].setEnabled(false);
            buttons[8].setEnabled(false);
            buttons[9].setEnabled(false);
        }
        else if (squares[4] == i && squares[5] == i && squares[6] == i){
            WinGame.setText("You have won the game! 2");
            buttons[1].setEnabled(false);
            buttons[2].setEnabled(false);
            buttons[3].setEnabled(false);
            buttons[4].setEnabled(false);
            buttons[5].setEnabled(false);
            buttons[6].setEnabled(false);
            buttons[7].setEnabled(false);
            buttons[8].setEnabled(false);
            buttons[9].setEnabled(false);
        }
        else if (squares[7] == i && squares[8]== i && squares[9] == i){
            WinGame.setText("You have won the game! 3");
            buttons[1].setEnabled(false);
            buttons[2].setEnabled(false);
            buttons[3].setEnabled(false);
            buttons[4].setEnabled(false);
            buttons[5].setEnabled(false);
            buttons[6].setEnabled(false);
            buttons[7].setEnabled(false);
            buttons[8].setEnabled(false);
            buttons[9].setEnabled(false);
        }
        else if (squares[1]== i && squares[4] == i && squares[7] == i){
            WinGame.setText("You have won the game! 4");
            buttons[1].setEnabled(false);
            buttons[2].setEnabled(false);
            buttons[3].setEnabled(false);
            buttons[4].setEnabled(false);
            buttons[5].setEnabled(false);
            buttons[6].setEnabled(false);
            buttons[7].setEnabled(false);
            buttons[8].setEnabled(false);
            buttons[9].setEnabled(false);
        }
        else if (squares[2] == i && squares[5] == i && squares[8] == i){
            WinGame.setText("You have won the game! 5");
            buttons[1].setEnabled(false);
            buttons[2].setEnabled(false);
            buttons[3].setEnabled(false);
            buttons[4].setEnabled(false);
            buttons[5].setEnabled(false);
            buttons[6].setEnabled(false);
            buttons[7].setEnabled(false);
            buttons[8].setEnabled(false);
            buttons[9].setEnabled(false);
        }
        else if (squares[3] == i && squares[6] == i && squares[9] == i){
            WinGame.setText("You have won the game! 6");
            buttons[1].setEnabled(false);
            buttons[2].setEnabled(false);
            buttons[3].setEnabled(false);
            buttons[4].setEnabled(false);
            buttons[5].setEnabled(false);
            buttons[6].setEnabled(false);
            buttons[7].setEnabled(false);
            buttons[8].setEnabled(false);
            buttons[9].setEnabled(false);
        }
        else if (squares[1] == i && squares[5] == i && squares[9] == i){
            WinGame.setText("You have won the game! 7");
            buttons[1].setEnabled(false);
            buttons[2].setEnabled(false);
            buttons[3].setEnabled(false);
            buttons[4].setEnabled(false);
            buttons[5].setEnabled(false);
            buttons[6].setEnabled(false);
            buttons[7].setEnabled(false);
            buttons[8].setEnabled(false);
            buttons[9].setEnabled(false);
        }
        else if (squares[3] == i && squares[5] == i && squares[7] == i){
            WinGame.setText("You have won the game! 8");
            buttons[1].setEnabled(false);
            buttons[2].setEnabled(false);
            buttons[3].setEnabled(false);
            buttons[4].setEnabled(false);
            buttons[5].setEnabled(false);
            buttons[6].setEnabled(false);
            buttons[7].setEnabled(false);
            buttons[8].setEnabled(false);
            buttons[9].setEnabled(false);
        }
    }


    public void makeMove(int i) {
        buttons[i].setText("X");
        buttons[i].setEnabled(false);
        squares[i] = 1;
        CheckIfPlayerWon(i);
    }


    public void respond() {
        for (int i = 1; i <= 9; i++) {
            if (buttons[i].isEnabled()) {
                buttons[i].setText("O");
                buttons[i].setEnabled(false);
                squares[i] = 2;
                break;
            }
        }
    }
}

XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/play_grid" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:layout_marginTop="5dp" >

    <TableRow android:gravity="center_horizontal">
        <Button android:id="@+id/one" android:layout_width="200dp"
        android:layout_height="200dp" android:text="@string/one"
        android:textSize="70sp" />
        <Button android:id="@+id/two" android:layout_width="200dp"
        android:layout_height="200dp" android:text="@string/two"
        android:textSize="70sp" />
        <Button android:id="@+id/three" android:layout_width="200dp"
        android:layout_height="200dp" android:text="@string/three"
        android:textSize="70sp" />
        </TableRow>

    <TableRow android:gravity="center_horizontal">
        <Button android:id="@+id/four" android:layout_width="200dp"
        android:layout_height="200dp" android:text="@string/four"
        android:textSize="70sp" />
        <Button android:id="@+id/five" android:layout_width="200dp"
        android:layout_height="200dp" android:text="@string/five"
        android:textSize="70sp" />
        <Button android:id="@+id/six" android:layout_width="200dp"
        android:layout_height="200dp" android:text="@string/six"
        android:textSize="70sp" />
    </TableRow>

    <TableRow android:gravity="center_horizontal">
        <Button android:id="@+id/seven" 
        android:layout_width="200dp"
        android:layout_height="200dp" 
        android:text="@string/seven"
        android:textSize="70sp" />
        <Button android:id="@+id/eight" android:layout_width="200dp"
        android:layout_height="200dp" android:text="@string/eight"
        android:textSize="70sp" />
        <Button android:id="@+id/nine" android:layout_width="200dp"
        android:layout_height="200dp" android:text="@string/nine"
        android:textSize="70sp" />
    </TableRow>

    <Button 
        android:id="@+id/new_game" 
        android:layout_width="200dp"
        android:layout_height="50dp" 
        android:text="@string/new_game"
        android:textSize="30sp" />

    <Button 
        android:id="@+id/exit_game" 
        android:layout_width="200dp"
        android:layout_height="50dp" 
        android:text="@string/exit_game"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/win_game"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:text="@string/win_game"
        android:textSize="12sp" />
</TableLayout>

字符串(应用程序其他部分的一些字符串):

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Noughts And Crosses</string>
    <string name="action_settings">Settings</string>
    <string name="Username">Enter Username</string>
    <string name="Password">Enter Password</string>
    <string name="LoginButton">Login</string>
    <string name="RegisterButton">Register</string>
    <string name="registerUserName">Enter new username</string>
    <string name="registerPassword">Enter new password</string>
    <string name="registerConfirmPassword">Confirm new password</string>
    <string name="registerAccount">Create new Account</string>
    <string name="one">1</string>  
    <string name="two">2</string>  
    <string name="three">3</string>  
    <string name="four">4</string>  
    <string name="five">5</string>  
    <string name="six">6</string>  
    <string name="seven">7</string>  
    <string name="eight">8</string>  
    <string name="nine">9</string>
    <string name="new_game_x">Play as a X</string>
    <string name="new_game_0">Play as a 0</string>
    <string name="instructions">Instructions to Play</string>
    <string name="exit_app">Exit App</string>
    <string name="exit_game">Exit Game</string>
    <string name="win_game"></string>
    <string name="new_game">New Game</string>
    <string name="BackToMenu">Back To Menu</string>
    <string name="title_activity_NoughtsAndCrossesXActivity">Noughts And Crosses</string>
    <string name="title_activity_LoginPageActivity">Login Page</string>
    <string name="title_activity_InstructionActivity">Instruction</string>

</resources>

最佳答案

您是否在游戏开始前错误地将所有 squares[] 数组元素初始化为 1?

public void clearGame(){
    for (int i = 1; i <= 9; i++){
        buttons[i].setText("");
        buttons[i].setEnabled(true);
        squares[i] = 1;               <---------------- set all elements to 1
    }
    WinGame.setText("");
}

关于java - Noughts & Crosses/Tic Tac Toe 获胜条件并在 Java/Android 中清除游戏(长文),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22013273/

相关文章:

java 日历获取第二天之前的小时

java - 为 Java 1.8 启用 jackOptions 时 Gradle 构建挂起

java - maven-site 插件 3.3 java.lang.ClassNotFoundException : org. apache.maven.doxia.siterenderer.DocumentContent

java - 如何正确覆盖 Spring 和 Hibernate 的 BasicDataSource

使用 HTML/CSS/jQuery 的 Android 应用程序。 [如何做呢?

c++ - 2个数字之间的随机数选择器

python - 使函数返回 bool 值并在 tic/tac/toe 游戏中实现 AI

java - 井字棋棋盘不随玩家移动而更新

java - 建议 spring mvc http 消息转换器

Java:字节算术运算