* Onda VX747: add browse screen, pitchscreen, context menu, quickscreen, rewind...
[kugel-rb.git] / apps / plugins / minesweeper.c
bloba1190d5a516e473c708654616af941f56edba886
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 == ONDAVX747_PAD) || CONFIG_KEYPAD == MROBE500_PAD
228 # define MINESWP_QUIT BUTTON_POWER
230 #elif (CONFIG_KEYPAD == SAMSUNG_YH_PAD)
231 # define MINESWP_LEFT BUTTON_LEFT
232 # define MINESWP_RIGHT BUTTON_RIGHT
233 # define MINESWP_UP BUTTON_UP
234 # define MINESWP_DOWN BUTTON_DOWN
235 # define MINESWP_QUIT BUTTON_REC
236 # define MINESWP_TOGGLE BUTTON_PLAY
237 # define MINESWP_DISCOVER BUTTON_REW
238 # define MINESWP_INFO BUTTON_FFWD
240 #else
241 #error No keymap defined!
242 #endif
244 #ifdef HAVE_TOUCHSCREEN
245 #ifndef MINESWP_QUIT
246 # define MINESWP_QUIT BUTTON_TOPLEFT
247 #endif
248 #ifndef MINESWP_LEFT
249 # define MINESWP_LEFT BUTTON_MIDLEFT
250 #endif
251 #ifndef MINESWP_RIGHT
252 # define MINESWP_RIGHT BUTTON_MIDRIGHT
253 #endif
254 #ifndef MINESWP_UP
255 # define MINESWP_UP BUTTON_TOPMIDDLE
256 #endif
257 #ifndef MINESWP_DOWN
258 # define MINESWP_DOWN BUTTON_BOTTOMMIDDLE
259 #endif
260 #ifndef MINESWP_TOGGLE
261 # define MINESWP_TOGGLE BUTTON_CENTER
262 #endif
263 #ifndef MINESWP_DISCOVER
264 # define MINESWP_DISCOVER BUTTON_BOTTOMLEFT
265 #endif
266 #ifndef MINESWP_INFO
267 # define MINESWP_INFO BUTTON_BOTTOMRIGHT
268 #endif
269 #endif
271 extern const fb_data minesweeper_tiles[];
273 #ifdef HAVE_LCD_COLOR
274 # if ( LCD_HEIGHT * LCD_WIDTH ) / ( 16 * 16 ) >= 130
275 /* We want to have at least 130 tiles on the screen */
276 # define TileSize 16
277 # elif ( LCD_HEIGHT * LCD_WIDTH ) / ( 12 * 12 ) >= 130
278 # define TileSize 12
279 # else
280 # define TileSize 10
281 # endif
282 # define BackgroundColor LCD_RGBPACK( 128, 128, 128 )
283 #elif LCD_DEPTH > 1
284 # define TileSize 12
285 #else
286 # define TileSize 8
287 #endif
289 #define Mine 9
290 #define Flag 10
291 #define Unknown 11
292 #define ExplodedMine 12
294 #define draw_tile( num, x, y ) \
295 rb->lcd_bitmap_part( minesweeper_tiles, 0, num * TileSize, \
296 TileSize, left+x*TileSize, top+y*TileSize, \
297 TileSize, TileSize )
299 #define invert_tile( x, y ) \
300 rb->lcd_set_drawmode(DRMODE_COMPLEMENT); \
301 rb->lcd_fillrect( left+x*TileSize, top+y*TileSize, TileSize, TileSize ); \
302 rb->lcd_set_drawmode(DRMODE_SOLID);
305 /* the tile struct
306 * if there is a mine, mine is true
307 * if tile is known by player, known is true
308 * if tile has a flag, flag is true
309 * neighbors is the total number of mines arround tile
311 typedef struct tile
313 unsigned char mine : 1;
314 unsigned char known : 1;
315 unsigned char flag : 1;
316 unsigned char neighbors : 4;
317 } tile;
319 /* the height and width of the field */
320 #define MAX_HEIGHT (LCD_HEIGHT/TileSize)
321 #define MAX_WIDTH (LCD_WIDTH/TileSize)
322 int height = MAX_HEIGHT;
323 int width = MAX_WIDTH;
324 int top;
325 int left;
327 /* The Minefield. Caution it is defined as Y, X! Not the opposite. */
328 tile minefield[MAX_HEIGHT][MAX_WIDTH];
330 /* total number of mines on the game */
331 int mine_num = 0;
333 /* percentage of mines on minefield used during generation */
334 int p = 16;
336 /* number of tiles left on the game */
337 int tiles_left;
339 /* number of used flags on the game */
340 int flags_used;
342 /* Because mines are set after the first move... */
343 bool no_mines = true;
345 /* We need a stack (created on discover()) for the cascade algorithm. */
346 int stack_pos = 0;
348 /* a usefull string for snprintf */
349 char str[30];
351 #ifdef HAVE_TOUCHSCREEN
353 #include "lib/pluginlib_touchscreen.h"
354 static struct ts_raster mine_raster = { 0, 0, MAX_WIDTH, MAX_HEIGHT, TileSize, TileSize };
355 #endif
358 void push( int *stack, int y, int x )
360 if( stack_pos <= height*width )
362 stack[++stack_pos] = y;
363 stack[++stack_pos] = x;
367 /* Unveil tiles and push them to stack if they are empty. */
368 void unveil( int *stack, int y, int x )
370 if( x < 0 || y < 0 || x > width - 1 || y > height - 1
371 || minefield[y][x].known
372 || minefield[y][x].mine || minefield[y][x].flag ) return;
374 minefield[y][x].known = 1;
376 if( minefield[y][x].neighbors == 0 )
377 push( stack, y, x );
380 void discover( int y, int x )
382 int stack[height*width];
384 /* Selected tile. */
385 if( x < 0 || y < 0 || x > width - 1 || y > height - 1
386 || minefield[y][x].known
387 || minefield[y][x].mine || minefield[y][x].flag ) return;
389 minefield[y][x].known = 1;
390 /* Exit if the tile is not empty. (no mines nearby) */
391 if( minefield[y][x].neighbors ) return;
393 push( stack, y, x );
395 /* Scan all nearby tiles. If we meet a tile with a number we just unveil
396 * it. If we meet an empty tile, we push the location in stack. For each
397 * location in stack we do the same thing. (scan again all nearby tiles)
399 while( stack_pos )
401 /* Pop x, y from stack. */
402 x = stack[stack_pos--];
403 y = stack[stack_pos--];
405 unveil( stack, y-1, x-1 );
406 unveil( stack, y-1, x );
407 unveil( stack, y-1, x+1 );
408 unveil( stack, y, x+1 );
409 unveil( stack, y+1, x+1 );
410 unveil( stack, y+1, x );
411 unveil( stack, y+1, x-1 );
412 unveil( stack, y, x-1 );
416 /* Reset the whole board for a new game. */
417 void minesweeper_init( void )
419 int i,j;
421 for( i = 0; i < MAX_HEIGHT; i++ )
423 for( j = 0; j < MAX_WIDTH; j++ )
425 minefield[i][j].known = 0;
426 minefield[i][j].flag = 0;
427 minefield[i][j].mine = 0;
428 minefield[i][j].neighbors = 0;
431 no_mines = true;
432 tiles_left = width*height;
436 /* put mines on the mine field */
437 /* there is p% chance that a tile is a mine */
438 /* if the tile has coordinates (x,y), then it can't be a mine */
439 void minesweeper_putmines( int p, int x, int y )
441 int i,j;
443 mine_num = 0;
444 for( i = 0; i < height; i++ )
446 for( j = 0; j < width; j++ )
448 if( rb->rand()%100 < p && !( y==i && x==j ) )
450 minefield[i][j].mine = 1;
451 mine_num++;
453 else
455 minefield[i][j].mine = 0;
457 minefield[i][j].neighbors = 0;
461 /* we need to compute the neighbor element for each tile */
462 for( i = 0; i < height; i++ )
464 for( j = 0; j < width; j++ )
466 if( i > 0 )
468 if( j > 0 )
469 minefield[i][j].neighbors += minefield[i-1][j-1].mine;
470 minefield[i][j].neighbors += minefield[i-1][j].mine;
471 if( j < width - 1 )
472 minefield[i][j].neighbors += minefield[i-1][j+1].mine;
474 if( j > 0 )
475 minefield[i][j].neighbors += minefield[i][j-1].mine;
476 if( j < width - 1 )
477 minefield[i][j].neighbors += minefield[i][j+1].mine;
478 if( i < height - 1 )
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;
489 no_mines = false;
491 /* In case the user is lucky and there are no mines positioned. */
492 if( !mine_num && height*width != 1 )
494 minesweeper_putmines(p, x, y);
498 /* A function that will uncover all the board, when the user wins or loses.
499 can easily be expanded, (just a call assigned to a button) as a solver. */
500 void mine_show( void )
502 int i, j, button;
504 for( i = 0; i < height; i++ )
506 for( j = 0; j < width; j++ )
508 if( minefield[i][j].mine )
510 if( minefield[i][j].known )
512 draw_tile( ExplodedMine, j, i );
514 else
516 draw_tile( Mine, j, i );
519 else
521 draw_tile( minefield[i][j].neighbors, j, i );
525 rb->lcd_update();
528 button = rb->button_get(true);
529 while( ( button == BUTTON_NONE )
530 || ( button & (BUTTON_REL|BUTTON_REPEAT) ) );
531 #ifdef HAVE_TOUCHSCREEN
532 button = BUTTON_NONE;
533 #endif
536 int count_tiles_left( void )
538 int tiles_left = 0;
539 int i, j;
540 for( i = 0; i < height; i++ )
541 for( j = 0; j < width; j++ )
542 if( minefield[i][j].known == 0 )
543 tiles_left++;
544 return tiles_left;
547 int count_flags( void )
549 int flags_used = 0;
550 int i, j;
551 for( i = 0; i < height; i++ )
552 for( j = 0; j < width; j++ )
553 if( minefield[i][j].flag == 1 )
554 flags_used++;
555 return flags_used;
558 /* welcome screen where player can chose mine percentage */
559 enum minesweeper_status menu( void )
561 int selection = 0, result = MINESWEEPER_QUIT;
562 bool menu_quit = false;
564 MENUITEM_STRINGLIST( menu, "Minesweeper Menu", NULL, "Play Minesweeper",
565 "Mine Percentage", "Number of Rows",
566 "Number of Columns", "Playback Control", "Quit" );
568 #ifdef HAVE_LCD_COLOR
569 rb->lcd_set_foreground( rb->global_settings->fg_color );
570 rb->lcd_set_background( rb->global_settings->bg_color );
571 #endif
573 while( !menu_quit )
575 switch( rb->do_menu( &menu, &selection, NULL, false ) )
577 case 0:
578 result = MINESWEEPER_WIN; /* start playing */
579 menu_quit = true;
580 break;
582 case 1:
583 rb->set_int( "Mine Percentage", "%", UNIT_INT, &p, NULL,
584 1, 2, 98, NULL );
585 break;
587 case 2:
588 rb->set_int( "Number of Rows", "", UNIT_INT, &height, NULL,
589 1, 1, MAX_HEIGHT, NULL );
590 break;
592 case 3:
593 rb->set_int( "Number of Columns", "", UNIT_INT, &width, NULL,
594 1, 1, MAX_WIDTH, NULL );
595 break;
597 case 4:
598 playback_control( NULL );
599 break;
601 default:
602 result = MINESWEEPER_QUIT; /* quit program */
603 menu_quit = true;
604 break;
608 return result;
611 /* the big and ugly game function */
612 enum minesweeper_status minesweeper( void )
614 int i, j;
615 int button;
616 int lastbutton = BUTTON_NONE;
618 /* the cursor coordinates */
619 int x=0, y=0;
622 * Show the menu
624 if( ( i = menu() ) != MINESWEEPER_WIN ) return i;
627 * Init game
629 top = (LCD_HEIGHT-height*TileSize)/2;
630 left = (LCD_WIDTH-width*TileSize)/2;
632 #ifdef HAVE_TOUCHSCREEN
633 mine_raster.tl_x = left;
634 mine_raster.tl_y = top;
635 mine_raster.width = width*TileSize;
636 mine_raster.height = height*TileSize;
637 #endif
639 rb->srand( *rb->current_tick );
640 minesweeper_init();
641 x = 0;
642 y = 0;
645 * Play
647 while( true )
650 /* clear the screen buffer */
651 #ifdef HAVE_LCD_COLOR
652 rb->lcd_set_background( BackgroundColor );
653 #endif
654 rb->lcd_clear_display();
656 /* display the mine field */
657 for( i = 0; i < height; i++ )
659 for( j = 0; j < width; j++ )
661 if( minefield[i][j].known )
663 draw_tile( minefield[i][j].neighbors, j, i );
665 else if(minefield[i][j].flag)
667 draw_tile( Flag, j, i );
669 else
671 draw_tile( Unknown, j, i );
676 /* display the cursor */
677 invert_tile( x, y );
679 /* update the screen */
680 rb->lcd_update();
682 button = rb->button_get(true);
683 #ifdef HAVE_TOUCHSCREEN
684 if(button & BUTTON_TOUCHSCREEN)
686 struct ts_raster_result res;
687 if(touchscreen_map_raster(&mine_raster, rb->button_get_data() >> 16, rb->button_get_data() & 0xffff, &res) == 1)
689 button &= ~BUTTON_TOUCHSCREEN;
690 lastbutton &= ~BUTTON_TOUCHSCREEN;
692 if(button & BUTTON_REPEAT && lastbutton != MINESWP_TOGGLE && lastbutton ^ BUTTON_REPEAT)
693 button = MINESWP_TOGGLE;
694 else if(button == BUTTON_REL && lastbutton ^ BUTTON_REPEAT)
695 button = MINESWP_DISCOVER;
696 else
697 button |= BUTTON_TOUCHSCREEN;
699 x = res.x;
700 y = res.y;
703 #endif
704 switch(button)
706 /* quit minesweeper (you really shouldn't use this button ...) */
707 #ifdef MINESWP_RC_QUIT
708 case MINESWP_RC_QUIT:
709 #endif
710 case MINESWP_QUIT:
711 return MINESWEEPER_QUIT;
713 /* move cursor left */
714 case MINESWP_LEFT:
715 case MINESWP_LEFT|BUTTON_REPEAT:
716 x = ( x + width - 1 )%width;
717 break;
719 /* move cursor right */
720 case MINESWP_RIGHT:
721 case MINESWP_RIGHT|BUTTON_REPEAT:
722 x = ( x + 1 )%width;
723 break;
725 /* move cursor down */
726 case MINESWP_DOWN:
727 case MINESWP_DOWN|BUTTON_REPEAT:
728 y = ( y + 1 )%height;
729 break;
731 /* move cursor up */
732 case MINESWP_UP:
733 case MINESWP_UP|BUTTON_REPEAT:
734 y = ( y + height - 1 )%height;
735 break;
737 /*move cursor though the entire field*/
738 #ifdef MINESWP_SCROLLWHEEL
739 case MINESWP_NEXT:
740 case MINESWP_NEXT|BUTTON_REPEAT:
741 if (x == width -1 ) {
742 y = ( y + 1 )%height;
744 x = ( x + 1 )%width;
745 break;
747 case MINESWP_PREV:
748 case MINESWP_PREV|BUTTON_REPEAT:
749 if (x == 0) {
750 y = ( y + height - 1 )%height;
752 x = ( x + width - 1 )%width;
753 break;
754 #endif
755 /* discover a tile (and it's neighbors if .neighbors == 0) */
756 case MINESWP_DISCOVER:
757 #ifdef MINESWP_DISCOVER2
758 case MINESWP_DISCOVER2:
759 #endif
760 if( minefield[y][x].flag ) break;
761 /* we put the mines on the first "click" so that you don't
762 * lose on the first "click" */
763 if( tiles_left == width*height && no_mines )
764 minesweeper_putmines(p,x,y);
766 discover(y, x);
768 if( minefield[y][x].mine )
770 minefield[y][x].known = 1;
771 return MINESWEEPER_LOSE;
773 tiles_left = count_tiles_left();
774 if( tiles_left == mine_num )
776 return MINESWEEPER_WIN;
778 break;
780 /* toggle flag under cursor */
781 case MINESWP_TOGGLE:
782 #ifdef MINESWP_TOGGLE_PRE
783 if( lastbutton != MINESWP_TOGGLE_PRE )
784 break;
785 #endif
786 #ifdef MINESWP_TOGGLE2
787 case MINESWP_TOGGLE2:
788 #endif
789 minefield[y][x].flag = ( minefield[y][x].flag + 1 )%2;
790 break;
792 /* show how many mines you think you have found and how many
793 * there really are on the game */
794 case MINESWP_INFO:
795 if( no_mines )
796 break;
797 flags_used = count_flags();
798 if (flags_used == 1) {
799 rb->splashf( HZ*2, "You marked 1 field. There are %d mines.",
800 mine_num );
802 else
804 rb->splashf( HZ*2, "You marked %d fields. There are %d mines.",
805 flags_used, mine_num );
807 break;
809 default:
810 if( rb->default_event_handler( button ) == SYS_USB_CONNECTED )
811 return MINESWEEPER_USB;
812 break;
814 if( button != BUTTON_NONE )
815 lastbutton = button;
820 /* plugin entry point */
821 enum plugin_status plugin_start(const void* parameter)
823 bool exit = false;
825 (void)parameter;
826 #if LCD_DEPTH > 1
827 rb->lcd_set_backdrop(NULL);
828 #endif
830 while( !exit )
832 switch( minesweeper() )
834 case MINESWEEPER_WIN:
835 rb->splash( HZ, "You Win!" );
836 rb->lcd_clear_display();
837 mine_show();
838 break;
840 case MINESWEEPER_LOSE:
841 rb->splash( HZ, "You Lose!" );
842 rb->lcd_clear_display();
843 mine_show();
844 break;
846 case MINESWEEPER_USB:
847 return PLUGIN_USB_CONNECTED;
849 case MINESWEEPER_QUIT:
850 exit = true;
851 break;
853 default:
854 break;
858 return PLUGIN_OK;
861 #endif