Android Development : Tic-Tac-Toe Series

Android Development : TicTacToe Part 1


This is the first part of our tutorial series on making a tic tac toe app for android.  In this part we code the java file that will be used "behind the scenes" to generate the computer's moves and check to see if there is a winner, if the game is a tie, or if there are spaces available to play in.





Files to go with this tutorial:

TicTacToeTutorialActivity.java

1:  package va.indiedevelopment.tictactoe;  
2:    
3:  import java.util.Random;  
4:    
5:  public class TicTacToeGame {  
6:         
7:       private char mBoard[];  
8:       private final static int BOARD_SIZE = 9;  
9:         
10:       public static final char HUMAN_PLAYER = 'X';  
11:       public static final char ANDROID_PLAYER = '0';  
12:       public static final char EMPTY_SPACE = ' ';  
13:         
14:       private Random mRand;  
15:         
16:       public static int getBOARD_SIZE() {  
17:            return BOARD_SIZE;  
18:       }  
19:         
20:       public TicTacToeGame(){  
21:              
22:            mBoard = new char[BOARD_SIZE];  
23:              
24:            for (int i = 0; i < BOARD_SIZE; i++)  
25:                 mBoard[i] = EMPTY_SPACE;  
26:              
27:            mRand = new Random();  
28:       }  
29:         
30:       public void clearBoard()  
31:       {  
32:            for (int i = 0; i < BOARD_SIZE; i++)  
33:            {  
34:                 mBoard[i] = EMPTY_SPACE;  
35:            }  
36:       }  
37:         
38:       public void setMove(char player, int location)  
39:       {  
40:            mBoard[location] = player;  
41:       }  
42:         
43:       public int getComputerMove()  
44:       {  
45:            int move;  
46:              
47:            for (int i = 0; i < getBOARD_SIZE(); i++)  
48:            {  
49:                 if (mBoard[i] != HUMAN_PLAYER && mBoard[i] != ANDROID_PLAYER)  
50:                 {  
51:                      char curr = mBoard[i];  
52:                      mBoard[i] = ANDROID_PLAYER;  
53:                      if (checkForWinner() == 3)  
54:                      {  
55:                           setMove(ANDROID_PLAYER, i);  
56:                           return i;  
57:                      }  
58:                      else  
59:                           mBoard[i] = curr;  
60:                 }  
61:            }  
62:              
63:            for (int i = 0; i < getBOARD_SIZE(); i++)  
64:            {  
65:                 if (mBoard[i] != HUMAN_PLAYER && mBoard[i] != ANDROID_PLAYER)  
66:                 {  
67:                      char curr = mBoard[i];  
68:                      mBoard[i] = HUMAN_PLAYER;  
69:                      if (checkForWinner() == 2)  
70:                      {  
71:                           setMove(ANDROID_PLAYER, i);  
72:                           return i;  
73:                      }  
74:                      else  
75:                           mBoard[i] = curr;  
76:                 }  
77:            }  
78:              
79:            do  
80:            {  
81:                 move = mRand.nextInt(getBOARD_SIZE());  
82:            } while (mBoard[move] == HUMAN_PLAYER || mBoard[move] == ANDROID_PLAYER);  
83:                   
84:                 setMove(ANDROID_PLAYER, move);  
85:            return move;  
86:       }  
87:         
88:       public int checkForWinner()  
89:       {  
90:            for (int i = 0; i <= 6; i += 3)  
91:            {  
92:                 if (mBoard[i] == HUMAN_PLAYER &&  
93:                   mBoard[i+1] == HUMAN_PLAYER &&  
94:                   mBoard[i+2] == HUMAN_PLAYER)  
95:                      return 2;  
96:                 if (mBoard[i] == ANDROID_PLAYER &&  
97:                      mBoard[i+1] == ANDROID_PLAYER &&  
98:                      mBoard[i+2] == ANDROID_PLAYER)  
99:                      return 3;  
100:            }  
101:              
102:            for (int i = 0; i <= 2; i++)  
103:            {  
104:                 if (mBoard[i] == HUMAN_PLAYER &&  
105:                      mBoard[i+3] == HUMAN_PLAYER &&  
106:                      mBoard[i+6] == HUMAN_PLAYER)  
107:                      return 2;  
108:                 if (mBoard[i] == ANDROID_PLAYER &&  
109:                      mBoard[i+3] == ANDROID_PLAYER &&  
110:                      mBoard[i+6] == ANDROID_PLAYER)  
111:                      return 3;  
112:            }  
113:              
114:            if ((mBoard[0] == HUMAN_PLAYER &&  
115:                  mBoard[4] == HUMAN_PLAYER &&  
116:                  mBoard[8] == HUMAN_PLAYER) ||  
117:                  mBoard[2] == HUMAN_PLAYER &&  
118:                  mBoard[4] == HUMAN_PLAYER &&  
119:                  mBoard[6] == HUMAN_PLAYER)  
120:                  return 2;  
121:            if ((mBoard[0] == ANDROID_PLAYER &&  
122:                  mBoard[4] == ANDROID_PLAYER &&  
123:                  mBoard[8] == ANDROID_PLAYER) ||  
124:                  mBoard[2] == ANDROID_PLAYER &&  
125:                  mBoard[4] == ANDROID_PLAYER &&  
126:                  mBoard[6] == ANDROID_PLAYER)  
127:                  return 3;  
128:              
129:            for (int i = 0; i < getBOARD_SIZE(); i++)  
130:            {  
131:                 if (mBoard[i] != HUMAN_PLAYER && mBoard[i] != ANDROID_PLAYER)  
132:                      return 0;  
133:            }  
134:              
135:            return 1;  
136:       }  
137:  }  

