Rohit Kumar Rohit Kumar - 1 month ago 9
Android Question

Android Tic Tac Toe - Draw and Win Bug after 9 moves

I have written a tic-tac-toe. However, its local multiplayer screen (I have one AI match screen too - completed) has 1 issue.

It detects win very easily until 9 boxes are played. If 9 boxes are played, it declares a draw even if any one has won.

Below is my code. Please help.

static int activePlayer = 0; //Handles the player state
static boolean gameIsActive = true; //Checks whether the game is active or not

static int gameMoves = 0; //Handles the number of game moves -- Useful in draw games (9 Moves)

static int[] gameState = {2, 2, 2, 2, 2, 2, 2, 2, 2}; //Handles the GameState -- 2 means Unplayed

ImageView homeImageView; //Green Home Button
GridLayout boardGrid; //Tic-Tac-Toe Game Board
TextView resultTextView; //Dummy Result Text
Button resetButton; //Dummy Reset Button

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_local_multiplayer);


//Initialization
homeImageView = (ImageView) findViewById(R.id.iv_green_home);
boardGrid = (GridLayout) findViewById(R.id.boardGrid);
resultTextView = (TextView) findViewById(R.id.resultTextView);
resetButton = (Button) findViewById(R.id.resetButton);

//Handles fading in animation of the board pieces + GAME LOGIC!
public void fadeIn(View view) {

gameMoves++; //Increments the number of moves with each click on board piece

//Getting the view into an boardPiece(ImageView) (ofcourse its an ImageView, thats why)
ImageView boardPiece = (ImageView) view;

//Game Logic

int tappedCounter = Integer.parseInt(boardPiece.getTag().toString()); //Tapped Location
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}}; //Winning Positions Location

if (gameState[tappedCounter] == 2 && gameIsActive) {

gameState[tappedCounter] = activePlayer;

//Checking SharedPreferences Themes
int themeChosen = Utils.loadPreferences(getBaseContext(), "theme", 0);

//Changing Board Pieces based on Theme Selected
if (themeChosen == 0) {
if (activePlayer == 0) {
boardPiece.setImageResource(R.drawable.nought);
activePlayer = 1;
} else if (activePlayer == 1) {
boardPiece.setImageResource(R.drawable.cross);
activePlayer = 0;
}
boardPiece.setTranslationY(-1000f); //Sets the view off-screen
boardPiece.animate().translationYBy(1000f).setDuration(1000); //Gets it back on screen
} else if (themeChosen == 1) {
if (activePlayer == 0) {
boardPiece.setImageResource(R.drawable.heart);
activePlayer = 1;
} else if (activePlayer == 1) {
boardPiece.setImageResource(R.drawable.chocolate);
activePlayer = 0;
}
boardPiece.setTranslationY(-1000f); //Sets the view off-screen
boardPiece.animate().translationYBy(1000f).setDuration(1000); //Gets it back on screen
} else if (themeChosen == 2) {
if (activePlayer == 0) {
boardPiece.setImageResource(R.drawable.tom);
activePlayer = 1;
} else if (activePlayer == 1) {
boardPiece.setImageResource(R.drawable.jerry);
activePlayer = 0;
}
boardPiece.setTranslationY(-1000f); //Sets the view off-screen
boardPiece.animate().translationYBy(1000f).setDuration(1000); //Gets it back on screen
} else if (themeChosen == 3) {
if (activePlayer == 0) {
boardPiece.setImageResource(R.drawable.wizard);
activePlayer = 1;
} else if (activePlayer == 1) {
boardPiece.setImageResource(R.drawable.hog);
activePlayer = 0;
}
boardPiece.setTranslationY(-1000f); //Sets the view off-screen
boardPiece.animate().translationYBy(1000f).setDuration(1000); //Gets it back on screen
} else if (themeChosen == 4) {
if (activePlayer == 0) {
boardPiece.setImageResource(R.drawable.bat);
activePlayer = 1;
} else if (activePlayer == 1) {
boardPiece.setImageResource(R.drawable.ball);
activePlayer = 0;
}
boardPiece.setTranslationY(-1000f); //Sets the view off-screen
boardPiece.animate().translationYBy(1000f).setDuration(1000); //Gets it back on screen
} else if (themeChosen == 5) {
if (activePlayer == 0) {
boardPiece.setImageResource(R.drawable.tiger);
activePlayer = 1;
} else if (activePlayer == 1) {
boardPiece.setImageResource(R.drawable.mammoth);
activePlayer = 0;
}
boardPiece.setTranslationY(-1000f); //Sets the view off-screen
boardPiece.animate().translationYBy(1000f).setDuration(1000); //Gets it back on screen
}
}

