Tic tac toe game is developed for android operating system. In present trend most of the games are mostly used in mobile devices which had increased scope of developing gaming applications for android platform, iphone.. etc. Android operating system is one of the widely used mobile operating system all over the world.

Here we provide tic tac toe android project source code for free download which can be used a sample project to lean android programming.

Computer science and information technology students can fine more java project reports and source code for free download.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
ublic class Tictactoelib extends VIEW {

public static final long FPS_MS = 1000/2;

public enum State {
UNKNOWN(-3),
WIN(-2),
EMPTY(0),
PLAYER1(1),
PLAYER2(2);

private INT mValue;

private State(INT VALUE) {
mValue = VALUE;
}

public INT getValue() {
RETURN mValue;
}

public static State fromInt(INT i) {
FOR (State s : VALUES()) {
IF (s.getValue() == i) {
RETURN s;
}
}
RETURN EMPTY;
}
}

private static final INT MARGIN = 4;
private static final INT MSG_BLINK = 1;

private final Handler mHandler = NEW Handler(NEW MyHandler());

private final Rect mSrcRect = NEW Rect();
private final Rect mDstRect = NEW Rect();

private INT mSxy;
private INT mOffetX;
private INT mOffetY;
private Paint mWinPaint;
private Paint mLinePaint;
private Paint mBmpPaint;
private Bitmap mBmpPlayer1;
private Bitmap mBmpPlayer2;
private Drawable mDrawableBg;

private ICellListener mCellListener;

/** Contains one of {@link State#EMPTY}, {@link State#PLAYER1} or {@link State#PLAYER2}. */
private final State[] mData = NEW State[9];

private INT mSelectedCell = -1;
private State mSelectedValue = State.EMPTY;
private State mCurrentPlayer = State.UNKNOWN;
private State mWinner = State.EMPTY;

private INT mWinCol = -1;
private INT mWinRow = -1;
private INT mWinDiag = -1;

private BOOLEAN mBlinkDisplayOff;
private final Rect mBlinkRect = NEW Rect();

 

public interface ICellListener {
abstract void onCellSelected();
}

public Tictactoelib(Context context, AttributeSet attrs) {
super(context, attrs);
requestFocus();

mDrawableBg = getResources().getDrawable(R.drawable.icon);
setBackgroundDrawable(mDrawableBg);

mBmpPlayer1 = getResBitmap(R.drawable.icon);
mBmpPlayer2 = getResBitmap(R.drawable.icon);

IF (mBmpPlayer1 != NULL) {
mSrcRect.SET(0, 0, mBmpPlayer1.getWidth() -1, mBmpPlayer1.getHeight() - 1);
}

mBmpPaint = NEW Paint(Paint.ANTI_ALIAS_FLAG);

mLinePaint = NEW Paint();
mLinePaint.setColor(0xFFFFFFFF);
mLinePaint.setStrokeWidth(5);
mLinePaint.setStyle(STYLE.STROKE);

mWinPaint = NEW Paint(Paint.ANTI_ALIAS_FLAG);
mWinPaint.setColor(0xFFFF0000);
mWinPaint.setStrokeWidth(10);
mWinPaint.setStyle(STYLE.STROKE);

FOR (INT i = 0; i < mData.LENGTH; i++) {
mData[i] = State.EMPTY;
}

IF (isInEditMode()) {
// IN edit mode (e.g. IN the Eclipse ADT graphical layout editor)
// we'll use some random data to display the state.
Random rnd = new Random();
for (int i = 0; i < mData.length; i++) {
mData[i] = State.fromInt(rnd.nextInt(3));
}
}
}

public State[] getData() {
return mData;
}

public void setCell(int cellIndex, State value) {
mData[cellIndex] = value;
invalidate();
}

public void setCellListener(ICellListener cellListener) {
mCellListener = cellListener;
}

public int getSelection() {
if (mSelectedValue == mCurrentPlayer) {
return mSelectedCell;
}

return -1;
}

public State getCurrentPlayer() {
return mCurrentPlayer;
}

public void setCurrentPlayer(State player) {
mCurrentPlayer = player;
mSelectedCell = -1;
}

public State getWinner() {
return mWinner;
}

public void setWinner(State winner) {
mWinner = winner;
}

/** Sets winning mark on specified column or row (0..2) or diagonal (0..1). */
public void setFinished(int col, int row, int diagonal) {
mWinCol = col;
mWinRow = row;
mWinDiag = diagonal;
}

//-----------------------------------------
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

int sxy = mSxy;
int s3 = sxy * 3;
int x7 = mOffetX;
int y7 = mOffetY;

for (int i = 0, k = sxy; i < 2; i++, k += sxy) {
canvas.drawLine(x7 , y7 + k, x7 + s3 - 1, y7 + k , mLinePaint);
canvas.drawLine(x7 + k, y7 , x7 + k , y7 + s3 - 1, mLinePaint);
}

for (int j = 0, k = 0, y = y7; j < 3; j++, y += sxy) {
for (int i = 0, x = x7; i < 3; i++, k++, x += sxy) {
mDstRect.offsetTo(MARGIN+x, MARGIN+y);

State v;
if (mSelectedCell == k) {
if (mBlinkDisplayOff) {
continue;
}
v = mSelectedValue;
} else {
v = mData[k];
}

switch(v) {
case PLAYER1:
if (mBmpPlayer1 != null) {
canvas.drawBitmap(mBmpPlayer1, mSrcRect, mDstRect, mBmpPaint);
}
break;
case PLAYER2:
if (mBmpPlayer2 != null) {
canvas.drawBitmap(mBmpPlayer2, mSrcRect, mDstRect, mBmpPaint);
}
break;
}
}
}

if (mWinRow >= 0) {
int y = y7 + mWinRow * sxy + sxy / 2;
canvas.drawLine(x7 + MARGIN, y, x7 + s3 - 1 - MARGIN, y, mWinPaint);

} else if (mWinCol >= 0) {
int x = x7 + mWinCol * sxy + sxy / 2;
canvas.drawLine(x, y7 + MARGIN, x, y7 + s3 - 1 - MARGIN, mWinPaint);

} else if (mWinDiag == 0) {
// diagonal 0 is from (0,0) to (2,2)

canvas.drawLine(x7 + MARGIN, y7 + MARGIN,
x7 + s3 - 1 - MARGIN, y7 + s3 - 1 - MARGIN, mWinPaint);

} else if (mWinDiag == 1) {
// diagonal 1 is from (0,2) to (2,0)

canvas.drawLine(x7 + MARGIN, y7 + s3 - 1 - MARGIN,
x7 + s3 - 1 - MARGIN, y7 + MARGIN, mWinPaint);
}
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Keep the view squared
int w = MeasureSpec.getSize(widthMeasureSpec);
int h = MeasureSpec.getSize(heightMeasureSpec);
int d = w == 0 ? h : h == 0 ? w : w < h ? w : h;
setMeasuredDimension(d, d);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);

