Fix yellow
[kugel-rb.git] / apps / plugins / minesweeper.c
bloba21efeec76b3bf772d7c74b27aea6cbdd2cdd980
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 #else
231 #error No keymap defined!
232 #endif
234 #ifdef HAVE_TOUCHSCREEN
235 #ifndef MINESWP_QUIT
236 # define MINESWP_QUIT BUTTON_TOPLEFT
237 #endif
238 #ifndef MINESWP_LEFT
239 # define MINESWP_LEFT BUTTON_MIDLEFT
240 #endif
241 #ifndef MINESWP_RIGHT
242 # define MINESWP_RIGHT BUTTON_MIDRIGHT
243 #endif
244 #ifndef MINESWP_UP
245 # define MINESWP_UP BUTTON_TOPMIDDLE
246 #endif
247 #ifndef MINESWP_DOWN
248 # define MINESWP_DOWN BUTTON_BOTTOMMIDDLE
249 #endif
250 #ifndef MINESWP_TOGGLE
251 # define MINESWP_TOGGLE BUTTON_CENTER
252 #endif
253 #ifndef MINESWP_DISCOVER
254 # define MINESWP_DISCOVER BUTTON_BOTTOMLEFT
255 #endif
256 #ifndef MINESWP_INFO
257 # define MINESWP_INFO BUTTON_BOTTOMRIGHT
258 #endif
259 #endif
261 extern const fb_data minesweeper_tiles[];
263 #ifdef HAVE_LCD_COLOR
264 # if ( LCD_HEIGHT * LCD_WIDTH ) / ( 16 * 16 ) >= 130
265 /* We want to have at least 130 tiles on the screen */
266 # define TileSize 16
267 # elif ( LCD_HEIGHT * LCD_WIDTH ) / ( 12 * 12 ) >= 130
268 # define TileSize 12
269 # else
270 # define TileSize 10
271 # endif
272 # define BackgroundColor LCD_RGBPACK( 128, 128, 128 )
273 #elif LCD_DEPTH > 1
274 # define TileSize 12
275 #else
276 # define TileSize 8
277 #endif
279 #define Mine 9
280 #define Flag 10
281 #define Unknown 11
282 #define ExplodedMine 12
284 #define draw_tile( num, x, y ) \
285 rb->lcd_bitmap_part( minesweeper_tiles, 0, num * TileSize, \
286 TileSize, left+x*TileSize, top+y*TileSize, \
287 TileSize, TileSize )
289 #define invert_tile( x, y ) \
290 rb->lcd_set_drawmode(DRMODE_COMPLEMENT); \
291 rb->lcd_fillrect( left+x*TileSize, top+y*TileSize, TileSize, TileSize ); \
292 rb->lcd_set_drawmode(DRMODE_SOLID);
295 /* the tile struct
296 * if there is a mine, mine is true
297 * if tile is known by player, known is true
298 * if tile has a flag, flag is true
299 * neighbors is the total number of mines arround tile
301 typedef struct tile
303 unsigned char mine : 1;
304 unsigned char known : 1;
305 unsigned char flag : 1;
306 unsigned char neighbors : 4;
307 } tile;
309 /* the height and width of the field */
310 #define MAX_HEIGHT (LCD_HEIGHT/TileSize)
311 #define MAX_WIDTH (LCD_WIDTH/TileSize)
312 int height = MAX_HEIGHT;
313 int width = MAX_WIDTH;
314 int top;
315 int left;
317 /* The Minefield. Caution it is defined as Y, X! Not the opposite. */
318 tile minefield[MAX_HEIGHT][MAX_WIDTH];
320 /* total number of mines on the game */
321 int mine_num = 0;
323 /* percentage of mines on minefield used during generation */
324 int p = 16;
326 /* number of tiles left on the game */
327 int tiles_left;
329 /* number of used flags on the game */
330 int flags_used;
332 /* Because mines are set after the first move... */
333 bool no_mines = true;
335 /* We need a stack (created on discover()) for the cascade algorithm. */
336 int stack_pos = 0;
338 /* a usefull string for snprintf */
339 char str[30];
341 #ifdef HAVE_TOUCHSCREEN
343 #include "lib/touchscreen.h"
344 static struct ts_raster mine_raster = { 0, 0, MAX_WIDTH, MAX_HEIGHT, TileSize, TileSize };
345 #endif
348 void push( int *stack, int y, int x )
350 if( stack_pos <= height*width )
352 stack[++stack_pos] = y;
353 stack[++stack_pos] = x;
357 /* Unveil tiles and push them to stack if they are empty. */
358 void unveil( int *stack, int y, int x )
360 if( x < 0 || y < 0 || x > width - 1 || y > height - 1
361 || minefield[y][x].known
362 || minefield[y][x].mine || minefield[y][x].flag ) return;
364 minefield[y][x].known = 1;
366 if( minefield[y][x].neighbors == 0 )
367 push( stack, y, x );
370 void discover( int y, int x )
372 int stack[height*width];
374 /* Selected tile. */
375 if( x < 0 || y < 0 || x > width - 1 || y > height - 1
376 || minefield[y][x].known
377 || minefield[y][x].mine || minefield[y][x].flag ) return;
379 minefield[y][x].known = 1;
380 /* Exit if the tile is not empty. (no mines nearby) */
381 if( minefield[y][x].neighbors ) return;
383 push( stack, y, x );
385 /* Scan all nearby tiles. If we meet a tile with a number we just unveil
386 * it. If we meet an empty tile, we push the location in stack. For each
387 * location in stack we do the same thing. (scan again all nearby tiles)
389 while( stack_pos )
391 /* Pop x, y from stack. */
392 x = stack[stack_pos--];
393 y = stack[stack_pos--];
395 unveil( stack, y-1, x-1 );
396 unveil( stack, y-1, x );
397 unveil( stack, y-1, x+1 );
398 unveil( stack, y, x+1 );
399 unveil( stack, y+1, x+1 );
400 unveil( stack, y+1, x );
401 unveil( stack, y+1, x-1 );
402 unveil( stack, y, x-1 );
406 /* Reset the whole board for a new game. */
407 void minesweeper_init( void )
409 int i,j;
411 for( i = 0; i < MAX_HEIGHT; i++ )
413 for( j = 0; j < MAX_WIDTH; j++ )
415 minefield[i][j].known = 0;
416 minefield[i][j].flag = 0;
417 minefield[i][j].mine = 0;
418 minefield[i][j].neighbors = 0;
421 no_mines = true;
422 tiles_left = width*height;
426 /* put mines on the mine field */
427 /* there is p% chance that a tile is a mine */
428 /* if the tile has coordinates (x,y), then it can't be a mine */
429 void minesweeper_putmines( int p, int x, int y )
431 int i,j;
433 mine_num = 0;
434 for( i = 0; i < height; i++ )
436 for( j = 0; j < width; j++ )
438 if( rb->rand()%100 < p && !( y==i && x==j ) )
440 minefield[i][j].mine = 1;
441 mine_num++;
443 else
445 minefield[i][j].mine = 0;
447 minefield[i][j].neighbors = 0;
451 /* we need to compute the neighbor element for each tile */
452 for( i = 0; i < height; i++ )
454 for( j = 0; j < width; j++ )
456 if( i > 0 )
458 if( j > 0 )
459 minefield[i][j].neighbors += minefield[i-1][j-1].mine;
460 minefield[i][j].neighbors += minefield[i-1][j].mine;
461 if( j < width - 1 )
462 minefield[i][j].neighbors += minefield[i-1][j+1].mine;
464 if( j > 0 )
465 minefield[i][j].neighbors += minefield[i][j-1].mine;
466 if( j < width - 1 )
467 minefield[i][j].neighbors += minefield[i][j+1].mine;
468 if( i < height - 1 )
470 if( j > 0 )
471 minefield[i][j].neighbors += minefield[i+1][j-1].mine;
472 minefield[i][j].neighbors += minefield[i+1][j].mine;
473 if( j < width - 1 )
474 minefield[i][j].neighbors += minefield[i+1][j+1].mine;
479 no_mines = false;
481 /* In case the user is lucky and there are no mines positioned. */
482 if( !mine_num && height*width != 1 )
484 minesweeper_putmines(p, x, y);
488 /* A function that will uncover all the board, when the user wins or loses.
489 can easily be expanded, (just a call assigned to a button) as a solver. */
490 void mine_show( void )
492 int i, j, button;
494 for( i = 0; i < height; i++ )
496 for( j = 0; j < width; j++ )
498 if( minefield[i][j].mine )
500 if( minefield[i][j].known )
502 draw_tile( ExplodedMine, j, i );
504 else
506 draw_tile( Mine, j, i );
509 else
511 draw_tile( minefield[i][j].neighbors, j, i );
515 rb->lcd_update();
518 button = rb->button_get(true);
519 while( ( button == BUTTON_NONE )
520 || ( button & (BUTTON_REL|BUTTON_REPEAT) ) );
521 #ifdef HAVE_TOUCHSCREEN
522 button = BUTTON_NONE;
523 #endif
526 int count_tiles_left( void )
528 int tiles_left = 0;
529 int i, j;
530 for( i = 0; i < height; i++ )
531 for( j = 0; j < width; j++ )
532 if( minefield[i][j].known == 0 )
533 tiles_left++;
534 return tiles_left;
537 int count_flags( void )
539 int flags_used = 0;
540 int i, j;
541 for( i = 0; i < height; i++ )
542 for( j = 0; j < width; j++ )
543 if( minefield[i][j].flag == 1 )
544 flags_used++;
545 return flags_used;
548 /* welcome screen where player can chose mine percentage */
549 enum minesweeper_status menu( void )
551 int selection, result = MINESWEEPER_QUIT;
552 bool menu_quit = false;
554 MENUITEM_STRINGLIST( menu, "Minesweeper Menu", NULL, "Play Minesweeper",
555 "Mine Percentage", "Number of Rows",
556 "Number of Columns", "Playback Control", "Quit" );
558 #ifdef HAVE_LCD_COLOR
559 rb->lcd_set_foreground( rb->global_settings->fg_color );
560 rb->lcd_set_background( rb->global_settings->bg_color );
561 #endif
563 while( !menu_quit )
565 switch( rb->do_menu( &menu, &selection, NULL, false ) )
567 case 0:
568 result = MINESWEEPER_WIN; /* start playing */
569 menu_quit = true;
570 break;
572 case 1:
573 rb->set_int( "Mine Percentage", "%", UNIT_INT, &p, NULL,
574 1, 2, 98, NULL );
575 break;
577 case 2:
578 rb->set_int( "Number of Rows", "", UNIT_INT, &height, NULL,
579 1, 1, MAX_HEIGHT, NULL );
580 break;
582 case 3:
583 rb->set_int( "Number of Columns", "", UNIT_INT, &width, NULL,
584 1, 1, MAX_WIDTH, NULL );
585 break;
587 case 4:
588 playback_control( NULL );
589 break;
591 default:
592 result = MINESWEEPER_QUIT; /* quit program */
593 menu_quit = true;
594 break;
598 return result;
601 /* the big and ugly game function */
602 enum minesweeper_status minesweeper( void )
604 int i, j;
605 int button;
606 int lastbutton = BUTTON_NONE;
608 /* the cursor coordinates */
609 int x=0, y=0;
612 * Show the menu
614 if( ( i = menu() ) != MINESWEEPER_WIN ) return i;
617 * Init game
619 top = (LCD_HEIGHT-height*TileSize)/2;
620 left = (LCD_WIDTH-width*TileSize)/2;
622 #ifdef HAVE_TOUCHSCREEN
623 mine_raster.tl_x = left;
624 mine_raster.tl_y = top;
625 mine_raster.width = width*TileSize;
626 mine_raster.height = height*TileSize;
627 #endif
629 rb->srand( *rb->current_tick );
630 minesweeper_init();
631 x = 0;
632 y = 0;
635 * Play
637 while( true )
640 /* clear the screen buffer */
641 #ifdef HAVE_LCD_COLOR
642 rb->lcd_set_background( BackgroundColor );
643 #endif
644 rb->lcd_clear_display();
646 /* display the mine field */
647 for( i = 0; i < height; i++ )
649 for( j = 0; j < width; j++ )
651 if( minefield[i][j].known )
653 draw_tile( minefield[i][j].neighbors, j, i );
655 else if(minefield[i][j].flag)
657 draw_tile( Flag, j, i );
659 else
661 draw_tile( Unknown, j, i );
666 /* display the cursor */
667 invert_tile( x, y );
669 /* update the screen */
670 rb->lcd_update();
672 button = rb->button_get(true);
673 #ifdef HAVE_TOUCHSCREEN
674 if(button & BUTTON_TOUCHSCREEN)
676 struct ts_raster_result res;
677 if(touchscreen_map_raster(&mine_raster, rb->button_get_data() >> 16, rb->button_get_data() & 0xffff, &res) == 1)
679 button &= ~BUTTON_TOUCHSCREEN;
680 lastbutton &= ~BUTTON_TOUCHSCREEN;
682 if(button & BUTTON_REPEAT && lastbutton != MINESWP_TOGGLE && lastbutton ^ BUTTON_REPEAT)
683 button = MINESWP_TOGGLE;
684 else if(button == BUTTON_REL && lastbutton ^ BUTTON_REPEAT)
685 button = MINESWP_DISCOVER;
686 else
687 button |= BUTTON_TOUCHSCREEN;
689 x = res.x;
690 y = res.y;
693 #endif
694 switch(button)
696 /* quit minesweeper (you really shouldn't use this button ...) */
697 #ifdef MINESWP_RC_QUIT
698 case MINESWP_RC_QUIT:
699 #endif
700 case MINESWP_QUIT:
701 return MINESWEEPER_QUIT;
703 /* move cursor left */
704 case MINESWP_LEFT:
705 case MINESWP_LEFT|BUTTON_REPEAT:
706 x = ( x + width - 1 )%width;
707 break;
709 /* move cursor right */
710 case MINESWP_RIGHT:
711 case MINESWP_RIGHT|BUTTON_REPEAT:
712 x = ( x + 1 )%width;
713 break;
715 /* move cursor down */
716 case MINESWP_DOWN:
717 case MINESWP_DOWN|BUTTON_REPEAT:
718 y = ( y + 1 )%height;
719 break;
721 /* move cursor up */
722 case MINESWP_UP:
723 case MINESWP_UP|BUTTON_REPEAT:
724 y = ( y + height - 1 )%height;
725 break;
727 /*move cursor though the entire field*/
728 #ifdef MINESWP_SCROLLWHEEL
729 case MINESWP_NEXT:
730 case MINESWP_NEXT|BUTTON_REPEAT:
731 if (x == width -1 ) {
732 y = ( y + 1 )%height;
734 x = ( x + 1 )%width;
735 break;
737 case MINESWP_PREV:
738 case MINESWP_PREV|BUTTON_REPEAT:
739 if (x == 0) {
740 y = ( y + height - 1 )%height;
742 x = ( x + width - 1 )%width;
743 break;
744 #endif
745 /* discover a tile (and it's neighbors if .neighbors == 0) */
746 case MINESWP_DISCOVER:
747 #ifdef MINESWP_DISCOVER2
748 case MINESWP_DISCOVER2:
749 #endif
750 if( minefield[y][x].flag ) break;
751 /* we put the mines on the first "click" so that you don't
752 * lose on the first "click" */
753 if( tiles_left == width*height && no_mines )
754 minesweeper_putmines(p,x,y);
756 discover(y, x);
758 if( minefield[y][x].mine )
760 minefield[y][x].known = 1;
761 return MINESWEEPER_LOSE;
763 tiles_left = count_tiles_left();
764 if( tiles_left == mine_num )
766 return MINESWEEPER_WIN;
768 break;
770 /* toggle flag under cursor */
771 case MINESWP_TOGGLE:
772 #ifdef MINESWP_TOGGLE_PRE
773 if( lastbutton != MINESWP_TOGGLE_PRE )
774 break;
775 #endif
776 #ifdef MINESWP_TOGGLE2
777 case MINESWP_TOGGLE2:
778 #endif
779 minefield[y][x].flag = ( minefield[y][x].flag + 1 )%2;
780 break;
782 /* show how many mines you think you have found and how many
783 * there really are on the game */
784 case MINESWP_INFO:
785 if( no_mines )
786 break;
787 flags_used = count_flags();
788 if (flags_used == 1) {
789 rb->splashf( HZ*2, "You marked 1 field. There are %d mines.",
790 mine_num );
792 else
794 rb->splashf( HZ*2, "You marked %d fields. There are %d mines.",
795 flags_used, mine_num );
797 break;
799 default:
800 if( rb->default_event_handler( button ) == SYS_USB_CONNECTED )
801 return MINESWEEPER_USB;
802 break;
804 if( button != BUTTON_NONE )
805 lastbutton = button;
810 /* plugin entry point */
811 enum plugin_status plugin_start(const void* parameter)
813 bool exit = false;
815 (void)parameter;
816 #if LCD_DEPTH > 1
817 rb->lcd_set_backdrop(NULL);
818 #endif
820 while( !exit )
822 switch( minesweeper() )
824 case MINESWEEPER_WIN:
825 rb->splash( HZ, "You Win!" );
826 rb->lcd_clear_display();
827 mine_show();
828 break;
830 case MINESWEEPER_LOSE:
831 rb->splash( HZ, "You Lose!" );
832 rb->lcd_clear_display();
833 mine_show();
834 break;
836 case MINESWEEPER_USB:
837 return PLUGIN_USB_CONNECTED;
839 case MINESWEEPER_QUIT:
840 exit = true;
841 break;
843 default:
844 break;
848 return PLUGIN_OK;
851 #endif