Android Development : TicTacToe Part 2


In this part of the tutorial we look at how to setup the strings that we will be using in the app.  In the next part of the series we will begin work on our layout file for our main activity.




Let me know what you think.  As always if you notice an error or something that could be done differently to improve this tutorial let me know.



Files to go with this tutorial:
strings.xml

1:  <?xml version="1.0" encoding="utf-8"?>  
2:  <resources>  
3:    <string name="app_name">TicTacToeTutorial</string>  
4:    <string name="first_human">You go first.</string>  
5:    <string name="turn_human">Your turn.</string>  
6:    <string name="turn_computer">Android\'s turn.</string>  
7:    <string name="result_tie">It\'s a tie!</string>  
8:    <string name="result_human_wins">You Won!</string>  
9:    <string name="result_android_wins">Android Won!</string>  
10:    <string name="one">1</string>  
11:    <string name="two">2</string>  
12:    <string name="three">3</string>  
13:    <string name="four">4</string>  
14:    <string name="five">5</string>  
15:    <string name="six">6</string>  
16:    <string name="seven">7</string>  
17:    <string name="eight">8</string>  
18:    <string name="nine">9</string>  
19:    <string name="info">Info</string>  
20:    <string name="human">Human: </string>  
21:    <string name="ties">Ties: </string>  
22:    <string name="android">Android: </string>  
23:    <string name="contact_heading">Contact Info</string>  
24:    <string name="contact_info">  
25:      <b>Name:</b> Indie Development\n  
26:      <b>Email:</b> my email here\n  
27:      <b>Phone:</b> my phone here\n  
28:      <b>Website:</b> website here  
29:    </string>  
30:    <string name="about_heading">About</string>  
31:    <string name="about_info">This app allows you to play a game  
32:      of tic tac toe against the computer. The rounds  
33:      will rotate who gets to play first. Also, the app  
34:      will keep track and display the result of the games  
35:      at the bottom of the screen.  
36:    </string>  
37:  </resources>  

Android Development : TicTacToe Part 3


In this part of the tutorial we look at how to setup the layout file for our main activity. Next in the series we will look at how to tie together our layout with the previous java code we wrote for the "behind the scenes" work by coding our main activity.




Let me know what you think.  As always if you notice an error or something that could be done differently to improve this tutorial let me know.




Files to go with this tutorial:
main.xml

