-3

i am a beginner in java android development and i have created a tic tac toe game. i have finished the code for the game . but i am confused about the winning condition of the game as i am just learning from an instructor online. this is the specific code that i'm clueless about the game

//winning condition
//for(int [] winningPosition : winningPositions){
        if(gameState[winningPosition[0]] == gameState[winningPosition[1]] &&
                gameState[winningPosition[1]] == gameState[winningPosition[2]] &&
                gameState[winningPosition[0]] != 2)

this is the code. (I did not posted the entire code as i just want to show the relevant code to help you understand what i'm trying to ask)

public class MainActivity extends AppCompatActivity {

//0 = yellow , 1 = red;
int activePlayer = 0;
boolean gameIsActive = true;
//2 mean 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";
                            }
                                LinearLayout layout = (LinearLayout) findViewById(R.id.playAgainLayout);
                                TextView winnerMessage = (TextView) findViewById(R.id.winnerMessage);
                                winnerMessage.setText( winner +" has won!");
                                layout.setVisibility(View.VISIBLE);




            } else {

                boolean gameIsOver = true;
                for(int counterState : gameState){

                    if(counterState == 2) gameIsOver = false;


                }
                if(gameIsOver){

                    LinearLayout layout = (LinearLayout) findViewById(R.id.playAgainLayout);
                    TextView winnerMessage = (TextView) findViewById(R.id.winnerMessage);
                    winnerMessage.setText( "It's a draw!");
                    layout.setVisibility(View.VISIBLE);

                }
            }
        }

    }


}
UmarZaii
  • 1,355
  • 1
  • 17
  • 26
Jasper StaTham
  • 117
  • 2
  • 13

3 Answers3

1

winningPositions holds all possible field-combinations that you can hold to win the game (the three rows, the three columns and the two diagonals). Your code iterates over these winningPositions and for each checks whether all three fields are held by the same person and that this person is not no-one (//2 mean unplayed).

Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43
Vampire
  • 35,631
  • 4
  • 76
  • 102
0

a winner in this game should match 3 symbol (cross or circle) in a row in Vertical, Diagonal or horizontal line. Please refer attached images in which player playing with circle is winner in every image.Vertical match

[ Diagonal match  match[2]Horizontal

DhaRmvEEr siNgh
  • 1,918
  • 2
  • 13
  • 17
0

Basically what it does is this: For every winning state (which is every possible line of three boxes), it wants to know if they have the same state (X, O or void), and if they are not void (which is what I suppose the number 2 means).

Shinra tensei
  • 1,283
  • 9
  • 21