Introduction

As a child, I used to love the Tic Tac Toe game. Even now it is the best way to pass time between boring lectures :p. So let us make our own Tic Tac Toe today.
Step 1
Make the following changes in "string.xml":
  1. <string name="app_name" >TicTacToefinal</string>
  2. <string name="action_settings" >Settings </string>
  3. <string name="hello_world" >Tic Tac Toe</string>
Step 2
Make the following changes in "dimens.xml":
  1. <dimen name="activity_horizontal_margin">16dp</dimen>
  2. <dimen name="activity_vertical_margin">16dp</dimen>
  3. <dimen name="heading">50dp</dimen>
  4. <dimen name="congo">35dp</dimen>
Step 3
For adding colors let us make a new resource file. Right-click on "Values" -> "New" -> "Values resource file". Name this file as "color" and add the following code to it:
  1. <resources>
  2. <color name="player">#360ec5</color>
  3. <color name="gamecol">#e7d39e</color>
  4. <color name="bgwin">#000000</color>
  5. <color name="drawbg">#977897</color>
  6. </resources>
Now let us start making the layouts.
Step 4
Making the first screen of our game.
Add the following code in "activity_main.xml" in "layout":
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingLeft="@dimen/activity_horizontal_margin"
  6. android:paddingRight="@dimen/activity_horizontal_margin"
  7. android:paddingTop="@dimen/activity_vertical_margin"
  8. android:paddingBottom="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity"
  10. android:background="@drawable/images">
  11. <TextView
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="@string/hello_world"
  15. android:layout_marginLeft="50dp"
  16. android:paddingTop="20dp"
  17. android:textSize="@dimen/heading"
  18. />
  19. <Button
  20. android:layout_width="fill_parent"
  21. android:layout_height="wrap_content"
  22. android:text="Clich Here"
  23. android:layout_marginTop="400dp"
  24. android:layout_marginLeft="-100dp"
  25. android:background="@drawable/button_lay"
  26. android:id="@+id/clickHere"/>
  27. </LinearLayout>
Note that I have added an image for the background. The process of adding the image is simple. Copy the required image to the clipboard and paste it in "drawables".
The layout looks like:
im1.jpg
Step 5
For making the menu, create a new layout file. Right-click on "Layout" -> "New" -> "Layout resource file". Name this file as "menu_layout" and add the following to it:
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:orientation="vertical"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@drawable/menu_back" >
  6. <RelativeLayout
  7. android:layout_height="wrap_content"
  8. android:layout_width="fill_parent">
  9. <TextView
  10. android:layout_height="wrap_content"
  11. android:layout_width="wrap_content"
  12. android:text="Player 1 "
  13. android:textSize="@dimen/heading"
  14. android:layout_marginTop="50dp"
  15. android:layout_marginLeft="20dp"
  16. android:textColor="@color/player"
  17. android:id="@+id/t1"/>
  18. <RadioGroup
  19. android:layout_height="wrap_content"
  20. android:layout_width="fill_parent"
  21. android:id="@+id/player1"
  22. android:layout_marginTop="50dp"
  23. android:layout_marginLeft="240dp">
  24. <RadioButton
  25. android:layout_height="wrap_content"
  26. android:layout_width="wrap_content"
  27. android:text="O"
  28. android:textSize="@dimen/heading"
  29. android:checked="true"
  30. android:id="@+id/zero"/>
  31. <RadioButton
  32. android:layout_height="wrap_content"
  33. android:layout_width="wrap_content"
  34. android:text="X"
  35. android:textSize="@dimen/heading"
  36. android:id="@+id/cross"/>
  37. </RadioGroup>
  38. </RelativeLayout>
  39. <RelativeLayout
  40. android:layout_height="wrap_content"
  41. android:layout_width="fill_parent">
  42. <TextView
  43. android:layout_height="wrap_content"
  44. android:layout_width="wrap_content"
  45. android:text="Player 2 "
  46. android:textSize="@dimen/heading"
  47. android:layout_marginTop="50dp"
  48. android:layout_marginLeft="20dp"
  49. android:textColor="@color/player"
  50. android:id="@+id/t2"/>
  51. <RadioGroup
  52. android:layout_height="wrap_content"
  53. android:layout_width="fill_parent"
  54. android:id="@+id/player2"
  55. android:layout_marginTop="50dp"
  56. android:layout_marginLeft="240dp">
  57. <RadioButton
  58. android:layout_height="wrap_content"
  59. android:layout_width="wrap_content"
  60. android:text="O"
  61. android:textSize="@dimen/heading"
  62. android:id="@+id/zero"/>
  63. <RadioButton
  64. android:layout_height="wrap_content"
  65. android:layout_width="wrap_content"
  66. android:text="X"
  67. android:checked="true"
  68. android:textSize="@dimen/heading"
  69. android:id="@+id/cross"/>
  70. </RadioGroup>
  71. </RelativeLayout>
  72. <Button
  73. android:id="@+id/play"
  74. android:layout_height="wrap_content"
  75. android:layout_width="wrap_content"
  76. android:text="Play"
  77. android:layout_marginTop="80dp"
  78. android:layout_marginLeft="150dp"
  79. android:paddingLeft="10dp"
  80. android:paddingRight="10dp"
  81. android:background="@drawable/button_lay"/>
  82. </LinearLayout>