1:  <?xml version="1.0" encoding="utf-8"?>  
2:  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
3:    android:layout_width="fill_parent"  
4:    android:layout_height="fill_parent"  
5:    android:orientation="vertical" >  
6:    
7:    <TableLayout  
8:      android:id="@+id/playArea"  
9:      android:layout_width="match_parent"  
10:      android:layout_height="wrap_content"  
11:      android:layout_marginTop="10dp" >  
12:    
13:      <TableRow  
14:        android:id="@+id/tableRow1"  
15:        android:layout_width="wrap_content"  
16:        android:layout_height="wrap_content"  
17:        android:gravity="center_horizontal" >  
18:    
19:        <Button  
20:          android:id="@+id/one"  
21:          android:layout_width="100dp"  
22:          android:layout_height="100dp"  
23:          android:text="@string/one"  
24:          android:textSize="70dp" />  
25:    
26:        <Button  
27:          android:id="@+id/two"  
28:          android:layout_width="100dp"  
29:          android:layout_height="100dp"  
30:          android:text="@string/two"  
31:          android:textSize="70dp" />  
32:    
33:        <Button  
34:          android:id="@+id/three"  
35:          android:layout_width="100dp"  
36:          android:layout_height="100dp"  
37:          android:text="@string/three"  
38:          android:textSize="70dp" />  
39:      </TableRow>  
40:    
41:      <TableRow  
42:        android:id="@+id/tableRow2"  
43:        android:layout_width="wrap_content"  
44:        android:layout_height="wrap_content"  
45:        android:gravity="center_horizontal" >  
46:    
47:        <Button  
48:          android:id="@+id/four"  
49:          android:layout_width="100dp"  
50:          android:layout_height="100dp"  
51:          android:text="@string/four"  
52:          android:textSize="70dp" />  
53:    
54:        <Button  
55:          android:id="@+id/five"  
56:          android:layout_width="100dp"  
57:          android:layout_height="100dp"  
58:          android:text="@string/five"  
59:          android:textSize="70dp" />  
60:    
61:        <Button  
62:          android:id="@+id/six"  
63:          android:layout_width="100dp"  
64:          android:layout_height="100dp"  
65:          android:text="@string/six"  
66:          android:textSize="70dp" />  
67:      </TableRow>  
68:    
69:      <TableRow  
70:        android:id="@+id/tableRow3"  
71:        android:layout_width="wrap_content"  
72:        android:layout_height="wrap_content"  
73:        android:gravity="center_horizontal" >  
74:    
75:        <Button  
76:          android:id="@+id/seven"  
77:          android:layout_width="100dp"  
78:          android:layout_height="100dp"  
79:          android:text="@string/seven"  
80:          android:textSize="70dp" />  
81:    
82:        <Button  
83:          android:id="@+id/eight"  
84:          android:layout_width="100dp"  
85:          android:layout_height="100dp"  
86:          android:text="@string/eight"  
87:          android:textSize="70dp" />  
88:    
89:        <Button  
90:          android:id="@+id/nine"  
91:          android:layout_width="100dp"  
92:          android:layout_height="100dp"  
93:          android:text="@string/nine"  
94:          android:textSize="70dp" />  
95:      </TableRow>  
96:    </TableLayout>  
97:    
98:    <TextView  
99:      android:id="@+id/information"  
100:      android:layout_width="match_parent"  
101:      android:layout_height="wrap_content"  
102:      android:layout_marginTop="5dp"  
103:      android:gravity="center_horizontal"  
104:      android:text="@string/info"  
105:      android:textSize="25dp" />  
106:    
107:    <TableLayout  
108:      android:id="@+id/tableLayout1"  
109:      android:layout_width="match_parent"  
110:      android:layout_height="wrap_content" >  
111:    
112:      <TableRow  
113:        android:id="@+id/tableRow4"  
114:        android:layout_width="wrap_content"  
115:        android:layout_height="wrap_content"  
116:        android:layout_marginTop="10dp"  
117:        android:gravity="center_horizontal" >  
118:    
119:        <TextView  
120:          android:id="@+id/human"  
121:          android:layout_width="wrap_content"  
122:          android:layout_height="wrap_content"  
123:          android:text="@string/human" />  
124:    
125:        <TextView  
126:          android:id="@+id/humanCount"  
127:          android:layout_width="wrap_content"  
128:          android:layout_height="wrap_content"  
129:          android:layout_marginRight="10dp" />  
130:    
131:        <TextView  
132:          android:id="@+id/ties"  
133:          android:layout_width="wrap_content"  
134:          android:layout_height="wrap_content"  
135:          android:text="@string/ties" />  
136:    
137:        <TextView  
138:          android:id="@+id/tiesCount"  
139:          android:layout_width="wrap_content"  
140:          android:layout_height="wrap_content"  
141:          android:layout_marginRight="10dp" />  
142:    
143:        <TextView  
144:          android:id="@+id/android"  
145:          android:layout_width="wrap_content"  
146:          android:layout_height="wrap_content"  
147:          android:text="@string/android" />  
148:    
149:        <TextView  
150:          android:id="@+id/androidCount"  
151:          android:layout_width="wrap_content"  
152:          android:layout_height="wrap_content" />  
153:    
154:      </TableRow>  
155:    </TableLayout>  
156:  </LinearLayout>  


