Two Player Tic Tac Toe Game in Android Studio

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.    
  12.   <TextView  
  13.       android:layout_width="wrap_content"  
  14.       android:layout_height="wrap_content"  
  15.       android:text="@string/hello_world"  
  16.       android:layout_marginLeft="50dp"  
  17.       android:paddingTop="20dp"  
  18.       android:textSize="@dimen/heading"  
  19.         />  
  20.   <Button  
  21.       android:layout_width="fill_parent"  
  22.       android:layout_height="wrap_content"  
  23.       android:text="Clich Here"  
  24.       android:layout_marginTop="400dp"  
  25.       android:layout_marginLeft="-100dp"  
  26.       android:background="@drawable/button_lay"  
  27.       android:id="@+id/clickHere"/>  
  28.    
  29. </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.    
  10.     <TextView  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_width="wrap_content"  
  13.         android:text="Player 1 "  
  14.         android:textSize="@dimen/heading"  
  15.         android:layout_marginTop="50dp"  
  16.         android:layout_marginLeft="20dp"  
  17.         android:textColor="@color/player"  
  18.         android:id="@+id/t1"/>  
  19.    
  20.     <RadioGroup  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_width="fill_parent"  
  23.         android:id="@+id/player1"  
  24.         android:layout_marginTop="50dp"  
  25.         android:layout_marginLeft="240dp">  
  26.    
  27.       <RadioButton  
  28.           android:layout_height="wrap_content"  
  29.           android:layout_width="wrap_content"  
  30.           android:text="O"  
  31.           android:textSize="@dimen/heading"  
  32.           android:checked="true"  
  33.           android:id="@+id/zero"/>  
  34.    
  35.       <RadioButton  
  36.               android:layout_height="wrap_content"  
  37.               android:layout_width="wrap_content"  
  38.               android:text="X"  
  39.               android:textSize="@dimen/heading"  
  40.               android:id="@+id/cross"/>  
  41.     </RadioGroup>  
  42.    
  43.   </RelativeLayout>  
  44.    
  45.   <RelativeLayout  
  46.           android:layout_height="wrap_content"  
  47.           android:layout_width="fill_parent">  
  48.    
  49.     <TextView  
  50.             android:layout_height="wrap_content"  
  51.             android:layout_width="wrap_content"  
  52.             android:text="Player 2 "  
  53.             android:textSize="@dimen/heading"  
  54.             android:layout_marginTop="50dp"  
  55.             android:layout_marginLeft="20dp"  
  56.             android:textColor="@color/player"  
  57.             android:id="@+id/t2"/>  
  58.    
  59.     <RadioGroup  
  60.             android:layout_height="wrap_content"  
  61.             android:layout_width="fill_parent"  
  62.             android:id="@+id/player2"  
  63.             android:layout_marginTop="50dp"  
  64.             android:layout_marginLeft="240dp">  
  65.    
  66.       <RadioButton  
  67.               android:layout_height="wrap_content"  
  68.               android:layout_width="wrap_content"  
  69.               android:text="O"  
  70.               android:textSize="@dimen/heading"  
  71.               android:id="@+id/zero"/>  
  72.    
  73.       <RadioButton  
  74.               android:layout_height="wrap_content"  
  75.               android:layout_width="wrap_content"  
  76.               android:text="X"  
  77.               android:checked="true"  
  78.               android:textSize="@dimen/heading"  
  79.               android:id="@+id/cross"/>  
  80.     </RadioGroup>  
  81.   </RelativeLayout>  
  82.    
  83.   <Button  
  84.       android:id="@+id/play"  
  85.       android:layout_height="wrap_content"  
  86.       android:layout_width="wrap_content"  
  87.       android:text="Play"  
  88.       android:layout_marginTop="80dp"  
  89.       android:layout_marginLeft="150dp"  
  90.       android:paddingLeft="10dp"  
  91.       android:paddingRight="10dp"  
  92.       android:background="@drawable/button_lay"/>  
  93.    
  94. </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.    
  49. </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.    
  14.   <!-- pressed -->  
  15.   <item android:state_pressed="true" android:state_enabled="true" >  
  16.     <shape android:shape="rectangle">  
  17.       <gradient  
  18.               android:startColor="#64334C"  
  19.               android:endColor="#300019"  
  20.               android:angle="-90"  
  21.               android:type="linear"  
  22.                     />  
  23.       <corners android:radius="5dp" />  
  24.     </shape>  
  25.   </item>  
  26.    
  27.   <!-- focused -->  
  28.   <item android:state_focused="true">  
  29.     <shape android:shape="rectangle">  
  30.       <gradient  
  31.               android:startColor="#C76699"  
  32.               android:endColor="#d9c292"  
  33.               android:angle="-90"  
  34.               android:type="linear"  
  35.                     />  
  36.       <corners android:radius="5dp" />  
  37.       <stroke android:width="2dp" android:color="#dddddd"/>  
  38.     </shape>  
  39.   </item>  
  40.    
  41.   <!-- default -->  
  42.   <item>  
  43.     <shape android:shape="rectangle">  
  44.       <gradient  
  45.               android:startColor="#C76699"  
  46.               android:endColor="#d9c292"  
  47.               android:angle="-90"  
  48.               android:type="linear"  
  49.                     />  
  50.       <corners android:radius="5dp" />  
  51.     </shape>  
  52.   </item>  
  53.    
  54. </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.  }  
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.    
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.*;  
  9.    
  10. public class Game extends Activity {  
  11.    
  12.     String p1;  
  13.     String p2;  
  14.     static int i=0;  
  15.     int j;  
  16.     GridView gridview;  
  17.     static final String[] symbols=new String[]{"","","","","","","","",""};  
  18.     int visitedp1[]={-1,-1,-1,-1,-1,-1,-1,-1,-1};  
  19.     int visitedp2[]={-1,-1,-1,-1,-1,-1,-1,-1,-1};  
  20.    
  21.     final Context context=this;  
  22.     public void check(int visitedp1[], int visitedp2[])  
  23.     {  
  24.         int flag=0;  
  25.         String winner=null;  
  26.    
  27.         //player1  
  28.         if((visitedp1[0]==1)&& (visitedp1[4]==1) &&(visitedp1[8]==1))  
  29.         {  
  30.             flag=1;  
  31.             winner="Player 1";  
  32.         }  
  33.         else if((visitedp1[2]==1)&& (visitedp1[4]==1) &&(visitedp1[6]==1))  
  34.         {  
  35.             flag=1;  
  36.             winner="Player 1";  
  37.         }  
  38.         else if((visitedp1[0]==1)&& (visitedp1[3]==1) &&(visitedp1[6]==1))  
  39.         {  
  40.             flag=1;  
  41.             winner="Player 1";  
  42.         }  
  43.         else if((visitedp1[1]==1)&& (visitedp1[4]==1) &&(visitedp1[7]==1))  
  44.         {  
  45.             flag=1;  
  46.             winner="Player 1";  
  47.         }  
  48.         else if((visitedp1[2]==1)&& (visitedp1[5]==1) &&(visitedp1[8]==1))  
  49.         {  
  50.             flag=1;  
  51.             winner="Player 1";  
  52.         }  
  53.         else if((visitedp1[0]==1)&& (visitedp1[1]==1) &&(visitedp1[2]==1))  
  54.         {  
  55.             flag=1;  
  56.             winner="Player 1";  
  57.         }  
  58.         else if((visitedp1[3]==1)&& (visitedp1[4]==1) &&(visitedp1[5]==1))  
  59.         {  
  60.             flag=1;  
  61.             winner="Player 1";  
  62.         }  
  63.         else if((visitedp1[6]==1)&& (visitedp1[7]==1) &&(visitedp1[8]==1))  
  64.         {  
  65.             flag=1;  
  66.             winner="Player 1";  
  67.         }  
  68.    
  69.         //player2  
  70.         if((visitedp2[0]==1)&& (visitedp2[4]==1) &&(visitedp2[8]==1))  
  71.         {  
  72.             flag=1;  
  73.             winner="Player 2";  
  74.         }  
  75.         else if((visitedp2[2]==1)&& (visitedp2[4]==1) &&(visitedp2[6]==1))  
  76.         {  
  77.             flag=1;  
  78.             winner="Player 2";  
  79.         }  
  80.         else if((visitedp2[0]==1)&& (visitedp2[3]==1) &&(visitedp2[6]==1))  
  81.         {  
  82.             flag=1;  
  83.             winner="Player 2";  
  84.         }  
  85.         else if((visitedp2[1]==1)&& (visitedp2[4]==1) &&(visitedp2[7]==1))  
  86.         {  
  87.             flag=1;  
  88.             winner="Player 2";  
  89.         }  
  90.         else if((visitedp2[2]==1)&& (visitedp2[5]==1) &&(visitedp2[8]==1))  
  91.         {  
  92.             flag=1;  
  93.             winner="Player 2";  
  94.         }  
  95.         else if((visitedp2[0]==1)&& (visitedp2[1]==1) &&(visitedp2[2]==1))  
  96.         {  
  97.             flag=1;  
  98.             winner="Player 2";  
  99.         }  
  100.         else if((visitedp2[3]==1)&& (visitedp2[4]==1) &&(visitedp2[5]==1))  
  101.         {  
  102.             flag=1;  
  103.             winner="Player 2";  
  104.         }  
  105.         else if((visitedp2[6]==1)&& (visitedp2[7]==1) &&(visitedp2[8]==1))  
  106.         {  
  107.             flag=1;  
  108.             winner="Player 2";  
  109.         }  
  110.    
  111.         if(flag==1)  
  112.         {  
  113.             Intent i=new Intent(context,Win.class);  
  114.             i.putExtra("winner",winner);  
  115.             startActivity(i);  
  116.    
  117.         }  
  118.         if(i==8)  
  119.         {  
  120.             Intent gamedraw=new Intent(context,DrawGame.class);  
  121.             startActivity(gamedraw);  
  122.         }  
  123.     }  
  124.     TextView t;  
  125.     @Override  
  126.     protected void onCreate(Bundle savedInstanceState) {  
  127.    
  128.         for(int k=0;k<9;++k)  
  129.         {  
  130.             visitedp1[k]=-1;  
  131.             visitedp2[k]=-1;  
  132.         }  
  133.         i=0;  
  134.         super.onCreate(savedInstanceState);  
  135.         setContentView(R.layout.game_layout);  
  136.    
  137.         Intent intent = getIntent();  
  138.         p1=intent.getStringExtra("Player1");  
  139.         p2=intent.getStringExtra("Player2");  
  140.    
  141.         if(p1.equalsIgnoreCase(p2))  
  142.         {  
  143.             Toast.makeText(context,"Both players cannot choose same symbol",2000).show();  
  144.             finish();  
  145.         }  
  146.         else  
  147.         {  
  148.    
  149.             gridview=(GridView)findViewById(R.id.gridview1);  
  150.    
  151.             ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.game_layout2,symbols);  
  152.             gridview.setAdapter(adapter);  
  153.    
  154.             gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
  155.                 @Override  
  156.                 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {  
  157.                     int flag=0;  
  158.                     t=(TextView)view.findViewById(R.id.txt);  
  159.                     for(j=0;j<9;++j)  
  160.                     {  
  161.                         if(visitedp1[position]==1 || visitedp2[position]==1)  
  162.                         {  
  163.                             flag=1;  
  164.                             Toast.makeText(context,"Invalid....",200).show();  
  165.                             break;  
  166.                         }  
  167.                     }  
  168.                     if(flag==0)  
  169.                     {  
  170.    
  171.                        if(i%2==0)  
  172.                       {  
  173.                         //Toast.makeText(context,"Player1.....",50).show();  
  174.                           t.setText(p1);  
  175.                           visitedp1[position]=1;  
  176.    
  177.    
  178.                       }  
  179.                       else  
  180.                       {  
  181.                         //Toast.makeText(context,"Player2.....",50).show();  
  182.                         t.setText(p2);  
  183.                           visitedp2[position]=1;  
  184.    
  185.                       }  
  186.                       check(visitedp1,visitedp2);  
  187.                       i=i+1;  
  188.    
  189.                     }  
  190.                 }  
  191.             });  
  192.    
  193.         }  
  194.     }  
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.    
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10. import android.widget.ImageView;  
  11. import android.widget.TextView;  
  12.     
  13. public class Win extends Activity {  
  14.     TextView winn;  
  15.     Button b;  
  16.     final Context context=this;  
  17.     ImageView im;  
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.win_layout);  
  22.         Intent intent = getIntent();  
  23.         String win=intent.getStringExtra("winner");  
  24.         winn=(TextView)findViewById(R.id.wintxt);  
  25.         b=(Button)findViewById(R.id.back);  
  26.         im=(ImageView)findViewById(R.id.trophy);  
  27.         im.setImageResource(R.drawable.trophy);  
  28.         winn.setText(win);  
  29.         b.setOnClickListener(new View.OnClickListener() {  
  30.             @Override  
  31.             public void onClick(View v) {  
  32.              Intent i=new Intent(context,MainActivity.class);  
  33.              startActivity(i);  
  34.             }  
  35.         });  
  36.     }  
Step 14
 
Create a new activity and name it as "DrawGame". Add the following code to it:
  1. package com.tictactoefinal;  
  2.    
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9.    
  10. public class DrawGame extends Activity {  
  11.     Button back;  
  12.     final Context context=this;  
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.draw_layout);  
  17.         back=(Button)findViewById(R.id.drawBack);  
  18.         back.setOnClickListener(new View.OnClickListener() {  
  19.             @Override  
  20.             public void onClick(View v) {  
  21.                 Intent i=new Intent(context,MainActivity.class);  
  22.                 startActivity(i);  
  23.             }  
  24.         });  
  25.     }  
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 :)


Similar Articles