The layout looks like:
im2.jpg
Step 6
The layout for displaying the main game screen will be a Grid. Create a new layout file. Name it as "game_layout" and add the following code to it:
  1. <GridView xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:orientation="vertical"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:id="@+id/gridview1"
  6. android:numColumns="3"
  7. android:layout_marginTop="30dp"
  8. android:layout_marginLeft="30dp"
  9. android:layout_marginRight="30dp"
  10. android:layout_marginBottom="30dp"
  11. android:gravity="center"
  12. android:horizontalSpacing="20dip"
  13. android:verticalSpacing="80dp"
  14. android:paddingTop="40dp"
  15. android:paddingBottom="40dp">
  16. </GridView>
The layout looks like:
im3.jpg
Step 7
Inside the Grid View I will use the text view. So, create a new layout file and name it as "game_layout2". Add the following code to it:
  1. <TextView xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:orientation="vertical"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@color/gamecol"
  6. android:id="@+id/txt"
  7. android:gravity="center">
  8. </TextView>
The layout looks like
im4.jpg
Step 8
Make a new layout file and name it as "win_layout". Add the following to it:
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:orientation="vertical"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@color/bgwin">
  6. <Button
  7. android:layout_height="wrap_content"
  8. android:layout_width="wrap_content"
  9. android:text="Play Again"
  10. android:layout_marginTop="10dp"
  11. android:layout_marginLeft="10dp"
  12. android:background="@drawable/button_lay"
  13. android:paddingRight="10dp"
  14. android:paddingLeft="10dp"
  15. android:id="@+id/back"/>
  16. <ImageView
  17. android:id="@+id/trophy"
  18. android:layout_height="150dp"
  19. android:layout_width="150dp"
  20. android:layout_marginLeft="10dp"
  21. android:layout_marginTop="30dp"/>
  22. <TextView
  23. android:layout_height="wrap_content"
  24. android:layout_width="fill_parent"
  25. android:textColor="@color/gamecol"
  26. android:textSize="@dimen/congo"
  27. android:layout_marginLeft="20dp"
  28. android:text="CONGRATULATIONS"
  29. android:layout_marginTop="30dp"
  30. />
  31. <TextView
  32. android:layout_height="wrap_content"
  33. android:layout_width="fill_parent"
  34. android:textSize="@dimen/congo"
  35. android:layout_marginTop="40dp"
  36. android:layout_marginLeft="100dp"
  37. android:id="@+id/wintxt"
  38. android:textColor="@color/gamecol"
  39. />
  40. <TextView
  41. android:layout_height="wrap_content"
  42. android:layout_width="fill_parent"
  43. android:text="WON!!!"
  44. android:textSize="@dimen/congo"
  45. android:layout_marginLeft="90dp"
  46. android:layout_marginTop="30dp"
  47. android:textColor="@color/gamecol"/>
  48. </LinearLayout>
