1 package de
.cwde
.shisensho
;
4 import java
.util
.Locale
;
5 import java
.util
.Timer
;
6 import java
.util
.TimerTask
;
8 import de
.cwde
.shisensho
.R
;
10 import android
.app
.Activity
;
11 import android
.content
.Context
;
12 import android
.graphics
.Bitmap
;
13 import android
.graphics
.BitmapFactory
;
14 import android
.graphics
.Canvas
;
15 import android
.graphics
.Color
;
16 import android
.graphics
.Paint
;
17 import android
.graphics
.Paint
.Align
;
18 import android
.graphics
.Paint
.Cap
;
19 import android
.graphics
.Paint
.Join
;
20 import android
.graphics
.Paint
.Style
;
21 import android
.graphics
.Rect
;
22 import android
.graphics
.Typeface
;
23 import android
.os
.Handler
;
24 import android
.os
.Message
;
25 import android
.view
.MenuItem
;
26 import android
.view
.MotionEvent
;
27 import android
.view
.SurfaceHolder
;
28 import android
.view
.SurfaceView
;
30 class ShisenShoView
extends SurfaceView
implements SurfaceHolder
.Callback
{
32 private enum StatePlay
{ UNINITIALIZED
, IDLE
, SELECTED1
, SELECTED2
, GAMEOVER
};
33 private enum StatePaint
{ BOARD
, SELECTED1
, SELECTED2
, MATCHED
, WIN
, LOSE
, HINT
, TIME
};
35 private int screenWidth
;
36 private int screenHeight
;
37 private int tilesetRows
;
38 private int tilesetCols
;
39 private int tileHeight
;
40 private int tileWidth
;
42 private Bitmap tile
[];
43 private int[] selection1
=new int[2];
44 private int[] selection2
=new int[2];
45 private List
<Point
> path
=null;
46 private List
<Line
> pairs
=null;
47 private long startTime
;
48 private long playTime
;
49 private long baseTime
;
51 private static Handler timerHandler
;
53 private boolean timerRegistered
=false;
54 private ShisenSho app
;
55 private StatePlay cstate
;
56 private StatePaint pstate
;
57 private Canvas canvas
= null;
58 private SurfaceHolder surfaceHolder
= null;
59 public ShisenShoView(ShisenSho shishenSho
) {
60 super((Context
)shishenSho
);
61 this.app
= shishenSho
;
62 cstate
= StatePlay
.UNINITIALIZED
;
63 surfaceHolder
= getHolder();
64 surfaceHolder
.addCallback(this);
67 private void paint(StatePaint pstate
) {
72 private void control(StatePlay cstate
) {
76 private void loadTileset() {
77 BitmapFactory
.Options ops
= new BitmapFactory
.Options();
79 Bitmap tileset
= BitmapFactory
.decodeResource(getResources(), R
.drawable
.tileset
, ops
);
80 tileset
.setDensity(Bitmap
.DENSITY_NONE
);
82 // The tile set has 4 rows x 9 columns
85 tileWidth
= tileset
.getWidth()/tilesetCols
;
86 tileHeight
= tileset
.getHeight()/tilesetRows
;
87 tile
= new Bitmap
[tilesetRows
*tilesetCols
];
89 for (int i
=0; i
<tilesetRows
; i
++) {
90 for (int j
=0; j
<tilesetCols
; j
++) {
91 tile
[k
] = Bitmap
.createBitmap(tileset
, j
*tileWidth
, i
*tileHeight
, tileWidth
, tileHeight
, null, false);
92 tile
[k
].setDensity(Bitmap
.DENSITY_NONE
);
96 tileWidth
= tile
[0].getWidth();
97 tileHeight
= tile
[0].getHeight();
100 private void loadBackground() {
101 BitmapFactory
.Options ops
= new BitmapFactory
.Options();
102 ops
.inScaled
= false;
103 bg
= BitmapFactory
.decodeResource(getResources(), R
.drawable
.kshisen_bgnd
, ops
);
104 bg
.setDensity(Bitmap
.DENSITY_NONE
);
107 private void registerTimer() {
108 if (timer
!=null) return; // Already registered
109 timerHandler
= new Handler() {
110 public void handleMessage(Message msg
) {
115 timer
.scheduleAtFixedRate(new TimerTask() {
117 timerHandler
.sendEmptyMessage(Activity
.RESULT_OK
);
120 timerRegistered
=true;
123 private void unregisterTimer() {
124 if (timer
==null) return; // Already unregistered
128 timerRegistered
=false;
131 public void pauseTime() {
134 startTime
= System
.currentTimeMillis();
138 public void resumeTime() {
139 startTime
= System
.currentTimeMillis();
143 private void updateTime() {
144 if (cstate
!=StatePlay
.GAMEOVER
) {
145 playTime
= (System
.currentTimeMillis()-startTime
)/1000+baseTime
;
149 private void initializeGame() {
152 screenWidth
=getWidth();
153 screenHeight
=getHeight();
154 //undo.sensitive=false;
155 pstate
=StatePaint
.BOARD
;
157 control(StatePlay
.IDLE
);
158 startTime
=System
.currentTimeMillis();
161 if (app
.timeCounter
&& !timerRegistered
) {
164 pairs
=app
.board
.getPairs(1);
167 public boolean onOptionsItemSelected(MenuItem item
) {
168 // Handle item selection
169 switch (item
.getItemId()) {
171 this.postDelayed(new Runnable() { public void run() { onHintActivate(); } }, 100);
174 this.postDelayed(new Runnable() { public void run() { onUndoActivate(); } }, 100);
177 this.postDelayed(new Runnable() { public void run() { reset(); } }, 100);
188 public void reset() {
189 control(StatePlay
.UNINITIALIZED
);
190 paint(StatePaint
.BOARD
);
193 private void onHintActivate() {
194 if (cstate
!=StatePlay
.GAMEOVER
) {
195 pairs
=app
.board
.getPairs(1);
196 paint(StatePaint
.HINT
);
198 paint(StatePaint
.BOARD
);
199 control(StatePlay
.IDLE
);
203 private void onUndoActivate() {
204 if (app
.board
.getCanUndo()) {
205 if (cstate
==StatePlay
.GAMEOVER
&& app
.timeCounter
&& !timerRegistered
) {
206 // Reprogram the time update that had been
207 // deactivated with the game over status
211 paint(StatePaint
.BOARD
);
212 //undo.sensitive=app.board.getCanUndo();
213 control(StatePlay
.IDLE
);
217 public void onTimeCounterActivate() {
218 if (app
.timeCounter
&& cstate
!=StatePlay
.GAMEOVER
&& !timerRegistered
) {
219 // Reprogram the time update that had been
220 // deactivated with the time_counter=false
225 private void onUpdateTime() {
227 if (!(app
.timeCounter
&& cstate
!=StatePlay
.GAMEOVER
)) {
232 public void drawMessage(Canvas canvas
, int x
, int y
, boolean centered
, String message
, String color
, float textSize
) {
233 Paint paint
= new Paint();
234 paint
.setColor(Color
.parseColor(color
));
235 paint
.setLinearText(true);
236 paint
.setAntiAlias(true);
237 paint
.setTextAlign(centered?Align
.CENTER
:Align
.LEFT
);
238 paint
.setTypeface(Typeface
.SANS_SERIF
);
239 paint
.setFakeBoldText(true);
240 paint
.setTextSize(textSize
);
241 canvas
.drawText(message
, x
, y
, paint
);
244 public void repaint() {
245 if (surfaceHolder
== null) return;
247 if (canvas
== null) canvas
= surfaceHolder
.lockCanvas(null);
248 if (canvas
== null) return;
249 if (cstate
==StatePlay
.UNINITIALIZED
) initializeGame();
250 synchronized (surfaceHolder
) {
254 if (canvas
!= null) {
255 surfaceHolder
.unlockCanvasAndPost(canvas
);
261 protected void doDraw(Canvas canvas
) {
264 // Bitmap buffer = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
265 //Canvas cbuffer = new Canvas(buffer);
266 Canvas cbuffer
= canvas
;
267 if (canvas
== null) return;
269 //super.onDraw(canvas);
271 // Board upper left corner on screen
275 if (app
!=null && app
.board
!=null) {
276 x0
=(screenWidth
-app
.board
.boardSize
[1]*tileWidth
)/2;
277 y0
=(screenHeight
-app
.board
.boardSize
[0]*tileHeight
)/2;
280 int red
= Color
.parseColor("#FF0000");
281 int orange
= Color
.parseColor("#F0C000");
282 Paint paint
= new Paint();
283 paint
.setFlags(Paint
.ANTI_ALIAS_FLAG
);
285 // Background & board painting
295 // Background painting
296 int bgWidth
= bg
.getWidth();
297 int bgHeight
= bg
.getHeight();
298 for (int i
=0; i
<screenHeight
/bgHeight
+1; i
++) {
299 for (int j
=0; j
<screenWidth
/bgWidth
+1; j
++) {
300 cbuffer
.drawBitmap(bg
, j
*bgWidth
, i
*bgHeight
, paint
);
305 // Max visible size: 7x17
306 if (app
!=null && app
.board
!=null) {
307 for (int i
=0;i
<app
.board
.boardSize
[0];i
++) {
308 for (int j
=0;j
<app
.board
.boardSize
[1];j
++) {
309 // Tiles are 56px height, 40px width each
310 char piece
=app
.board
.board
[i
][j
];
312 cbuffer
.drawBitmap(tile
[piece
], x0
+j
*tileWidth
, y0
+i
*tileHeight
, paint
);
320 // Red rectangle for selection 1
326 paint
.setStyle(Style
.STROKE
);
327 paint
.setStrokeCap(Cap
.ROUND
);
328 paint
.setStrokeJoin(Join
.ROUND
);
329 paint
.setStrokeWidth(3);
330 cbuffer
.drawRect(new Rect(
331 x0
+selection1
[1]*tileWidth
-2,
332 y0
+selection1
[0]*tileHeight
-2,
333 x0
+selection1
[1]*tileWidth
-2+tileWidth
+2*2,
334 y0
+selection1
[0]*tileHeight
-2+tileHeight
+2*2),
339 // Red rectangle for selection 2
344 paint
.setStyle(Style
.STROKE
);
345 paint
.setStrokeCap(Cap
.ROUND
);
346 paint
.setStrokeJoin(Join
.ROUND
);
347 paint
.setStrokeWidth(3);
348 cbuffer
.drawRect(new Rect(
349 x0
+selection2
[1]*tileWidth
-2,
350 y0
+selection2
[0]*tileHeight
-2,
351 x0
+selection2
[1]*tileWidth
-2+tileWidth
+2*2,
352 y0
+selection2
[0]*tileHeight
-2+tileHeight
+2*2),
361 paint
.setStyle(Style
.STROKE
);
362 paint
.setStrokeCap(Cap
.ROUND
);
363 paint
.setStrokeJoin(Join
.ROUND
);
364 paint
.setStrokeWidth(3);
368 for (Point p1
: path
) {
371 x0
+p0
.j
*tileWidth
-2+(tileWidth
/2),
372 y0
+p0
.i
*tileHeight
-2+(tileHeight
/2),
373 x0
+p1
.j
*tileWidth
-2+(tileWidth
/2),
374 y0
+p1
.i
*tileHeight
-2+(tileHeight
/2),
383 // Orange hint rectangles
386 if (pairs
!=null && pairs
.size()>0) {
387 Line pair
=pairs
.get(0);
390 path
=app
.board
.getPath(a
,b
);
391 paint
.setColor(orange
);
392 paint
.setStyle(Style
.STROKE
);
393 paint
.setStrokeCap(Cap
.ROUND
);
394 paint
.setStrokeJoin(Join
.ROUND
);
395 paint
.setStrokeWidth(3);
397 cbuffer
.drawRect(new Rect(
400 x0
+a
.j
*tileWidth
-2+tileWidth
+2*2,
401 y0
+a
.i
*tileHeight
-2+tileHeight
+2*2),
406 for (Point p1
: path
) {
409 x0
+p0
.j
*tileWidth
-2+(tileWidth
/2),
410 y0
+p0
.i
*tileHeight
-2+(tileHeight
/2),
411 x0
+p1
.j
*tileWidth
-2+(tileWidth
/2),
412 y0
+p1
.i
*tileHeight
-2+(tileHeight
/2),
420 cbuffer
.drawRect(new Rect(
423 x0
+b
.j
*tileWidth
-2+tileWidth
+2*2,
424 y0
+b
.i
*tileHeight
-2+tileHeight
+2*2),
430 // Win & loose notifications
433 drawMessage(cbuffer
, screenWidth
/2,screenHeight
/2,true,"You Win!", "#FFFFFF", 100);
436 drawMessage(cbuffer
, screenWidth
/2,screenHeight
/2,true,"Game Over", "#FFFFFF", 100);
440 if (app
.timeCounter
) switch (pstate
) {
450 int hours
=(int)(playTime
/(60*60));
451 int minutes
=(int)((playTime
/60)%60);
452 int seconds
=(int)(playTime
%60);
453 String time
=String
.format(Locale
.US
, "%01d:%02d:%02d", hours
, minutes
, seconds
);
455 int timePosX
=screenWidth
-120;
456 int timePosY
=screenHeight
-10;
458 drawMessage(cbuffer
, timePosX
+1,timePosY
+1,false,time
,"#000000",30);
459 drawMessage(cbuffer
, timePosX
,timePosY
,false,time
,"#FFFFFF",30);
465 debugMessage="StatePlay: "+cstate+"\n"+"StatePaint: "+pstate;
466 if (debugMessage!=null && debugMessage.length()>0) {
468 String lines[] = debugMessage.split("\n");
469 for (int i=0; i<lines.length; i++) {
470 drawMessage(cbuffer,1,l,false,lines[i],"#FFFF00",30);
476 // Double buffer dumping
477 // canvas.drawBitmap(buffer, 0, 0, null);
479 } catch (Exception e
) {
486 public boolean onTouchEvent(MotionEvent event
) {
487 if (event
.getAction()==MotionEvent
.ACTION_DOWN
) {
488 onClick(Math
.round(event
.getX()),Math
.round(event
.getY()));
490 return super.onTouchEvent(event
);
493 private void onClick(int x
, int y
) {
495 int i
=(y
-(screenHeight
-app
.board
.boardSize
[0]*tileHeight
)/2)/tileHeight
;
496 int j
=(x
-(screenWidth
-app
.board
.boardSize
[1]*tileWidth
)/2)/tileWidth
;
501 i
<app
.board
.boardSize
[0] &&
502 j
>=0 && j
<app
.board
.boardSize
[1] &&
503 app
.board
.board
[i
][j
]!=0) {
506 paint(StatePaint
.SELECTED1
);
507 control(StatePlay
.SELECTED1
);
511 if (i
>=0 && i
<app
.board
.boardSize
[0] &&
512 j
>=0 && j
<app
.board
.boardSize
[1] &&
513 app
.board
.board
[i
][j
]!=0) {
514 if (i
==selection1
[0] && j
==selection1
[1]) {
515 paint(StatePaint
.BOARD
);
516 control(StatePlay
.IDLE
);
520 paint(StatePaint
.SELECTED2
);
522 Point a
=new Point(selection1
[0],selection1
[1]);
523 Point b
=new Point(selection2
[0],selection2
[1]);
524 path
=app
.board
.getPath(a
,b
);
525 paint(StatePaint
.MATCHED
);
527 paint(StatePaint
.BOARD
);
532 paint(StatePaint
.BOARD
);
534 pairs
=app
.board
.getPairs(1);
535 if (pairs
.size()==0) {
536 if (app
.board
.getNumPieces()==0) {
537 paint(StatePaint
.WIN
);
539 paint(StatePaint
.LOSE
);
541 control(StatePlay
.GAMEOVER
);
543 control(StatePlay
.IDLE
);
545 //undo.sensitive=app.board.getCanUndo();
551 paint(StatePaint
.BOARD
);
554 } catch (Exception e
) {
559 public void surfaceChanged(SurfaceHolder holder
, int format
, int width
,
561 surfaceHolder
= holder
;
562 if (cstate
!=StatePlay
.GAMEOVER
&& app
.timeCounter
&& !timerRegistered
) {
568 public void surfaceCreated(SurfaceHolder holder
) {
569 surfaceHolder
= holder
;
573 public void surfaceDestroyed(SurfaceHolder holder
) {
574 surfaceHolder
= null;
575 if (timerRegistered
) {
582 protected void onDraw(Canvas canvas) {
583 super.onDraw(canvas);
585 if (!initialized) initialize();
587 long currTime = System.currentTimeMillis();
589 a = (float)(currTime - startTime) / (float)duration;
590 if (a > (float)1.0) a = (float)1.0;
592 x = Math.round(nextx*a + prevx*(1-a));
593 y = Math.round(nexty*a + prevy*(1-a));
595 if (a == (float)1.0) computeNextTarget();
597 int bgWidth = bg.getWidth();
598 int bgHeight = bg.getHeight();
599 for (int i=0; i<height/bgHeight+1; i++) {
600 for (int j=0; j<width/bgWidth+1; j++) {
601 canvas.drawBitmap(bg, j*bgWidth, i*bgHeight, paint);
605 canvas.drawBitmap(tile[randomtile], x, y, paint);
611 public boolean onTouchEvent(MotionEvent event) {
612 if (event.getActionMasked()==MotionEvent.ACTION_DOWN) {
613 //computeNextTarget();
614 //nextx=Math.round(event.getX());
615 //nexty=Math.round(event.getY());
617 return super.onTouchEvent(event);
620 private void initialize() {
622 height = getHeight();
624 bg = BitmapFactory.decodeResource(getResources(), R.drawable.kshisen_bgnd);
625 Bitmap tileset = BitmapFactory.decodeResource(getResources(), R.drawable.tileset);
627 // The tile set has 4 rows x 9 columns
630 twidth = tileset.getWidth()/tscols;
631 theight = tileset.getHeight()/tsrows;
632 tile = new Bitmap[tsrows*tscols];
634 for (int i=0; i<tsrows; i++) {
635 for (int j=0; j<tscols; j++) {
636 tile[k] = Bitmap.createBitmap(tileset, j*twidth, i*theight, twidth, theight, null, false);
649 private void computeNextTarget() {
650 startTime = System.currentTimeMillis();
653 nextx = (int) Math.floor(Math.random() * width);
654 nexty = (int) Math.floor(Math.random() * height);
655 randomtile = (int) Math.floor(Math.random() * tile.length);
658 paint.setColor(Color.parseColor("#006666"));
659 paint.setFlags(Paint.ANTI_ALIAS_FLAG);