Overview: Trying to make game like Tic Tac toe but not working with 3/3 grid. Cant understand what's wrong with the code. Please help.
Problem: Grid is not making 3 lines to complete the game. Problem is with correct logic making.
What I want to achieve: I want to make a 3 by 3 grid to showcase when a game completes with the message you won.
public class MainActivity extends AppCompatActivity {
// 0: yellow, 1: red, 2: empty
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 } };
int activePlayer = 0;
boolean gameActive = true;
public void dropIn(View view) {
ImageView counter = (ImageView) view;
int tappedCounter = Integer.parseInt(counter.getTag().toString());
if (gameState[tappedCounter] == 1 && gameActive) {
gameState[tappedCounter] = activePlayer;
counter.setTranslationY(-1500);
if (activePlayer == 0) {
counter.setImageResource(R.drawable.yellow);
activePlayer = 1;
} else {
counter.setImageResource(R.drawable.red);
activePlayer = 0;
}
counter.animate().translationYBy(1500).rotation(3600).setDuration(300);
for (int[] winningPosition : winningPositions) {
if (gameState[winningPosition[0]] == gameState[winningPosition[1]]
&& gameState[winningPosition[1]] == gameState[winningPosition[2]]
&& gameState[winningPosition[0]] != 2) {
// Somone has won!
gameActive = false;
String winner = "";
if (activePlayer == 1) {
winner = "Yellow";
} else {
winner = "Red";
}
}
}
}
}
}

