Enable calendar plugin for non-rtc targets (FS#10786 by Teruaki Kawashima)
[kugel-rb.git] / apps / plugins / minesweeper.c
blob4f27af671ee6e44d583c160a9443c6aef5592b4a
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 == COWOND2_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 #else
253 #error No keymap defined!
254 #endif
256 #ifdef HAVE_TOUCHSCREEN
257 #ifndef MINESWP_QUIT
258 # define MINESWP_QUIT BUTTON_TOPLEFT
259 #endif
260 #ifndef MINESWP_LEFT
261 # define MINESWP_LEFT BUTTON_MIDLEFT
262 #endif
263 #ifndef MINESWP_RIGHT
264 # define MINESWP_RIGHT BUTTON_MIDRIGHT
265 #endif
266 #ifndef MINESWP_UP
267 # define MINESWP_UP BUTTON_TOPMIDDLE
268 #endif
269 #ifndef MINESWP_DOWN
270 # define MINESWP_DOWN BUTTON_BOTTOMMIDDLE
271 #endif
272 #ifndef MINESWP_TOGGLE
273 # define MINESWP_TOGGLE BUTTON_CENTER
274 #endif
275 #ifndef MINESWP_DISCOVER
276 # define MINESWP_DISCOVER BUTTON_BOTTOMLEFT
277 #endif
278 #ifndef MINESWP_INFO
279 # define MINESWP_INFO BUTTON_BOTTOMRIGHT
280 #endif
281 #endif
283 extern const fb_data minesweeper_tiles[];
285 #ifdef HAVE_LCD_COLOR
286 # if ( LCD_HEIGHT * LCD_WIDTH ) / ( 16 * 16 ) >= 130
287 /* We want to have at least 130 tiles on the screen */
288 # define TileSize 16
289 # elif ( LCD_HEIGHT * LCD_WIDTH ) / ( 12 * 12 ) >= 130
290 # define TileSize 12
291 # else
292 # define TileSize 10
293 # endif
294 # define BackgroundColor LCD_RGBPACK( 128, 128, 128 )
295 #elif LCD_DEPTH > 1
296 # define TileSize 12
297 #else
298 # define TileSize 8
299 #endif
301 #define Mine 9
302 #define Flag 10
303 #define Unknown 11
304 #define ExplodedMine 12
306 #define draw_tile( num, x, y ) \
307 rb->lcd_bitmap_part( minesweeper_tiles, 0, num * TileSize, \
308 TileSize, left+x*TileSize, top+y*TileSize, \
309 TileSize, TileSize )
311 #define invert_tile( x, y ) \
312 rb->lcd_set_drawmode(DRMODE_COMPLEMENT); \
313 rb->lcd_fillrect( left+x*TileSize, top+y*TileSize, TileSize, TileSize ); \
314 rb->lcd_set_drawmode(DRMODE_SOLID);
317 /* the tile struct
318 * if there is a mine, mine is true
319 * if tile is known by player, known is true
320 * if tile has a flag, flag is true
321 * neighbors is the total number of mines arround tile
323 typedef struct tile
325 unsigned char mine : 1;
326 unsigned char known : 1;
327 unsigned char flag : 1;
328 unsigned char neighbors : 4;
329 } tile;
331 /* the height and width of the field */
332 #define MAX_HEIGHT (LCD_HEIGHT/TileSize)
333 #define MAX_WIDTH (LCD_WIDTH/TileSize)
334 int height = MAX_HEIGHT;
335 int width = MAX_WIDTH;
336 int top;
337 int left;
339 /* The Minefield. Caution it is defined as Y, X! Not the opposite. */
340 tile minefield[MAX_HEIGHT][MAX_WIDTH];
342 /* total number of mines on the game */
343 int mine_num = 0;
345 /* percentage of mines on minefield used during generation */
346 int p = 16;
348 /* number of tiles left on the game */
349 int tiles_left;
351 /* number of used flags on the game */
352 int flags_used;
354 /* Because mines are set after the first move... */
355 bool no_mines = true;
357 /* We need a stack (created on discover()) for the cascade algorithm. */
358 int stack_pos = 0;
360 /* a usefull string for snprintf */
361 char str[30];
363 #ifdef HAVE_TOUCHSCREEN
365 #include "lib/pluginlib_touchscreen.h"
366 static struct ts_raster mine_raster = { 0, 0, MAX_WIDTH, MAX_HEIGHT, TileSize, TileSize };
367 #endif
370 void push( int *stack, int y, int x )
372 if( stack_pos <= height*width )
374 stack[++stack_pos] = y;
375 stack[++stack_pos] = x;
379 /* Unveil tiles and push them to stack if they are empty. */
380 void unveil( int *stack, int y, int x )
382 if( x < 0 || y < 0 || x > width - 1 || y > height - 1
383 || minefield[y][x].known
384 || minefield[y][x].mine || minefield[y][x].flag ) return;
386 minefield[y][x].known = 1;
388 if( minefield[y][x].neighbors == 0 )
389 push( stack, y, x );
392 void discover( int y, int x )
394 int stack[height*width];
396 /* Selected tile. */
397 if( x < 0 || y < 0 || x > width - 1 || y > height - 1
398 || minefield[y][x].known
399 || minefield[y][x].mine || minefield[y][x].flag ) return;
401 minefield[y][x].known = 1;
402 /* Exit if the tile is not empty. (no mines nearby) */
403 if( minefield[y][x].neighbors ) return;
405 push( stack, y, x );
407 /* Scan all nearby tiles. If we meet a tile with a number we just unveil
408 * it. If we meet an empty tile, we push the location in stack. For each
409 * location in stack we do the same thing. (scan again all nearby tiles)
411 while( stack_pos )
413 /* Pop x, y from stack. */
414 x = stack[stack_pos--];
415 y = stack[stack_pos--];
417 unveil( stack, y-1, x-1 );
418 unveil( stack, y-1, x );
419 unveil( stack, y-1, x+1 );
420 unveil( stack, y, x+1 );
421 unveil( stack, y+1, x+1 );
422 unveil( stack, y+1, x );
423 unveil( stack, y+1, x-1 );
424 unveil( stack, y, x-1 );
428 /* Reset the whole board for a new game. */
429 void minesweeper_init( void )
431 int i,j;
433 for( i = 0; i < MAX_HEIGHT; i++ )
435 for( j = 0; j < MAX_WIDTH; j++ )
437 minefield[i][j].known = 0;
438 minefield[i][j].flag = 0;
439 minefield[i][j].mine = 0;
440 minefield[i][j].neighbors = 0;
443 no_mines = true;
444 tiles_left = width*height;
448 /* put mines on the mine field */
449 /* there is p% chance that a tile is a mine */
450 /* if the tile has coordinates (x,y), then it can't be a mine */
451 void minesweeper_putmines( int p, int x, int y )
453 int i,j;
455 mine_num = 0;
456 for( i = 0; i < height; i++ )
458 for( j = 0; j < width; j++ )
460 if( rb->rand()%100 < p && !( y==i && x==j ) )
462 minefield[i][j].mine = 1;
463 mine_num++;
465 else
467 minefield[i][j].mine = 0;
469 minefield[i][j].neighbors = 0;
473 /* we need to compute the neighbor element for each tile */
474 for( i = 0; i < height; i++ )
476 for( j = 0; j < width; j++ )
478 if( i > 0 )
480 if( j > 0 )
481 minefield[i][j].neighbors += minefield[i-1][j-1].mine;
482 minefield[i][j].neighbors += minefield[i-1][j].mine;
483 if( j < width - 1 )
484 minefield[i][j].neighbors += minefield[i-1][j+1].mine;
486 if( j > 0 )
487 minefield[i][j].neighbors += minefield[i][j-1].mine;
488 if( j < width - 1 )
489 minefield[i][j].neighbors += minefield[i][j+1].mine;
490 if( i < height - 1 )
492 if( j > 0 )
493 minefield[i][j].neighbors += minefield[i+1][j-1].mine;
494 minefield[i][j].neighbors += minefield[i+1][j].mine;
495 if( j < width - 1 )
496 minefield[i][j].neighbors += minefield[i+1][j+1].mine;
501 no_mines = false;
503 /* In case the user is lucky and there are no mines positioned. */
504 if( !mine_num && height*width != 1 )
506 minesweeper_putmines(p, x, y);
510 /* A function that will uncover all the board, when the user wins or loses.
511 can easily be expanded, (just a call assigned to a button) as a solver. */
512 void mine_show( void )
514 int i, j, button;
516 for( i = 0; i < height; i++ )
518 for( j = 0; j < width; j++ )
520 if( minefield[i][j].mine )
522 if( minefield[i][j].known )
524 draw_tile( ExplodedMine, j, i );
526 else
528 draw_tile( Mine, j, i );
531 else
533 draw_tile( minefield[i][j].neighbors, j, i );
537 rb->lcd_update();
540 button = rb->button_get(true);
541 while( ( button == BUTTON_NONE )
542 || ( button & (BUTTON_REL|BUTTON_REPEAT) ) );
543 #ifdef HAVE_TOUCHSCREEN
544 button = BUTTON_NONE;
545 #endif
548 int count_tiles_left( void )
550 int tiles_left = 0;
551 int i, j;
552 for( i = 0; i < height; i++ )
553 for( j = 0; j < width; j++ )
554 if( minefield[i][j].known == 0 )
555 tiles_left++;
556 return tiles_left;
559 int count_flags( void )
561 int flags_used = 0;
562 int i, j;
563 for( i = 0; i < height; i++ )
564 for( j = 0; j < width; j++ )
565 if( minefield[i][j].flag == 1 )
566 flags_used++;
567 return flags_used;
570 /* welcome screen where player can chose mine percentage */
571 enum minesweeper_status menu( void )
573 int selection = 0, result = MINESWEEPER_QUIT;
574 bool menu_quit = false;
576 MENUITEM_STRINGLIST( menu, "Minesweeper Menu", NULL, "Play Minesweeper",
577 "Mine Percentage", "Number of Rows",
578 "Number of Columns", "Playback Control", "Quit" );
580 #ifdef HAVE_LCD_COLOR
581 rb->lcd_set_foreground( rb->global_settings->fg_color );
582 rb->lcd_set_background( rb->global_settings->bg_color );
583 #endif
585 while( !menu_quit )
587 switch( rb->do_menu( &menu, &selection, NULL, false ) )
589 case 0:
590 result = MINESWEEPER_WIN; /* start playing */
591 menu_quit = true;
592 break;
594 case 1:
595 rb->set_int( "Mine Percentage", "%", UNIT_INT, &p, NULL,
596 1, 2, 98, NULL );
597 break;
599 case 2:
600 rb->set_int( "Number of Rows", "", UNIT_INT, &height, NULL,
601 1, 1, MAX_HEIGHT, NULL );
602 break;
604 case 3:
605 rb->set_int( "Number of Columns", "", UNIT_INT, &width, NULL,
606 1, 1, MAX_WIDTH, NULL );
607 break;
609 case 4:
610 playback_control( NULL );
611 break;
613 default:
614 result = MINESWEEPER_QUIT; /* quit program */
615 menu_quit = true;
616 break;
620 return result;
623 /* the big and ugly game function */
624 enum minesweeper_status minesweeper( void )
626 int i, j;
627 int button;
628 int lastbutton = BUTTON_NONE;
630 /* the cursor coordinates */
631 int x=0, y=0;
634 * Show the menu
636 if( ( i = menu() ) != MINESWEEPER_WIN ) return i;
639 * Init game
641 top = (LCD_HEIGHT-height*TileSize)/2;
642 left = (LCD_WIDTH-width*TileSize)/2;
644 #ifdef HAVE_TOUCHSCREEN
645 mine_raster.tl_x = left;
646 mine_raster.tl_y = top;
647 mine_raster.width = width*TileSize;
648 mine_raster.height = height*TileSize;
649 #endif
651 rb->srand( *rb->current_tick );
652 minesweeper_init();
653 x = 0;
654 y = 0;
657 * Play
659 while( true )
662 /* clear the screen buffer */
663 #ifdef HAVE_LCD_COLOR
664 rb->lcd_set_background( BackgroundColor );
665 #endif
666 rb->lcd_clear_display();
668 /* display the mine field */
669 for( i = 0; i < height; i++ )
671 for( j = 0; j < width; j++ )
673 if( minefield[i][j].known )
675 draw_tile( minefield[i][j].neighbors, j, i );
677 else if(minefield[i][j].flag)
679 draw_tile( Flag, j, i );
681 else
683 draw_tile( Unknown, j, i );
688 /* display the cursor */
689 invert_tile( x, y );
691 /* update the screen */
692 rb->lcd_update();
694 button = rb->button_get(true);
695 #ifdef HAVE_TOUCHSCREEN
696 if(button & BUTTON_TOUCHSCREEN)
698 struct ts_raster_result res;
699 if(touchscreen_map_raster(&mine_raster, rb->button_get_data() >> 16, rb->button_get_data() & 0xffff, &res) == 1)
701 button &= ~BUTTON_TOUCHSCREEN;
702 lastbutton &= ~BUTTON_TOUCHSCREEN;
704 if(button & BUTTON_REPEAT && lastbutton != MINESWP_TOGGLE && lastbutton ^ BUTTON_REPEAT)
705 button = MINESWP_TOGGLE;
706 else if(button == BUTTON_REL && lastbutton ^ BUTTON_REPEAT)
707 button = MINESWP_DISCOVER;
708 else
709 button |= BUTTON_TOUCHSCREEN;
711 x = res.x;
712 y = res.y;
715 #endif
716 switch(button)
718 /* quit minesweeper (you really shouldn't use this button ...) */
719 #ifdef MINESWP_RC_QUIT
720 case MINESWP_RC_QUIT:
721 #endif
722 case MINESWP_QUIT:
723 return MINESWEEPER_QUIT;
725 /* move cursor left */
726 case MINESWP_LEFT:
727 case MINESWP_LEFT|BUTTON_REPEAT:
728 x = ( x + width - 1 )%width;
729 break;
731 /* move cursor right */
732 case MINESWP_RIGHT:
733 case MINESWP_RIGHT|BUTTON_REPEAT:
734 x = ( x + 1 )%width;
735 break;
737 /* move cursor down */
738 case MINESWP_DOWN:
739 case MINESWP_DOWN|BUTTON_REPEAT:
740 y = ( y + 1 )%height;
741 break;
743 /* move cursor up */
744 case MINESWP_UP:
745 case MINESWP_UP|BUTTON_REPEAT:
746 y = ( y + height - 1 )%height;
747 break;
749 /*move cursor though the entire field*/
750 #ifdef MINESWP_SCROLLWHEEL
751 case MINESWP_NEXT:
752 case MINESWP_NEXT|BUTTON_REPEAT:
753 if (x == width -1 ) {
754 y = ( y + 1 )%height;
756 x = ( x + 1 )%width;
757 break;
759 case MINESWP_PREV:
760 case MINESWP_PREV|BUTTON_REPEAT:
761 if (x == 0) {
762 y = ( y + height - 1 )%height;
764 x = ( x + width - 1 )%width;
765 break;
766 #endif
767 /* discover a tile (and it's neighbors if .neighbors == 0) */
768 case MINESWP_DISCOVER:
769 #ifdef MINESWP_DISCOVER2
770 case MINESWP_DISCOVER2:
771 #endif
772 if( minefield[y][x].flag ) break;
773 /* we put the mines on the first "click" so that you don't
774 * lose on the first "click" */
775 if( tiles_left == width*height && no_mines )
776 minesweeper_putmines(p,x,y);
778 discover(y, x);
780 if( minefield[y][x].mine )
782 minefield[y][x].known = 1;
783 return MINESWEEPER_LOSE;
785 tiles_left = count_tiles_left();
786 if( tiles_left == mine_num )
788 return MINESWEEPER_WIN;
790 break;
792 /* toggle flag under cursor */
793 case MINESWP_TOGGLE:
794 #ifdef MINESWP_TOGGLE_PRE
795 if( lastbutton != MINESWP_TOGGLE_PRE )
796 break;
797 #endif
798 #ifdef MINESWP_TOGGLE2
799 case MINESWP_TOGGLE2:
800 #endif
801 minefield[y][x].flag = ( minefield[y][x].flag + 1 )%2;
802 break;
804 /* show how many mines you think you have found and how many
805 * there really are on the game */
806 case MINESWP_INFO:
807 if( no_mines )
808 break;
809 flags_used = count_flags();
810 if (flags_used == 1) {
811 rb->splashf( HZ*2, "You marked 1 field. There are %d mines.",
812 mine_num );
814 else
816 rb->splashf( HZ*2, "You marked %d fields. There are %d mines.",
817 flags_used, mine_num );
819 break;
821 default:
822 if( rb->default_event_handler( button ) == SYS_USB_CONNECTED )
823 return MINESWEEPER_USB;
824 break;
826 if( button != BUTTON_NONE )
827 lastbutton = button;
832 /* plugin entry point */
833 enum plugin_status plugin_start(const void* parameter)
835 bool exit = false;
837 (void)parameter;
838 #if LCD_DEPTH > 1
839 rb->lcd_set_backdrop(NULL);
840 #endif
842 while( !exit )
844 switch( minesweeper() )
846 case MINESWEEPER_WIN:
847 rb->splash( HZ, "You Win!" );
848 rb->lcd_clear_display();
849 mine_show();
850 break;
852 case MINESWEEPER_LOSE:
853 rb->splash( HZ, "You Lose!" );
854 rb->lcd_clear_display();
855 mine_show();
856 break;
858 case MINESWEEPER_USB:
859 return PLUGIN_USB_CONNECTED;
861 case MINESWEEPER_QUIT:
862 exit = true;
863 break;
865 default:
866 break;
870 return PLUGIN_OK;
873 #endif