//Handles Winning - Losing
for (int[] winningPosition : winningPositions) {

//Checking SharedPreferences Themes
int themeChosen = Utils.loadPreferences(getBaseContext(), "theme", 0);

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

//Someone has won!
gameIsActive = false; //Disable Game Playablity

if (themeChosen == 0) {
if (gameState[winningPosition[0]] == 0) {
//Noughts Won!
Log.i("winner", "normal1");
resultTextView.setText("Winner: Nought");
} else {
//Cross Won!
Log.i("winner", "normal2");
resultTextView.setText("Winner: Cross");
}
} else if (themeChosen == 1) {
if (gameState[winningPosition[0]] == 0) {
//Hearts Won!
Log.i("winner", "love1");
resultTextView.setText("Winner: Heart");
} else {
//Chocolates Won!
Log.i("winner", "love2");
resultTextView.setText("Winner: Chocolate");
}
} else if (themeChosen == 2) {
if (gameState[winningPosition[0]] == 0) {
//Tom Won!
Log.i("winner", "tomandjerry1");
resultTextView.setText("Winner: Tom");
} else {
//Jerry Won!
Log.i("winner", "tomandjerry2");
resultTextView.setText("Winner: Jerry");
}
} else if (themeChosen == 3) {
if (gameState[winningPosition[0]] == 0) {
//Wizards Won!
Log.i("winner", "clashofclans1");
resultTextView.setText("Winner: Wizard");
} else {
//Hogs Won!
Log.i("winner", "clashofclans2");
resultTextView.setText("Winner: Hog");
}
} else if (themeChosen == 4) {
if (gameState[winningPosition[0]] == 0) {
//Bat Won!
Log.i("winner", "cricket1");
resultTextView.setText("Winner: Bat");
} else {
//Ball Won!
Log.i("winner", "cricket2");
resultTextView.setText("Winner: Ball");
}
} else if (themeChosen == 5) {
if (gameState[winningPosition[0]] == 0) {
//Tiger Won!
Log.i("winner", "iceage1");
resultTextView.setText("Winner: Tiger");
} else {
//Mammoth Won!
Log.i("winner", "iceage2");
resultTextView.setText("Winner: Mammmoth");
}
}
} else if (gameState[winningPosition[0]] != 2
&& gameState[winningPosition[0]] != gameState[winningPosition[1]]
&& gameState[winningPosition[1]] != gameState[winningPosition[2]]
&& gameMoves >= 9) {

gameIsActive = false; //Disable Game Playablity

//Its a draw!
if (themeChosen == 0) {
Log.i("winner", "normal");
resultTextView.setText("Winner: Draw 1");
} else if(themeChosen == 1) {
Log.i("winner", "love");
resultTextView.setText("Winner: Draw 2");
} else if(themeChosen == 2) {
Log.i("winner", "tomandjerry");
resultTextView.setText("Winner: Draw 3");
} else if(themeChosen == 3) {
Log.i("winner", "clashofclans");
resultTextView.setText("Winner: Draw 4");
} else if(themeChosen == 4) {
Log.i("winner", "cricket");
resultTextView.setText("Winner: Draw 5");
} else if(themeChosen == 5) {
Log.i("winner", "iceage");
resultTextView.setText("Winner: Draw 6");
}
}
}

}

//Resetting game - Method
public void resetGame() {

//Resets the Result Text
resultTextView.setText("");

//Resets the Game
activePlayer = 0; //Clears the player state
gameIsActive = true; //Reactivates the game - Sets it to activated state

gameMoves = 0; //Resets the number of game moves done to 0

//Clears the Game State
gameState[0] = 2;
gameState[1] = 2;
gameState[2] = 2;
gameState[3] = 2;
gameState[4] = 2;
gameState[5] = 2;
gameState[6] = 2;
gameState[7] = 2;
gameState[8] = 2;

//Clears the ImageViews
for(int i = 0; i < boardGrid.getChildCount(); i++){
ImageView iv = (ImageView) boardGrid.getChildAt(i);
iv.setImageResource(0);
}
}


}

Thanks in advance. Any help is greatly appreciated. :)

Answer

So the issue is that when it is looping through the different possible winning positions, if it encounters a possible winning position that is not 3 in a row, it will report it as a tie once 9 moves have been played. What you need to do is create an int winner and default that to 2. Your for should look like this now

int winner = 2;
for (int[] winningPosition : winningPositions) {

    //Checking SharedPreferences Themes
    int themeChosen = Utils.loadPreferences(getBaseContext(), "theme", 0);

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

        //Someone has won!
        gameIsActive = false; //Disable Game Playablity

        winner = gameState[winningPosition[0]];
        break;
    } else if(gameMoves >= 9) {
        gameIsActive = false;
    }
}

Then outside the for loop you should have

if(winner != 2) {
  CODE FOR HANDLING WIN HERE
} else {
  CODE FOR HANDLING DRAW HERE
}