lundi 4 janvier 2016

Android Development : Don't understand for each loop with 2d array

First, sorry for the vague question because i'm not a native english speaker. I'm learning Android Development at the moment and i came across this code in a tutorial for creating a simple tic tac toe game.

int[] gameState = {2,2,2,2,2,2,2,2,2};
int[][] winningStates = {{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}};
boolean gameIsActive = true;

/*
what does this for loop and if do?
Does the [0],[1],[2] increment itself when the for loop loops? 
If a 2d array is being convert into 1d array what it will look like? does it maintain the same position?
e.g int[][] winningStates = {{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}};
if converted into 1d array will be like this?
{0,1,2,3,4,5,6,7,8,0,3,6,1,4,7,2,5,8,0,4,8,2,4,6};
*/
for(int[] winningState : winningStates){
            if(gameState[winningState[0]] == gameState[winningState[1]] &&
                    gameState[winningState[1]] == gameState[winningState[2]] &&
                    gameState[winningState[0]] != 2){
                gameIsActive = false;
                String winner = "Black";

                if(gameState[winningState[0]] == 0){
                    winner = "Red";
                }
                //someone has won!
                playAgainMessage again = new playAgainMessage();
                again.playAgainMethod(winner + " has Won !!");
            }

The complete code can be found here(Mini Tic Tac Toe game) http://ift.tt/1Z2xzx4

Any help will be appreciated! Thanks!

Aucun commentaire:

Enregistrer un commentaire