The layout looks like
im5.jpg
Step 9
Make a new layout file and name it as "draw_layout". Add the following to it:
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:orientation="vertical"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@color/drawbg">
  6. <Button
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="Play again"
  10. android:background="@drawable/button_lay"
  11. android:paddingLeft="10dp"
  12. android:paddingRight="10dp"
  13. android:layout_marginLeft="30dp"
  14. android:layout_marginTop="30dp"
  15. android:id="@+id/drawBack"/>
  16. <TextView
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"
  19. android:text="No One Wins"
  20. android:layout_marginTop="150dp"
  21. android:layout_marginLeft="30dp"
  22. android:textSize="@dimen/heading"/>
  23. <TextView
  24. android:layout_width="fill_parent"
  25. android:layout_height="wrap_content"
  26. android:text="Game DRAW!!"
  27. android:layout_marginTop="80dp"
  28. android:layout_marginLeft="70dp"
  29. android:textSize="@dimen/congo"/>
  30. </LinearLayout>
The layout looks like
im6.jpg
Step 10:
For beautification, all my buttons will use the following drawable resource as their background. Right-click on "Drawable" -> "New" -> "Drawable resource file". Name this file as "admin_design" and add the following to it:
  1. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  2. <item android:state_enabled="false" >
  3. <shape android:shape="rectangle">
  4. <gradient
  5. android:startColor="#454545"
  6. android:endColor="#454545"
  7. android:angle="-90"
  8. android:type="linear"
  9. />
  10. <corners android:radius="5dp" />
  11. </shape>
  12. </item>
  13. <!-- pressed -->
  14. <item android:state_pressed="true" android:state_enabled="true" >
  15. <shape android:shape="rectangle">
  16. <gradient
  17. android:startColor="#64334C"
  18. android:endColor="#300019"
  19. android:angle="-90"
  20. android:type="linear"
  21. />
  22. <corners android:radius="5dp" />
  23. </shape>
  24. </item>
  25. <!-- focused -->
  26. <item android:state_focused="true">
  27. <shape android:shape="rectangle">
  28. <gradient
  29. android:startColor="#C76699"
  30. android:endColor="#d9c292"
  31. android:angle="-90"
  32. android:type="linear"
  33. />
  34. <corners android:radius="5dp" />
  35. <stroke android:width="2dp" android:color="#dddddd"/>
  36. </shape>
  37. </item>
  38. <!-- default -->
  39. <item>
  40. <shape android:shape="rectangle">
  41. <gradient
  42. android:startColor="#C76699"
  43. android:endColor="#d9c292"
  44. android:angle="-90"
  45. android:type="linear"
  46. />
  47. <corners android:radius="5dp" />
  48. </shape>
  49. </item>
  50. </selector>
Now let us start with the Java part.
Step 11
Add the following code to "MainActivity.java":
  1. package com.tictactoefinal;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.app.Activity;
  6. import android.view.Menu;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.RadioButton;
  10. import android.widget.RadioGroup;
  11. public class MainActivity extends Activity {
  12. Button b;
  13. RadioGroup g1;
  14. RadioGroup g2;
  15. RadioButton rd;
  16. String p1;
  17. String p2;
  18. Button click;
  19. final Context context = this;
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_main);
  24. click = (Button) findViewById(R.id.clickHere);
  25. click.setOnClickListener(new View.OnClickListener() {
  26. @Override
  27. public void onClick(View v) {
  28. setContentView(R.layout.menu_layout);
  29. b = (Button) findViewById(R.id.play);
  30. g1 = (RadioGroup) findViewById(R.id.player1);
  31. g2 = (RadioGroup) findViewById(R.id.player2);
  32. b.setOnClickListener(new View.OnClickListener() {
  33. @Override
  34. public void onClick(View v) {
  35. int sel = g1.getCheckedRadioButtonId();
  36. rd = (RadioButton) findViewById(sel);
  37. p1 = rd.getText().toString();
  38. sel = g2.getCheckedRadioButtonId();
  39. rd = (RadioButton) findViewById(sel);
  40. p2 = rd.getText().toString();
  41. Intent i = new Intent(context, Game.class);
  42. i.putExtra("Player1", p1);
  43. i.putExtra("Player2", p2);
  44. startActivity(i);
  45. }
  46. });
  47. }
  48. });
  49. }
  50. @Override
  51. public boolean onCreateOptionsMenu(Menu menu) {
  52. getMenuInflater().inflate(R.menu.main, menu);
  53. return true;
  54. }
  55. }
