Friday, February 17, 2012

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


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

No comments:

Post a Comment