You are on page 1of 12

The project describes how to use 2D graphics and draw TicTacToe board.

Underlying Algorithm:
Basic description of algorithm in step by step form:
1.) Create a Project TicTacToe
2.) Add some images into your drawable folder for drawing cross and ball image.
3.) Run the application.
Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e.
TicTacToe. Enter following information:
Project name: TicTacToe
Build Target: Android 2.3.3
Application name: TicTacToe
Package name: org.example.TicTacToe
Create Activity: TicTacToe

On Clicking Finish TicTacToe code structure is generated with the necessary Android Packages being imported
along with TicTacToe.java. TicTacToe class will look like following:
1
2 package org.example.TicTacToe;
3
4 import android.app.Activity;
5 import android.os.Bundle;
6
public class TicTacToe extends Activity {
7 private Game game1;
8
9 /** Called when the activity is first created. */
10 @Override
11 public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
12 game1 = new Game(this);
13 setContentView(game1);
14 }
15 }
16
1 package org.example.TicTacToe;
2
3 import android.content.res.Resources;
4 import android.graphics.Bitmap;
5 import android.graphics.BitmapFactory;
import android.graphics.Canvas;
6 import android.graphics.Paint;
7 import android.graphics.Rect;
8
9 public class Ball extends Cell {
10
11 public Ball(int x, int y) {
super(x, y);
12 }
13
14 public void draw(Canvas g, Resources res, int x, int y, int w, int h)
15 {
16 Bitmap im = BitmapFactory.decodeResource(res, R.drawable.bola);
17 g.drawBitmap(im, null, new Rect(x*w, y*h, (x*w)+w, (y*h)+h), new
Paint());
18 }
19
20 @Override
21 public boolean equals(Object obj) {
22 if (obj instanceof Ball) {
return true;
23 } else {
24 return false;
25 }
26 }
27
28 @Override
public String toString() {
29 return "O";
30 }
31 }
32
33
34
1 package org.example.TicTacToe;
2
import android.content.Context;
3 import android.graphics.Canvas;
4 import android.graphics.Paint;
5 import android.graphics.Paint.Style;
6 import android.os.Handler;
import android.os.Message;
7 import android.view.MotionEvent;
8 import android.view.View;
9 import android.widget.Toast;
10
11 public class Game extends View {
12
13 private Cell[][] singlesquare = null;
int x = 3;
14 int y = 3;
15 private int l;
16 private int a;
17 private boolean whatdrawn = false;
private int playerwin = 3;
18 private Paint caneta;
19
20 Handler handler = new Handler() {
21 // @Override
22 public void handleMessage(Message msg) {
switch (msg.what) {
23 case 0:
24 invalidate();
25 break;
26 case 1:
27 Toast.makeText(getContext(), "You Win!",
Toast.LENGTH_SHORT).show();
28 break;
29 case 2:
30 Toast.makeText(getContext(), "Computer Win!",
31 Toast.LENGTH_SHORT).show();
break;
32 case 3:
33 Toast.makeText(getContext(), "Loose!",
34 Toast.LENGTH_SHORT).show();
35 break;
36 default:
break;
37 }
38
39 super.handleMessage(msg);
40 }
41 };
42
43 public int getGameSize() {
return x;
44 }
45
46 public Game(Context context) {
47 super(context);
48
49 caneta = new Paint();
50
this.caneta.setARGB(255, 0, 0, 0);
51 this.caneta.setAntiAlias(true);
52 this.caneta.setStyle(Style.STROKE);
53 this.caneta.setStrokeWidth(5);
54
55 l = this.getWidth();
56 a = this.getHeight();
57
singlesquare = new Cell[x][y];
58
59 int xss = l / x;
60 int yss = a / y;
61
for (int z = 0; z < y; z++) {
62 for (int i = 0; i < x; i++) {
63 singlesquare[z][i] = new Empty(xss * i, z * yss);
}
64 }
65 }
66
67 @Override
68 protected void onDraw(Canvas canvas) {
for (int i = 0; i < singlesquare.length; i++) {
69 for (int j = 0; j < singlesquare[0].length; j++) {
70 singlesquare[i][j].draw(canvas, getResources(), j, i, (t
71 .getWidth() + 3)
72 / singlesquare.length, this.getHeight()
73 / singlesquare[0].length);
}
74 }
75
76 int xs = this.getWidth() / x;
77 int ys = this.getHeight() / y;
78 for (int i = 0; i <= x; i++) {
canvas.drawLine(xs * i, 0, xs * i, this.getHeight(), caneta)
79 }
80 for (int i = 0; i <= y; i++) {
81 canvas.drawLine(0, ys * i, this.getWidth(), ys * i, caneta);
82 }
83
84 super.onDraw(canvas);
}
85
86 @Override
87 public boolean onTouchEvent(MotionEvent event) {
88 int x_aux = (int) (event.getX() / (this.getWidth() / x));
89 int y_aux = (int) (event.getY() / (this.getHeight() / y));
90 drawimage(x_aux, y_aux);
return super.onTouchEvent(event);
91 }
92
93 public String getPiece(int player) {
94 switch (player) {
95 case 1:
return "x";
96 case -1:
97 return "o";
98 }
99 return null;
100 }
101
public void drawimage(int x_aux, int y_aux) {
102 Cell cel = null;
103 if (whatdrawn)
104 {
105 cel = new
Cross(singlesquare[x_aux][y_aux].x,singlesquare[x_aux][y_aux].y);
106 whatdrawn = false;
107 }
108 else
109 {
cel = new Ball(singlesquare[x_aux][y_aux].x,
110 singlesquare[x_aux][y_aux].y);
111 whatdrawn = true;
112 }
113
114 singlesquare[y_aux][x_aux] = cel;
115
handler.sendMessage(Message.obtain(handler, 0));
116
117 if (validate_game()) {
118 if (whatdrawn) {
119 System.out.println("You Win");
120 handler.sendMessage(Message.obtain(handler, 1));
121 } else {
System.out.println("Computer Win");
122 handler.sendMessage(Message.obtain(handler, 2));
123 }
124 resizegame(x);
125
126 } else if (isFull()) {
127 System.out.println("Loose");
handler.sendMessage(Message.obtain(handler, 3));
128 resizegame(x);
129
130 }
131 }
132
133 private boolean validate_game() {
int contador = 0;
134 Cell anterior = null;
135
136 for (int i = 0; i < singlesquare.length; i++) {
137 for (int j = 0; j < singlesquare[0].length; j++) {
138 System.out.print(singlesquare[i][j]);
139 if (!singlesquare[i][j].equals(anterior)
|| singlesquare[i][j] instanceof Empty) {
140
141 anterior = singlesquare[i][j];
142 contador = 0;
143 } else {
144 contador++;
}
145
if (contador >= getPlayerwin() - 1) {
146 return true;
147 }
148
149 }
150 System.out.println("");
anterior = null;
151 contador = 0;
152 }
153
154 anterior = null;
155 for (int j = 0; j < singlesquare[0].length; j++) {
for (int i = 0; i < singlesquare.length; i++) {
156 System.out.print(singlesquare[i][j]);
157 if (!singlesquare[i][j].equals(anterior)
158 || singlesquare[i][j] instanceof Empty) {
159 anterior = singlesquare[i][j];
contador = 0;
160 } else {
161 contador++;
162 }
163
164 if (contador >= getPlayerwin() - 1) {
165 return true;
}
166
167 }
168 System.out.println("");
169 anterior = null;
170 contador = 0;
}
171
172 anterior = null;
173 for (int j = singlesquare[0].length - 1; j >= 0; j--) {
174 int yau = 0;
175 for (int z = j; z < singlesquare[0].length; z++) {
176 if (!singlesquare[yau][z].equals(anterior)
|| singlesquare[yau][z] instanceof Empty) {
177 anterior = singlesquare[yau][z];
178 contador = 0;
179 } else {
180 contador++;
181 }
182
if (contador >= getPlayerwin() - 1) {
183 return true;
184 }
185 yau++;
186 }
contador = 0;
187 anterior = null;
188 }
189
190 anterior = null;
191 for (int j = 0; j < singlesquare[0].length; j++) {
192 int yau = 0;
for (int z = j; z >= 0; z--) {
193 if (!singlesquare[yau][z].equals(anterior)
194 || singlesquare[yau][z] instanceof Empty) {
195 anterior = singlesquare[yau][z];
196 contador = 0;
} else {
197 contador++;
198 }
199
200 if (contador >= getPlayerwin() - 1) {
201 return true;
}
202
203 yau++;
204 }
205 contador = 0;
206 anterior = null;
}
207 return false;
208 }
209
210 public boolean isFull() {
211 for (int i = 0; i < singlesquare.length; i++) {
212 for (int j = 0; j < singlesquare[0].length; j++) {
if (singlesquare[i][j] instanceof Empty) {
213 return false;
214 }
215 }
216 }
return true;
217 }
218
219 public void resizegame(int s) {
220 x = s;
221 y = s;
222
223 singlesquare = new Cell[x][y];
224
int xss = l / x;
225 int yss = a / y;
226
227 for (int z = 0; z < y; z++) {
228 for (int i = 0; i < x; i++) {
229 singlesquare[z][i] = new Empty(xss * i, z * yss);
230 }
}
231 handler.sendMessage(Message.obtain(handler, 0));
232 }
233
234 public int getPlayerwin() {
235 return playerwin;
}
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
package org.example.TicTacToe;
1
2 import
3 android.content.res.Resources;
import
4 android.graphics.Canvas;
5 import android.graphics.Point;
6
7 public abstract class Cell
8 extends Point {
9
10 public Cell(int x, int y) {
super(x, y);
11 }
12
13 abstract public void
14 draw(Canvas g,Resources res,
int x, int y, int w, int h);
}
1
2
package org.example.TicTacToe;
3
4 import android.content.res.Resources;
5 import android.graphics.Bitmap;
6 import android.graphics.BitmapFactory;
7 import android.graphics.Canvas;
8 import android.graphics.Paint;
import android.graphics.Rect;
9
10public class Empty extends Cell {
11
12 public Empty(int x, int y) {
13 super(x, y);
14 }
15
public void draw(Canvas g, Resources res, int x, int y, int w, int h)
16{
17 Bitmap im = BitmapFactory.decodeResource(res, R.drawable.vazio);
18 g.drawBitmap(im, null, new Rect(x*w, y*h, (x*w)+w, (y*h)+h), new
19Paint());
20 }
21
@Override
22 public boolean equals(Object obj) {
23 if (obj instanceof Empty) {
24 return true;
25 } else {
return false;
26 }
27 }
28 @Override
29 public String toString() {
30 return " ";
}
31}
32
33
1 package org.example.TicTacToe;
2 import android.content.res.Resources;
import android.graphics.Bitmap;
3 import android.graphics.BitmapFactory;
4 import android.graphics.Canvas;
5 import android.graphics.Paint;
6 import android.graphics.Rect;
7
8 public class Cross extends Cell {
9
public Cross(int x, int y) {
10 super(x, y);
11 }
12
13 public void draw(Canvas g, Resources res, int x, int y, int w, int h) {
14 Bitmap im = BitmapFactory.decodeResource(res, R.drawable.cruz);
15 g.drawBitmap(im, null, new Rect(x*w, y*h, (x*w)+w, (y*h)+h), new
Paint());
16 }
17
18 @Override
19 public boolean equals(Object obj) {
20 if (obj instanceof Cross) {
return true;
21 } else {
22 return false;
23 }
24 }
25
26 @Override
public String toString() {
27 return "X";
28 }
29}
30
31
32
33
Output The final output:
Use following images as your resource:
1.)

2.)

3.)

You might also like