Step 12
For the actual game, make a new activity. In the same package make a new activity and name it as "Game". Add the following code to it:
  1. package com.tictactoefinal;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.*;
  8. public class Game extends Activity {
  9. String p1;
  10. String p2;
  11. static int i=0;
  12. int j;
  13. GridView gridview;
  14. static final String[] symbols=new String[]{"","","","","","","","",""};
  15. int visitedp1[]={-1,-1,-1,-1,-1,-1,-1,-1,-1};
  16. int visitedp2[]={-1,-1,-1,-1,-1,-1,-1,-1,-1};
  17. final Context context=this;
  18. public void check(int visitedp1[], int visitedp2[])
  19. {
  20. int flag=0;
  21. String winner=null;
  22. //player1
  23. if((visitedp1[0]==1)&& (visitedp1[4]==1) &&(visitedp1[8]==1))
  24. {
  25. flag=1;
  26. winner="Player 1";
  27. }
  28. else if((visitedp1[2]==1)&& (visitedp1[4]==1) &&(visitedp1[6]==1))
  29. {
  30. flag=1;
  31. winner="Player 1";
  32. }
  33. else if((visitedp1[0]==1)&& (visitedp1[3]==1) &&(visitedp1[6]==1))
  34. {
  35. flag=1;
  36. winner="Player 1";
  37. }
  38. else if((visitedp1[1]==1)&& (visitedp1[4]==1) &&(visitedp1[7]==1))
  39. {
  40. flag=1;
  41. winner="Player 1";
  42. }
  43. else if((visitedp1[2]==1)&& (visitedp1[5]==1) &&(visitedp1[8]==1))
  44. {
  45. flag=1;
  46. winner="Player 1";
  47. }
  48. else if((visitedp1[0]==1)&& (visitedp1[1]==1) &&(visitedp1[2]==1))
  49. {
  50. flag=1;
  51. winner="Player 1";
  52. }
  53. else if((visitedp1[3]==1)&& (visitedp1[4]==1) &&(visitedp1[5]==1))
  54. {
  55. flag=1;
  56. winner="Player 1";
  57. }
  58. else if((visitedp1[6]==1)&& (visitedp1[7]==1) &&(visitedp1[8]==1))
  59. {
  60. flag=1;
  61. winner="Player 1";
  62. }
  63. //player2
  64. if((visitedp2[0]==1)&& (visitedp2[4]==1) &&(visitedp2[8]==1))
  65. {
  66. flag=1;
  67. winner="Player 2";
  68. }
  69. else if((visitedp2[2]==1)&& (visitedp2[4]==1) &&(visitedp2[6]==1))
  70. {
  71. flag=1;
  72. winner="Player 2";
  73. }
  74. else if((visitedp2[0]==1)&& (visitedp2[3]==1) &&(visitedp2[6]==1))
  75. {
  76. flag=1;
  77. winner="Player 2";
  78. }
  79. else if((visitedp2[1]==1)&& (visitedp2[4]==1) &&(visitedp2[7]==1))
  80. {
  81. flag=1;
  82. winner="Player 2";
  83. }
  84. else if((visitedp2[2]==1)&& (visitedp2[5]==1) &&(visitedp2[8]==1))
  85. {
  86. flag=1;
  87. winner="Player 2";
  88. }
  89. else if((visitedp2[0]==1)&& (visitedp2[1]==1) &&(visitedp2[2]==1))
  90. {
  91. flag=1;
  92. winner="Player 2";
  93. }
  94. else if((visitedp2[3]==1)&& (visitedp2[4]==1) &&(visitedp2[5]==1))
  95. {
  96. flag=1;
  97. winner="Player 2";
  98. }
  99. else if((visitedp2[6]==1)&& (visitedp2[7]==1) &&(visitedp2[8]==1))
  100. {
  101. flag=1;
  102. winner="Player 2";
  103. }
  104. if(flag==1)
  105. {
  106. Intent i=new Intent(context,Win.class);
  107. i.putExtra("winner",winner);
  108. startActivity(i);
  109. }
  110. if(i==8)
  111. {
  112. Intent gamedraw=new Intent(context,DrawGame.class);
  113. startActivity(gamedraw);
  114. }
  115. }
  116. TextView t;
  117. @Override
  118. protected void onCreate(Bundle savedInstanceState) {
  119. for(int k=0;k<9;++k)
  120. {
  121. visitedp1[k]=-1;
  122. visitedp2[k]=-1;
  123. }
  124. i=0;
  125. super.onCreate(savedInstanceState);
  126. setContentView(R.layout.game_layout);
  127. Intent intent = getIntent();
  128. p1=intent.getStringExtra("Player1");
  129. p2=intent.getStringExtra("Player2");
  130. if(p1.equalsIgnoreCase(p2))
  131. {
  132. Toast.makeText(context,"Both players cannot choose same symbol",2000).show();
  133. finish();
  134. }
  135. else
  136. {
  137. gridview=(GridView)findViewById(R.id.gridview1);
  138. ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.game_layout2,symbols);
  139. gridview.setAdapter(adapter);
  140. gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  141. @Override
  142. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  143. int flag=0;
  144. t=(TextView)view.findViewById(R.id.txt);
  145. for(j=0;j<9;++j)
  146. {
  147. if(visitedp1[position]==1 || visitedp2[position]==1)
  148. {
  149. flag=1;
  150. Toast.makeText(context,"Invalid....",200).show();
  151. break;
  152. }
  153. }
  154. if(flag==0)
  155. {
  156. if(i%2==0)
  157. {
  158. //Toast.makeText(context,"Player1.....",50).show();
  159. t.setText(p1);
  160. visitedp1[position]=1;
  161. }
  162. else
  163. {
  164. //Toast.makeText(context,"Player2.....",50).show();
  165. t.setText(p2);
  166. visitedp2[position]=1;
  167. }
  168. check(visitedp1,visitedp2);
  169. i=i+1;
  170. }
  171. }
  172. });
  173. }
  174. }
  175. }
