title: Android Studio - JAVA - TIC TAC TOE
body: <p>I have made my first tic tac toe game on android, but I have found a bug and I dont know how to fix it.</p>
  
  <p>When player1 wins with last turn in certain combinations like bottom row horizontal or first vertical row then it pops - It is a draw - when there is winner.</p>
  
  <p>Here I define my variables.</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  // 0 yellow ; 1 red;
  int activePlayer = 0;
  
  boolean gameIsActive = true;
  
  // 2 unplayed;
  int[] gameState = {2, 2, 2, 2, 2, 2, 2, 2, 2};
  int[][] winningPositions = {
          {0,1,2}, {3,4,5}, {6,7,8}, //hor
          {0,3,6}, {1,4,7}, {2,5,8}, // ver
          {0,4,8}, {2,4,6}
  };         // dia
  </code></pre>
  
  <p>Here is my logic behind it. I know that fixing need to be made somewhere after the comment: checking for the winner! Because it does not go trough if part but goes into else and pops - it is a draw.</p>
  
  <p>full code as requested JAVA:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  // 0 yellow ; 1 red;
  int activePlayer = 0;
  boolean gameIsActive = true;
  // 2 unplayed;
  int[] gameState = {2, 2, 2, 2, 2, 2, 2, 2, 2};
  int[][] winningPositions = {
          {0,1,2}, {3,4,5}, {6,7,8}, //hor
          {0,3,6}, {1,4,7}, {2,5,8}, // ver
          {0,4,8}, {2,4,6}
  };         // dia
  public void dropIn (View view) {
      ImageView counter = (ImageView) view;
      int tappedCounter = Integer.parseInt(counter.getTag().toString());
      if (gameState[tappedCounter] == 2  &amp;&amp; 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(500);
          //checking for the winner
          for (int[] winningPosition : winningPositions) {
              if (gameState[winningPosition[0]] ==      gameState[winningPosition[1]] &amp;&amp;
                      gameState[winningPosition[1]] == gameState[winningPosition[2]] &amp;&amp;
                      gameState[winningPosition[0]] !=2 ) {
                  //Someone has won
                  gameIsActive = false;
                  String winner = "Red";
                  if (gameState[winningPosition[0]] == 0){
                      winner = "Yellow";
                  }
                  TextView winnerMsg = (TextView)  findViewById(R.id.winnerMsg);
                  winnerMsg.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 winnerMsg = (TextView) findViewById(R.id.winnerMsg);
                      winnerMsg.setText("It is a DRAW!");
                      LinearLayout layout = (LinearLayout)findViewById(R.id.playAgainLayout);
                      layout.setVisibility(View.VISIBLE);
  
                  }
              }
          }
      }
  }
  public void playAgain(View view) {
      gameIsActive = true;
      LinearLayout layout = (LinearLayout)findViewById(R.id.playAgainLayout);
      layout.setVisibility(View.INVISIBLE);
      activePlayer = 0;
      for (int i = 0; i &lt; gameState.length; i++) {
          gameState[i] = 2;
      }
      GridLayout gridLayout = (GridLayout)findViewById(R.id.gridLayout);
      for (int i = 0; i &lt; gridLayout.getChildCount(); i++) {
          ((ImageView) gridLayout.getChildAt(i)).setImageResource(0);
      }
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  }
  </code></pre>
  
  <p>and XML:</p>
  
  <pre><code>&lt;GridLayout
      android:layout_width="match_parent"
      android:layout_height="360dp"
      android:layout_centerVertical="true"
      android:layout_centerHorizontal="true"
      android:columnCount="3"
      android:rowCount="3"
      android:background="@drawable/board"
      android:id="@+id/gridLayout"&gt;
  
      &lt;ImageView
          android:layout_width="90dp"
          android:layout_height="90dp"
          android:id="@+id/imageView"
          android:layout_row="0"
          android:layout_column="0"
          android:layout_margin="10dp"
          android:onClick="dropIn"
          android:tag="0"/&gt;
  
      &lt;ImageView
          android:layout_width="90dp"
          android:layout_height="90dp"
          android:id="@+id/imageView2"
          android:layout_row="0"
          android:layout_column="1"
          android:layout_marginTop="10dp"
          android:layout_marginLeft="15dp"
          android:onClick="dropIn"
          android:tag="1"/&gt;
  
      &lt;ImageView
          android:layout_width="90dp"
          android:layout_height="90dp"
          android:id="@+id/imageView3"
          android:layout_row="0"
          android:layout_column="2"
          android:layout_marginTop="10dp"
          android:layout_marginLeft="15dp"
          android:onClick="dropIn"
          android:tag="2"/&gt;
  
      &lt;ImageView
          android:layout_width="90dp"
          android:layout_height="90dp"
          android:id="@+id/imageView4"
          android:layout_row="1"
          android:layout_column="0"
          android:layout_marginLeft="10dp"
          android:layout_marginTop="25dp"
          android:onClick="dropIn"
          android:tag="3"/&gt;
  
      &lt;ImageView
          android:layout_width="90dp"
          android:layout_height="90dp"
          android:id="@+id/imageView5"
          android:layout_row="1"
          android:layout_column="1"
          android:layout_marginLeft="15dp"
          android:layout_marginTop="25dp"
          android:onClick="dropIn"
          android:tag="4"/&gt;
  
      &lt;ImageView
          android:layout_width="90dp"
          android:layout_height="90dp"
          android:id="@+id/imageView6"
          android:layout_row="1"
          android:layout_column="2"
          android:layout_marginLeft="15dp"
          android:layout_marginTop="25dp"
          android:onClick="dropIn"
          android:tag="5"/&gt;
  
      &lt;ImageView
          android:layout_width="90dp"
          android:layout_height="90dp"
          android:id="@+id/imageView7"
          android:layout_row="2"
          android:layout_column="0"
          android:layout_marginLeft="10dp"
          android:layout_marginTop="30dp"
          android:onClick="dropIn"
          android:tag="6"/&gt;&gt;
  
      &lt;ImageView
          android:layout_width="90dp"
          android:layout_height="90dp"
          android:id="@+id/imageView8"
          android:layout_row="2"
          android:layout_column="1"
          android:layout_marginLeft="15dp"
          android:layout_marginTop="30dp"
          android:onClick="dropIn"
          android:tag="7"/&gt;
  
      &lt;ImageView
          android:layout_width="90dp"
          android:layout_height="90dp"
          android:id="@+id/imageView9"
          android:layout_row="2"
          android:layout_column="2"
          android:layout_marginLeft="15dp"
          android:layout_marginTop="30dp"
          android:onClick="dropIn"
          android:tag="8"/&gt;
  &lt;/GridLayout&gt;
  
  &lt;LinearLayout
      android:orientation="vertical"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerVertical="true"
      android:layout_centerHorizontal="true"
      android:background="#deed17"
      android:padding="30dp"
      android:id="@+id/playAgainLayout"
      android:baselineAligned="false"
      android:visibility="invisible"&gt;
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="New Text"
          android:id="@+id/winnerMsg"
          android:layout_gravity="center_horizontal"
          android:textSize="30sp" /&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Play Again!"
          android:id="@+id/playBtn"
          android:layout_gravity="center_horizontal"
          android:onClick="playAgain" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p></p>