Wednesday, April 18, 2012

Android Tutorial : Tic Tac Toe 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).

2 comments:

  1. thanks for tut and source code :)

    ReplyDelete
  2. Hello thank you for tutorial i followed your tutorial till part 5 every thing goes perfect but there are some bug in this game that you wrote
    for example when nobody wins it say android winner sometimes
    can you tell me how can i rid of this awful bug??

    ReplyDelete