1 package de
.cwde
.freeshisen
;
4 import java
.util
.Locale
;
5 import java
.util
.Timer
;
6 import java
.util
.TimerTask
;
8 import android
.app
.Activity
;
9 import android
.content
.Context
;
10 import android
.graphics
.Bitmap
;
11 import android
.graphics
.BitmapFactory
;
12 import android
.graphics
.Canvas
;
13 import android
.graphics
.Color
;
14 import android
.graphics
.Matrix
;
15 import android
.graphics
.Paint
;
16 import android
.graphics
.Paint
.Align
;
17 import android
.graphics
.Paint
.Cap
;
18 import android
.graphics
.Paint
.Join
;
19 import android
.graphics
.Paint
.Style
;
20 import android
.graphics
.Rect
;
21 import android
.graphics
.Typeface
;
22 import android
.os
.Handler
;
23 import android
.os
.Message
;
24 import android
.view
.MenuItem
;
25 import android
.view
.MotionEvent
;
26 import android
.view
.SurfaceHolder
;
27 import android
.view
.SurfaceView
;
29 class ShisenShoView
extends SurfaceView
implements SurfaceHolder
.Callback
{
31 private enum StatePlay
{ UNINITIALIZED
, IDLE
, SELECTED1
, SELECTED2
, GAMEOVER
};
32 private enum StatePaint
{ BOARD
, SELECTED1
, SELECTED2
, MATCHED
, WIN
, LOSE
, HINT
, TIME
};
34 private int screenWidth
;
35 private int screenHeight
;
36 private int tilesetRows
;
37 private int tilesetCols
;
38 private int tileHeight
;
39 private int tileWidth
;
41 private Bitmap tile
[];
42 private int[] selection1
=new int[2];
43 private int[] selection2
=new int[2];
44 private List
<Point
> path
=null;
45 private List
<Line
> pairs
=null;
46 private long startTime
;
47 private long playTime
;
48 private long baseTime
;
50 private static Handler timerHandler
;
52 private boolean timerRegistered
=false;
53 private ShisenSho app
;
54 private StatePlay cstate
;
55 private StatePaint pstate
;
56 private Canvas canvas
= null;
57 private SurfaceHolder surfaceHolder
= null;
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 public ShisenShoView(Context ctx
) {
71 private void paint(StatePaint pstate
) {
76 private void control(StatePlay cstate
) {
80 private void loadTileset() {
81 BitmapFactory
.Options ops
= new BitmapFactory
.Options();
83 Bitmap tileset
= BitmapFactory
.decodeResource(getResources(), R
.drawable
.tileset
, ops
);
84 tileset
.setDensity(Bitmap
.DENSITY_NONE
);
86 // The tile set has 4 rows x 9 columns
89 int loadedtileWidth
= tileset
.getWidth()/tilesetCols
;
90 int loadedtileHeight
= tileset
.getHeight()/tilesetRows
;
91 tile
= new Bitmap
[tilesetRows
*tilesetCols
];
94 // "large" is 16x6, and we want to have a nice border, so we use 17x7 and
95 // choose the lowest scale so everything fits
96 float scalex
= ((float) screenWidth
/17) / loadedtileWidth
;
97 float scaley
= ((float) screenHeight
/7) / loadedtileHeight
;
98 if (scaley
< scalex
) {
103 Matrix matrix
= new Matrix();
104 matrix
.setScale(scalex
, scaley
);
107 for (int i
=0; i
<tilesetRows
; i
++) {
108 for (int j
=0; j
<tilesetCols
; j
++) {
109 tile
[k
] = Bitmap
.createBitmap(tileset
, j
*loadedtileWidth
, i
*loadedtileHeight
,
110 loadedtileWidth
, loadedtileHeight
, matrix
, false);
111 tile
[k
].setDensity(Bitmap
.DENSITY_NONE
);
115 tileWidth
= tile
[0].getWidth();
116 tileHeight
= tile
[0].getHeight();
119 private void loadBackground() {
120 BitmapFactory
.Options ops
= new BitmapFactory
.Options();
121 ops
.inScaled
= false;
122 bg
= BitmapFactory
.decodeResource(getResources(), R
.drawable
.kshisen_bgnd
, ops
);
123 bg
.setDensity(Bitmap
.DENSITY_NONE
);
126 private void registerTimer() {
127 if (timer
!=null) return; // Already registered
128 timerHandler
= new Handler() {
129 public void handleMessage(Message msg
) {
134 timer
.scheduleAtFixedRate(new TimerTask() {
136 timerHandler
.sendEmptyMessage(Activity
.RESULT_OK
);
139 timerRegistered
=true;
142 private void unregisterTimer() {
143 if (timer
==null) return; // Already unregistered
147 timerRegistered
=false;
150 public void pauseTime() {
153 startTime
= System
.currentTimeMillis();
157 public void resumeTime() {
158 startTime
= System
.currentTimeMillis();
162 private void updateTime() {
163 if (cstate
!=StatePlay
.GAMEOVER
) {
164 playTime
= (System
.currentTimeMillis()-startTime
)/1000+baseTime
;
168 private void initializeGame() {
170 screenWidth
=getWidth();
171 screenHeight
=getHeight();
173 //undo.sensitive=false;
174 pstate
=StatePaint
.BOARD
;
176 control(StatePlay
.IDLE
);
177 startTime
=System
.currentTimeMillis();
180 if (app
.timeCounter
&& !timerRegistered
) {
183 pairs
=app
.board
.getPairs(1);
186 public boolean onOptionsItemSelected(MenuItem item
) {
187 // Handle item selection
188 switch (item
.getItemId()) {
190 this.postDelayed(new Runnable() { public void run() { onHintActivate(); } }, 100);
193 this.postDelayed(new Runnable() { public void run() { onUndoActivate(); } }, 100);
196 this.postDelayed(new Runnable() { public void run() { reset(); } }, 100);
207 public void reset() {
208 control(StatePlay
.UNINITIALIZED
);
209 paint(StatePaint
.BOARD
);
212 private void onHintActivate() {
213 if (cstate
!=StatePlay
.GAMEOVER
) {
214 pairs
=app
.board
.getPairs(1);
215 paint(StatePaint
.HINT
);
217 paint(StatePaint
.BOARD
);
218 control(StatePlay
.IDLE
);
222 private void onUndoActivate() {
223 if (app
.board
.getCanUndo()) {
224 if (cstate
==StatePlay
.GAMEOVER
&& app
.timeCounter
&& !timerRegistered
) {
225 // Reprogram the time update that had been
226 // deactivated with the game over status
230 paint(StatePaint
.BOARD
);
231 //undo.sensitive=app.board.getCanUndo();
232 control(StatePlay
.IDLE
);
236 public void onTimeCounterActivate() {
237 if (app
.timeCounter
&& cstate
!=StatePlay
.GAMEOVER
&& !timerRegistered
) {
238 // Reprogram the time update that had been
239 // deactivated with the time_counter=false
244 private void onUpdateTime() {
246 if (!(app
.timeCounter
&& cstate
!=StatePlay
.GAMEOVER
)) {
251 @SuppressWarnings("deprecation")
252 public void drawMessage(Canvas canvas
, int x
, int y
, boolean centered
, String message
, String color
, float textSize
) {
253 Paint paint
= new Paint();
254 paint
.setColor(Color
.parseColor(color
));
255 paint
.setLinearText(true);
256 paint
.setAntiAlias(true);
257 paint
.setTextAlign(centered?Align
.CENTER
:Align
.LEFT
);
258 paint
.setTypeface(Typeface
.SANS_SERIF
);
259 paint
.setFakeBoldText(true);
260 paint
.setTextSize(textSize
);
261 canvas
.drawText(message
, x
, y
, paint
);
264 public void repaint() {
265 if (surfaceHolder
== null) return;
267 if (canvas
== null) canvas
= surfaceHolder
.lockCanvas(null);
268 if (canvas
== null) return;
269 if (cstate
==StatePlay
.UNINITIALIZED
) initializeGame();
270 synchronized (surfaceHolder
) {
274 if (canvas
!= null) {
275 surfaceHolder
.unlockCanvasAndPost(canvas
);
281 protected void doDraw(Canvas canvas
) {
284 // Bitmap buffer = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
285 //Canvas cbuffer = new Canvas(buffer);
286 Canvas cbuffer
= canvas
;
287 if (canvas
== null) return;
289 //super.onDraw(canvas);
291 // Board upper left corner on screen
295 if (app
!=null && app
.board
!=null) {
296 x0
=(screenWidth
-app
.board
.boardSize
[1]*tileWidth
)/2;
297 y0
=(screenHeight
-app
.board
.boardSize
[0]*tileHeight
)/2;
300 int red
= Color
.parseColor("#FF0000");
301 int orange
= Color
.parseColor("#F0C000");
302 Paint paint
= new Paint();
303 paint
.setFlags(Paint
.ANTI_ALIAS_FLAG
);
305 // Background & board painting
315 // Background painting
316 int bgWidth
= bg
.getWidth();
317 int bgHeight
= bg
.getHeight();
318 for (int i
=0; i
<screenHeight
/bgHeight
+1; i
++) {
319 for (int j
=0; j
<screenWidth
/bgWidth
+1; j
++) {
320 cbuffer
.drawBitmap(bg
, j
*bgWidth
, i
*bgHeight
, paint
);
325 // Max visible size: 7x17
326 if (app
!=null && app
.board
!=null) {
327 for (int i
=0;i
<app
.board
.boardSize
[0];i
++) {
328 for (int j
=0;j
<app
.board
.boardSize
[1];j
++) {
329 // Tiles are 56px height, 40px width each
330 char piece
=app
.board
.board
[i
][j
];
332 cbuffer
.drawBitmap(tile
[piece
], x0
+j
*tileWidth
, y0
+i
*tileHeight
, paint
);
340 // Red rectangle for selection 1
346 paint
.setStyle(Style
.STROKE
);
347 paint
.setStrokeCap(Cap
.ROUND
);
348 paint
.setStrokeJoin(Join
.ROUND
);
349 paint
.setStrokeWidth(3);
350 cbuffer
.drawRect(new Rect(
351 x0
+selection1
[1]*tileWidth
-2,
352 y0
+selection1
[0]*tileHeight
-2,
353 x0
+selection1
[1]*tileWidth
-2+tileWidth
+2*2,
354 y0
+selection1
[0]*tileHeight
-2+tileHeight
+2*2),
359 // Red rectangle for selection 2
364 paint
.setStyle(Style
.STROKE
);
365 paint
.setStrokeCap(Cap
.ROUND
);
366 paint
.setStrokeJoin(Join
.ROUND
);
367 paint
.setStrokeWidth(3);
368 cbuffer
.drawRect(new Rect(
369 x0
+selection2
[1]*tileWidth
-2,
370 y0
+selection2
[0]*tileHeight
-2,
371 x0
+selection2
[1]*tileWidth
-2+tileWidth
+2*2,
372 y0
+selection2
[0]*tileHeight
-2+tileHeight
+2*2),
381 paint
.setStyle(Style
.STROKE
);
382 paint
.setStrokeCap(Cap
.ROUND
);
383 paint
.setStrokeJoin(Join
.ROUND
);
384 paint
.setStrokeWidth(3);
388 for (Point p1
: path
) {
391 x0
+p0
.j
*tileWidth
-2+(tileWidth
/2),
392 y0
+p0
.i
*tileHeight
-2+(tileHeight
/2),
393 x0
+p1
.j
*tileWidth
-2+(tileWidth
/2),
394 y0
+p1
.i
*tileHeight
-2+(tileHeight
/2),
403 // Orange hint rectangles
406 if (pairs
!=null && pairs
.size()>0) {
407 Line pair
=pairs
.get(0);
410 path
=app
.board
.getPath(a
,b
);
411 paint
.setColor(orange
);
412 paint
.setStyle(Style
.STROKE
);
413 paint
.setStrokeCap(Cap
.ROUND
);
414 paint
.setStrokeJoin(Join
.ROUND
);
415 paint
.setStrokeWidth(3);
417 cbuffer
.drawRect(new Rect(
420 x0
+a
.j
*tileWidth
-2+tileWidth
+2*2,
421 y0
+a
.i
*tileHeight
-2+tileHeight
+2*2),
426 for (Point p1
: path
) {
429 x0
+p0
.j
*tileWidth
-2+(tileWidth
/2),
430 y0
+p0
.i
*tileHeight
-2+(tileHeight
/2),
431 x0
+p1
.j
*tileWidth
-2+(tileWidth
/2),
432 y0
+p1
.i
*tileHeight
-2+(tileHeight
/2),
440 cbuffer
.drawRect(new Rect(
443 x0
+b
.j
*tileWidth
-2+tileWidth
+2*2,
444 y0
+b
.i
*tileHeight
-2+tileHeight
+2*2),
450 // Win & loose notifications
453 drawMessage(cbuffer
, screenWidth
/2,screenHeight
/2,true,"You Win!", "#FFFFFF", 100);
456 drawMessage(cbuffer
, screenWidth
/2,screenHeight
/2,true,"Game Over", "#FFFFFF", 100);
460 if (app
.timeCounter
) switch (pstate
) {
470 int hours
=(int)(playTime
/(60*60));
471 int minutes
=(int)((playTime
/60)%60);
472 int seconds
=(int)(playTime
%60);
473 String time
=String
.format(Locale
.US
, "%01d:%02d:%02d", hours
, minutes
, seconds
);
475 int timePosX
=screenWidth
-120;
476 int timePosY
=screenHeight
-10;
478 drawMessage(cbuffer
, timePosX
+1,timePosY
+1,false,time
,"#000000",30);
479 drawMessage(cbuffer
, timePosX
,timePosY
,false,time
,"#FFFFFF",30);
485 debugMessage="StatePlay: "+cstate+"\n"+"StatePaint: "+pstate;
486 if (debugMessage!=null && debugMessage.length()>0) {
488 String lines[] = debugMessage.split("\n");
489 for (int i=0; i<lines.length; i++) {
490 drawMessage(cbuffer,1,l,false,lines[i],"#FFFF00",30);
496 // Double buffer dumping
497 // canvas.drawBitmap(buffer, 0, 0, null);
499 } catch (Exception e
) {
506 public boolean onTouchEvent(MotionEvent event
) {
507 if (event
.getAction()==MotionEvent
.ACTION_DOWN
) {
508 onClick(Math
.round(event
.getX()),Math
.round(event
.getY()));
510 return super.onTouchEvent(event
);
513 private void onClick(int x
, int y
) {
515 int i
=(y
-(screenHeight
-app
.board
.boardSize
[0]*tileHeight
)/2)/tileHeight
;
516 int j
=(x
-(screenWidth
-app
.board
.boardSize
[1]*tileWidth
)/2)/tileWidth
;
521 i
<app
.board
.boardSize
[0] &&
522 j
>=0 && j
<app
.board
.boardSize
[1] &&
523 app
.board
.board
[i
][j
]!=0) {
526 paint(StatePaint
.SELECTED1
);
527 control(StatePlay
.SELECTED1
);
531 if (i
>=0 && i
<app
.board
.boardSize
[0] &&
532 j
>=0 && j
<app
.board
.boardSize
[1] &&
533 app
.board
.board
[i
][j
]!=0) {
534 if (i
==selection1
[0] && j
==selection1
[1]) {
535 paint(StatePaint
.BOARD
);
536 control(StatePlay
.IDLE
);
540 paint(StatePaint
.SELECTED2
);
542 Point a
=new Point(selection1
[0],selection1
[1]);
543 Point b
=new Point(selection2
[0],selection2
[1]);
544 path
=app
.board
.getPath(a
,b
);
545 paint(StatePaint
.MATCHED
);
547 paint(StatePaint
.BOARD
);
552 paint(StatePaint
.BOARD
);
554 pairs
=app
.board
.getPairs(1);
555 if (pairs
.size()==0) {
556 if (app
.board
.getNumPieces()==0) {
557 paint(StatePaint
.WIN
);
559 paint(StatePaint
.LOSE
);
561 control(StatePlay
.GAMEOVER
);
563 control(StatePlay
.IDLE
);
565 //undo.sensitive=app.board.getCanUndo();
571 paint(StatePaint
.BOARD
);
574 } catch (Exception e
) {
579 public void surfaceChanged(SurfaceHolder holder
, int format
, int width
,
581 surfaceHolder
= holder
;
582 if (cstate
!=StatePlay
.GAMEOVER
&& app
.timeCounter
&& !timerRegistered
) {
588 public void surfaceCreated(SurfaceHolder holder
) {
589 surfaceHolder
= holder
;
593 public void surfaceDestroyed(SurfaceHolder holder
) {
594 surfaceHolder
= null;
595 if (timerRegistered
) {
602 protected void onDraw(Canvas canvas) {
603 super.onDraw(canvas);
605 if (!initialized) initialize();
607 long currTime = System.currentTimeMillis();
609 a = (float)(currTime - startTime) / (float)duration;
610 if (a > (float)1.0) a = (float)1.0;
612 x = Math.round(nextx*a + prevx*(1-a));
613 y = Math.round(nexty*a + prevy*(1-a));
615 if (a == (float)1.0) computeNextTarget();
617 int bgWidth = bg.getWidth();
618 int bgHeight = bg.getHeight();
619 for (int i=0; i<height/bgHeight+1; i++) {
620 for (int j=0; j<width/bgWidth+1; j++) {
621 canvas.drawBitmap(bg, j*bgWidth, i*bgHeight, paint);
625 canvas.drawBitmap(tile[randomtile], x, y, paint);
631 public boolean onTouchEvent(MotionEvent event) {
632 if (event.getActionMasked()==MotionEvent.ACTION_DOWN) {
633 //computeNextTarget();
634 //nextx=Math.round(event.getX());
635 //nexty=Math.round(event.getY());
637 return super.onTouchEvent(event);
640 private void initialize() {
642 height = getHeight();
644 bg = BitmapFactory.decodeResource(getResources(), R.drawable.kshisen_bgnd);
645 Bitmap tileset = BitmapFactory.decodeResource(getResources(), R.drawable.tileset);
647 // The tile set has 4 rows x 9 columns
650 twidth = tileset.getWidth()/tscols;
651 theight = tileset.getHeight()/tsrows;
652 tile = new Bitmap[tsrows*tscols];
654 for (int i=0; i<tsrows; i++) {
655 for (int j=0; j<tscols; j++) {
656 tile[k] = Bitmap.createBitmap(tileset, j*twidth, i*theight, twidth, theight, null, false);
669 private void computeNextTarget() {
670 startTime = System.currentTimeMillis();
673 nextx = (int) Math.floor(Math.random() * width);
674 nexty = (int) Math.floor(Math.random() * height);
675 randomtile = (int) Math.floor(Math.random() * tile.length);
678 paint.setColor(Color.parseColor("#006666"));
679 paint.setFlags(Paint.ANTI_ALIAS_FLAG);