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:
![]() |
![]() |
![]() |
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:
20: <Button
21: android:id="@+id/one"
22: android:layout_width="100dp"
23: android:layout_height="100dp"
24: android:textSize="70dp" />
25:
26:
27: <Button
28: android:id="@+id/two"
29: android:layout_width="100dp"
30: android:layout_height="100dp"
31: android:textSize="70dp" />
32:
33:
34: <Button
35: android:id="@+id/three"
36: android:layout_width="100dp"
37: android:layout_height="100dp"
38: android:textSize="70dp" />
39:
40: </TableRow>
41:
42: <TableRow
43: android:id="@+id/tableRow2"
44: android:layout_width="wrap_content"
45: android:layout_height="wrap_content"
46: android:gravity="center_horizontal" >
47:
48:
49: <Button
50: android:id="@+id/four"
51: android:layout_width="100dp"
52: android:layout_height="100dp"
53: android:textSize="70dp" />
54:
55:
56: <Button
57: android:id="@+id/five"
58: android:layout_width="100dp"
59: android:layout_height="100dp"
60: android:textSize="70dp" />
61:
62:
63: <Button
64: android:id="@+id/six"
65: android:layout_width="100dp"
66: android:layout_height="100dp"
67: android:textSize="70dp" />
68:
69: </TableRow>
70:
71: <TableRow
72: android:id="@+id/tableRow3"
73: android:layout_width="wrap_content"
74: android:layout_height="wrap_content"
75: android:gravity="center_horizontal" >
76:
77:
78: <Button
79: android:id="@+id/seven"
80: android:layout_width="100dp"
81: android:layout_height="100dp"
82: android:textSize="70dp" />
83:
84:
85: <Button
86: android:id="@+id/eight"
87: android:layout_width="100dp"
88: android:layout_height="100dp"
89: android:textSize="70dp" />
90:
91:
92: <Button
93: android:id="@+id/nine"
94: android:layout_width="100dp"
95: android:layout_height="100dp"
96: android:textSize="70dp" />
97:
98: </TableRow>
99: </TableLayout>
100:
101: <TextView
102: android:id="@+id/information"
103: android:layout_width="match_parent"
104: android:layout_height="wrap_content"
105: android:layout_marginTop="5dp"
106: android:gravity="center_horizontal"
107: android:text="@string/info"
108: android:textSize="25dp" />
109:
110: <TableLayout
111: android:id="@+id/tableLayout1"
112: android:layout_width="match_parent"
113: android:layout_height="wrap_content" >
114:
115: <TableRow
116: android:id="@+id/tableRow4"
117: android:layout_width="wrap_content"
118: android:layout_height="wrap_content"
119: android:layout_marginTop="10dp"
120: android:gravity="center_horizontal" >
121:
122: <TextView
123: android:id="@+id/human"
124: android:layout_width="wrap_content"
125: android:layout_height="wrap_content"
126: android:text="@string/human" />
127:
128: <TextView
129: android:id="@+id/humanCount"
130: android:layout_width="wrap_content"
131: android:layout_height="wrap_content"
132: android:layout_marginRight="10dp"/>
133:
134: <TextView
135: android:id="@+id/ties"
136: android:layout_width="wrap_content"
137: android:layout_height="wrap_content"
138: android:text="@string/ties" />
139:
140: <TextView
141: android:id="@+id/tiesCount"
142: android:layout_width="wrap_content"
143: android:layout_height="wrap_content"
144: android:layout_marginRight="10dp" />
145:
146: <TextView
147: android:id="@+id/android"
148: android:layout_width="wrap_content"
149: android:layout_height="wrap_content"
150: android:text="@string/android" />
151:
152: <TextView
153: android:id="@+id/androidCount"
154: android:layout_width="wrap_content"
155: android:layout_height="wrap_content" />
156:
157: </TableRow>
158: </TableLayout>
159: </LinearLayout>
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: mBoardButtons[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.blank));
99: }
100:
101: if (mHumanFirst)
102: {
103: mInfoTextView.setText(R.string.first_human);
104: mHumanFirst = false;
105: }
106: else
107: {
108: mInfoTextView.setText(R.string.turn_computer);
109: int move = mGame.getComputerMove();
110: setMove(mGame.ANDROID_PLAYER, move);
111: mHumanFirst = true;
112: }
113:
114: mGameOver = false;
115: }
116:
117: private class ButtonClickListener implements View.OnClickListener
118: {
119: int location;
120:
121: public ButtonClickListener(int location)
122: {
123: this.location = location;
124: }
125:
126: public void onClick(View view)
127: {
128: if (!mGameOver)
129: {
130: if (mBoardButtons[location].isEnabled())
131: {
132: setMove(mGame.HUMAN_PLAYER, location);
133:
134: int winner = mGame.checkForWinner();
135:
136: if (winner == 0)
137: {
138: mInfoTextView.setText(R.string.turn_computer);
139: int move = mGame.getComputerMove();
140: setMove(mGame.ANDROID_PLAYER, move);
141: winner = mGame.checkForWinner();
142: }
143:
144: if (winner == 0)
145: mInfoTextView.setText(R.string.turn_human);
146: else if (winner == 1)
147: {
148: mInfoTextView.setText(R.string.result_tie);
149: mTieCounter++;
150: mTieCount.setText(Integer.toString(mTieCounter));
151: mGameOver = true;
152: }
153: else if (winner == 2)
154: {
155: mInfoTextView.setText(R.string.result_human_wins);
156: mHumanCounter++;
157: mHumanCount.setText(Integer.toString(mHumanCounter));
158: mGameOver = true;
159: }
160: else
161: {
162: mInfoTextView.setText(R.string.result_android_wins);
163: mAndroidCounter++;
164: mAndroidCount.setText(Integer.toString(mAndroidCounter));
165: mGameOver = true;
166: }
167: }
168: }
169: }
170: }
171:
172: private void setMove(char player, int location)
173: {
174: mGame.setMove(player, location);
175: mBoardButtons[location].setEnabled(false);
176: //mBoardButtons[location].setText(String.valueOf(player));
177: if (player == mGame.HUMAN_PLAYER)
178: {
179: //mBoardButtons[location].setTextColor(Color.GREEN);
180: mBoardButtons[location].setBackgroundDrawable(getResources().getDrawable(R.drawable.x));
181: }
182: else
183: {
184: //mBoardButtons[location].setTextColor(Color.RED);
185: mBoardButtons[location].setBackgroundDrawable(getResources().getDrawable(R.drawable.o));
186: }
187: }
188: }
Check us out on Google+ (Indie Development).
Check us out on our .co site (Indie Development).
No comments:
Post a Comment