int sx = (w - 2 * MARGIN) / 3;
int sy = (h - 2 * MARGIN) / 3;

int size = sx < sy ? sx : sy;

mSxy = size;
mOffetX = (w - 3 * size) / 2;
mOffetY = (h - 3 * size) / 2;

mDstRect.set(MARGIN, MARGIN, size - MARGIN, size - MARGIN);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();

if (action == MotionEvent.ACTION_DOWN) {
return true;

} else if (action == MotionEvent.ACTION_UP) {
int x = (int) event.getX();
int y = (int) event.getY();

int sxy = mSxy;
x = (x - MARGIN) / sxy;
y = (y - MARGIN) / sxy;

if (isEnabled() && x >= 0 && x < 3 && y >= 0 & y < 3) {
int cell = x + 3 * y;

State state = cell == mSelectedCell ? mSelectedValue : mData[cell];
state = state == State.EMPTY ? mCurrentPlayer : State.EMPTY;

stopBlink();

mSelectedCell = cell;
mSelectedValue = state;
mBlinkDisplayOff = false;
mBlinkRect.set(MARGIN + x * sxy, MARGIN + y * sxy,
MARGIN + (x + 1) * sxy, MARGIN + (y + 1) * sxy);

if (state != State.EMPTY) {
// Start the blinker
mHandler.sendEmptyMessageDelayed(MSG_BLINK, FPS_MS);
}

if (mCellListener != null) {
mCellListener.onCellSelected();
}
}

return true;
}

return false;
}

Download tic tac toe android source code.