我的Tic Tac Toe应用程序有一个bugg

时间:2018-02-16 07:56:45

标签: android algorithm tic-tac-toe

public class MainActivity extends AppCompatActivity {

// 0=yellow and 1= red

int activePlayer =0;

boolean gameIsActive=true;

// 2 means unplayed

int[] gameState ={2, 2, 2, 2, 2, 2, 2, 2, 2};

int [][] winningPositions = {{0,1,2}, {3,4,5}, {6,7,8}, {0,3,6}, {1,4,7}, {2,5,8}, {0,4,8}, {2,4,6} };


public void dropIn(View view) {

    ImageView counter = (ImageView) view;

    System.out.println(counter.getTag().toString());

    int tappedCounter = Integer.parseInt(counter.getTag().toString());

    if (gameState[tappedCounter] == 2 && gameIsActive) {

        gameState[tappedCounter] = activePlayer;


        counter.setTranslationY(-1000f);

        if (activePlayer == 0) {

            counter.setImageResource(R.drawable.yellow);
            activePlayer = 1;

        } else {

            counter.setImageResource(R.drawable.red);
            activePlayer = 0;
        }
        counter.animate().translationYBy(1000f).rotation(360).setDuration(300);

        for (int[] winningPosition : winningPositions){

            if (gameState[winningPosition[0]]==gameState[winningPosition[1]] &&
                    gameState[winningPosition[1]]==gameState[winningPosition[2]] &&
                    gameState[winningPosition[0]] !=2) {

                 gameIsActive=false;
                String winner="Red";
              if ( gameState[winningPosition[0]] ==0){
                  winner = "Yellow";


              }

                TextView winnerMessage =(TextView)findViewById(R.id.winnerMessage) ;
                winnerMessage.setText(winner + " has won!");

                LinearLayout layout =(LinearLayout)findViewById(R.id.playAgainLayout);
                layout.setVisibility(View.VISIBLE);
            } else {
                boolean gameIsOver=true;
                for (int counterState: gameState){
                    if (counterState==2) gameIsOver =false;

                    if (gameIsOver) {

                        TextView winnerMessage =(TextView)findViewById(R.id.winnerMessage) ;
                        winnerMessage.setText("It's a draw!");

                        LinearLayout layout =(LinearLayout)findViewById(R.id.playAgainLayout);
                        layout.setVisibility(View.VISIBLE);
                    }
                }
            }
        }

    }

}
 public void playAgain(View view) {

     LinearLayout layout =(LinearLayout)findViewById(R.id.playAgainLayout);
     layout.setVisibility(View.INVISIBLE);

     gameIsActive=true;

     activePlayer=0;

    for (int i =0;i<gameState.length;i++){
        gameState[i]=2;
    }

     GridLayout gridlayout =(GridLayout)findViewById(R.id.gridLayout);

     for(int i=0; i<gridlayout.getChildCount();i++){
         ((ImageView)gridlayout.getChildAt(i)).setImageResource(0);
     }

 }

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

我的tic tac toe app有一个奇怪的问题。在实现绘制功能之前,一切都按预期工作。然后我点击它激活绘制消息的位置0。如果我不在我的网格中使用0位置它也可以。为什么&#34;它是平局&#34;激活然后我点击网格中的第一个空格?

1 个答案:

答案 0 :(得分:1)

在你的代码中setText方法调用至少一个counterState!= 2.尝试改变这样

boolean gameIsOver=true;
for (int counterState: gameState){
    if (counterState==2){
       gameIsOver =false;  
       break;
    }         
}
if (gameIsOver) {
    TextView winnerMessage =(TextView)findViewById(R.id.winnerMessage);
    winnerMessage.setText("It's a draw!");
    LinearLayout layout =(LinearLayout)findViewById(R.id.playAgainLayout);
                    layout.setVisibility(View.VISIBLE);
}