The basic logic for checking if anyone has won the game or not is the function named "check". "visitedp1" and "visitedp2" are the arrays that store the position where player1 and player2 have marked, respectively.
Step 13
Create a new activity and name it as "Win". Add the following code to it:
  1. package com.tictactoefinal;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.ImageView;
  10. import android.widget.TextView;
  11. public class Win extends Activity {
  12. TextView winn;
  13. Button b;
  14. final Context context=this;
  15. ImageView im;
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.win_layout);
  20. Intent intent = getIntent();
  21. String win=intent.getStringExtra("winner");
  22. winn=(TextView)findViewById(R.id.wintxt);
  23. b=(Button)findViewById(R.id.back);
  24. im=(ImageView)findViewById(R.id.trophy);
  25. im.setImageResource(R.drawable.trophy);
  26. winn.setText(win);
  27. b.setOnClickListener(new View.OnClickListener() {
  28. @Override
  29. public void onClick(View v) {
  30. Intent i=new Intent(context,MainActivity.class);
  31. startActivity(i);
  32. }
  33. });
  34. }
  35. }
Step 14
Create a new activity and name it as "DrawGame". Add the following code to it:
  1. package com.tictactoefinal;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. public class DrawGame extends Activity {
  9. Button back;
  10. final Context context=this;
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.draw_layout);
  15. back=(Button)findViewById(R.id.drawBack);
  16. back.setOnClickListener(new View.OnClickListener() {
  17. @Override
  18. public void onClick(View v) {
  19. Intent i=new Intent(context,MainActivity.class);
  20. startActivity(i);
  21. }
  22. });
  23. }
  24. }
Step 15
In the end, don't forget to add the name of the new activities in the manifest file ie "AndroidManifest"; see:
  1. <activity android:name=".Game"
  2. android:label="Game"/>
  3. <activity android:name=".Win"
  4. android:label="win"/>
  5. <activity android:name=".DrawGame"
  6. android:label="Game Draw"/>
The following are the output screenshots.
The first screen will look like:
im7.jpg
Clicking on "Click Here" will give you:
im8.jpg
Clicking on "Play" will give you:
im9.jpg
Let us play now.
im10.jpg
A winning screen:
im11.jpg
The screen when the game will draw:
im12.jpg
Thank you... Enjoy playing :)