Creating a simple Tic Tac Toe android game application using Java
In the following tutorial, we’ll be working to create a basic TicTacToe game for android platform. We’ll be coding the game in Java, using Android Studio IDE. The tutorial has been divided into 9 steps, each step moving progressively on top of last one.
This tutorial assumes, you have a basic working knowledge of android studio and the framework in place.
First, I have created images to be used for the application using GNU Image Manipulation Program, popularly known as GIMP. The game board, X’s and 0’s were created as part of these images. If you’re doing the same, make sure the images are created as ‘.png’ or ‘.jpg’ format only. Otherwise, you may download copyright free images from the internet, which ever way works for you.
After creating the necessary images, launch the Android Studio and open a new project with an empty activity. Once the gradle build finishes, continue with setting up the layout of the application. An empty project by default has a TextView set up within a Constraint Layout. So, go ahead and delete the Text View and pull in a grid view on top of the existing constraint layout. For the grid, add the rowCount and columnCount attributes of layout, as 3 and 3 respectively. Now move forward by setting up the tic tac toe board, on the grid layout, by placing the background attribute of the layout, as the board image created in the first step.
By the beginning of step3, your application would be looking something like this.
Now for each of the cell, to contain the image or X or 0, we need to add 9 ImageViews. One for each cell of the grid. So, go ahead and start placing ImageViews for each of the cells. You can place any of the image (either X or 0) for now, as we’ll be removing them after the view is created and be placing them back in place programmatically.
After the ImageViews are in place, we’ll need to add an onClick function for all the image views. This function will do the task of animating the X or O falling into the Grid cell which is being tapped by the user. This will be done by a simple piece of code, being added to MainActivity.java file, in app/main/java:
The above piece of code, takes care of the animated movement for the symbol X, but doesn’t do anything close to, what is required for the game to work like we want it to, yet.
To make it work like the tic tac toe game, one of fundamental things is to be able to alternate between the symbols being used. We’ll handle it by creating two players and assigning them each one of the symbols. The above code snippet changes to:
The above piece of code allows the user to alternate between X’s(activePlayer=1) and O’s(activePlayer=0) being placed on the grid.
Now the difficult part, how do we keep track of the game’s current state? Since, it’s one of the easy games, we needn’t look much further then using arrays. We are going to use a 1-D int array and tag attribute of the ImageView to achieve this. Assign each of the Imageview value of tag; begin with 0, at the top left corner; all the way to 8, at the bottom right corner. The tags values may look like below:
|0|1|2| |3|4|5| |6|7|8|
After this we can start with the coding, we add a
gameState array, initialized with value 2 in all places(Since, we are using 0, 1, to identify the player, we can use 2 as neutral value) and a int variable to capture which of the cells were tapped by user, by checking the
So, now our code looks like below:
The above code, places Zeros and ones in the gameState integer array. It does so, by getting tag value of the tapped image view and replacing that index(tag value) in
gameState array with activePlayer value.
Now we have a somewhat functioning application. We now need to add function to check for a winner when someone gets three in a row and to reset the game when someone has won.
Since, we are able to track the current game state now, we need to figure out a way to know as soon as the current game state resembles to any winning situation. We have just a few possibilities (8, precisely) for a three in a row situation in a 3X3 matrix. We can use a 2-D array to handle this situation.
Now, as we have all possible winning positions in the gameState, we can loop over each of the position array every time a
gameState change occurs.
Now, our application is in a position to see if someone has won and make a toast for it. But we still lack a few more things.
In this step, we’ll add a reset button and a text view displaying the winner of the game. And in the step afterwards, we’ll add the functionality to the reset button.
We’ll need to add a button to the layout for this and change the visibility of the button to Invisibile. Same process to be repeated for the Textview which is to be added. We’ll be changing the visibility of these elements in the program. So, we need them to be invisible by default.
If you’re using Android Studio, you can change the values directly from the Preview. Otherwise, we can go to activity_main.xml and add the following:
And the for loop in the previous step, now changes to be like below, changing the visibility of the elements after a winner is decided. Also, we have introduced a boolean value
gameActive at class level, to let system know when the game’s to be stopped:
Now as, we have added the
resetGame function to
onClick attribute of resetButton, we need to add that method to
MainActivity.java. The method does a few things for us:
- reset the visibility of elements(Button & a TextView) back to invisible.
- remove the images from the grid/board.
- reset the game to be active state, i.e.,
activeStatebool value is reset to true.
We are now good to go for packaging the program into an “.apk” file.
The application looks like below on Nexus 5X.
Keep coding! Have fun.
The full code of the application is available at GitHub: Github/The-Cyberian