1 package org
.proofofconcept
.shisensho
;
4 import java
.util
.Timer
;
5 import java
.util
.TimerTask
;
7 import android
.app
.Activity
;
8 import android
.content
.Context
;
9 import android
.graphics
.Bitmap
;
10 import android
.graphics
.BitmapFactory
;
11 import android
.graphics
.Canvas
;
12 import android
.graphics
.Color
;
13 import android
.graphics
.Paint
;
14 import android
.graphics
.Paint
.Align
;
15 import android
.graphics
.Paint
.Cap
;
16 import android
.graphics
.Paint
.Join
;
17 import android
.graphics
.Paint
.Style
;
18 import android
.graphics
.Rect
;
19 import android
.graphics
.Typeface
;
20 import android
.os
.Handler
;
21 import android
.os
.Message
;
22 import android
.view
.MenuItem
;
23 import android
.view
.MotionEvent
;
24 import android
.view
.SurfaceHolder
;
25 import android
.view
.SurfaceView
;
27 class ShisenShoView
extends SurfaceView
implements SurfaceHolder
.Callback
{
29 private enum StatePlay
{ UNINITIALIZED
, IDLE
, SELECTED1
, SELECTED2
, GAMEOVER
};
30 private enum StatePaint
{ BOARD
, SELECTED1
, SELECTED2
, MATCHED
, WIN
, LOSE
, HINT
, TIME
};
32 private int screenWidth
;
33 private int screenHeight
;
34 private int tilesetRows
;
35 private int tilesetCols
;
36 private int tileHeight
;
37 private int tileWidth
;
39 private Bitmap tile
[];
40 private int[] selection1
=new int[2];
41 private int[] selection2
=new int[2];
42 private List
<Point
> path
=null;
43 private List
<Line
> pairs
=null;
44 private long startTime
;
45 private long playTime
;
46 private long baseTime
;
48 private Handler timerHandler
;
50 private boolean timerRegistered
=false;
51 private ShisenSho app
;
52 private StatePlay cstate
;
53 private StatePaint pstate
;
54 private Canvas canvas
= null;
55 private SurfaceHolder surfaceHolder
= null;
56 private String debugMessage
= "";
58 public ShisenShoView(ShisenSho shishenSho
) {
59 super((Context
)shishenSho
);
60 this.app
= shishenSho
;
61 cstate
= StatePlay
.UNINITIALIZED
;
62 surfaceHolder
= getHolder();
63 surfaceHolder
.addCallback(this);
66 private void paint(StatePaint pstate
) {
71 private void control(StatePlay cstate
) {
75 private void loadTileset() {
76 BitmapFactory
.Options ops
= new BitmapFactory
.Options();
78 Bitmap tileset
= BitmapFactory
.decodeResource(getResources(), R
.drawable
.tileset
, ops
);
79 tileset
.setDensity(Bitmap
.DENSITY_NONE
);
81 // The tile set has 4 rows x 9 columns
84 tileWidth
= tileset
.getWidth()/tilesetCols
;
85 tileHeight
= tileset
.getHeight()/tilesetRows
;
86 tile
= new Bitmap
[tilesetRows
*tilesetCols
];
88 for (int i
=0; i
<tilesetRows
; i
++) {
89 for (int j
=0; j
<tilesetCols
; j
++) {
90 tile
[k
] = Bitmap
.createBitmap(tileset
, j
*tileWidth
, i
*tileHeight
, tileWidth
, tileHeight
, null, false);
91 tile
[k
].setDensity(Bitmap
.DENSITY_NONE
);
95 tileWidth
= tile
[0].getWidth();
96 tileHeight
= tile
[0].getHeight();
99 private void loadBackground() {
100 BitmapFactory
.Options ops
= new BitmapFactory
.Options();
101 ops
.inScaled
= false;
102 bg
= BitmapFactory
.decodeResource(getResources(), R
.drawable
.kshisen_bgnd
, ops
);
103 bg
.setDensity(Bitmap
.DENSITY_NONE
);
106 private void registerTimer() {
107 if (timer
!=null) return; // Already registered
108 timerHandler
= new Handler() {
109 public void handleMessage(Message msg
) {
114 timer
.scheduleAtFixedRate(new TimerTask() {
116 timerHandler
.sendEmptyMessage(Activity
.RESULT_OK
);
119 timerRegistered
=true;
122 private void unregisterTimer() {
123 if (timer
==null) return; // Already unregistered
127 timerRegistered
=false;
130 public void pauseTime() {
133 startTime
= System
.currentTimeMillis();
137 public void resumeTime() {
138 startTime
= System
.currentTimeMillis();
142 private void updateTime() {
143 if (cstate
!=StatePlay
.GAMEOVER
) {
144 playTime
= (System
.currentTimeMillis()-startTime
)/1000+baseTime
;
148 private void initializeGame() {
151 screenWidth
=getWidth();
152 screenHeight
=getHeight();
153 //undo.sensitive=false;
154 pstate
=StatePaint
.BOARD
;
156 control(StatePlay
.IDLE
);
157 startTime
=System
.currentTimeMillis();
160 if (app
.timeCounter
&& !timerRegistered
) {
163 pairs
=app
.board
.getPairs(1);
166 public boolean onOptionsItemSelected(MenuItem item
) {
167 // Handle item selection
168 switch (item
.getItemId()) {
170 this.postDelayed(new Runnable() { public void run() { onHintActivate(); } }, 100);
173 this.postDelayed(new Runnable() { public void run() { onUndoActivate(); } }, 100);
176 this.postDelayed(new Runnable() { public void run() { reset(); } }, 100);
187 public void reset() {
188 control(StatePlay
.UNINITIALIZED
);
189 paint(StatePaint
.BOARD
);
192 private void onHintActivate() {
193 if (cstate
!=StatePlay
.GAMEOVER
) {
194 pairs
=app
.board
.getPairs(1);
195 paint(StatePaint
.HINT
);
197 paint(StatePaint
.BOARD
);
198 control(StatePlay
.IDLE
);
202 private void onUndoActivate() {
203 if (app
.board
.getCanUndo()) {
204 if (cstate
==StatePlay
.GAMEOVER
&& app
.timeCounter
&& !timerRegistered
) {
205 // Reprogram the time update that had been
206 // deactivated with the game over status
210 paint(StatePaint
.BOARD
);
211 //undo.sensitive=app.board.getCanUndo();
212 control(StatePlay
.IDLE
);
216 public void onTimeCounterActivate() {
217 if (app
.timeCounter
&& cstate
!=StatePlay
.GAMEOVER
&& !timerRegistered
) {
218 // Reprogram the time update that had been
219 // deactivated with the time_counter=false
224 private void onUpdateTime() {
226 if (!(app
.timeCounter
&& cstate
!=StatePlay
.GAMEOVER
)) {
231 public void drawMessage(Canvas canvas
, int x
, int y
, boolean centered
, String message
, String color
, float textSize
) {
232 Paint paint
= new Paint();
233 paint
.setColor(Color
.parseColor(color
));
234 paint
.setLinearText(true);
235 paint
.setAntiAlias(true);
236 paint
.setTextAlign(centered?Align
.CENTER
:Align
.LEFT
);
237 paint
.setTypeface(Typeface
.SANS_SERIF
);
238 paint
.setFakeBoldText(true);
239 paint
.setTextSize(textSize
);
240 canvas
.drawText(message
, x
, y
, paint
);
243 public void repaint() {
244 if (surfaceHolder
== null) return;
246 if (canvas
== null) canvas
= surfaceHolder
.lockCanvas(null);
247 if (canvas
== null) return;
248 if (cstate
==StatePlay
.UNINITIALIZED
) initializeGame();
249 synchronized (surfaceHolder
) {
253 if (canvas
!= null) {
254 surfaceHolder
.unlockCanvasAndPost(canvas
);
260 protected void doDraw(Canvas canvas
) {
263 // Bitmap buffer = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
264 //Canvas cbuffer = new Canvas(buffer);
265 Canvas cbuffer
= canvas
;
266 if (canvas
== null) return;
268 //super.onDraw(canvas);
270 // Board upper left corner on screen
274 if (app
!=null && app
.board
!=null) {
275 x0
=(screenWidth
-app
.board
.boardSize
[1]*tileWidth
)/2;
276 y0
=(screenHeight
-app
.board
.boardSize
[0]*tileHeight
)/2;
279 int red
= Color
.parseColor("#FF0000");
280 int orange
= Color
.parseColor("#F0C000");
281 Paint paint
= new Paint();
282 paint
.setFlags(Paint
.ANTI_ALIAS_FLAG
);
284 // Background & board painting
294 // Background painting
295 int bgWidth
= bg
.getWidth();
296 int bgHeight
= bg
.getHeight();
297 for (int i
=0; i
<screenHeight
/bgHeight
+1; i
++) {
298 for (int j
=0; j
<screenWidth
/bgWidth
+1; j
++) {
299 cbuffer
.drawBitmap(bg
, j
*bgWidth
, i
*bgHeight
, paint
);
304 // Max visible size: 7x17
305 if (app
!=null && app
.board
!=null) {
306 for (int i
=0;i
<app
.board
.boardSize
[0];i
++) {
307 for (int j
=0;j
<app
.board
.boardSize
[1];j
++) {
308 // Tiles are 56px height, 40px width each
309 char piece
=app
.board
.board
[i
][j
];
311 cbuffer
.drawBitmap(tile
[piece
], x0
+j
*tileWidth
, y0
+i
*tileHeight
, paint
);
319 // Red rectangle for selection 1
325 paint
.setStyle(Style
.STROKE
);
326 paint
.setStrokeCap(Cap
.ROUND
);
327 paint
.setStrokeJoin(Join
.ROUND
);
328 paint
.setStrokeWidth(3);
329 cbuffer
.drawRect(new Rect(
330 x0
+selection1
[1]*tileWidth
-2,
331 y0
+selection1
[0]*tileHeight
-2,
332 x0
+selection1
[1]*tileWidth
-2+tileWidth
+2*2,
333 y0
+selection1
[0]*tileHeight
-2+tileHeight
+2*2),
338 // Red rectangle for selection 2
343 paint
.setStyle(Style
.STROKE
);
344 paint
.setStrokeCap(Cap
.ROUND
);
345 paint
.setStrokeJoin(Join
.ROUND
);
346 paint
.setStrokeWidth(3);
347 cbuffer
.drawRect(new Rect(
348 x0
+selection2
[1]*tileWidth
-2,
349 y0
+selection2
[0]*tileHeight
-2,
350 x0
+selection2
[1]*tileWidth
-2+tileWidth
+2*2,
351 y0
+selection2
[0]*tileHeight
-2+tileHeight
+2*2),
360 paint
.setStyle(Style
.STROKE
);
361 paint
.setStrokeCap(Cap
.ROUND
);
362 paint
.setStrokeJoin(Join
.ROUND
);
363 paint
.setStrokeWidth(3);
367 for (Point p1
: path
) {
370 x0
+p0
.j
*tileWidth
-2+(tileWidth
/2),
371 y0
+p0
.i
*tileHeight
-2+(tileHeight
/2),
372 x0
+p1
.j
*tileWidth
-2+(tileWidth
/2),
373 y0
+p1
.i
*tileHeight
-2+(tileHeight
/2),
382 // Orange hint rectangles
385 if (pairs
!=null && pairs
.size()>0) {
386 Line pair
=pairs
.get(0);
389 path
=app
.board
.getPath(a
,b
);
390 paint
.setColor(orange
);
391 paint
.setStyle(Style
.STROKE
);
392 paint
.setStrokeCap(Cap
.ROUND
);
393 paint
.setStrokeJoin(Join
.ROUND
);
394 paint
.setStrokeWidth(3);
396 cbuffer
.drawRect(new Rect(
399 x0
+a
.j
*tileWidth
-2+tileWidth
+2*2,
400 y0
+a
.i
*tileHeight
-2+tileHeight
+2*2),
405 for (Point p1
: path
) {
408 x0
+p0
.j
*tileWidth
-2+(tileWidth
/2),
409 y0
+p0
.i
*tileHeight
-2+(tileHeight
/2),
410 x0
+p1
.j
*tileWidth
-2+(tileWidth
/2),
411 y0
+p1
.i
*tileHeight
-2+(tileHeight
/2),
419 cbuffer
.drawRect(new Rect(
422 x0
+b
.j
*tileWidth
-2+tileWidth
+2*2,
423 y0
+b
.i
*tileHeight
-2+tileHeight
+2*2),
429 // Win & loose notifications
432 drawMessage(cbuffer
, screenWidth
/2,screenHeight
/2,true,"You Win!", "#FFFFFF", 100);
435 drawMessage(cbuffer
, screenWidth
/2,screenHeight
/2,true,"Game Over", "#FFFFFF", 100);
439 if (app
.timeCounter
) switch (pstate
) {
449 int hours
=(int)(playTime
/(60*60));
450 int minutes
=(int)((playTime
/60)%60);
451 int seconds
=(int)(playTime
%60);
452 String time
=String
.format("%01d:%02d:%02d", hours
, minutes
, seconds
);
454 int timePosX
=screenWidth
-120;
455 int timePosY
=screenHeight
-10;
457 drawMessage(cbuffer
, timePosX
+1,timePosY
+1,false,time
,"#000000",30);
458 drawMessage(cbuffer
, timePosX
,timePosY
,false,time
,"#FFFFFF",30);
464 debugMessage="StatePlay: "+cstate+"\n"+"StatePaint: "+pstate;
465 if (debugMessage!=null && debugMessage.length()>0) {
467 String lines[] = debugMessage.split("\n");
468 for (int i=0; i<lines.length; i++) {
469 drawMessage(cbuffer,1,l,false,lines[i],"#FFFF00",30);
475 // Double buffer dumping
476 // canvas.drawBitmap(buffer, 0, 0, null);
478 } catch (Exception e
) {
485 public boolean onTouchEvent(MotionEvent event
) {
486 if (event
.getAction()==MotionEvent
.ACTION_DOWN
) {
487 onClick(Math
.round(event
.getX()),Math
.round(event
.getY()));
489 return super.onTouchEvent(event
);
492 private void onClick(int x
, int y
) {
494 int i
=(y
-(screenHeight
-app
.board
.boardSize
[0]*tileHeight
)/2)/tileHeight
;
495 int j
=(x
-(screenWidth
-app
.board
.boardSize
[1]*tileWidth
)/2)/tileWidth
;
500 i
<app
.board
.boardSize
[0] &&
501 j
>=0 && j
<app
.board
.boardSize
[1] &&
502 app
.board
.board
[i
][j
]!=0) {
505 paint(StatePaint
.SELECTED1
);
506 control(StatePlay
.SELECTED1
);
510 if (i
>=0 && i
<app
.board
.boardSize
[0] &&
511 j
>=0 && j
<app
.board
.boardSize
[1] &&
512 app
.board
.board
[i
][j
]!=0) {
513 if (i
==selection1
[0] && j
==selection1
[1]) {
514 paint(StatePaint
.BOARD
);
515 control(StatePlay
.IDLE
);
519 paint(StatePaint
.SELECTED2
);
521 Point a
=new Point(selection1
[0],selection1
[1]);
522 Point b
=new Point(selection2
[0],selection2
[1]);
523 path
=app
.board
.getPath(a
,b
);
524 paint(StatePaint
.MATCHED
);
526 paint(StatePaint
.BOARD
);
531 paint(StatePaint
.BOARD
);
533 pairs
=app
.board
.getPairs(1);
534 if (pairs
.size()==0) {
535 if (app
.board
.getNumPieces()==0) {
536 paint(StatePaint
.WIN
);
538 paint(StatePaint
.LOSE
);
540 control(StatePlay
.GAMEOVER
);
542 control(StatePlay
.IDLE
);
544 //undo.sensitive=app.board.getCanUndo();
550 paint(StatePaint
.BOARD
);
553 } catch (Exception e
) {
558 public void surfaceChanged(SurfaceHolder holder
, int format
, int width
,
560 surfaceHolder
= holder
;
561 if (cstate
!=StatePlay
.GAMEOVER
&& app
.timeCounter
&& !timerRegistered
) {
567 public void surfaceCreated(SurfaceHolder holder
) {
568 surfaceHolder
= holder
;
572 public void surfaceDestroyed(SurfaceHolder holder
) {
573 surfaceHolder
= null;
574 if (timerRegistered
) {
581 protected void onDraw(Canvas canvas) {
582 super.onDraw(canvas);
584 if (!initialized) initialize();
586 long currTime = System.currentTimeMillis();
588 a = (float)(currTime - startTime) / (float)duration;
589 if (a > (float)1.0) a = (float)1.0;
591 x = Math.round(nextx*a + prevx*(1-a));
592 y = Math.round(nexty*a + prevy*(1-a));
594 if (a == (float)1.0) computeNextTarget();
596 int bgWidth = bg.getWidth();
597 int bgHeight = bg.getHeight();
598 for (int i=0; i<height/bgHeight+1; i++) {
599 for (int j=0; j<width/bgWidth+1; j++) {
600 canvas.drawBitmap(bg, j*bgWidth, i*bgHeight, paint);
604 canvas.drawBitmap(tile[randomtile], x, y, paint);
610 public boolean onTouchEvent(MotionEvent event) {
611 if (event.getActionMasked()==MotionEvent.ACTION_DOWN) {
612 //computeNextTarget();
613 //nextx=Math.round(event.getX());
614 //nexty=Math.round(event.getY());
616 return super.onTouchEvent(event);
619 private void initialize() {
621 height = getHeight();
623 bg = BitmapFactory.decodeResource(getResources(), R.drawable.kshisen_bgnd);
624 Bitmap tileset = BitmapFactory.decodeResource(getResources(), R.drawable.tileset);
626 // The tile set has 4 rows x 9 columns
629 twidth = tileset.getWidth()/tscols;
630 theight = tileset.getHeight()/tsrows;
631 tile = new Bitmap[tsrows*tscols];
633 for (int i=0; i<tsrows; i++) {
634 for (int j=0; j<tscols; j++) {
635 tile[k] = Bitmap.createBitmap(tileset, j*twidth, i*theight, twidth, theight, null, false);
648 private void computeNextTarget() {
649 startTime = System.currentTimeMillis();
652 nextx = (int) Math.floor(Math.random() * width);
653 nexty = (int) Math.floor(Math.random() * height);
654 randomtile = (int) Math.floor(Math.random() * tile.length);
657 paint.setColor(Color.parseColor("#006666"));
658 paint.setFlags(Paint.ANTI_ALIAS_FLAG);