Fix the pitch detector plugin for iaudioM3
[kugel-rb.git] / apps / plugins / minesweeper.c
blob7d946b1c5d5b7c6f165931b758747fbc1ebb7961
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) || \
228 CONFIG_KEYPAD == ONDAVX777_PAD || \
229 CONFIG_KEYPAD == MROBE500_PAD
230 # define MINESWP_QUIT BUTTON_POWER
232 #elif (CONFIG_KEYPAD == SAMSUNG_YH_PAD)
233 # define MINESWP_LEFT BUTTON_LEFT
234 # define MINESWP_RIGHT BUTTON_RIGHT
235 # define MINESWP_UP BUTTON_UP
236 # define MINESWP_DOWN BUTTON_DOWN
237 # define MINESWP_QUIT BUTTON_REC
238 # define MINESWP_TOGGLE BUTTON_PLAY
239 # define MINESWP_DISCOVER BUTTON_REW
240 # define MINESWP_INFO BUTTON_FFWD
242 #else
243 #error No keymap defined!
244 #endif
246 #ifdef HAVE_TOUCHSCREEN
247 #ifndef MINESWP_QUIT
248 # define MINESWP_QUIT BUTTON_TOPLEFT
249 #endif
250 #ifndef MINESWP_LEFT
251 # define MINESWP_LEFT BUTTON_MIDLEFT
252 #endif
253 #ifndef MINESWP_RIGHT
254 # define MINESWP_RIGHT BUTTON_MIDRIGHT
255 #endif
256 #ifndef MINESWP_UP
257 # define MINESWP_UP BUTTON_TOPMIDDLE
258 #endif
259 #ifndef MINESWP_DOWN
260 # define MINESWP_DOWN BUTTON_BOTTOMMIDDLE
261 #endif
262 #ifndef MINESWP_TOGGLE
263 # define MINESWP_TOGGLE BUTTON_CENTER
264 #endif
265 #ifndef MINESWP_DISCOVER
266 # define MINESWP_DISCOVER BUTTON_BOTTOMLEFT
267 #endif
268 #ifndef MINESWP_INFO
269 # define MINESWP_INFO BUTTON_BOTTOMRIGHT
270 #endif
271 #endif
273 extern const fb_data minesweeper_tiles[];
275 #ifdef HAVE_LCD_COLOR
276 # if ( LCD_HEIGHT * LCD_WIDTH ) / ( 16 * 16 ) >= 130
277 /* We want to have at least 130 tiles on the screen */
278 # define TileSize 16
279 # elif ( LCD_HEIGHT * LCD_WIDTH ) / ( 12 * 12 ) >= 130
280 # define TileSize 12
281 # else
282 # define TileSize 10
283 # endif
284 # define BackgroundColor LCD_RGBPACK( 128, 128, 128 )
285 #elif LCD_DEPTH > 1
286 # define TileSize 12
287 #else
288 # define TileSize 8
289 #endif
291 #define Mine 9
292 #define Flag 10
293 #define Unknown 11
294 #define ExplodedMine 12
296 #define draw_tile( num, x, y ) \
297 rb->lcd_bitmap_part( minesweeper_tiles, 0, num * TileSize, \
298 TileSize, left+x*TileSize, top+y*TileSize, \
299 TileSize, TileSize )
301 #define invert_tile( x, y ) \
302 rb->lcd_set_drawmode(DRMODE_COMPLEMENT); \
303 rb->lcd_fillrect( left+x*TileSize, top+y*TileSize, TileSize, TileSize ); \
304 rb->lcd_set_drawmode(DRMODE_SOLID);
307 /* the tile struct
308 * if there is a mine, mine is true
309 * if tile is known by player, known is true
310 * if tile has a flag, flag is true
311 * neighbors is the total number of mines arround tile
313 typedef struct tile
315 unsigned char mine : 1;
316 unsigned char known : 1;
317 unsigned char flag : 1;
318 unsigned char neighbors : 4;
319 } tile;
321 /* the height and width of the field */
322 #define MAX_HEIGHT (LCD_HEIGHT/TileSize)
323 #define MAX_WIDTH (LCD_WIDTH/TileSize)
324 int height = MAX_HEIGHT;
325 int width = MAX_WIDTH;
326 int top;
327 int left;
329 /* The Minefield. Caution it is defined as Y, X! Not the opposite. */
330 tile minefield[MAX_HEIGHT][MAX_WIDTH];
332 /* total number of mines on the game */
333 int mine_num = 0;
335 /* percentage of mines on minefield used during generation */
336 int p = 16;
338 /* number of tiles left on the game */
339 int tiles_left;
341 /* number of used flags on the game */
342 int flags_used;
344 /* Because mines are set after the first move... */
345 bool no_mines = true;
347 /* We need a stack (created on discover()) for the cascade algorithm. */
348 int stack_pos = 0;
350 /* a usefull string for snprintf */
351 char str[30];
353 #ifdef HAVE_TOUCHSCREEN
355 #include "lib/pluginlib_touchscreen.h"
356 static struct ts_raster mine_raster = { 0, 0, MAX_WIDTH, MAX_HEIGHT, TileSize, TileSize };
357 #endif
360 void push( int *stack, int y, int x )
362 if( stack_pos <= height*width )
364 stack[++stack_pos] = y;
365 stack[++stack_pos] = x;
369 /* Unveil tiles and push them to stack if they are empty. */
370 void unveil( int *stack, int y, int x )
372 if( x < 0 || y < 0 || x > width - 1 || y > height - 1
373 || minefield[y][x].known
374 || minefield[y][x].mine || minefield[y][x].flag ) return;
376 minefield[y][x].known = 1;
378 if( minefield[y][x].neighbors == 0 )
379 push( stack, y, x );
382 void discover( int y, int x )
384 int stack[height*width];
386 /* Selected tile. */
387 if( x < 0 || y < 0 || x > width - 1 || y > height - 1
388 || minefield[y][x].known
389 || minefield[y][x].mine || minefield[y][x].flag ) return;
391 minefield[y][x].known = 1;
392 /* Exit if the tile is not empty. (no mines nearby) */
393 if( minefield[y][x].neighbors ) return;
395 push( stack, y, x );
397 /* Scan all nearby tiles. If we meet a tile with a number we just unveil
398 * it. If we meet an empty tile, we push the location in stack. For each
399 * location in stack we do the same thing. (scan again all nearby tiles)
401 while( stack_pos )
403 /* Pop x, y from stack. */
404 x = stack[stack_pos--];
405 y = stack[stack_pos--];
407 unveil( stack, y-1, x-1 );
408 unveil( stack, y-1, x );
409 unveil( stack, y-1, x+1 );
410 unveil( stack, y, x+1 );
411 unveil( stack, y+1, x+1 );
412 unveil( stack, y+1, x );
413 unveil( stack, y+1, x-1 );
414 unveil( stack, y, x-1 );
418 /* Reset the whole board for a new game. */
419 void minesweeper_init( void )
421 int i,j;
423 for( i = 0; i < MAX_HEIGHT; i++ )
425 for( j = 0; j < MAX_WIDTH; j++ )
427 minefield[i][j].known = 0;
428 minefield[i][j].flag = 0;
429 minefield[i][j].mine = 0;
430 minefield[i][j].neighbors = 0;
433 no_mines = true;
434 tiles_left = width*height;
438 /* put mines on the mine field */
439 /* there is p% chance that a tile is a mine */
440 /* if the tile has coordinates (x,y), then it can't be a mine */
441 void minesweeper_putmines( int p, int x, int y )
443 int i,j;
445 mine_num = 0;
446 for( i = 0; i < height; i++ )
448 for( j = 0; j < width; j++ )
450 if( rb->rand()%100 < p && !( y==i && x==j ) )
452 minefield[i][j].mine = 1;
453 mine_num++;
455 else
457 minefield[i][j].mine = 0;
459 minefield[i][j].neighbors = 0;
463 /* we need to compute the neighbor element for each tile */
464 for( i = 0; i < height; i++ )
466 for( j = 0; j < width; j++ )
468 if( i > 0 )
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;
476 if( j > 0 )
477 minefield[i][j].neighbors += minefield[i][j-1].mine;
478 if( j < width - 1 )
479 minefield[i][j].neighbors += minefield[i][j+1].mine;
480 if( i < height - 1 )
482 if( j > 0 )
483 minefield[i][j].neighbors += minefield[i+1][j-1].mine;
484 minefield[i][j].neighbors += minefield[i+1][j].mine;
485 if( j < width - 1 )
486 minefield[i][j].neighbors += minefield[i+1][j+1].mine;
491 no_mines = false;
493 /* In case the user is lucky and there are no mines positioned. */
494 if( !mine_num && height*width != 1 )
496 minesweeper_putmines(p, x, y);
500 /* A function that will uncover all the board, when the user wins or loses.
501 can easily be expanded, (just a call assigned to a button) as a solver. */
502 void mine_show( void )
504 int i, j, button;
506 for( i = 0; i < height; i++ )
508 for( j = 0; j < width; j++ )
510 if( minefield[i][j].mine )
512 if( minefield[i][j].known )
514 draw_tile( ExplodedMine, j, i );
516 else
518 draw_tile( Mine, j, i );
521 else
523 draw_tile( minefield[i][j].neighbors, j, i );
527 rb->lcd_update();
530 button = rb->button_get(true);
531 while( ( button == BUTTON_NONE )
532 || ( button & (BUTTON_REL|BUTTON_REPEAT) ) );
533 #ifdef HAVE_TOUCHSCREEN
534 button = BUTTON_NONE;
535 #endif
538 int count_tiles_left( void )
540 int tiles_left = 0;
541 int i, j;
542 for( i = 0; i < height; i++ )
543 for( j = 0; j < width; j++ )
544 if( minefield[i][j].known == 0 )
545 tiles_left++;
546 return tiles_left;
549 int count_flags( void )
551 int flags_used = 0;
552 int i, j;
553 for( i = 0; i < height; i++ )
554 for( j = 0; j < width; j++ )
555 if( minefield[i][j].flag == 1 )
556 flags_used++;
557 return flags_used;
560 /* welcome screen where player can chose mine percentage */
561 enum minesweeper_status menu( void )
563 int selection = 0, result = MINESWEEPER_QUIT;
564 bool menu_quit = false;
566 MENUITEM_STRINGLIST( menu, "Minesweeper Menu", NULL, "Play Minesweeper",
567 "Mine Percentage", "Number of Rows",
568 "Number of Columns", "Playback Control", "Quit" );
570 #ifdef HAVE_LCD_COLOR
571 rb->lcd_set_foreground( rb->global_settings->fg_color );
572 rb->lcd_set_background( rb->global_settings->bg_color );
573 #endif
575 while( !menu_quit )
577 switch( rb->do_menu( &menu, &selection, NULL, false ) )
579 case 0:
580 result = MINESWEEPER_WIN; /* start playing */
581 menu_quit = true;
582 break;
584 case 1:
585 rb->set_int( "Mine Percentage", "%", UNIT_INT, &p, NULL,
586 1, 2, 98, NULL );
587 break;
589 case 2:
590 rb->set_int( "Number of Rows", "", UNIT_INT, &height, NULL,
591 1, 1, MAX_HEIGHT, NULL );
592 break;
594 case 3:
595 rb->set_int( "Number of Columns", "", UNIT_INT, &width, NULL,
596 1, 1, MAX_WIDTH, NULL );
597 break;
599 case 4:
600 playback_control( NULL );
601 break;
603 default:
604 result = MINESWEEPER_QUIT; /* quit program */
605 menu_quit = true;
606 break;
610 return result;
613 /* the big and ugly game function */
614 enum minesweeper_status minesweeper( void )
616 int i, j;
617 int button;
618 int lastbutton = BUTTON_NONE;
620 /* the cursor coordinates */
621 int x=0, y=0;
624 * Show the menu
626 if( ( i = menu() ) != MINESWEEPER_WIN ) return i;
629 * Init game
631 top = (LCD_HEIGHT-height*TileSize)/2;
632 left = (LCD_WIDTH-width*TileSize)/2;
634 #ifdef HAVE_TOUCHSCREEN
635 mine_raster.tl_x = left;
636 mine_raster.tl_y = top;
637 mine_raster.width = width*TileSize;
638 mine_raster.height = height*TileSize;
639 #endif
641 rb->srand( *rb->current_tick );
642 minesweeper_init();
643 x = 0;
644 y = 0;
647 * Play
649 while( true )
652 /* clear the screen buffer */
653 #ifdef HAVE_LCD_COLOR
654 rb->lcd_set_background( BackgroundColor );
655 #endif
656 rb->lcd_clear_display();
658 /* display the mine field */
659 for( i = 0; i < height; i++ )
661 for( j = 0; j < width; j++ )
663 if( minefield[i][j].known )
665 draw_tile( minefield[i][j].neighbors, j, i );
667 else if(minefield[i][j].flag)
669 draw_tile( Flag, j, i );
671 else
673 draw_tile( Unknown, j, i );
678 /* display the cursor */
679 invert_tile( x, y );
681 /* update the screen */
682 rb->lcd_update();
684 button = rb->button_get(true);
685 #ifdef HAVE_TOUCHSCREEN
686 if(button & BUTTON_TOUCHSCREEN)
688 struct ts_raster_result res;
689 if(touchscreen_map_raster(&mine_raster, rb->button_get_data() >> 16, rb->button_get_data() & 0xffff, &res) == 1)
691 button &= ~BUTTON_TOUCHSCREEN;
692 lastbutton &= ~BUTTON_TOUCHSCREEN;
694 if(button & BUTTON_REPEAT && lastbutton != MINESWP_TOGGLE && lastbutton ^ BUTTON_REPEAT)
695 button = MINESWP_TOGGLE;
696 else if(button == BUTTON_REL && lastbutton ^ BUTTON_REPEAT)
697 button = MINESWP_DISCOVER;
698 else
699 button |= BUTTON_TOUCHSCREEN;
701 x = res.x;
702 y = res.y;
705 #endif
706 switch(button)
708 /* quit minesweeper (you really shouldn't use this button ...) */
709 #ifdef MINESWP_RC_QUIT
710 case MINESWP_RC_QUIT:
711 #endif
712 case MINESWP_QUIT:
713 return MINESWEEPER_QUIT;
715 /* move cursor left */
716 case MINESWP_LEFT:
717 case MINESWP_LEFT|BUTTON_REPEAT:
718 x = ( x + width - 1 )%width;
719 break;
721 /* move cursor right */
722 case MINESWP_RIGHT:
723 case MINESWP_RIGHT|BUTTON_REPEAT:
724 x = ( x + 1 )%width;
725 break;
727 /* move cursor down */
728 case MINESWP_DOWN:
729 case MINESWP_DOWN|BUTTON_REPEAT:
730 y = ( y + 1 )%height;
731 break;
733 /* move cursor up */
734 case MINESWP_UP:
735 case MINESWP_UP|BUTTON_REPEAT:
736 y = ( y + height - 1 )%height;
737 break;
739 /*move cursor though the entire field*/
740 #ifdef MINESWP_SCROLLWHEEL
741 case MINESWP_NEXT:
742 case MINESWP_NEXT|BUTTON_REPEAT:
743 if (x == width -1 ) {
744 y = ( y + 1 )%height;
746 x = ( x + 1 )%width;
747 break;
749 case MINESWP_PREV:
750 case MINESWP_PREV|BUTTON_REPEAT:
751 if (x == 0) {
752 y = ( y + height - 1 )%height;
754 x = ( x + width - 1 )%width;
755 break;
756 #endif
757 /* discover a tile (and it's neighbors if .neighbors == 0) */
758 case MINESWP_DISCOVER:
759 #ifdef MINESWP_DISCOVER2
760 case MINESWP_DISCOVER2:
761 #endif
762 if( minefield[y][x].flag ) break;
763 /* we put the mines on the first "click" so that you don't
764 * lose on the first "click" */
765 if( tiles_left == width*height && no_mines )
766 minesweeper_putmines(p,x,y);
768 discover(y, x);
770 if( minefield[y][x].mine )
772 minefield[y][x].known = 1;
773 return MINESWEEPER_LOSE;
775 tiles_left = count_tiles_left();
776 if( tiles_left == mine_num )
778 return MINESWEEPER_WIN;
780 break;
782 /* toggle flag under cursor */
783 case MINESWP_TOGGLE:
784 #ifdef MINESWP_TOGGLE_PRE
785 if( lastbutton != MINESWP_TOGGLE_PRE )
786 break;
787 #endif
788 #ifdef MINESWP_TOGGLE2
789 case MINESWP_TOGGLE2:
790 #endif
791 minefield[y][x].flag = ( minefield[y][x].flag + 1 )%2;
792 break;
794 /* show how many mines you think you have found and how many
795 * there really are on the game */
796 case MINESWP_INFO:
797 if( no_mines )
798 break;
799 flags_used = count_flags();
800 if (flags_used == 1) {
801 rb->splashf( HZ*2, "You marked 1 field. There are %d mines.",
802 mine_num );
804 else
806 rb->splashf( HZ*2, "You marked %d fields. There are %d mines.",
807 flags_used, mine_num );
809 break;
811 default:
812 if( rb->default_event_handler( button ) == SYS_USB_CONNECTED )
813 return MINESWEEPER_USB;
814 break;
816 if( button != BUTTON_NONE )
817 lastbutton = button;
822 /* plugin entry point */
823 enum plugin_status plugin_start(const void* parameter)
825 bool exit = false;
827 (void)parameter;
828 #if LCD_DEPTH > 1
829 rb->lcd_set_backdrop(NULL);
830 #endif
832 while( !exit )
834 switch( minesweeper() )
836 case MINESWEEPER_WIN:
837 rb->splash( HZ, "You Win!" );
838 rb->lcd_clear_display();
839 mine_show();
840 break;
842 case MINESWEEPER_LOSE:
843 rb->splash( HZ, "You Lose!" );
844 rb->lcd_clear_display();
845 mine_show();
846 break;
848 case MINESWEEPER_USB:
849 return PLUGIN_USB_CONNECTED;
851 case MINESWEEPER_QUIT:
852 exit = true;
853 break;
855 default:
856 break;
860 return PLUGIN_OK;
863 #endif