1 package de
.cwde
.freeshisen
;
3 import java
.lang
.ref
.WeakReference
;
5 import java
.util
.Locale
;
6 import java
.util
.Timer
;
7 import java
.util
.TimerTask
;
9 import android
.app
.Activity
;
10 import android
.content
.Context
;
11 import android
.graphics
.Bitmap
;
12 import android
.graphics
.BitmapFactory
;
13 import android
.graphics
.Canvas
;
14 import android
.graphics
.Color
;
15 import android
.graphics
.Matrix
;
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 static final String INVALID_TIME
= "9:99:99";
33 private static final String COLOR_TEXT
= "#FFFFFF";
34 private static final String COLOR_TEXT_SHADOW
= "#000000";
35 private static final String COLOR_HINT
= "#F0C000";
36 private static final String COLOR_SELECTED
= "#FF0000";
38 private enum StatePlay
{ UNINITIALIZED
, IDLE
, SELECTED1
, SELECTED2
, GAMEOVER
};
39 private enum StatePaint
{ BOARD
, SELECTED1
, SELECTED2
, MATCHED
, WIN
, LOSE
, HINT
, TIME
};
41 private int screenWidth
;
42 private int screenHeight
;
43 private int tilesetRows
;
44 private int tilesetCols
;
45 private int tileHeight
;
46 private int tileWidth
;
48 private Bitmap tile
[];
49 private Point selection1
= new Point(0, 0);
50 private Point selection2
= new Point(0, 0);
51 private List
<Point
> path
= null;
52 private List
<Line
> pairs
= null;
53 private long startTime
;
54 private long playTime
;
55 private long baseTime
;
58 static class hHandler
extends Handler
{
59 private final WeakReference
<ShisenShoView
> mTarget
;
61 hHandler(ShisenShoView target
) {
62 mTarget
= new WeakReference
<ShisenShoView
>(target
);
66 public void handleMessage(Message msg
) {
67 ShisenShoView target
= mTarget
.get();
69 target
.onUpdateTime();
73 private Handler timerHandler
= new hHandler(this);
75 private boolean timerRegistered
= false;
76 private ShisenSho app
;
77 private StatePlay cstate
;
78 private StatePaint pstate
;
79 private Canvas canvas
= null;
80 private SurfaceHolder surfaceHolder
= null;
81 private String time
= INVALID_TIME
;
83 public ShisenShoView(ShisenSho shishenSho
) {
84 super((Context
) shishenSho
);
85 this.app
= shishenSho
;
86 cstate
= StatePlay
.UNINITIALIZED
;
87 surfaceHolder
= getHolder();
88 surfaceHolder
.addCallback(this);
91 public ShisenShoView(Context ctx
) {
96 private void paint(StatePaint pstate
) {
101 private void control(StatePlay cstate
) {
105 private void loadTileset() {
106 BitmapFactory
.Options ops
= new BitmapFactory
.Options();
107 ops
.inScaled
= false;
108 Bitmap tileset
= BitmapFactory
.decodeResource(getResources(), R
.drawable
.tileset
, ops
);
109 tileset
.setDensity(Bitmap
.DENSITY_NONE
);
111 // The tile set has 4 rows x 9 columns
114 int loadedtileWidth
= tileset
.getWidth()/tilesetCols
;
115 int loadedtileHeight
= tileset
.getHeight()/tilesetRows
;
116 tile
= new Bitmap
[tilesetRows
*tilesetCols
];
119 // "large" is 16x6, and we want to have a nice border, so we use 17x7 and
120 // choose the lowest scale so everything fits
121 float scalex
= ((float) screenWidth
/17) / loadedtileWidth
;
122 float scaley
= ((float) screenHeight
/7) / loadedtileHeight
;
123 if (scaley
< scalex
) {
128 Matrix matrix
= new Matrix();
129 matrix
.setScale(scalex
, scaley
);
132 for (int i
=0; i
<tilesetRows
; i
++) {
133 for (int j
=0; j
<tilesetCols
; j
++) {
134 tile
[k
] = Bitmap
.createBitmap(tileset
, j
*loadedtileWidth
, i
*loadedtileHeight
,
135 loadedtileWidth
, loadedtileHeight
, matrix
, false);
136 tile
[k
].setDensity(Bitmap
.DENSITY_NONE
);
140 tileWidth
= tile
[0].getWidth();
141 tileHeight
= tile
[0].getHeight();
144 private void loadBackground() {
145 BitmapFactory
.Options ops
= new BitmapFactory
.Options();
146 ops
.inScaled
= false;
147 bg
= BitmapFactory
.decodeResource(getResources(), R
.drawable
.kshisen_bgnd
, ops
);
148 bg
.setDensity(Bitmap
.DENSITY_NONE
);
151 private void registerTimer() {
153 return; // Already registered
155 timer
.scheduleAtFixedRate(new TimerTask() {
157 timerHandler
.sendEmptyMessage(Activity
.RESULT_OK
);
160 timerRegistered
= true;
163 private void unregisterTimer() {
165 return; // Already unregistered
168 timerRegistered
= false;
171 public void pauseTime() {
174 startTime
= System
.currentTimeMillis();
178 public void resumeTime() {
179 startTime
= System
.currentTimeMillis();
183 private void updateTime() {
184 if (cstate
!=StatePlay
.GAMEOVER
) {
185 playTime
= (System
.currentTimeMillis()-startTime
)/1000+baseTime
;
189 private void initializeGame() {
191 screenWidth
=getWidth();
192 screenHeight
=getHeight();
194 //undo.sensitive=false;
195 pstate
=StatePaint
.BOARD
;
197 control(StatePlay
.IDLE
);
198 startTime
=System
.currentTimeMillis();
201 if (app
.timeCounter
&& !timerRegistered
) {
204 pairs
=app
.board
.getPairs(1);
207 public boolean onOptionsItemSelected(MenuItem item
) {
208 // Handle item selection
209 switch (item
.getItemId()) {
211 this.postDelayed(new Runnable() { public void run() { onHintActivate(); } }, 100);
214 this.postDelayed(new Runnable() { public void run() { onUndoActivate(); } }, 100);
217 this.postDelayed(new Runnable() { public void run() { reset(); } }, 100);
228 public void reset() {
229 control(StatePlay
.UNINITIALIZED
);
230 paint(StatePaint
.BOARD
);
233 private void onHintActivate() {
234 if (cstate
!=StatePlay
.GAMEOVER
) {
235 pairs
=app
.board
.getPairs(1);
236 paint(StatePaint
.HINT
);
238 paint(StatePaint
.BOARD
);
239 control(StatePlay
.IDLE
);
243 private void onUndoActivate() {
244 if (app
.board
.getCanUndo()) {
245 if (cstate
==StatePlay
.GAMEOVER
&& app
.timeCounter
&& !timerRegistered
) {
246 // Reprogram the time update that had been
247 // deactivated with the game over status
251 paint(StatePaint
.BOARD
);
252 //undo.sensitive=app.board.getCanUndo();
253 control(StatePlay
.IDLE
);
257 public void onTimeCounterActivate() {
258 if (app
.timeCounter
&& cstate
!=StatePlay
.GAMEOVER
&& !timerRegistered
) {
259 // Reprogram the time update that had been
260 // deactivated with the time_counter=false
265 private void onUpdateTime() {
267 if (!(app
.timeCounter
&& cstate
!=StatePlay
.GAMEOVER
)) {
272 @SuppressWarnings("deprecation")
273 public static void drawMessage(Canvas canvas
, int x
, int y
,
274 boolean centered
, String message
, float textSize
) {
275 Paint paint
= new Paint();
276 paint
.setLinearText(true);
277 paint
.setAntiAlias(true);
278 paint
.setTextAlign(centered ? Align
.CENTER
: Align
.LEFT
);
279 paint
.setTypeface(Typeface
.SANS_SERIF
);
280 paint
.setFakeBoldText(true);
281 paint
.setTextSize(textSize
);
282 paint
.setColor(Color
.parseColor(COLOR_TEXT_SHADOW
));
283 canvas
.drawText(message
, x
+ 1, y
+ 1, paint
);
284 paint
.setColor(Color
.parseColor(COLOR_TEXT
));
285 canvas
.drawText(message
, x
, y
, paint
);
288 public void repaint() {
289 if (surfaceHolder
== null) return;
291 if (canvas
== null) canvas
= surfaceHolder
.lockCanvas(null);
292 if (canvas
== null) return;
293 if (cstate
==StatePlay
.UNINITIALIZED
) initializeGame();
294 synchronized (surfaceHolder
) {
298 if (canvas
!= null) {
299 surfaceHolder
.unlockCanvasAndPost(canvas
);
305 protected void doDraw(Canvas canvas
) {
307 if (canvas
== null) return;
309 // Board upper left corner on screen
313 if (app
!=null && app
.board
!=null) {
314 x0
=(screenWidth
-app
.board
.boardSize
[1]*tileWidth
)/2;
315 y0
=(screenHeight
-app
.board
.boardSize
[0]*tileHeight
)/2;
318 int selectcolor
= Color
.parseColor(COLOR_SELECTED
);
319 int hintcolor
= Color
.parseColor(COLOR_HINT
);
321 // Background & board painting
331 // Background painting
332 int bgWidth
= bg
.getWidth();
333 int bgHeight
= bg
.getHeight();
334 for (int i
=0; i
<screenHeight
/bgHeight
+1; i
++) {
335 for (int j
=0; j
<screenWidth
/bgWidth
+1; j
++) {
336 canvas
.drawBitmap(bg
, j
*bgWidth
, i
*bgHeight
, null);
341 // Max visible size: 7x17
342 if (app
!=null && app
.board
!=null) {
343 for (int i
=0;i
<app
.board
.boardSize
[0];i
++) {
344 for (int j
=0;j
<app
.board
.boardSize
[1];j
++) {
345 // Tiles are 56px height, 40px width each
346 char piece
=app
.board
.board
[i
][j
];
348 canvas
.drawBitmap(tile
[piece
], x0
+j
*tileWidth
, y0
+i
*tileHeight
, null);
356 // rectangle for selection 1
361 highlightTile(canvas
, x0
, y0
, selection1
, selectcolor
);
365 // rectangle for selection 2
369 highlightTile(canvas
, x0
, y0
, selection2
, selectcolor
);
378 for (Point p1
: path
) {
380 drawLine(canvas
, x0
, y0
, p0
, p1
, selectcolor
);
391 if (pairs
!= null && pairs
.size() > 0) {
392 Line pair
= pairs
.get(0);
395 path
= app
.board
.getPath(a
, b
);
397 highlightTile(canvas
, x0
, y0
, a
, hintcolor
);
401 for (Point p1
: path
) {
403 drawLine(canvas
, x0
, y0
, p0
, p1
, hintcolor
);
410 highlightTile(canvas
, x0
, y0
, b
, hintcolor
);
415 // Win & loose notifications
418 drawMessage(canvas
, screenWidth
/ 2, screenHeight
/ 2, true,
422 drawMessage(canvas
, screenWidth
/ 2, screenHeight
/ 2, true,
437 int hours
= (int) (playTime
/ (60 * 60));
438 int minutes
= (int) ((playTime
/ 60) % 60);
439 int seconds
= (int) (playTime
% 60);
441 time
= String
.format(Locale
.US
, "%01d:%02d:%02d",
442 hours
, minutes
, seconds
);
447 int timePosX
=screenWidth
-120;
448 int timePosY
=screenHeight
-10;
450 if (app
.timeCounter
) {
451 drawMessage(canvas
, timePosX
, timePosY
, false, time
, 30);
456 } catch (Exception e
) {
462 private void drawLine(Canvas canvas
, int x0
, int y0
, Point p0
, Point p1
, int color
) {
463 Paint paint
= new Paint();
464 paint
.setFlags(Paint
.ANTI_ALIAS_FLAG
);
465 paint
.setColor(color
);
466 paint
.setStyle(Style
.STROKE
);
467 paint
.setStrokeCap(Cap
.ROUND
);
468 paint
.setStrokeJoin(Join
.ROUND
);
469 paint
.setStrokeWidth(3);
471 x0
+ p0
.j
* tileWidth
- 2 + (tileWidth
/ 2),
472 y0
+ p0
.i
* tileHeight
- 2 + (tileHeight
/ 2),
473 x0
+ p1
.j
* tileWidth
- 2 + (tileWidth
/ 2),
474 y0
+ p1
.i
* tileHeight
- 2 + (tileHeight
/ 2), paint
);
477 private void highlightTile(Canvas canvas
, int x0
, int y0
, Point p
, int color
) {
478 Paint paint
= new Paint();
479 paint
.setFlags(Paint
.ANTI_ALIAS_FLAG
);
480 paint
.setColor(color
);
481 paint
.setStyle(Style
.STROKE
);
482 paint
.setStrokeCap(Cap
.ROUND
);
483 paint
.setStrokeJoin(Join
.ROUND
);
484 paint
.setStrokeWidth(3);
486 x0
+ p
.j
* tileWidth
- 2,
487 y0
+ p
.i
* tileHeight
- 2,
488 x0
+ p
.j
* tileWidth
+ tileWidth
+ 2,
489 y0
+ p
.i
* tileHeight
+ tileHeight
+ 2);
490 canvas
.drawRect(r
, paint
);
494 public boolean onTouchEvent(MotionEvent event
) {
495 if (event
.getAction()==MotionEvent
.ACTION_DOWN
) {
496 onClick(Math
.round(event
.getX()),Math
.round(event
.getY()));
498 return super.onTouchEvent(event
);
501 private void onClick(int x
, int y
) {
503 int i
=(y
-(screenHeight
-app
.board
.boardSize
[0]*tileHeight
)/2)/tileHeight
;
504 int j
=(x
-(screenWidth
-app
.board
.boardSize
[1]*tileWidth
)/2)/tileWidth
;
508 if (i
>= 0 && i
< app
.board
.boardSize
[0] && j
>= 0
509 && j
< app
.board
.boardSize
[1]
510 && app
.board
.board
[i
][j
] != 0) {
511 selection1
.set(i
, j
);
512 paint(StatePaint
.SELECTED1
);
513 control(StatePlay
.SELECTED1
);
517 if (i
>= 0 && i
< app
.board
.boardSize
[0] && j
>= 0
518 && j
< app
.board
.boardSize
[1]
519 && app
.board
.board
[i
][j
] != 0) {
520 if (selection1
.equals(i
, j
)) {
521 paint(StatePaint
.BOARD
);
522 control(StatePlay
.IDLE
);
524 selection2
.set(i
, j
);
525 paint(StatePaint
.SELECTED2
);
527 Point a
= selection1
.copy();
528 Point b
= selection2
.copy();
529 path
= app
.board
.getPath(a
, b
);
530 paint(StatePaint
.MATCHED
);
532 paint(StatePaint
.BOARD
);
533 if (path
.size() > 0) {
534 app
.board
.play(a
, b
);
537 paint(StatePaint
.BOARD
);
539 pairs
= app
.board
.getPairs(1);
540 if (pairs
.size() == 0) {
541 if (app
.board
.getNumPieces() == 0) {
542 paint(StatePaint
.WIN
);
544 paint(StatePaint
.LOSE
);
546 control(StatePlay
.GAMEOVER
);
548 control(StatePlay
.IDLE
);
550 //undo.sensitive=app.board.getCanUndo();
556 paint(StatePaint
.BOARD
);
559 } catch (Exception e
) {
564 public void surfaceChanged(SurfaceHolder holder
, int format
, int width
,
566 surfaceHolder
= holder
;
567 if (cstate
!=StatePlay
.GAMEOVER
&& app
.timeCounter
&& !timerRegistered
) {
573 public void surfaceCreated(SurfaceHolder holder
) {
574 surfaceHolder
= holder
;
578 public void surfaceDestroyed(SurfaceHolder holder
) {
579 surfaceHolder
= null;
580 if (timerRegistered
) {