Android Development : TicTacToe Part 4


Here is part 4 of the tutorial series on creating a tic tac toe app for the android. In this part we code our main activity. We are now able to play our tic tac toe game... but how can we go about starting a new game? or maybe add in some additional activities and be able to switch to them?

In the next part of the series we will look at how we can navigate and accomplish starting a new game. We will setup a menu that will allow us to start a new game, switch to another activity, and exit our app. We could just put a new game button and be done with the series, but I believe that learning how to setup a working menu will be beneficial.






Let me know what you think.  As always if you notice an error or something that could be done differently to improve this tutorial let me know.




Files to go with this tutorial:

TicTacToeTutorialActivity.java

1:  package va.indiedevelopment.tictactoe;  
2:    
3:  import android.app.Activity;  
4:  import android.graphics.Color;  
5:  import android.os.Bundle;  
6:  import android.view.View;  
7:  import android.widget.Button;  
8:  import android.widget.TextView;  
9:    
10:    
11:  public class TicTacToeTutorialActivity extends Activity {  
12:         
13:       private TicTacToeGame mGame;  
14:         
15:       private Button mBoardButtons[];  
16:         
17:       private TextView mInfoTextView;  
18:       private TextView mHumanCount;  
19:       private TextView mTieCount;  
20:       private TextView mAndroidCount;  
21:         
22:       private int mHumanCounter = 0;  
23:       private int mTieCounter = 0;  
24:       private int mAndroidCounter = 0;  
25:         
26:       private boolean mHumanFirst = true;  
27:       private boolean mGameOver = false;  
28:         
29:    /** Called when the activity is first created. */  
30:    @Override  
31:    public void onCreate(Bundle savedInstanceState) {  
32:      super.onCreate(savedInstanceState);  
33:      setContentView(R.layout.main);  
34:        
35:      mBoardButtons = new Button[mGame.getBOARD_SIZE()];  
36:      mBoardButtons[0] = (Button) findViewById(R.id.one);  
37:      mBoardButtons[1] = (Button) findViewById(R.id.two);  
38:      mBoardButtons[2] = (Button) findViewById(R.id.three);  
39:      mBoardButtons[3] = (Button) findViewById(R.id.four);  
40:      mBoardButtons[4] = (Button) findViewById(R.id.five);  
41:      mBoardButtons[5] = (Button) findViewById(R.id.six);  
42:      mBoardButtons[6] = (Button) findViewById(R.id.seven);  
43:      mBoardButtons[7] = (Button) findViewById(R.id.eight);  
44:      mBoardButtons[8] = (Button) findViewById(R.id.nine);  
45:        
46:      mInfoTextView = (TextView) findViewById(R.id.information);  
47:      mHumanCount = (TextView) findViewById(R.id.humanCount);  
48:      mTieCount = (TextView) findViewById(R.id.tiesCount);  
49:      mAndroidCount = (TextView) findViewById(R.id.androidCount);  
50:        
51:      mHumanCount.setText(Integer.toString(mHumanCounter));  
52:      mTieCount.setText(Integer.toString(mTieCounter));  
53:      mAndroidCount.setText(Integer.toString(mAndroidCounter));  
54:        
55:      mGame = new TicTacToeGame();  
56:        
57:      startNewGame();  
58:        
59:    }  
60:      
61:    private void startNewGame()  
62:    {  
63:         mGame.clearBoard();  
64:           
65:         for (int i = 0; i < mBoardButtons.length; i++)  
66:         {  
67:              mBoardButtons[i].setText("");  
68:              mBoardButtons[i].setEnabled(true);  
69:              mBoardButtons[i].setOnClickListener(new ButtonClickListener(i));  
70:         }  
71:           
72:         if (mHumanFirst)  
73:         {  
74:              mInfoTextView.setText(R.string.first_human);  
75:              mHumanFirst = false;  
76:         }  
77:         else  
78:         {  
79:              mInfoTextView.setText(R.string.turn_computer);  
80:              int move = mGame.getComputerMove();  
81:              setMove(mGame.ANDROID_PLAYER, move);  
82:              mHumanFirst = true;  
83:         }  
84:    }  
85:      
86:    private class ButtonClickListener implements View.OnClickListener  
87:    {  
88:         int location;  
89:           
90:         public ButtonClickListener(int location)  
91:         {  
92:              this.location = location;  
93:         }  
94:           
95:         public void onClick(View view)  
96:         {  
97:              if (!mGameOver)  
98:              {  
99:                   if (mBoardButtons[location].isEnabled())  
100:                   {  
101:                        setMove(mGame.HUMAN_PLAYER, location);  
102:                          
103:                        int winner = mGame.checkForWinner();  
104:                          
105:                        if (winner == 0)  
106:                        {  
107:                             mInfoTextView.setText(R.string.turn_computer);  
108:                             int move = mGame.getComputerMove();  
109:                             setMove(mGame.ANDROID_PLAYER, move);  
110:                             winner = mGame.checkForWinner();                             
111:                        }  
112:                          
113:                        if (winner == 0)  
114:                             mInfoTextView.setText(R.string.turn_human);  
115:                        else if (winner == 1)  
116:                        {  
117:                             mInfoTextView.setText(R.string.result_tie);  
118:                             mTieCounter++;  
119:                             mTieCount.setText(Integer.toString(mTieCounter));  
120:                             mGameOver = true;  
121:                        }  
122:                        else if (winner == 2)  
123:                        {  
124:                             mInfoTextView.setText(R.string.result_human_wins);  
125:                             mHumanCounter++;  
126:                             mHumanCount.setText(Integer.toString(mHumanCounter));  
127:                             mGameOver = true;  
128:                        }  
129:                        else  
130:                        {  
131:                             mInfoTextView.setText(R.string.result_android_wins);  
132:                             mAndroidCounter++;  
133:                             mAndroidCount.setText(Integer.toString(mAndroidCounter));  
134:                             mGameOver = true;  
135:                        }  
136:                   }  
137:              }  
138:         }  
139:    }  
140:      
141:    private void setMove(char player, int location)  
142:    {  
143:         mGame.setMove(player, location);  
144:         mBoardButtons[location].setEnabled(false);  
145:         mBoardButtons[location].setText(String.valueOf(player));  
146:         if (player == mGame.HUMAN_PLAYER)  
147:              mBoardButtons[location].setTextColor(Color.GREEN);  
148:         else  
149:              mBoardButtons[location].setTextColor(Color.RED);  
150:    }  
151:  }  


