2015-11-10 67 views
-1

我正在製作一個Tic Tac Toe遊戲。我想創建一個玩家部分。如何讓用戶在android中點擊後自動點擊按鈕?

用戶總是X.

開始當在用戶點擊按鈕寫X,我希望設備上的任何按鈕自動寫入O,其用戶播放。

這裏是我的代碼,但它不工作,當X贏得比賽,它會崩潰,但如果Ø獲勝,遊戲作品:

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

我看到方法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"); 
} 

我認爲它會工作。

+0

抱歉,但我不知道如何選擇隨機按鈕nextB ?! –

+0

我試過這個,但沒有工作: @Override public void onClick(View v){ Button b =(Button)v; buttonlicked(b); Random rand = new Random(); Button nextB = bArrary [rand.nextInt(bArrary.length)]; buttonClicked(nextB); } –

+0

bArrary包含禁用的按鈕 - >它無法工作。 現在您可以在tic_tac_toe地圖的其餘部分(未設置setClickable(false)的按鈕)上找到列表。在這個列表中,您可以選擇一個隨機按鈕。 我編輯了我的答案。 – thantieuhodo