Packard Bell Vibe 500: More plugin keymaps (second portion).
[kugel-rb.git] / apps / plugins / minesweeper.c
blob7b26169da7a9b167c0d29d7bff1618fac1f482af
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2004-2006 Antoine Cellerier <dionoea -at- videolan -dot- org>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "plugin.h"
24 #ifdef HAVE_LCD_BITMAP
26 #include "lib/playback_control.h"
28 PLUGIN_HEADER
30 /* what the minesweeper() function can return */
31 enum minesweeper_status {
32 MINESWEEPER_WIN,
33 MINESWEEPER_LOSE,
34 MINESWEEPER_QUIT,
35 MINESWEEPER_USB
38 /* variable button definitions */
39 #if CONFIG_KEYPAD == RECORDER_PAD
40 # define MINESWP_LEFT BUTTON_LEFT
41 # define MINESWP_RIGHT BUTTON_RIGHT
42 # define MINESWP_UP BUTTON_UP
43 # define MINESWP_DOWN BUTTON_DOWN
44 # define MINESWP_QUIT BUTTON_OFF
45 # define MINESWP_TOGGLE BUTTON_ON
46 # define MINESWP_TOGGLE2 BUTTON_F1
47 # define MINESWP_DISCOVER BUTTON_PLAY
48 # define MINESWP_DISCOVER2 BUTTON_F2
49 # define MINESWP_INFO BUTTON_F3
51 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
52 # define MINESWP_LEFT BUTTON_LEFT
53 # define MINESWP_RIGHT BUTTON_RIGHT
54 # define MINESWP_UP BUTTON_UP
55 # define MINESWP_DOWN BUTTON_DOWN
56 # define MINESWP_QUIT BUTTON_OFF
57 # define MINESWP_TOGGLE BUTTON_ON
58 # define MINESWP_TOGGLE2 BUTTON_F1
59 # define MINESWP_DISCOVER BUTTON_SELECT
60 # define MINESWP_DISCOVER2 BUTTON_F2
61 # define MINESWP_INFO BUTTON_F3
63 #elif CONFIG_KEYPAD == ONDIO_PAD
64 # define MINESWP_LEFT BUTTON_LEFT
65 # define MINESWP_RIGHT BUTTON_RIGHT
66 # define MINESWP_UP BUTTON_UP
67 # define MINESWP_DOWN BUTTON_DOWN
68 # define MINESWP_QUIT BUTTON_OFF
69 # define MINESWP_TOGGLE_PRE BUTTON_MENU
70 # define MINESWP_TOGGLE (BUTTON_MENU | BUTTON_REL)
71 # define MINESWP_DISCOVER (BUTTON_MENU | BUTTON_REPEAT)
72 # define MINESWP_INFO (BUTTON_MENU | BUTTON_OFF)
74 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
75 (CONFIG_KEYPAD == IRIVER_H300_PAD)
76 # define MINESWP_LEFT BUTTON_LEFT
77 # define MINESWP_RIGHT BUTTON_RIGHT
78 # define MINESWP_UP BUTTON_UP
79 # define MINESWP_DOWN BUTTON_DOWN
80 # define MINESWP_QUIT BUTTON_OFF
81 # define MINESWP_TOGGLE BUTTON_ON
82 # define MINESWP_TOGGLE2 BUTTON_REC
83 # define MINESWP_DISCOVER BUTTON_SELECT
84 # define MINESWP_INFO BUTTON_MODE
86 # define MINESWP_RC_QUIT BUTTON_RC_STOP
88 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
89 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
90 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
91 # define MINESWP_SCROLLWHEEL
92 # define MINESWP_LEFT BUTTON_LEFT
93 # define MINESWP_RIGHT BUTTON_RIGHT
94 # define MINESWP_UP BUTTON_MENU
95 # define MINESWP_DOWN BUTTON_PLAY
96 # define MINESWP_NEXT BUTTON_SCROLL_FWD
97 # define MINESWP_PREV BUTTON_SCROLL_BACK
98 # define MINESWP_QUIT (BUTTON_SELECT | BUTTON_MENU)
99 # define MINESWP_TOGGLE_PRE BUTTON_SELECT
100 # define MINESWP_TOGGLE (BUTTON_SELECT | BUTTON_REL)
101 # define MINESWP_DISCOVER (BUTTON_SELECT | BUTTON_REPEAT)
102 # define MINESWP_INFO (BUTTON_SELECT | BUTTON_PLAY)
104 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
105 # define MINESWP_LEFT BUTTON_LEFT
106 # define MINESWP_RIGHT BUTTON_RIGHT
107 # define MINESWP_UP BUTTON_UP
108 # define MINESWP_DOWN BUTTON_DOWN
109 # define MINESWP_QUIT BUTTON_POWER
110 # define MINESWP_TOGGLE BUTTON_PLAY
111 # define MINESWP_DISCOVER BUTTON_SELECT
112 # define MINESWP_INFO BUTTON_REC
114 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
115 # define MINESWP_LEFT BUTTON_LEFT
116 # define MINESWP_RIGHT BUTTON_RIGHT
117 # define MINESWP_UP BUTTON_UP
118 # define MINESWP_DOWN BUTTON_DOWN
119 # define MINESWP_QUIT BUTTON_POWER
120 # define MINESWP_TOGGLE BUTTON_A
121 # define MINESWP_DISCOVER BUTTON_SELECT
122 # define MINESWP_INFO BUTTON_MENU
124 #elif (CONFIG_KEYPAD == SANSA_E200_PAD)
126 # define MINESWP_SCROLLWHEEL
127 # define MINESWP_LEFT BUTTON_LEFT
128 # define MINESWP_RIGHT BUTTON_RIGHT
129 # define MINESWP_UP BUTTON_UP
130 # define MINESWP_DOWN BUTTON_DOWN
131 # define MINESWP_QUIT BUTTON_POWER
132 # define MINESWP_NEXT BUTTON_SCROLL_FWD
133 # define MINESWP_PREV BUTTON_SCROLL_BACK
134 # define MINESWP_TOGGLE BUTTON_REC
135 # define MINESWP_DISCOVER BUTTON_SELECT
136 # define MINESWP_INFO (BUTTON_REC|BUTTON_REPEAT)
138 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
140 # define MINESWP_LEFT BUTTON_LEFT
141 # define MINESWP_RIGHT BUTTON_RIGHT
142 # define MINESWP_UP BUTTON_UP
143 # define MINESWP_DOWN BUTTON_DOWN
144 # define MINESWP_QUIT (BUTTON_HOME|BUTTON_REPEAT)
145 # define MINESWP_TOGGLE BUTTON_SCROLL_FWD
146 # define MINESWP_DISCOVER BUTTON_SELECT
147 # define MINESWP_INFO BUTTON_SCROLL_BACK
149 #elif (CONFIG_KEYPAD == SANSA_C200_PAD) || \
150 (CONFIG_KEYPAD == SANSA_CLIP_PAD) || \
151 (CONFIG_KEYPAD == SANSA_M200_PAD)
152 # define MINESWP_LEFT BUTTON_LEFT
153 # define MINESWP_RIGHT BUTTON_RIGHT
154 # define MINESWP_UP BUTTON_UP
155 # define MINESWP_DOWN BUTTON_DOWN
156 # define MINESWP_QUIT BUTTON_POWER
157 # define MINESWP_TOGGLE_PRE BUTTON_SELECT
158 # define MINESWP_TOGGLE (BUTTON_SELECT | BUTTON_REL)
159 # define MINESWP_TOGGLE2 BUTTON_VOL_DOWN
160 # define MINESWP_DISCOVER (BUTTON_SELECT | BUTTON_REPEAT)
161 # define MINESWP_DISCOVER2 BUTTON_VOL_UP
162 # define MINESWP_INFO (BUTTON_SELECT | BUTTON_UP)
164 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
165 # define MINESWP_LEFT BUTTON_LEFT
166 # define MINESWP_RIGHT BUTTON_RIGHT
167 # define MINESWP_UP BUTTON_SCROLL_UP
168 # define MINESWP_DOWN BUTTON_SCROLL_DOWN
169 # define MINESWP_QUIT BUTTON_POWER
170 # define MINESWP_TOGGLE BUTTON_PLAY
171 # define MINESWP_DISCOVER BUTTON_REW
172 # define MINESWP_INFO (BUTTON_REW | BUTTON_PLAY)
174 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
175 # define MINESWP_LEFT BUTTON_LEFT
176 # define MINESWP_RIGHT BUTTON_RIGHT
177 # define MINESWP_UP BUTTON_UP
178 # define MINESWP_DOWN BUTTON_DOWN
179 # define MINESWP_QUIT BUTTON_BACK
180 # define MINESWP_TOGGLE BUTTON_PLAY
181 # define MINESWP_DISCOVER BUTTON_SELECT
182 # define MINESWP_INFO BUTTON_MENU
184 #elif (CONFIG_KEYPAD == MROBE100_PAD)
185 # define MINESWP_LEFT BUTTON_LEFT
186 # define MINESWP_RIGHT BUTTON_RIGHT
187 # define MINESWP_UP BUTTON_UP
188 # define MINESWP_DOWN BUTTON_DOWN
189 # define MINESWP_QUIT BUTTON_POWER
190 # define MINESWP_TOGGLE BUTTON_DISPLAY
191 # define MINESWP_DISCOVER BUTTON_SELECT
192 # define MINESWP_INFO BUTTON_MENU
194 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
195 # define MINESWP_LEFT BUTTON_RC_REW
196 # define MINESWP_RIGHT BUTTON_RC_FF
197 # define MINESWP_UP BUTTON_RC_VOL_UP
198 # define MINESWP_DOWN BUTTON_RC_VOL_DOWN
199 # define MINESWP_QUIT BUTTON_RC_REC
200 # define MINESWP_TOGGLE BUTTON_RC_MODE
201 # define MINESWP_DISCOVER BUTTON_RC_PLAY
202 # define MINESWP_INFO BUTTON_RC_MENU
204 #elif (CONFIG_KEYPAD == COWON_D2_PAD)
205 # define MINESWP_QUIT BUTTON_POWER
207 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
208 # define MINESWP_LEFT BUTTON_LEFT
209 # define MINESWP_RIGHT BUTTON_RIGHT
210 # define MINESWP_UP BUTTON_UP
211 # define MINESWP_DOWN BUTTON_DOWN
212 # define MINESWP_QUIT BUTTON_BACK
213 # define MINESWP_TOGGLE BUTTON_SELECT
214 # define MINESWP_DISCOVER BUTTON_PLAY
215 # define MINESWP_INFO BUTTON_MENU
217 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
218 # define MINESWP_LEFT BUTTON_LEFT
219 # define MINESWP_RIGHT BUTTON_RIGHT
220 # define MINESWP_UP BUTTON_UP
221 # define MINESWP_DOWN BUTTON_DOWN
222 # define MINESWP_QUIT BUTTON_POWER
223 # define MINESWP_TOGGLE BUTTON_VIEW
224 # define MINESWP_DISCOVER BUTTON_SELECT
225 # define MINESWP_INFO BUTTON_MENU
227 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
228 # define MINESWP_LEFT BUTTON_PREV
229 # define MINESWP_RIGHT BUTTON_NEXT
230 # define MINESWP_UP BUTTON_UP
231 # define MINESWP_DOWN BUTTON_DOWN
232 # define MINESWP_QUIT BUTTON_POWER
233 # define MINESWP_TOGGLE BUTTON_RIGHT
234 # define MINESWP_DISCOVER BUTTON_PLAY
235 # define MINESWP_INFO BUTTON_MENU
237 #elif (CONFIG_KEYPAD == ONDAVX747_PAD) || \
238 CONFIG_KEYPAD == ONDAVX777_PAD || \
239 CONFIG_KEYPAD == MROBE500_PAD
240 # define MINESWP_QUIT BUTTON_POWER
242 #elif (CONFIG_KEYPAD == SAMSUNG_YH_PAD)
243 # define MINESWP_LEFT BUTTON_LEFT
244 # define MINESWP_RIGHT BUTTON_RIGHT
245 # define MINESWP_UP BUTTON_UP
246 # define MINESWP_DOWN BUTTON_DOWN
247 # define MINESWP_QUIT BUTTON_REC
248 # define MINESWP_TOGGLE BUTTON_PLAY
249 # define MINESWP_DISCOVER BUTTON_REW
250 # define MINESWP_INFO BUTTON_FFWD
252 #elif (CONFIG_KEYPAD == PBELL_VIBE500_PAD)
253 # define MINESWP_LEFT BUTTON_PREV
254 # define MINESWP_RIGHT BUTTON_NEXT
255 # define MINESWP_UP BUTTON_UP
256 # define MINESWP_DOWN BUTTON_DOWN
257 # define MINESWP_QUIT BUTTON_REC
258 # define MINESWP_TOGGLE BUTTON_PLAY
259 # define MINESWP_DISCOVER BUTTON_OK
260 # define MINESWP_INFO BUTTON_MENU
262 #else
263 #error No keymap defined!
264 #endif
266 #ifdef HAVE_TOUCHSCREEN
267 #ifndef MINESWP_QUIT
268 # define MINESWP_QUIT BUTTON_TOPLEFT
269 #endif
270 #ifndef MINESWP_LEFT
271 # define MINESWP_LEFT BUTTON_MIDLEFT
272 #endif
273 #ifndef MINESWP_RIGHT
274 # define MINESWP_RIGHT BUTTON_MIDRIGHT
275 #endif
276 #ifndef MINESWP_UP
277 # define MINESWP_UP BUTTON_TOPMIDDLE
278 #endif
279 #ifndef MINESWP_DOWN
280 # define MINESWP_DOWN BUTTON_BOTTOMMIDDLE
281 #endif
282 #ifndef MINESWP_TOGGLE
283 # define MINESWP_TOGGLE BUTTON_CENTER
284 #endif
285 #ifndef MINESWP_DISCOVER
286 # define MINESWP_DISCOVER BUTTON_BOTTOMLEFT
287 #endif
288 #ifndef MINESWP_INFO
289 # define MINESWP_INFO BUTTON_BOTTOMRIGHT
290 #endif
291 #endif
293 extern const fb_data minesweeper_tiles[];
295 #ifdef HAVE_LCD_COLOR
296 # if ( LCD_HEIGHT * LCD_WIDTH ) / ( 16 * 16 ) >= 130
297 /* We want to have at least 130 tiles on the screen */
298 # define TileSize 16
299 # elif ( LCD_HEIGHT * LCD_WIDTH ) / ( 12 * 12 ) >= 130
300 # define TileSize 12
301 # else
302 # define TileSize 10
303 # endif
304 # define BackgroundColor LCD_RGBPACK( 128, 128, 128 )
305 #elif LCD_DEPTH > 1
306 # define TileSize 12
307 #else
308 # define TileSize 8
309 #endif
311 #define Mine 9
312 #define Flag 10
313 #define Unknown 11
314 #define ExplodedMine 12
315 #define WrongFlag 13
316 #define CorrectFlag 14
318 #define draw_tile( num, x, y ) \
319 rb->lcd_bitmap_part( minesweeper_tiles, 0, num * TileSize, \
320 TileSize, left+x*TileSize, top+y*TileSize, \
321 TileSize, TileSize )
323 #define invert_tile( x, y ) \
324 rb->lcd_set_drawmode(DRMODE_COMPLEMENT); \
325 rb->lcd_fillrect( left+x*TileSize, top+y*TileSize, TileSize, TileSize ); \
326 rb->lcd_set_drawmode(DRMODE_SOLID);
329 /* the tile struct
330 * if there is a mine, mine is true
331 * if tile is known by player, known is true
332 * if tile has a flag, flag is true
333 * neighbors is the total number of mines arround tile
335 typedef struct tile
337 unsigned char mine : 1;
338 unsigned char known : 1;
339 unsigned char flag : 1;
340 unsigned char neighbors : 4;
341 } tile;
343 /* the height and width of the field */
344 #define MAX_HEIGHT (LCD_HEIGHT/TileSize)
345 #define MAX_WIDTH (LCD_WIDTH/TileSize)
346 int height = MAX_HEIGHT;
347 int width = MAX_WIDTH;
348 int top;
349 int left;
351 /* The Minefield. Caution it is defined as Y, X! Not the opposite. */
352 tile minefield[MAX_HEIGHT][MAX_WIDTH];
354 /* total number of mines on the game */
355 int mine_num = 0;
357 /* percentage of mines on minefield used during generation */
358 int p = 16;
360 /* number of tiles left on the game */
361 int tiles_left;
363 /* number of used flags on the game */
364 int flags_used;
366 /* Because mines are set after the first move... */
367 bool no_mines = true;
369 /* We need a stack (created on discover()) for the cascade algorithm. */
370 int stack_pos = 0;
372 /* a usefull string for snprintf */
373 char str[30];
375 #ifdef HAVE_TOUCHSCREEN
377 #include "lib/pluginlib_touchscreen.h"
378 static struct ts_raster mine_raster = { 0, 0, MAX_WIDTH, MAX_HEIGHT, TileSize, TileSize };
379 #endif
382 void push( int *stack, int y, int x )
384 if( stack_pos <= height*width )
386 stack[++stack_pos] = y;
387 stack[++stack_pos] = x;
391 /* Unveil tiles and push them to stack if they are empty. */
392 void unveil( int *stack, int y, int x )
394 if( x < 0 || y < 0 || x > width - 1 || y > height - 1
395 || minefield[y][x].known
396 || minefield[y][x].mine || minefield[y][x].flag ) return;
398 minefield[y][x].known = 1;
400 if( minefield[y][x].neighbors == 0 )
401 push( stack, y, x );
404 int is_flagged( int y, int x )
406 if( x >= 0 && y >= 0 && x < width && y < height && minefield[y][x].flag )
407 return 1;
408 return 0;
411 int neighbors_flagged( int y, int x )
413 return is_flagged( y-1, x-1 ) +
414 is_flagged( y-1, x ) +
415 is_flagged( y-1, x+1 ) +
416 is_flagged( y, x-1 ) +
417 is_flagged( y, x ) +
418 is_flagged( y, x+1 ) +
419 is_flagged( y+1, x-1 ) +
420 is_flagged( y+1, x ) +
421 is_flagged( y+1, x+1 );
424 bool discover( int y, int x, bool explore_neighbors )
426 /* Selected tile. */
427 if( x < 0 || y < 0 || x > width - 1 || y > height - 1
428 || minefield[y][x].known
429 || minefield[y][x].mine || minefield[y][x].flag )
431 if( !minefield[y][x].flag && minefield[y][x].mine )
432 return true;
434 if( explore_neighbors && minefield[y][x].known &&
435 minefield[y][x].neighbors == neighbors_flagged( y, x ) )
437 return discover( y-1, x-1, false ) ||
438 discover( y-1, x, false ) ||
439 discover( y-1, x+1, false ) ||
440 discover( y, x-1, false ) ||
441 discover( y, x, false ) ||
442 discover( y, x+1, false ) ||
443 discover( y+1, x-1, false ) ||
444 discover( y+1, x, false ) ||
445 discover( y+1, x+1, false );
448 return false;
451 minefield[y][x].known = 1;
452 /* Exit if the tile is not empty. (no mines nearby) */
453 if( minefield[y][x].neighbors ) return false;
455 int stack[height*width];
457 push( stack, y, x );
459 /* Scan all nearby tiles. If we meet a tile with a number we just unveil
460 * it. If we meet an empty tile, we push the location in stack. For each
461 * location in stack we do the same thing. (scan again all nearby tiles)
463 while( stack_pos )
465 /* Pop x, y from stack. */
466 x = stack[stack_pos--];
467 y = stack[stack_pos--];
469 unveil( stack, y-1, x-1 );
470 unveil( stack, y-1, x );
471 unveil( stack, y-1, x+1 );
472 unveil( stack, y, x+1 );
473 unveil( stack, y+1, x+1 );
474 unveil( stack, y+1, x );
475 unveil( stack, y+1, x-1 );
476 unveil( stack, y, x-1 );
479 return false;
482 /* Reset the whole board for a new game. */
483 void minesweeper_init( void )
485 int i,j;
487 for( i = 0; i < MAX_HEIGHT; i++ )
489 for( j = 0; j < MAX_WIDTH; j++ )
491 minefield[i][j].known = 0;
492 minefield[i][j].flag = 0;
493 minefield[i][j].mine = 0;
494 minefield[i][j].neighbors = 0;
497 no_mines = true;
498 tiles_left = width*height;
502 /* put mines on the mine field */
503 /* there is p% chance that a tile is a mine */
504 /* if the tile has coordinates (x,y), then it can't be a mine */
505 void minesweeper_putmines( int p, int x, int y )
507 int i,j;
509 mine_num = 0;
510 for( i = 0; i < height; i++ )
512 for( j = 0; j < width; j++ )
514 if( rb->rand()%100 < p && !( y==i && x==j ) )
516 minefield[i][j].mine = 1;
517 mine_num++;
519 else
521 minefield[i][j].mine = 0;
523 minefield[i][j].neighbors = 0;
527 /* we need to compute the neighbor element for each tile */
528 for( i = 0; i < height; i++ )
530 for( j = 0; j < width; j++ )
532 if( i > 0 )
534 if( j > 0 )
535 minefield[i][j].neighbors += minefield[i-1][j-1].mine;
536 minefield[i][j].neighbors += minefield[i-1][j].mine;
537 if( j < width - 1 )
538 minefield[i][j].neighbors += minefield[i-1][j+1].mine;
540 if( j > 0 )
541 minefield[i][j].neighbors += minefield[i][j-1].mine;
542 if( j < width - 1 )
543 minefield[i][j].neighbors += minefield[i][j+1].mine;
544 if( i < height - 1 )
546 if( j > 0 )
547 minefield[i][j].neighbors += minefield[i+1][j-1].mine;
548 minefield[i][j].neighbors += minefield[i+1][j].mine;
549 if( j < width - 1 )
550 minefield[i][j].neighbors += minefield[i+1][j+1].mine;
555 no_mines = false;
557 /* In case the user is lucky and there are no mines positioned. */
558 if( !mine_num && height*width != 1 )
560 minesweeper_putmines(p, x, y);
564 /* A function that will uncover all the board, when the user wins or loses.
565 can easily be expanded, (just a call assigned to a button) as a solver. */
566 void mine_show( void )
568 int i, j, button;
570 for( i = 0; i < height; i++ )
572 for( j = 0; j < width; j++ )
574 if( minefield[i][j].mine )
576 if( minefield[i][j].known )
577 draw_tile( ExplodedMine, j, i );
578 else if( minefield[i][j].flag )
579 draw_tile( CorrectFlag, j, i );
580 else
581 draw_tile( Mine, j, i );
583 else
585 if( minefield[i][j].flag )
586 draw_tile( WrongFlag, j, i );
587 else
588 draw_tile( minefield[i][j].neighbors, j, i );
592 rb->lcd_update();
595 button = rb->button_get(true);
596 while( ( button == BUTTON_NONE )
597 || ( button & (BUTTON_REL|BUTTON_REPEAT) ) );
598 #ifdef HAVE_TOUCHSCREEN
599 button = BUTTON_NONE;
600 #endif
603 int count_tiles_left( void )
605 int tiles_left = 0;
606 int i, j;
607 for( i = 0; i < height; i++ )
608 for( j = 0; j < width; j++ )
609 if( minefield[i][j].known == 0 )
610 tiles_left++;
611 return tiles_left;
614 int count_flags( void )
616 int flags_used = 0;
617 int i, j;
618 for( i = 0; i < height; i++ )
619 for( j = 0; j < width; j++ )
620 if( minefield[i][j].flag == 1 )
621 flags_used++;
622 return flags_used;
625 /* welcome screen where player can chose mine percentage */
626 enum minesweeper_status menu( void )
628 int selection = 0, result = MINESWEEPER_QUIT;
629 bool menu_quit = false;
631 MENUITEM_STRINGLIST( menu, "Minesweeper Menu", NULL, "Play Minesweeper",
632 "Mine Percentage", "Number of Rows",
633 "Number of Columns", "Playback Control", "Quit" );
635 #ifdef HAVE_LCD_COLOR
636 rb->lcd_set_foreground( rb->global_settings->fg_color );
637 rb->lcd_set_background( rb->global_settings->bg_color );
638 #endif
640 while( !menu_quit )
642 switch( rb->do_menu( &menu, &selection, NULL, false ) )
644 case 0:
645 result = MINESWEEPER_WIN; /* start playing */
646 menu_quit = true;
647 break;
649 case 1:
650 rb->set_int( "Mine Percentage", "%", UNIT_INT, &p, NULL,
651 1, 2, 98, NULL );
652 break;
654 case 2:
655 rb->set_int( "Number of Rows", "", UNIT_INT, &height, NULL,
656 1, 1, MAX_HEIGHT, NULL );
657 break;
659 case 3:
660 rb->set_int( "Number of Columns", "", UNIT_INT, &width, NULL,
661 1, 1, MAX_WIDTH, NULL );
662 break;
664 case 4:
665 playback_control( NULL );
666 break;
668 default:
669 result = MINESWEEPER_QUIT; /* quit program */
670 menu_quit = true;
671 break;
675 return result;
678 /* the big and ugly game function */
679 enum minesweeper_status minesweeper( void )
681 int i, j;
682 int button;
683 int lastbutton = BUTTON_NONE;
685 /* the cursor coordinates */
686 int x=0, y=0;
689 * Show the menu
691 if( ( i = menu() ) != MINESWEEPER_WIN ) return i;
694 * Init game
696 top = (LCD_HEIGHT-height*TileSize)/2;
697 left = (LCD_WIDTH-width*TileSize)/2;
699 #ifdef HAVE_TOUCHSCREEN
700 mine_raster.tl_x = left;
701 mine_raster.tl_y = top;
702 mine_raster.width = width*TileSize;
703 mine_raster.height = height*TileSize;
704 #endif
706 rb->srand( *rb->current_tick );
707 minesweeper_init();
708 x = 0;
709 y = 0;
712 * Play
714 while( true )
717 /* clear the screen buffer */
718 #ifdef HAVE_LCD_COLOR
719 rb->lcd_set_background( BackgroundColor );
720 #endif
721 rb->lcd_clear_display();
723 /* display the mine field */
724 for( i = 0; i < height; i++ )
726 for( j = 0; j < width; j++ )
728 if( minefield[i][j].known )
730 draw_tile( minefield[i][j].neighbors, j, i );
732 else if(minefield[i][j].flag)
734 draw_tile( Flag, j, i );
736 else
738 draw_tile( Unknown, j, i );
743 /* display the cursor */
744 invert_tile( x, y );
746 /* update the screen */
747 rb->lcd_update();
749 button = rb->button_get(true);
750 #ifdef HAVE_TOUCHSCREEN
751 if(button & BUTTON_TOUCHSCREEN)
753 struct ts_raster_result res;
754 if(touchscreen_map_raster(&mine_raster, rb->button_get_data() >> 16, rb->button_get_data() & 0xffff, &res) == 1)
756 button &= ~BUTTON_TOUCHSCREEN;
757 lastbutton &= ~BUTTON_TOUCHSCREEN;
759 if(button & BUTTON_REPEAT && lastbutton != MINESWP_TOGGLE && lastbutton ^ BUTTON_REPEAT)
760 button = MINESWP_TOGGLE;
761 else if(button == BUTTON_REL && lastbutton ^ BUTTON_REPEAT)
762 button = MINESWP_DISCOVER;
763 else
764 button |= BUTTON_TOUCHSCREEN;
766 x = res.x;
767 y = res.y;
770 #endif
771 switch(button)
773 /* quit minesweeper (you really shouldn't use this button ...) */
774 #ifdef MINESWP_RC_QUIT
775 case MINESWP_RC_QUIT:
776 #endif
777 case MINESWP_QUIT:
778 return MINESWEEPER_QUIT;
780 /* move cursor left */
781 case MINESWP_LEFT:
782 case MINESWP_LEFT|BUTTON_REPEAT:
783 x = ( x + width - 1 )%width;
784 break;
786 /* move cursor right */
787 case MINESWP_RIGHT:
788 case MINESWP_RIGHT|BUTTON_REPEAT:
789 x = ( x + 1 )%width;
790 break;
792 /* move cursor down */
793 case MINESWP_DOWN:
794 case MINESWP_DOWN|BUTTON_REPEAT:
795 y = ( y + 1 )%height;
796 break;
798 /* move cursor up */
799 case MINESWP_UP:
800 case MINESWP_UP|BUTTON_REPEAT:
801 y = ( y + height - 1 )%height;
802 break;
804 /*move cursor though the entire field*/
805 #ifdef MINESWP_SCROLLWHEEL
806 case MINESWP_NEXT:
807 case MINESWP_NEXT|BUTTON_REPEAT:
808 if (x == width -1 ) {
809 y = ( y + 1 )%height;
811 x = ( x + 1 )%width;
812 break;
814 case MINESWP_PREV:
815 case MINESWP_PREV|BUTTON_REPEAT:
816 if (x == 0) {
817 y = ( y + height - 1 )%height;
819 x = ( x + width - 1 )%width;
820 break;
821 #endif
822 /* discover a tile (and it's neighbors if .neighbors == 0) */
823 case MINESWP_DISCOVER:
824 #ifdef MINESWP_DISCOVER2
825 case MINESWP_DISCOVER2:
826 #endif
827 if( minefield[y][x].flag ) break;
828 /* we put the mines on the first "click" so that you don't
829 * lose on the first "click" */
830 if( tiles_left == width*height && no_mines )
831 minesweeper_putmines(p,x,y);
833 if( discover( y, x, true ) )
835 minefield[y][x].known = 1;
836 return MINESWEEPER_LOSE;
839 tiles_left = count_tiles_left();
840 if( tiles_left == mine_num )
842 return MINESWEEPER_WIN;
844 break;
846 /* toggle flag under cursor */
847 case MINESWP_TOGGLE:
848 #ifdef MINESWP_TOGGLE_PRE
849 if( lastbutton != MINESWP_TOGGLE_PRE )
850 break;
851 #endif
852 #ifdef MINESWP_TOGGLE2
853 case MINESWP_TOGGLE2:
854 #endif
855 minefield[y][x].flag = ( minefield[y][x].flag + 1 )%2;
856 break;
858 /* show how many mines you think you have found and how many
859 * there really are on the game */
860 case MINESWP_INFO:
861 if( no_mines )
862 break;
863 flags_used = count_flags();
864 if (flags_used == 1) {
865 rb->splashf( HZ*2, "You marked 1 field. There are %d mines.",
866 mine_num );
868 else
870 rb->splashf( HZ*2, "You marked %d fields. There are %d mines.",
871 flags_used, mine_num );
873 break;
875 default:
876 if( rb->default_event_handler( button ) == SYS_USB_CONNECTED )
877 return MINESWEEPER_USB;
878 break;
880 if( button != BUTTON_NONE )
881 lastbutton = button;
886 /* plugin entry point */
887 enum plugin_status plugin_start(const void* parameter)
889 bool exit = false;
891 (void)parameter;
892 #if LCD_DEPTH > 1
893 rb->lcd_set_backdrop(NULL);
894 #endif
896 while( !exit )
898 switch( minesweeper() )
900 case MINESWEEPER_WIN:
901 rb->splash( HZ, "You Win!" );
902 rb->lcd_clear_display();
903 mine_show();
904 break;
906 case MINESWEEPER_LOSE:
907 rb->splash( HZ, "You Lose!" );
908 rb->lcd_clear_display();
909 mine_show();
910 break;
912 case MINESWEEPER_USB:
913 return PLUGIN_USB_CONNECTED;
915 case MINESWEEPER_QUIT:
916 exit = true;
917 break;
919 default:
920 break;
924 return PLUGIN_OK;
927 #endif