【问题标题】:2 players Tic Tac toe game2 名玩家井字游戏
【发布时间】:2015-01-21 12:59:11
【问题描述】:

我正在尝试制作一个两人井字游戏。当第一个玩家单击按钮上带有标记 X 的 OnClick 方法时,但我卡住了 - 我不知道如何使 onClick() 检测第二个玩家何时单击以及如何在按钮上标记 O ..plz 帮助..我的.java在下面

public class MainActivity extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setBoard();
    }
    int c[][];
    int i, j, k = 0;
    Button b[][];
    TextView textView;

    private void setBoard() {

        b = new Button[4][4];
        c = new int[4][4];


        // Button = (TextView) findViewById(R.id.newgame);


        b[1][3] = (Button) findViewById(R.id.one);
        b[1][2] = (Button) findViewById(R.id.two);
        b[1][1] = (Button) findViewById(R.id.three);


        b[2][3] = (Button) findViewById(R.id.four);
        b[2][2] = (Button) findViewById(R.id.five);
        b[2][1] = (Button) findViewById(R.id.six);


        b[3][3] = (Button) findViewById(R.id.seven);
        b[3][2] = (Button) findViewById(R.id.eight);
        b[3][1] = (Button) findViewById(R.id.nine);

        for (i = 1; i <= 3; i++) {
            for (j = 1; j <= 3; j++)
                c[i][j] = 2;
        }

        for (i = 1; i <= 3; i++) {
            for (j = 1; j <= 3; j++) {
                b[i][j].setOnClickListener(new MyClickListener(i, j));
                if (!b[i][j].isEnabled()) {
                    b[i][j].setText("o");
                    b[i][j].setEnabled(true);
                }
            }
        }
    }
    class MyClickListener implements View.OnClickListener {
            int x;
            int y;


            public MyClickListener(int x, int y) {
                this.x = x;
                this.y = y;
            }


            public void onClick(View view) {
                if (b[x][y].isEnabled()) {
                    b[x][y].setEnabled(false);
                    b[x][y].setText("X");
                    c[x][y] = 0;
                    textView.setText("");
//WHAT NEXT




                    }
                }
            }
        }
}

【问题讨论】:

    标签: java android android-activity


    【解决方案1】:

    只需使用布尔值来检测点击。假设第一次单击按钮时布尔值 isFirstPlayerTurn 为真。在第二回合将其设为假。在所有回合都这样做,您将知道哪个玩家正在点击按钮。 示例:

    private boolean isFirstPlayerTurn = true;
    

    ...

    void onclick() {
            if (isFirstPlayerTurn) {
                // clicked by player 1
                isFirstPlayerTurn = false;
            } else {
                // clicked by player 2
                isFirstPlayerTurn = true;
            }
    
        }
    

    【讨论】:

    • 我需要使用你在 onClick() 本身中建议的内容吗?
    • 我已经为您的需要添加了一个示例。
    • 这意味着我需要为两个玩家添加变量,并根据他们的 X 或 O 放置。
    • 一个变量就足够了。我提供的示例清楚地表明您需要更改两个玩家的变量。
    • 好的,谢谢..我现在就试试...我是 android 和 java 新手,非常混乱
    【解决方案2】:

    您可能应该添加一个新变量

    int playerTurn;
    

    最初应该设置为 1。在第一次执行 onClick() 时,playerTurn 的值为 1,因此放置一个 X,并将 playerTurn 更改为 2。在第二次单击时,当您检查 playerTurn 的值,您将放置一个 O,并将其值更改为 1。然后继续。

    int player = 1;
    public void onClick(View view){ 
        if (b[x][y].isEnabled() { 
            b[x][y].setEnabled(false); 
            if (player == 1){ 
                b[x][y].setText("X"); 
                c[x][y] = 0; 
                player = 1; 
            } else {
                b[x][y].setText("O"); 
                c[x][y] = 1; 
                player = 2; 
            } 
        }
    }
    

    【讨论】:

    • 好的,我明白了,非常感谢...我现在就试试..ChEERS
    • 这是我做的好吗
    • 'int player=1; public void onClick(View view) { if (b[x][y].isEnabled()&& player==1) { b[x][y].setEnabled(false); b[x][y].setText("X"); c[x][y] = 0;玩家=0; textView.setText(""); }else { b[x][y].setEnabled(false); b[x][y].setText("O"); c[x][y]=1;玩家=1; } } }'
    • 很难用源代码阅读您的评论。我编辑了我的答案,包括您的评论和一些修复。我唯一缺少的部分是: c[x][y]=1; c[x][y]=0;
    • 你很有帮助...这是我第一次在网上发布问题...它帮助了我...非常感谢...我相信我会遇到更多问题来发布大声笑和我一定会在这里做...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多