Trouble understanding the "checking-winner" algorithm behind tic-toe game in android java by Rob Perceival

Here is the code for tic-tac toe game in Android Java I am learning for Udemy. I am able to undertand how to get the position of the tap by using setting an android:tag value to every image value. It is stored in tappedCounter. I am not able to decipher why gamestate is taken as 2,2,2,2,2,... and the algorithm behind winning the game.

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;
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) {

                // Someone has won!

                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);

                }

I am unable to understand this part specially. How is the checking and the array working?

for (int[] winningPosition : winningPositions) {

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

                // Someone has won!

1 answer

  • answered 2020-11-25 06:42 tom

    Although I am not aware of the Udemy course from where you are learning this, but from the given code I can understand the following things.

    1. You can assume every cell as positions in a 3x3 board. So the board would appear like this:

      0 1 2

      3 4 5

      6 7 8

    Now if are aware of the tic tac toe winning rules, then it should be pretty simple to understand the winningPositions array. It contains all the horizontal, vertical and diagonal paths which would result in a victory.

    1. Now to answer your second question, the gamestate is set to 2 initially to indicate that currently all of the cells are unmarked i.e. none of the 2 players have made a move in any of the cells (assigned as 2). So whenever any player would make a move on a cell, it will be marked by the player id (either as 0 or 1).

    2. The winning logic then is pretty straightforward. You are simply iterating over all of the path that are possible for any winner and then you are checking if in any of the paths all of the 3 cells are occupied by any one of the players.

      So there are basically 2 things that need to be checked:

      1. All of the 3 cells are equal i.e. occupied by the same player id.

        gameState[winningPosition[0]] == gameState[winningPosition[1]] && gameState[winningPosition[1]] == gameState[winningPosition[2]]

      2. If the cells are equal then they should be occupied by a player so the value should not be equal to 2.

        gameState[winningPosition[0]] != 2 indicates that the cells are not unoccupied

    For example if player 1 wins by occupying all the top horizontal cells together then the winning position {0 1 2} would all be having the same id as player 1's id.

    Hope that is clear!