]>
Commit | Line | Data |
---|---|---|
92b19250 | 1 | package de.cwde.freeshisen; |
c6f3dff3 | 2 | |
375d3fd8 | 3 | import java.lang.ref.WeakReference; |
c6f3dff3 | 4 | import java.util.List; |
d0e04237 | 5 | import java.util.Locale; |
c6f3dff3 | 6 | import java.util.Timer; |
7 | import java.util.TimerTask; | |
8 | ||
9 | import android.app.Activity; | |
45abc547 | 10 | import android.app.AlertDialog; |
c6f3dff3 | 11 | import android.content.Context; |
45abc547 | 12 | import android.content.SharedPreferences; |
c6f3dff3 | 13 | import android.graphics.Bitmap; |
14 | import android.graphics.BitmapFactory; | |
15 | import android.graphics.Canvas; | |
16 | import android.graphics.Color; | |
c995dcb4 | 17 | import android.graphics.Matrix; |
c6f3dff3 | 18 | import android.graphics.Paint; |
19 | import android.graphics.Paint.Align; | |
20 | import android.graphics.Paint.Cap; | |
21 | import android.graphics.Paint.Join; | |
22 | import android.graphics.Paint.Style; | |
23 | import android.graphics.Rect; | |
24 | import android.graphics.Typeface; | |
25 | import android.os.Handler; | |
26 | import android.os.Message; | |
45abc547 | 27 | import android.preference.PreferenceManager; |
c6f3dff3 | 28 | import android.view.MenuItem; |
29 | import android.view.MotionEvent; | |
30 | import android.view.SurfaceHolder; | |
31 | import android.view.SurfaceView; | |
32 | ||
33 | class ShisenShoView extends SurfaceView implements SurfaceHolder.Callback { | |
34 | ||
95d92876 | 35 | private static final String INVALID_TIME = "9:99:99"; |
1709c0fa | 36 | private static final String COLOR_TEXT = "#FFFFFF"; |
37 | private static final String COLOR_TEXT_SHADOW = "#000000"; | |
38 | private static final String COLOR_HINT = "#F0C000"; | |
39 | private static final String COLOR_SELECTED = "#FF0000"; | |
40 | ||
c6f3dff3 | 41 | private enum StatePlay { UNINITIALIZED, IDLE, SELECTED1, SELECTED2, GAMEOVER }; |
42 | private enum StatePaint { BOARD, SELECTED1, SELECTED2, MATCHED, WIN, LOSE, HINT, TIME }; | |
43 | ||
44 | private int screenWidth; | |
45 | private int screenHeight; | |
46 | private int tilesetRows; | |
47 | private int tilesetCols; | |
48 | private int tileHeight; | |
49 | private int tileWidth; | |
50 | private Bitmap bg; | |
51 | private Bitmap tile[]; | |
375d3fd8 | 52 | private Point selection1 = new Point(0, 0); |
53 | private Point selection2 = new Point(0, 0); | |
54 | private List<Point> path = null; | |
55 | private List<Line> pairs = null; | |
c6f3dff3 | 56 | private long startTime; |
57 | private long playTime; | |
58 | private long baseTime; | |
59 | private Timer timer; | |
c6f3dff3 | 60 | |
375d3fd8 | 61 | static class hHandler extends Handler { |
62 | private final WeakReference<ShisenShoView> mTarget; | |
63 | ||
64 | hHandler(ShisenShoView target) { | |
65 | mTarget = new WeakReference<ShisenShoView>(target); | |
66 | } | |
67 | ||
68 | @Override | |
69 | public void handleMessage(Message msg) { | |
70 | ShisenShoView target = mTarget.get(); | |
71 | if (target != null) | |
72 | target.onUpdateTime(); | |
73 | } | |
74 | } | |
75 | ||
76 | private Handler timerHandler = new hHandler(this); | |
77 | ||
78 | private boolean timerRegistered = false; | |
c6f3dff3 | 79 | private ShisenSho app; |
80 | private StatePlay cstate; | |
81 | private StatePaint pstate; | |
82 | private Canvas canvas = null; | |
83 | private SurfaceHolder surfaceHolder = null; | |
95d92876 | 84 | private String time = INVALID_TIME; |
109ae6fe | 85 | |
c6f3dff3 | 86 | public ShisenShoView(ShisenSho shishenSho) { |
109ae6fe | 87 | super((Context) shishenSho); |
c6f3dff3 | 88 | this.app = shishenSho; |
89 | cstate = StatePlay.UNINITIALIZED; | |
90 | surfaceHolder = getHolder(); | |
91 | surfaceHolder.addCallback(this); | |
92 | } | |
93 | ||
42aa846a | 94 | public ShisenShoView(Context ctx) { |
45abc547 | 95 | super(ctx); |
96 | this.app = (ShisenSho) ctx; | |
97 | cstate = StatePlay.UNINITIALIZED; | |
98 | surfaceHolder = getHolder(); | |
99 | surfaceHolder.addCallback(this); | |
42aa846a | 100 | } |
101 | ||
c6f3dff3 | 102 | private void paint(StatePaint pstate) { |
103 | this.pstate=pstate; | |
104 | repaint(); | |
105 | } | |
106 | ||
107 | private void control(StatePlay cstate) { | |
108 | this.cstate=cstate; | |
109 | } | |
110 | ||
07fd4c9f | 111 | public void loadTileset() { |
c6f3dff3 | 112 | BitmapFactory.Options ops = new BitmapFactory.Options(); |
113 | ops.inScaled = false; | |
07fd4c9f | 114 | Bitmap tileset = BitmapFactory.decodeResource(getResources(), app.tilesetid, ops); |
c6f3dff3 | 115 | tileset.setDensity(Bitmap.DENSITY_NONE); |
116 | ||
117 | // The tile set has 4 rows x 9 columns | |
118 | tilesetRows = 4; | |
119 | tilesetCols = 9; | |
952b2e4d | 120 | int loadedtileWidth = tileset.getWidth()/tilesetCols; |
121 | int loadedtileHeight = tileset.getHeight()/tilesetRows; | |
c6f3dff3 | 122 | tile = new Bitmap[tilesetRows*tilesetCols]; |
655c3517 | 123 | |
952b2e4d | 124 | // align to screen: |
125 | // "large" is 16x6, and we want to have a nice border, so we use 17x7 and | |
126 | // choose the lowest scale so everything fits | |
a42b5381 | 127 | float scalex = ((float) (screenWidth - 2)/17) / loadedtileWidth; |
128 | float scaley = ((float) (screenHeight - 2)/7) / loadedtileHeight; | |
952b2e4d | 129 | if (scaley < scalex) { |
130 | scalex = scaley; | |
131 | } else { | |
132 | scaley = scalex; | |
133 | } | |
c995dcb4 | 134 | Matrix matrix = new Matrix(); |
952b2e4d | 135 | matrix.setScale(scalex, scaley); |
655c3517 | 136 | |
c6f3dff3 | 137 | int k=0; |
138 | for (int i=0; i<tilesetRows; i++) { | |
139 | for (int j=0; j<tilesetCols; j++) { | |
952b2e4d | 140 | tile[k] = Bitmap.createBitmap(tileset, j*loadedtileWidth, i*loadedtileHeight, |
141 | loadedtileWidth, loadedtileHeight, matrix, false); | |
c6f3dff3 | 142 | tile[k].setDensity(Bitmap.DENSITY_NONE); |
143 | k++; | |
144 | } | |
145 | } | |
146 | tileWidth = tile[0].getWidth(); | |
147 | tileHeight = tile[0].getHeight(); | |
148 | } | |
149 | ||
150 | private void loadBackground() { | |
151 | BitmapFactory.Options ops = new BitmapFactory.Options(); | |
152 | ops.inScaled = false; | |
153 | bg = BitmapFactory.decodeResource(getResources(), R.drawable.kshisen_bgnd, ops); | |
154 | bg.setDensity(Bitmap.DENSITY_NONE); | |
155 | } | |
156 | ||
157 | private void registerTimer() { | |
375d3fd8 | 158 | if (timer != null) |
159 | return; // Already registered | |
160 | timer = new Timer(); | |
655c3517 | 161 | timer.scheduleAtFixedRate(new TimerTask() { |
162 | public void run() { | |
163 | timerHandler.sendEmptyMessage(Activity.RESULT_OK); | |
164 | } | |
165 | }, 0, 1000); | |
375d3fd8 | 166 | timerRegistered = true; |
c6f3dff3 | 167 | } |
168 | ||
169 | private void unregisterTimer() { | |
375d3fd8 | 170 | if (timer == null) |
171 | return; // Already unregistered | |
c6f3dff3 | 172 | timer.cancel(); |
655c3517 | 173 | timer = null; |
375d3fd8 | 174 | timerRegistered = false; |
c6f3dff3 | 175 | } |
176 | ||
177 | public void pauseTime() { | |
178 | updateTime(); | |
179 | baseTime = playTime; | |
180 | startTime = System.currentTimeMillis(); | |
181 | ||
182 | } | |
183 | ||
184 | public void resumeTime() { | |
185 | startTime = System.currentTimeMillis(); | |
186 | updateTime(); | |
187 | } | |
188 | ||
189 | private void updateTime() { | |
190 | if (cstate!=StatePlay.GAMEOVER) { | |
191 | playTime = (System.currentTimeMillis()-startTime)/1000+baseTime; | |
192 | } | |
193 | } | |
194 | ||
195 | private void initializeGame() { | |
196 | loadBackground(); | |
c6f3dff3 | 197 | screenWidth=getWidth(); |
198 | screenHeight=getHeight(); | |
c995dcb4 | 199 | loadTileset(); |
c6f3dff3 | 200 | //undo.sensitive=false; |
201 | pstate=StatePaint.BOARD; | |
202 | app.newPlay(); | |
203 | control(StatePlay.IDLE); | |
204 | startTime=System.currentTimeMillis(); | |
205 | playTime=0; | |
206 | baseTime=0; | |
b5d36450 | 207 | if (!timerRegistered) { |
c6f3dff3 | 208 | registerTimer(); |
209 | } | |
210 | pairs=app.board.getPairs(1); | |
211 | } | |
212 | ||
213 | public boolean onOptionsItemSelected(MenuItem item) { | |
655c3517 | 214 | // Handle item selection |
215 | switch (item.getItemId()) { | |
216 | case R.id.hint: | |
217 | this.postDelayed(new Runnable() { public void run() { onHintActivate(); } }, 100); | |
218 | return true; | |
219 | case R.id.undo: | |
220 | this.postDelayed(new Runnable() { public void run() { onUndoActivate(); } }, 100); | |
221 | return true; | |
222 | case R.id.clean: | |
223 | this.postDelayed(new Runnable() { public void run() { reset(); } }, 100); | |
224 | return true; | |
225 | case R.id.options: | |
226 | return true; | |
227 | case R.id.about: | |
228 | return true; | |
229 | default: | |
230 | return false; | |
231 | } | |
c6f3dff3 | 232 | } |
233 | ||
234 | public void reset() { | |
235 | control(StatePlay.UNINITIALIZED); | |
236 | paint(StatePaint.BOARD); | |
237 | } | |
238 | ||
239 | private void onHintActivate() { | |
240 | if (cstate!=StatePlay.GAMEOVER) { | |
241 | pairs=app.board.getPairs(1); | |
242 | paint(StatePaint.HINT); | |
243 | app.sleep(10); | |
244 | paint(StatePaint.BOARD); | |
245 | control(StatePlay.IDLE); | |
246 | } | |
247 | } | |
248 | ||
249 | private void onUndoActivate() { | |
250 | if (app.board.getCanUndo()) { | |
b5d36450 | 251 | if (cstate==StatePlay.GAMEOVER && !timerRegistered) { |
c6f3dff3 | 252 | // Reprogram the time update that had been |
253 | // deactivated with the game over status | |
254 | registerTimer(); | |
255 | } | |
256 | app.board.undo(); | |
257 | paint(StatePaint.BOARD); | |
258 | //undo.sensitive=app.board.getCanUndo(); | |
259 | control(StatePlay.IDLE); | |
260 | } | |
261 | } | |
262 | ||
263 | public void onTimeCounterActivate() { | |
b5d36450 | 264 | if (cstate!=StatePlay.GAMEOVER && !timerRegistered) { |
c6f3dff3 | 265 | // Reprogram the time update that had been |
266 | // deactivated with the time_counter=false | |
267 | registerTimer(); | |
268 | } | |
269 | } | |
270 | ||
271 | private void onUpdateTime() { | |
272 | paint(pstate); | |
b5d36450 | 273 | if (cstate==StatePlay.GAMEOVER) { |
655c3517 | 274 | unregisterTimer(); |
275 | } | |
c6f3dff3 | 276 | } |
277 | ||
42aa846a | 278 | @SuppressWarnings("deprecation") |
1709c0fa | 279 | public static void drawMessage(Canvas canvas, int x, int y, |
280 | boolean centered, String message, float textSize) { | |
c6f3dff3 | 281 | Paint paint = new Paint(); |
c6f3dff3 | 282 | paint.setLinearText(true); |
283 | paint.setAntiAlias(true); | |
1709c0fa | 284 | paint.setTextAlign(centered ? Align.CENTER : Align.LEFT); |
c6f3dff3 | 285 | paint.setTypeface(Typeface.SANS_SERIF); |
286 | paint.setFakeBoldText(true); | |
287 | paint.setTextSize(textSize); | |
1709c0fa | 288 | paint.setColor(Color.parseColor(COLOR_TEXT_SHADOW)); |
289 | canvas.drawText(message, x + 1, y + 1, paint); | |
290 | paint.setColor(Color.parseColor(COLOR_TEXT)); | |
c6f3dff3 | 291 | canvas.drawText(message, x, y, paint); |
292 | } | |
293 | ||
294 | public void repaint() { | |
295 | if (surfaceHolder == null) return; | |
296 | try { | |
297 | if (canvas == null) canvas = surfaceHolder.lockCanvas(null); | |
298 | if (canvas == null) return; | |
299 | if (cstate==StatePlay.UNINITIALIZED) initializeGame(); | |
300 | synchronized (surfaceHolder) { | |
301 | doDraw(canvas); | |
302 | } | |
303 | } finally { | |
304 | if (canvas != null) { | |
305 | surfaceHolder.unlockCanvasAndPost(canvas); | |
306 | canvas = null; | |
307 | } | |
308 | } | |
309 | } | |
310 | ||
311 | protected void doDraw(Canvas canvas) { | |
312 | try { | |
c6f3dff3 | 313 | if (canvas == null) return; |
314 | ||
c6f3dff3 | 315 | // Board upper left corner on screen |
316 | int x0=0; | |
317 | int y0=0; | |
318 | ||
319 | if (app!=null && app.board!=null) { | |
320 | x0=(screenWidth-app.board.boardSize[1]*tileWidth)/2; | |
321 | y0=(screenHeight-app.board.boardSize[0]*tileHeight)/2; | |
322 | } | |
323 | ||
1709c0fa | 324 | int selectcolor = Color.parseColor(COLOR_SELECTED); |
325 | int hintcolor = Color.parseColor(COLOR_HINT); | |
c6f3dff3 | 326 | |
327 | // Background & board painting | |
328 | switch (pstate) { | |
329 | case BOARD: | |
330 | case SELECTED1: | |
331 | case SELECTED2: | |
332 | case MATCHED: | |
333 | case WIN: | |
334 | case LOSE: | |
335 | case HINT: | |
336 | case TIME: | |
337 | // Background painting | |
338 | int bgWidth = bg.getWidth(); | |
339 | int bgHeight = bg.getHeight(); | |
340 | for (int i=0; i<screenHeight/bgHeight+1; i++) { | |
341 | for (int j=0; j<screenWidth/bgWidth+1; j++) { | |
c12aff68 | 342 | canvas.drawBitmap(bg, j*bgWidth, i*bgHeight, null); |
c6f3dff3 | 343 | } |
344 | } | |
345 | ||
346 | // Board painting | |
347 | // Max visible size: 7x17 | |
348 | if (app!=null && app.board!=null) { | |
349 | for (int i=0;i<app.board.boardSize[0];i++) { | |
350 | for (int j=0;j<app.board.boardSize[1];j++) { | |
351 | // Tiles are 56px height, 40px width each | |
352 | char piece=app.board.board[i][j]; | |
353 | if (piece!=0) { | |
c12aff68 | 354 | canvas.drawBitmap(tile[piece], x0+j*tileWidth, y0+i*tileHeight, null); |
c6f3dff3 | 355 | } |
356 | } | |
357 | } | |
358 | } | |
359 | break; | |
360 | } | |
361 | ||
95d92876 | 362 | // rectangle for selection 1 |
c6f3dff3 | 363 | switch (pstate) { |
364 | case SELECTED1: | |
365 | case SELECTED2: | |
366 | case MATCHED: | |
95d92876 | 367 | highlightTile(canvas, x0, y0, selection1, selectcolor); |
c6f3dff3 | 368 | break; |
369 | } | |
370 | ||
95d92876 | 371 | // rectangle for selection 2 |
c6f3dff3 | 372 | switch (pstate) { |
373 | case SELECTED2: | |
374 | case MATCHED: | |
95d92876 | 375 | highlightTile(canvas, x0, y0, selection2, selectcolor); |
c6f3dff3 | 376 | break; |
377 | } | |
378 | ||
379 | // Matching path | |
380 | switch (pstate) { | |
381 | case MATCHED: | |
c6f3dff3 | 382 | if (path!=null) { |
383 | Point p0=null; | |
384 | for (Point p1 : path) { | |
385 | if (p0!=null) { | |
c12aff68 | 386 | drawLine(canvas, x0, y0, p0, p1, selectcolor); |
c6f3dff3 | 387 | } |
388 | p0=p1; | |
389 | } | |
390 | } | |
391 | break; | |
392 | } | |
393 | ||
95d92876 | 394 | // hint rectangles |
c6f3dff3 | 395 | switch (pstate) { |
396 | case HINT: | |
109ae6fe | 397 | if (pairs != null && pairs.size() > 0) { |
398 | Line pair = pairs.get(0); | |
399 | Point a = pair.a; | |
400 | Point b = pair.b; | |
401 | path = app.board.getPath(a, b); | |
c6f3dff3 | 402 | |
95d92876 | 403 | highlightTile(canvas, x0, y0, a, hintcolor); |
c6f3dff3 | 404 | |
109ae6fe | 405 | if (path != null) { |
406 | Point p0 = null; | |
c6f3dff3 | 407 | for (Point p1 : path) { |
109ae6fe | 408 | if (p0 != null) { |
c12aff68 | 409 | drawLine(canvas, x0, y0, p0, p1, hintcolor); |
c6f3dff3 | 410 | } |
109ae6fe | 411 | p0 = p1; |
c6f3dff3 | 412 | } |
109ae6fe | 413 | path = null; |
c6f3dff3 | 414 | } |
415 | ||
95d92876 | 416 | highlightTile(canvas, x0, y0, b, hintcolor); |
c6f3dff3 | 417 | } |
418 | break; | |
419 | } | |
420 | ||
421 | // Win & loose notifications | |
422 | switch (pstate) { | |
423 | case WIN: | |
95d92876 | 424 | drawMessage(canvas, screenWidth / 2, screenHeight / 2, true, |
1709c0fa | 425 | "You Win!", 100); |
c6f3dff3 | 426 | break; |
427 | case LOSE: | |
95d92876 | 428 | drawMessage(canvas, screenWidth / 2, screenHeight / 2, true, |
1709c0fa | 429 | "Game Over", 100); |
c6f3dff3 | 430 | break; |
431 | } | |
432 | ||
95d92876 | 433 | switch (pstate) { |
c6f3dff3 | 434 | case BOARD: |
435 | case SELECTED1: | |
436 | case SELECTED2: | |
437 | case MATCHED: | |
438 | case WIN: | |
439 | case LOSE: | |
440 | case HINT: | |
441 | case TIME: | |
442 | updateTime(); | |
1709c0fa | 443 | int hours = (int) (playTime / (60 * 60)); |
444 | int minutes = (int) ((playTime / 60) % 60); | |
445 | int seconds = (int) (playTime % 60); | |
95d92876 | 446 | if (hours < 10) { |
447 | time = String.format(Locale.US, "%01d:%02d:%02d", | |
448 | hours, minutes, seconds); | |
449 | } else { | |
450 | time = INVALID_TIME; | |
451 | } | |
c6f3dff3 | 452 | |
453 | int timePosX=screenWidth-120; | |
454 | int timePosY=screenHeight-10; | |
95d92876 | 455 | |
456 | if (app.timeCounter) { | |
457 | drawMessage(canvas, timePosX, timePosY, false, time, 30); | |
c6f3dff3 | 458 | } |
95d92876 | 459 | break; |
c6f3dff3 | 460 | } |
c6f3dff3 | 461 | |
462 | } catch (Exception e) { | |
463 | e.printStackTrace(); | |
464 | } | |
465 | ||
466 | } | |
467 | ||
c12aff68 | 468 | private void drawLine(Canvas canvas, int x0, int y0, Point p0, Point p1, int color) { |
469 | Paint paint = new Paint(); | |
470 | paint.setFlags(Paint.ANTI_ALIAS_FLAG); | |
471 | paint.setColor(color); | |
472 | paint.setStyle(Style.STROKE); | |
473 | paint.setStrokeCap(Cap.ROUND); | |
474 | paint.setStrokeJoin(Join.ROUND); | |
475 | paint.setStrokeWidth(3); | |
95d92876 | 476 | canvas.drawLine( |
109ae6fe | 477 | x0 + p0.j * tileWidth - 2 + (tileWidth / 2), |
478 | y0 + p0.i * tileHeight - 2 + (tileHeight / 2), | |
479 | x0 + p1.j * tileWidth - 2 + (tileWidth / 2), | |
480 | y0 + p1.i * tileHeight - 2 + (tileHeight / 2), paint); | |
481 | } | |
482 | ||
95d92876 | 483 | private void highlightTile(Canvas canvas, int x0, int y0, Point p, int color) { |
109ae6fe | 484 | Paint paint = new Paint(); |
485 | paint.setFlags(Paint.ANTI_ALIAS_FLAG); | |
486 | paint.setColor(color); | |
487 | paint.setStyle(Style.STROKE); | |
488 | paint.setStrokeCap(Cap.ROUND); | |
489 | paint.setStrokeJoin(Join.ROUND); | |
490 | paint.setStrokeWidth(3); | |
491 | Rect r = new Rect( | |
492 | x0 + p.j * tileWidth - 2, | |
493 | y0 + p.i * tileHeight - 2, | |
95d92876 | 494 | x0 + p.j * tileWidth + tileWidth + 2, |
495 | y0 + p.i * tileHeight + tileHeight + 2); | |
496 | canvas.drawRect(r, paint); | |
109ae6fe | 497 | } |
498 | ||
c6f3dff3 | 499 | @Override |
500 | public boolean onTouchEvent(MotionEvent event) { | |
501 | if (event.getAction()==MotionEvent.ACTION_DOWN) { | |
502 | onClick(Math.round(event.getX()),Math.round(event.getY())); | |
503 | } | |
504 | return super.onTouchEvent(event); | |
505 | } | |
506 | ||
507 | private void onClick(int x, int y) { | |
508 | try { | |
509 | int i=(y-(screenHeight-app.board.boardSize[0]*tileHeight)/2)/tileHeight; | |
510 | int j=(x-(screenWidth-app.board.boardSize[1]*tileWidth)/2)/tileWidth; | |
511 | ||
512 | switch (cstate) { | |
513 | case IDLE: | |
109ae6fe | 514 | if (i >= 0 && i < app.board.boardSize[0] && j >= 0 |
515 | && j < app.board.boardSize[1] | |
516 | && app.board.board[i][j] != 0) { | |
517 | selection1.set(i, j); | |
c6f3dff3 | 518 | paint(StatePaint.SELECTED1); |
519 | control(StatePlay.SELECTED1); | |
520 | } | |
521 | break; | |
522 | case SELECTED1: | |
109ae6fe | 523 | if (i >= 0 && i < app.board.boardSize[0] && j >= 0 |
524 | && j < app.board.boardSize[1] | |
525 | && app.board.board[i][j] != 0) { | |
526 | if (selection1.equals(i, j)) { | |
c6f3dff3 | 527 | paint(StatePaint.BOARD); |
528 | control(StatePlay.IDLE); | |
529 | } else { | |
109ae6fe | 530 | selection2.set(i, j); |
c6f3dff3 | 531 | paint(StatePaint.SELECTED2); |
532 | ||
109ae6fe | 533 | Point a = selection1.copy(); |
534 | Point b = selection2.copy(); | |
535 | path = app.board.getPath(a, b); | |
c6f3dff3 | 536 | paint(StatePaint.MATCHED); |
537 | app.sleep(2); | |
538 | paint(StatePaint.BOARD); | |
109ae6fe | 539 | if (path.size() > 0) { |
540 | app.board.play(a, b); | |
c6f3dff3 | 541 | } |
109ae6fe | 542 | path = null; |
c6f3dff3 | 543 | paint(StatePaint.BOARD); |
544 | ||
109ae6fe | 545 | pairs = app.board.getPairs(1); |
546 | if (pairs.size() == 0) { | |
547 | if (app.board.getNumPieces() == 0) { | |
c6f3dff3 | 548 | paint(StatePaint.WIN); |
45abc547 | 549 | checkforhiscore(); |
c6f3dff3 | 550 | } else { |
551 | paint(StatePaint.LOSE); | |
552 | } | |
553 | control(StatePlay.GAMEOVER); | |
554 | } else { | |
555 | control(StatePlay.IDLE); | |
556 | } | |
557 | //undo.sensitive=app.board.getCanUndo(); | |
558 | } | |
559 | } | |
560 | break; | |
561 | case GAMEOVER: | |
562 | reset(); | |
563 | paint(StatePaint.BOARD); | |
564 | break; | |
565 | } | |
566 | } catch (Exception e) { | |
567 | e.printStackTrace(); | |
568 | } | |
569 | } | |
570 | ||
45abc547 | 571 | private void checkforhiscore() { |
572 | if (timerRegistered) { | |
573 | unregisterTimer(); | |
574 | } | |
575 | final String[] sizes = { "S", "M", "L" }; | |
576 | final String[] diffs = { "E", "H" }; | |
577 | String prefname1 = "hiscore_" + diffs[app.difficulty-1] + sizes[app.size-1] + "1"; | |
578 | String prefname2 = "hiscore_" + diffs[app.difficulty-1] + sizes[app.size-1] + "2"; | |
579 | // get hiscores for current size/difficulty | |
580 | SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(app); | |
581 | String besttime1 = sp.getString(prefname1, INVALID_TIME); | |
582 | String besttime2 = sp.getString(prefname2, INVALID_TIME); | |
583 | // did we win something? | |
584 | if (time.compareTo(besttime2) < 0) { | |
585 | // score! | |
586 | new AlertDialog.Builder(app.activity) | |
587 | .setTitle("Hiscore!") | |
588 | .setCancelable(true) | |
589 | .setIcon(R.drawable.icon) | |
590 | .setPositiveButton(app.getString(android.R.string.ok), null) | |
591 | .setMessage("You've made the highscore list!").create() // FIXME: hardcoded string | |
592 | .show(); | |
593 | ||
594 | SharedPreferences.Editor editor = sp.edit(); | |
595 | if (time.compareTo(besttime1) < 0) { | |
596 | editor.putString(prefname1, time); | |
597 | editor.putString(prefname2, besttime1); | |
598 | } else { | |
599 | editor.putString(prefname2, time); | |
600 | } | |
601 | editor.commit(); | |
602 | } | |
603 | } | |
604 | ||
c6f3dff3 | 605 | public void surfaceChanged(SurfaceHolder holder, int format, int width, |
606 | int height) { | |
607 | surfaceHolder = holder; | |
b5d36450 | 608 | if (cstate!=StatePlay.GAMEOVER && !timerRegistered) { |
c6f3dff3 | 609 | registerTimer(); |
610 | } | |
611 | repaint(); | |
612 | } | |
613 | ||
614 | public void surfaceCreated(SurfaceHolder holder) { | |
615 | surfaceHolder = holder; | |
616 | repaint(); | |
617 | } | |
618 | ||
619 | public void surfaceDestroyed(SurfaceHolder holder) { | |
620 | surfaceHolder = null; | |
621 | if (timerRegistered) { | |
622 | unregisterTimer(); | |
623 | } | |
624 | } | |
42aa846a | 625 | } |