Reputation: 31
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);
}
}
I have a strange problem with my tic tac toe app. Everything works as expected before implementing the functions for the draw. Then I click the position 0 it is activated the draw message. If I dont use the 0 position in my grid it also works. Why is the "It is a draw" activated then I click the first space in the grid?
Upvotes: 2
Views: 105
Reputation: 672
In your code setText method calls if at least one counterState != 2. Try to change in like this
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);
}
Upvotes: 1