如何在用户点击android后自动点击该按钮?

时间:2015-11-10 00:22:45

标签: android android-studio tic-tac-toe

我正在进行Tic Tac Toe游戏。我想创建一个玩家部分。

用户始终以X开头。

当用户点击按钮写X时,我希望设备自动在任何按钮上写O以与用户一起玩。

这是我的代码,但它不起作用,当X赢得比赛时,它会崩溃,但如果O获胜,游戏就有效:

Button a1,a2,a3,b1,b2,b3,c1,c2,c3,newGameBtn;
Button[] bArrary;
TextView thincrativTV;

// X = true ; O = false.
boolean turn = true;
int turn_count = 0;



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

    //set font path
    String fontpath = "fonts/ubuntub.ttf";
    //loading the font
    Typeface tf = Typeface.createFromAsset(getAssets(), fontpath);


    newGameBtn = (Button) findViewById(R.id.onePlayer_BNewGame);

    a1 = (Button) findViewById(R.id.onePlayer_A1);
    a2 = (Button) findViewById(R.id.onePlayer_A2);
    a3 = (Button) findViewById(R.id.onePlayer_A3);

    b1 = (Button) findViewById(R.id.onePlayer_B1);
    b2 = (Button) findViewById(R.id.onePlayer_B2);
    b3 = (Button) findViewById(R.id.onePlayer_B3);

    c1 = (Button) findViewById(R.id.onePlayer_C1);
    c2 = (Button) findViewById(R.id.onePlayer_C2);
    c3 = (Button) findViewById(R.id.onePlayer_C3);

    bArrary = new Button[]{a1,a2,a3,b1,b2,b3,c1,c2,c3};

    for (Button b : bArrary){
        b.setOnClickListener(this);
        //set custom font to all buttons
        b.setTypeface(tf);
    }

    newGameBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // we need to reset turn and turn_count and re enable all buttons
            turn = true;
            turn_count = 0;
            enableDisableAllButtons(true);
        }
    });

    // to make the copyrights work
    thincrativTV = (TextView) findViewById(R.id.onePlayer_TextView);
    if (thincrativTV !=null) {
        thincrativTV.setMovementMethod(LinkMovementMethod.getInstance());
    }

}



@Override
public void onClick(View v) {
    Button b = (Button) v;
    buttonClicked(b);

    Handler myHandler = new Handler();

    myHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            randomClick();
        }
    },500);
    //randomClick();

}



public void randomClick(){
    Random rand = new Random();
    if (turn_count<9) {
        Button nextB = bArrary[rand.nextInt(bArrary.length)];
        while (!nextB.isClickable()) {
            nextB = bArrary[rand.nextInt(bArrary.length)];
        }
        buttonClicked(nextB);
    }
}



public void buttonClicked(Button b){

    // i just wanna change the text on the button to X/O.
    if (turn){
        // X's turn
        b.setText("X");
    }else {
        // O's turn
        b.setText("O");
    }

    turn_count++;

    // and change the turn
    b.setBackgroundColor(Color.LTGRAY);
    b.setClickable(false);
    turn = !turn;

    checkForWinner();
}



private void checkForWinner(){

    boolean there_is_a_winner = false;

    // first we check the horizontals.
    if (a1.getText() == a2.getText() && a2.getText() == a3.getText() && !a1.isClickable()){
        there_is_a_winner = true;
    }else if (b1.getText() == b2.getText() && b2.getText() == b3.getText() && !b1.isClickable()){
        there_is_a_winner = true;
    }else if (c1.getText() == c2.getText() && c2.getText() == c3.getText() && !c1.isClickable()){
        there_is_a_winner = true;
    }

    // second we check the verticals.
    if (a1.getText() == b1.getText() && b1.getText() == c1.getText() && !a1.isClickable()){
        there_is_a_winner = true;
    }else if (a2.getText() == b2.getText() && b2.getText() == c2.getText() && !b2.isClickable()){
        there_is_a_winner = true;
    }else if (a3.getText() == b3.getText() && b3.getText() == c3.getText() && !c3.isClickable()){
        there_is_a_winner = true;
    }

    // finally we check the diagonal.
    if (a1.getText() == b2.getText() && b2.getText() == c3.getText() && !a1.isClickable()){
        there_is_a_winner = true;
    }else if (a3.getText() == b2.getText() && b2.getText() == c1.getText() && !b2.isClickable()){
        there_is_a_winner = true;
    }


    if (there_is_a_winner) {
        if (!turn){
            toast("X wins");
        }else {
            toast("O wins");
        }

        enableDisableAllButtons(false);
    } else if (turn_count == 9){
        // when all the buttons are clicked and disabled.
        toast("Oops!, Play Again");
    }
}



private void enableDisableAllButtons(boolean enable){

    // false to disable
    for (Button b : bArrary){
        b.setClickable(enable);

        //this is for color.
        if (enable){
            b.setBackgroundColor(Color.parseColor("#3c94d3"));
            //set the text back to original blank
            b.setText("");
        }else {
            b.setBackgroundColor(Color.LTGRAY);
        }
    }
}



private void toast(String message){
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();

}

*注意:X是用户,O是设备。

1 个答案:

答案 0 :(得分:1)

我看到方法buttonClicked(Button b)仅在用户点击任何按钮时被调用。轮到用户,不是机器。 所以在你处理完用户之后,你可以选择随机按钮并用它来调用buttonClicked()。

@Override
public void onClick(View v) {
    Button b = (Button) v;
    buttonClicked(b);

    // now you choose random button nextB 
    // -> call buttonClicked(nextB);
    Random rand = new Random();
    // machine only can set "O" when the map has any button  (when turn_count < 9)
    if (turn_count < 9) {
        Button nextB = bArrary[rand.nextInt(bArrary.length)];
        while (!nextB.isClickable()) {
            nextB = bArrary[rand.nextInt(bArrary.length)];
        }
        buttonClicked(nextB);
    }
}

在方法randomClicks(按钮b)上我们将调用b.setText(“0”)。

public void randomClicks(Button b){
    b.setText("O");
}

我认为它会奏效。