Noughts&十字架/ Tic Tac Toe获胜条件&用Java / Android清除游戏(长篇文章)

时间:2014-02-25 11:46:36

标签: java android tic-tac-toe

我遇到了一个简单的牛轧糖和一个问题。穿越游戏我试图设置一个获胜条件,在击中三个位置/块时它会显示一条消息,声明你赢了游戏并禁用了所有其他位置/块但是我的行为很奇怪,例如如果你试图通过位置1,4,7获胜,当你点击位置1和1时它会结束4它已经显示消息并禁用所有按钮。以下是代码:

的活动:

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>

1 个答案:

答案 0 :(得分:0)

在游戏开始之前,您是否错误地将所有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("");
}