Tic tac toe android app code

More Android Tutorials

As, we want 9 blocks to fill either cross or circle for the game, we will do this by placing 9 ImageViews inside the GridView of 3X3 dimension. Lets give a color to GridView to make it distinct from the background.

Results for: tic tac toe android source code

Go ahead and add another color inside colors. The lines are achieved by separating the ImageViews from each other. When ImageViews are pushed far from each other, then we see background of the GridView which works as lines for the game. For this, we make margins to these ImageViews. Similarly we do for the next seven ImageViews. These along with the above two are placed inside the GridLayout. Now we have finished making the board to play game. They are either horizontal, vertical or diagonal.

In total, there can be at most 8 such lines in 3X3 tictactoe. First line is displayed when block 1, block 2 and block 3 have all same selection that is either cross or circle. So we call blocks 1,2,3 as set 1. As these lines are not shown at the beginning of the game shown only after game is finished , we will hide these using visibility attribute of View. The height and width of the view is so adjusted that it looks vertical. We are going to define various game states under a java class and figure out the winner and block set involved.

Go to your package name and right click on it. Android studio will allow you to enter your class name, which we name GameLogic and press OK. Create an array of ImageView to store blocks which has to be field for this class. Create a String field to store winner and an integer field to trace down which set is satisfied for game to end. Here final keyword reflects that these variables cannot have some other values than this. We want to check if all three blocks of certain set have same kind of selection at certain point.

For this we need to make a method which takes position of these blocks first block's position, second block's position and third block's position and also the block set as last parameter. The main objective of this method is to return true when match is found or else it will return false. Also this method need not be called from outside the class and hence declared private. If the value is true meaning game has been won by either cross or circle , we get id and find the winner. The following line inside this method is needed to find out which block set has been involved to have this game ended.

Next we create the most important method of this class. This method is called when we tap on one of the block during game and hence takes position of the block tapped along with all those blocks as array. We have to check for possible sets for every position. For example, for position 1, we have 1,4 and 7 as valid set refer to the image below to understand more clearly.

To do this, we take help of switch statement and set a local variable isComplete to true if at least one of them is valid. This is done by using logical OR operator. The first one will track if the exit button is pressed twice and hence we have to close the app while the second one will track the number of blocks used and hence we declare game to be drawn if its value reaches 9.

As 9 means, all of the blocks are used but no one is the winner. So we create another methods as below:. Inside it we initialize mExit ImageView and set event listene r which exits app on tapped twice. After that, we will initialize mDisplay and mReplay ImageView. We will recall this game activity when mReplay is tapped. So to do this dynamically, we can use the getResources.

#3 Android Tic Tac Toe App Tutorial - Player Move

We will make this method next. So we call these two methods from inside the onCreate method because onCreate method is run when application runs. Thus the onCreate method should look like. We also disable the ImageView from further being tapped. The main thing done here is to use GameLogic class to check if the game has completed. At the mean time, we also keep the display status in mind. Override the onBackPressed method and make it empty. This will disable the back button of device.

Account Options

I am more than happy to answer any of your questions related to this article. Just leave a comment and I will reply you within a day. Sign in or sign up and post using a HubPages Network account. Comments are not for promoting your articles or other sites. You seemed to have missed creating enum. Look the "Create the following fields which will define the state of the game. Inside MainActivity. CROSS ;". Actually I am facing a problem with the layout section the grid outer lining is visible like appart from the 1st row in the 2nd and 3rd row the outer boundary of my grid layout is also showing..

Other product and company names shown may be trademarks of their respective owners. HubPages and Hubbers authors may earn revenue on this page based on affiliate relationships and advertisements with partners including Amazon, Google, and others. HubPages Inc, a part of Maven Inc.

  • best useful paid iphone apps?
  • samsung kies air app download?
  • Creating Simple Tic-tac-toe Game for Android | Free Source Code & Tutorials.
  • apple ios 6 email not working.
  • Latest Blog Posts.
  • cell phone themes free download samsung galaxy y?

As a user in the EEA, your approval is needed on a few things. To provide a better website experience, owlcation. Please choose which areas of our service you consent to our doing so. Asked 3 years, 5 months ago.

Subscribe to RSS

Active 3 years, 5 months ago. Viewed 7k times. Here I define my variables. Please paste your full file as it is not clear what you are doing in rest of the file. DarkJade DarkJade 1 1 silver badge 8 8 bronze badges. Thank you so much!

  • Related Searches!
  • descargar jetpack joyride para iphone 3g gratis.
  • fast tube downloader for nokia belle.
  • Subscribe via email.
  • 104 programs for "tic tac toe android source code".

It did the trick! Glad to hear it worked. If you liked the answer, could you please upvote it? I need bit more reputation to do that, sry. Seems fair. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password.

Two Player Tic Tac Toe Game in Android Studio

Post as a guest Name. Email Required, but never shown. Featured on Meta.