Android Development : TicTacToe Part 5


Here is part 5 of our tutorial series on developing our Tic Tac Toe Game (finally... sorry for the delay).  In this part we look at how to change the icon for our app and add in a menu that will allow us to start a new game and exit our app.  I hope everyone has enjoyed this series and was able to get something from what we have covered.







Let me know what you think.  As always if you notice an error or something that could be done differently to improve this tutorial let me know.



Files to go with this tutorial:

Tic Tac Toe Ico
AndroidManifest.xml

1:  <?xml version="1.0" encoding="utf-8"?>  
2:  <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
3:    package="va.indiedevelopment.tictactoe"  
4:    android:versionCode="1"  
5:    android:versionName="1.0" >  
6:    
7:    <uses-sdk android:minSdkVersion="10" />  
8:    
9:    <application  
10:      android:icon="@drawable/ic_tictactoe"  
11:      android:label="@string/app_name" >  
12:      <activity  
13:        android:name=".TicTacToeTutorialActivity"  
14:        android:label="@string/app_name" >  
15:        <intent-filter>  
16:          <action android:name="android.intent.action.MAIN" />  
17:    
18:          <category android:name="android.intent.category.LAUNCHER" />  
19:        </intent-filter>  
20:      </activity>  
21:    </application>  
22:    
23:  </manifest>  


strings.xml

1:  <?xml version="1.0" encoding="utf-8"?>  
2:  <resources>  
3:    <string name="app_name">TicTacToeTutorial</string>  
4:    <string name="first_human">You go first.</string>  
5:    <string name="turn_human">Your turn.</string>  
6:    <string name="turn_computer">Android\'s turn.</string>  
7:    <string name="result_tie">It\'s a tie!</string>  
8:    <string name="result_human_wins">You Won!</string>  
9:    <string name="result_android_wins">Android Won!</string>  
10:    <string name="one">1</string>  
11:    <string name="two">2</string>  
12:    <string name="three">3</string>  
13:    <string name="four">4</string>  
14:    <string name="five">5</string>  
15:    <string name="six">6</string>  
16:    <string name="seven">7</string>  
17:    <string name="eight">8</string>  
18:    <string name="nine">9</string>  
19:    <string name="info">Info</string>  
20:    <string name="human">Human: </string>  
21:    <string name="ties">Ties: </string>  
22:    <string name="android">Android: </string>  
23:    <string name="contact_heading">Contact Info</string>  
24:    <string name="contact_info">  
25:      <b>Name:</b> Indie Development\n  
26:      <b>Email:</b> my email here\n  
27:      <b>Phone:</b> my phone here\n  
28:      <b>Website:</b> website here  
29:    </string>  
30:    <string name="about_heading">About</string>  
31:    <string name="about_info">This app allows you to play a game  
32:      of tic tac toe against the computer. The rounds  
33:      will rotate who gets to play first. Also, the app  
34:      will keep track and display the result of the games  
35:      at the bottom of the screen.  
36:    </string>  
37:    <string name="newGame_label">New Game</string>  
38:    <string name="exitGame_label">Exit Game</string>  
39:  </resources>  


game_menu.xml

1:  <?xml version="1.0" encoding="utf-8"?>  
2:  <menu xmlns:android="http://schemas.android.com/apk/res/android" >  
3:    <item android:id="@+id/newGame" android:title="@string/newGame_label" android:icon="@android:drawable/ic_menu_add"></item>  
4:    <item android:id="@+id/exitGame" android:title="@string/exitGame_label" android:icon="@android:drawable/ic_menu_close_clear_cancel"></item>  
5:  </menu>  


TicTacToeTutorialActivity.java

1:  package va.indiedevelopment.tictactoe;  
2:    
3:  import android.app.Activity;  
4:  import android.graphics.Color;  
5:  import android.os.Bundle;  
6:  import android.view.Menu;  
7:  import android.view.MenuInflater;  
8:  import android.view.MenuItem;  
9:  import android.view.View;  
10:  import android.widget.Button;  
11:  import android.widget.TextView;  
12:    
13:    
14:  public class TicTacToeTutorialActivity extends Activity {  
15:         
16:       private TicTacToeGame mGame;  
17:         
18:       private Button mBoardButtons[];  
19:         
20:       private TextView mInfoTextView;  
21:       private TextView mHumanCount;  
22:       private TextView mTieCount;  
23:       private TextView mAndroidCount;  
24:         
25:       private int mHumanCounter = 0;  
26:       private int mTieCounter = 0;  
27:       private int mAndroidCounter = 0;  
28:         
29:       private boolean mHumanFirst = true;  
30:       private boolean mGameOver = false;  
31:         
32:    /** Called when the activity is first created. */  
33:    @Override  
34:    public void onCreate(Bundle savedInstanceState) {  
35:      super.onCreate(savedInstanceState);  
36:      setContentView(R.layout.main);  
37:        
38:      mBoardButtons = new Button[mGame.getBOARD_SIZE()];  
39:      mBoardButtons[0] = (Button) findViewById(R.id.one);  
40:      mBoardButtons[1] = (Button) findViewById(R.id.two);  
41:      mBoardButtons[2] = (Button) findViewById(R.id.three);  
42:      mBoardButtons[3] = (Button) findViewById(R.id.four);  
43:      mBoardButtons[4] = (Button) findViewById(R.id.five);  
44:      mBoardButtons[5] = (Button) findViewById(R.id.six);  
45:      mBoardButtons[6] = (Button) findViewById(R.id.seven);  
46:      mBoardButtons[7] = (Button) findViewById(R.id.eight);  
47:      mBoardButtons[8] = (Button) findViewById(R.id.nine);  
48:        
49:      mInfoTextView = (TextView) findViewById(R.id.information);  
50:      mHumanCount = (TextView) findViewById(R.id.humanCount);  
51:      mTieCount = (TextView) findViewById(R.id.tiesCount);  
52:      mAndroidCount = (TextView) findViewById(R.id.androidCount);  
53:        
54:      mHumanCount.setText(Integer.toString(mHumanCounter));  
55:      mTieCount.setText(Integer.toString(mTieCounter));  
56:      mAndroidCount.setText(Integer.toString(mAndroidCounter));  
57:        
58:      mGame = new TicTacToeGame();  
59:        
60:      startNewGame();  
61:        
62:    }  
63:      
64:    @Override  
65:    public boolean onCreateOptionsMenu(Menu menu)  
66:    {  
67:         MenuInflater inflater = getMenuInflater();  
68:         inflater.inflate(R.menu.game_menu, menu);  
69:           
70:         return true;  
71:    }  
72:      
73:    @Override  
74:    public boolean onOptionsItemSelected(MenuItem item)  
75:    {  
76:         switch(item.getItemId())  
77:         {  
78:         case R.id.newGame:  
79:              startNewGame();  
80:              break;  
81:         case R.id.exitGame:  
82:              TicTacToeTutorialActivity.this.finish();  
83:              break;  
84:         }  
85:           
86:         return true;  
87:    }  
88:      
89:    private void startNewGame()  
90:    {  
91:         mGame.clearBoard();  
92:           
93:         for (int i = 0; i < mBoardButtons.length; i++)  
94:         {  
95:              mBoardButtons[i].setText("");  
96:              mBoardButtons[i].setEnabled(true);  
97:              mBoardButtons[i].setOnClickListener(new ButtonClickListener(i));  
98:         }  
99:           
100:         if (mHumanFirst)  
101:         {  
102:              mInfoTextView.setText(R.string.first_human);  
103:              mHumanFirst = false;  
104:         }  
105:         else  
106:         {  
107:              mInfoTextView.setText(R.string.turn_computer);  
108:              int move = mGame.getComputerMove();  
109:              setMove(mGame.ANDROID_PLAYER, move);  
110:              mHumanFirst = true;  
111:         }  
112:           
113:         mGameOver = false;  
114:    }  
115:      
116:    private class ButtonClickListener implements View.OnClickListener  
117:    {  
118:         int location;  
119:           
120:         public ButtonClickListener(int location)  
121:         {  
122:              this.location = location;  
123:         }  
124:           
125:         public void onClick(View view)  
126:         {  
127:              if (!mGameOver)  
128:              {  
129:                   if (mBoardButtons[location].isEnabled())  
130:                   {  
131:                        setMove(mGame.HUMAN_PLAYER, location);  
132:                          
133:                        int winner = mGame.checkForWinner();  
134:                          
135:                        if (winner == 0)  
136:                        {  
137:                             mInfoTextView.setText(R.string.turn_computer);  
138:                             int move = mGame.getComputerMove();  
139:                             setMove(mGame.ANDROID_PLAYER, move);  
140:                             winner = mGame.checkForWinner();                             
141:                        }  
142:                          
143:                        if (winner == 0)  
144:                             mInfoTextView.setText(R.string.turn_human);  
145:                        else if (winner == 1)  
146:                        {  
147:                             mInfoTextView.setText(R.string.result_tie);  
148:                             mTieCounter++;  
149:                             mTieCount.setText(Integer.toString(mTieCounter));  
150:                             mGameOver = true;  
151:                        }  
152:                        else if (winner == 2)  
153:                        {  
154:                             mInfoTextView.setText(R.string.result_human_wins);  
155:                             mHumanCounter++;  
156:                             mHumanCount.setText(Integer.toString(mHumanCounter));  
157:                             mGameOver = true;  
158:                        }  
159:                        else  
160:                        {  
161:                             mInfoTextView.setText(R.string.result_android_wins);  
162:                             mAndroidCounter++;  
163:                             mAndroidCount.setText(Integer.toString(mAndroidCounter));  
164:                             mGameOver = true;  
165:                        }  
166:                   }  
167:              }  
168:         }  
169:    }  
170:      
171:    private void setMove(char player, int location)  
172:    {  
173:         mGame.setMove(player, location);  
174:         mBoardButtons[location].setEnabled(false);  
175:         mBoardButtons[location].setText(String.valueOf(player));  
176:         if (player == mGame.HUMAN_PLAYER)  
177:              mBoardButtons[location].setTextColor(Color.GREEN);  
178:         else  
179:              mBoardButtons[location].setTextColor(Color.RED);  
180:    }  
181:  }  


Check us out on Google+ (Indie Development).
Check us out on our .co site (Indie Development).

6 comments:

  1. Hi, have you got download links to your example files please? Thanks, Peter

    ReplyDelete
  2. hey nice tutorial!
    but button is not showing text on button Click
    Please Help.
    Thanks in advance

    ReplyDelete
  3. Your blog is good source for mobile app information. Its a good blog with lots of information. Keep the good work on. Hire Android Developers

    ReplyDelete
  4. please a correct code there are to many conflicted in these code there are to many numbers !
    how can I get a correct one ?

    ReplyDelete

  5. Good job, but if you could increase artificial intelligence

    ReplyDelete