1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2004-2006 Antoine Cellerier <dionoea -at- videolan -dot- org>
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
22 #ifdef HAVE_LCD_BITMAP
26 /* what the minesweeper() function can return */
27 enum minesweeper_status
{
34 /* variable button definitions */
35 #if CONFIG_KEYPAD == RECORDER_PAD
36 # define MINESWP_UP BUTTON_UP
37 # define MINESWP_DOWN BUTTON_DOWN
38 # define MINESWP_QUIT BUTTON_OFF
39 # define MINESWP_TOGGLE BUTTON_ON
40 # define MINESWP_TOGGLE2 BUTTON_F1
41 # define MINESWP_DISCOVER BUTTON_PLAY
42 # define MINESWP_DISCOVER2 BUTTON_F2
43 # define MINESWP_INFO BUTTON_F3
45 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
46 # define MINESWP_UP BUTTON_UP
47 # define MINESWP_DOWN BUTTON_DOWN
48 # define MINESWP_QUIT BUTTON_OFF
49 # define MINESWP_TOGGLE BUTTON_ON
50 # define MINESWP_TOGGLE2 BUTTON_F1
51 # define MINESWP_DISCOVER BUTTON_SELECT
52 # define MINESWP_DISCOVER2 BUTTON_F2
53 # define MINESWP_INFO BUTTON_F3
55 #elif CONFIG_KEYPAD == ONDIO_PAD
56 # define MINESWP_UP BUTTON_UP
57 # define MINESWP_DOWN BUTTON_DOWN
58 # define MINESWP_QUIT BUTTON_OFF
59 # define MINESWP_TOGGLE_PRE BUTTON_MENU
60 # define MINESWP_TOGGLE (BUTTON_MENU | BUTTON_REL)
61 # define MINESWP_DISCOVER (BUTTON_MENU | BUTTON_REPEAT)
62 # define MINESWP_INFO (BUTTON_MENU | BUTTON_OFF)
64 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
65 (CONFIG_KEYPAD == IRIVER_H300_PAD)
66 # define MINESWP_UP BUTTON_UP
67 # define MINESWP_DOWN BUTTON_DOWN
68 # define MINESWP_QUIT BUTTON_OFF
69 # define MINESWP_TOGGLE BUTTON_ON
70 # define MINESWP_TOGGLE2 BUTTON_REC
71 # define MINESWP_DISCOVER BUTTON_SELECT
72 # define MINESWP_INFO BUTTON_MODE
74 # define MINESWP_RC_QUIT BUTTON_RC_STOP
76 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
77 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
78 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
79 # define MINESWP_SCROLLWHEEL
80 # define MINESWP_UP BUTTON_MENU
81 # define MINESWP_DOWN BUTTON_PLAY
82 # define MINESWP_NEXT BUTTON_SCROLL_FWD
83 # define MINESWP_PREV BUTTON_SCROLL_BACK
84 # define MINESWP_QUIT (BUTTON_SELECT | BUTTON_MENU)
85 # define MINESWP_TOGGLE_PRE BUTTON_SELECT
86 # define MINESWP_TOGGLE (BUTTON_SELECT | BUTTON_REL)
87 # define MINESWP_DISCOVER (BUTTON_SELECT | BUTTON_REPEAT)
88 # define MINESWP_INFO (BUTTON_SELECT | BUTTON_PLAY)
90 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
91 # define MINESWP_UP BUTTON_UP
92 # define MINESWP_DOWN BUTTON_DOWN
93 # define MINESWP_QUIT BUTTON_POWER
94 # define MINESWP_TOGGLE BUTTON_PLAY
95 # define MINESWP_DISCOVER BUTTON_SELECT
96 # define MINESWP_INFO BUTTON_REC
98 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
99 # define MINESWP_UP BUTTON_UP
100 # define MINESWP_DOWN BUTTON_DOWN
101 # define MINESWP_QUIT BUTTON_POWER
102 # define MINESWP_TOGGLE BUTTON_A
103 # define MINESWP_DISCOVER BUTTON_SELECT
104 # define MINESWP_INFO BUTTON_MENU
106 #elif (CONFIG_KEYPAD == SANSA_E200_PAD)
107 # define MINESWP_SCROLLWHEEL
108 # define MINESWP_UP BUTTON_UP
109 # define MINESWP_DOWN BUTTON_DOWN
110 # define MINESWP_QUIT BUTTON_POWER
111 # define MINESWP_NEXT BUTTON_SCROLL_FWD
112 # define MINESWP_PREV BUTTON_SCROLL_BACK
113 # define MINESWP_TOGGLE BUTTON_REC
114 # define MINESWP_DISCOVER BUTTON_SELECT
115 # define MINESWP_INFO (BUTTON_REC|BUTTON_REPEAT)
117 #elif (CONFIG_KEYPAD == SANSA_C200_PAD)
118 # define MINESWP_UP BUTTON_UP
119 # define MINESWP_DOWN BUTTON_DOWN
120 # define MINESWP_QUIT BUTTON_POWER
121 # define MINESWP_TOGGLE_PRE BUTTON_SELECT
122 # define MINESWP_TOGGLE (BUTTON_SELECT | BUTTON_REL)
123 # define MINESWP_TOGGLE2 BUTTON_VOL_DOWN
124 # define MINESWP_DISCOVER (BUTTON_SELECT | BUTTON_REPEAT)
125 # define MINESWP_DISCOVER2 BUTTON_VOL_UP
126 # define MINESWP_INFO BUTTON_REC
128 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
129 # define MINESWP_UP BUTTON_SCROLL_UP
130 # define MINESWP_DOWN BUTTON_SCROLL_DOWN
131 # define MINESWP_QUIT BUTTON_POWER
132 # define MINESWP_TOGGLE BUTTON_PLAY
133 # define MINESWP_DISCOVER BUTTON_REW
134 # define MINESWP_INFO (BUTTON_REW | BUTTON_PLAY)
136 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
137 # define MINESWP_UP BUTTON_UP
138 # define MINESWP_DOWN BUTTON_DOWN
139 # define MINESWP_QUIT BUTTON_BACK
140 # define MINESWP_TOGGLE BUTTON_PLAY
141 # define MINESWP_DISCOVER BUTTON_SELECT
142 # define MINESWP_INFO BUTTON_MENU
144 #elif (CONFIG_KEYPAD == MROBE100_PAD)
145 # define MINESWP_UP BUTTON_UP
146 # define MINESWP_DOWN BUTTON_DOWN
147 # define MINESWP_QUIT BUTTON_POWER
148 # define MINESWP_TOGGLE BUTTON_DISPLAY
149 # define MINESWP_DISCOVER BUTTON_SELECT
150 # define MINESWP_INFO BUTTON_MENU
153 #error No keymap defined!
156 /* here is a global api struct pointer. while not strictly necessary,
157 * it's nice not to have to pass the api pointer in all function calls
160 static struct plugin_api
*rb
;
162 extern const fb_data minesweeper_tiles
[];
164 #ifdef HAVE_LCD_COLOR
165 # if ( LCD_HEIGHT * LCD_WIDTH ) / ( 16 * 16 ) >= 130
166 /* We want to have at least 130 tiles on the screen */
168 # elif ( LCD_HEIGHT * LCD_WIDTH ) / ( 12 * 12 ) >= 130
173 # define BackgroundColor LCD_RGBPACK( 128, 128, 128 )
183 #define ExplodedMine 12
185 #define draw_tile( num, x, y ) \
186 rb->lcd_bitmap_part( minesweeper_tiles, 0, num * TileSize, \
187 TileSize, left+x*TileSize, top+y*TileSize, \
190 #define invert_tile( x, y ) \
191 rb->lcd_set_drawmode(DRMODE_COMPLEMENT); \
192 rb->lcd_fillrect( left+x*TileSize, top+y*TileSize, TileSize, TileSize ); \
193 rb->lcd_set_drawmode(DRMODE_SOLID);
197 * if there is a mine, mine is true
198 * if tile is known by player, known is true
199 * if tile has a flag, flag is true
200 * neighbors is the total number of mines arround tile
204 unsigned char mine
: 1;
205 unsigned char known
: 1;
206 unsigned char flag
: 1;
207 unsigned char neighbors
: 4;
210 /* the height and width of the field */
211 #define MAX_HEIGHT (LCD_HEIGHT/TileSize)
212 #define MAX_WIDTH (LCD_WIDTH/TileSize)
213 int height
= MAX_HEIGHT
;
214 int width
= MAX_WIDTH
;
218 /* The Minefield. Caution it is defined as Y, X! Not the opposite. */
219 tile minefield
[MAX_HEIGHT
][MAX_WIDTH
];
221 /* total number of mines on the game */
224 /* percentage of mines on minefield used during generation */
227 /* number of tiles left on the game */
230 /* number of used flags on the game */
233 /* Because mines are set after the first move... */
234 bool no_mines
= true;
236 /* We need a stack (created on discover()) for the cascade algorithm. */
239 /* a usefull string for snprintf */
243 void push( int *stack
, int y
, int x
)
245 if( stack_pos
<= height
*width
)
247 stack
[++stack_pos
] = y
;
248 stack
[++stack_pos
] = x
;
252 /* Unveil tiles and push them to stack if they are empty. */
253 void unveil( int *stack
, int y
, int x
)
255 if( x
< 0 || y
< 0 || x
> width
- 1 || y
> height
- 1
256 || minefield
[y
][x
].known
257 || minefield
[y
][x
].mine
|| minefield
[y
][x
].flag
) return;
259 minefield
[y
][x
].known
= 1;
261 if( minefield
[y
][x
].neighbors
== 0 )
265 void discover( int y
, int x
)
267 int stack
[height
*width
];
270 if( x
< 0 || y
< 0 || x
> width
- 1 || y
> height
- 1
271 || minefield
[y
][x
].known
272 || minefield
[y
][x
].mine
|| minefield
[y
][x
].flag
) return;
274 minefield
[y
][x
].known
= 1;
275 /* Exit if the tile is not empty. (no mines nearby) */
276 if( minefield
[y
][x
].neighbors
) return;
280 /* Scan all nearby tiles. If we meet a tile with a number we just unveil
281 * it. If we meet an empty tile, we push the location in stack. For each
282 * location in stack we do the same thing. (scan again all nearby tiles)
286 /* Pop x, y from stack. */
287 x
= stack
[stack_pos
--];
288 y
= stack
[stack_pos
--];
290 unveil( stack
, y
-1, x
-1 );
291 unveil( stack
, y
-1, x
);
292 unveil( stack
, y
-1, x
+1 );
293 unveil( stack
, y
, x
+1 );
294 unveil( stack
, y
+1, x
+1 );
295 unveil( stack
, y
+1, x
);
296 unveil( stack
, y
+1, x
-1 );
297 unveil( stack
, y
, x
-1 );
301 /* Reset the whole board for a new game. */
302 void minesweeper_init( void )
306 for( i
= 0; i
< MAX_HEIGHT
; i
++ )
308 for( j
= 0; j
< MAX_WIDTH
; j
++ )
310 minefield
[i
][j
].known
= 0;
311 minefield
[i
][j
].flag
= 0;
312 minefield
[i
][j
].mine
= 0;
313 minefield
[i
][j
].neighbors
= 0;
317 tiles_left
= width
*height
;
321 /* put mines on the mine field */
322 /* there is p% chance that a tile is a mine */
323 /* if the tile has coordinates (x,y), then it can't be a mine */
324 void minesweeper_putmines( int p
, int x
, int y
)
329 for( i
= 0; i
< height
; i
++ )
331 for( j
= 0; j
< width
; j
++ )
333 if( rb
->rand()%100 < p
&& !( y
==i
&& x
==j
) )
335 minefield
[i
][j
].mine
= 1;
340 minefield
[i
][j
].mine
= 0;
342 minefield
[i
][j
].neighbors
= 0;
346 /* we need to compute the neighbor element for each tile */
347 for( i
= 0; i
< height
; i
++ )
349 for( j
= 0; j
< width
; j
++ )
354 minefield
[i
][j
].neighbors
+= minefield
[i
-1][j
-1].mine
;
355 minefield
[i
][j
].neighbors
+= minefield
[i
-1][j
].mine
;
357 minefield
[i
][j
].neighbors
+= minefield
[i
-1][j
+1].mine
;
360 minefield
[i
][j
].neighbors
+= minefield
[i
][j
-1].mine
;
362 minefield
[i
][j
].neighbors
+= minefield
[i
][j
+1].mine
;
366 minefield
[i
][j
].neighbors
+= minefield
[i
+1][j
-1].mine
;
367 minefield
[i
][j
].neighbors
+= minefield
[i
+1][j
].mine
;
369 minefield
[i
][j
].neighbors
+= minefield
[i
+1][j
+1].mine
;
376 /* In case the user is lucky and there are no mines positioned. */
377 if( !mine_num
&& height
*width
!= 1 )
379 minesweeper_putmines(p
, x
, y
);
383 /* A function that will uncover all the board, when the user wins or loses.
384 can easily be expanded, (just a call assigned to a button) as a solver. */
385 void mine_show( void )
389 for( i
= 0; i
< height
; i
++ )
391 for( j
= 0; j
< width
; j
++ )
393 if( minefield
[i
][j
].mine
)
395 if( minefield
[i
][j
].known
)
397 draw_tile( ExplodedMine
, j
, i
);
401 draw_tile( Mine
, j
, i
);
406 draw_tile( minefield
[i
][j
].neighbors
, j
, i
);
413 button
= rb
->button_get(true);
414 while( ( button
== BUTTON_NONE
)
415 || ( button
& (BUTTON_REL
|BUTTON_REPEAT
) ) );
418 int count_tiles_left( void )
422 for( i
= 0; i
< height
; i
++ )
423 for( j
= 0; j
< width
; j
++ )
424 if( minefield
[i
][j
].known
== 0 )
429 int count_flags( void )
433 for( i
= 0; i
< height
; i
++ )
434 for( j
= 0; j
< width
; j
++ )
435 if( minefield
[i
][j
].flag
== 1 )
440 /* welcome screen where player can chose mine percentage */
441 enum minesweeper_status
menu( void )
443 int selection
, result
= MINESWEEPER_QUIT
;
444 bool menu_quit
= false;
446 MENUITEM_STRINGLIST( menu
, "Minesweeper Menu", NULL
, "Play Minesweeper",
447 "Mine Percentage", "Number of Rows",
448 "Number of Columns", "Quit" );
450 #ifdef HAVE_LCD_COLOR
451 rb
->lcd_set_foreground( rb
->global_settings
->fg_color
);
452 rb
->lcd_set_background( rb
->global_settings
->bg_color
);
457 switch( rb
->do_menu( &menu
, &selection
) )
460 result
= MINESWEEPER_WIN
; /* start playing */
465 rb
->set_int( "Mine Percentage", "%", UNIT_INT
, &p
, NULL
,
470 rb
->set_int( "Number of Rows", "", UNIT_INT
, &height
, NULL
,
471 1, 1, MAX_HEIGHT
, NULL
);
475 rb
->set_int( "Number of Columns", "", UNIT_INT
, &width
, NULL
,
476 1, 1, MAX_WIDTH
, NULL
);
480 result
= MINESWEEPER_QUIT
; /* quit program */
489 /* the big and ugly game function */
490 enum minesweeper_status
minesweeper( void )
494 int lastbutton
= BUTTON_NONE
;
496 /* the cursor coordinates */
502 if( ( i
= menu() ) != MINESWEEPER_WIN
) return i
;
507 top
= (LCD_HEIGHT
-height
*TileSize
)/2;
508 left
= (LCD_WIDTH
-width
*TileSize
)/2;
510 rb
->srand( *rb
->current_tick
);
521 /* clear the screen buffer */
522 #ifdef HAVE_LCD_COLOR
523 rb
->lcd_set_background( BackgroundColor
);
525 rb
->lcd_clear_display();
527 /* display the mine field */
528 for( i
= 0; i
< height
; i
++ )
530 for( j
= 0; j
< width
; j
++ )
532 if( minefield
[i
][j
].known
)
534 draw_tile( minefield
[i
][j
].neighbors
, j
, i
);
536 else if(minefield
[i
][j
].flag
)
538 draw_tile( Flag
, j
, i
);
542 draw_tile( Unknown
, j
, i
);
547 /* display the cursor */
550 /* update the screen */
553 switch( button
= rb
->button_get( true ) )
555 /* quit minesweeper (you really shouldn't use this button ...) */
556 #ifdef MINESWP_RC_QUIT
557 case MINESWP_RC_QUIT
:
560 return MINESWEEPER_QUIT
;
562 /* move cursor left */
564 case BUTTON_LEFT
|BUTTON_REPEAT
:
565 x
= ( x
+ width
- 1 )%width
;
568 /* move cursor right */
570 case BUTTON_RIGHT
|BUTTON_REPEAT
:
574 /* move cursor down */
576 case MINESWP_DOWN
|BUTTON_REPEAT
:
577 y
= ( y
+ 1 )%height
;
582 case MINESWP_UP
|BUTTON_REPEAT
:
583 y
= ( y
+ height
- 1 )%height
;
586 /*move cursor though the entire field*/
587 #ifdef MINESWP_SCROLLWHEEL
589 case MINESWP_NEXT
|BUTTON_REPEAT
:
590 if (x
== width
-1 ) {
591 y
= ( y
+ 1 )%height
;
597 case MINESWP_PREV
|BUTTON_REPEAT
:
599 y
= ( y
+ height
- 1 )%height
;
601 x
= ( x
+ width
- 1 )%width
;
604 /* discover a tile (and it's neighbors if .neighbors == 0) */
605 case MINESWP_DISCOVER
:
606 #ifdef MINESWP_DISCOVER2
607 case MINESWP_DISCOVER2
:
609 if( minefield
[y
][x
].flag
) break;
610 /* we put the mines on the first "click" so that you don't
611 * lose on the first "click" */
612 if( tiles_left
== width
*height
&& no_mines
)
613 minesweeper_putmines(p
,x
,y
);
617 if( minefield
[y
][x
].mine
)
619 minefield
[y
][x
].known
= 1;
620 return MINESWEEPER_LOSE
;
622 tiles_left
= count_tiles_left();
623 if( tiles_left
== mine_num
)
625 return MINESWEEPER_WIN
;
629 /* toggle flag under cursor */
631 #ifdef MINESWP_TOGGLE_PRE
632 if( lastbutton
!= MINESWP_TOGGLE_PRE
)
635 #ifdef MINESWP_TOGGLE2
636 case MINESWP_TOGGLE2
:
638 minefield
[y
][x
].flag
= ( minefield
[y
][x
].flag
+ 1 )%2;
641 /* show how many mines you think you have found and how many
642 * there really are on the game */
646 flags_used
= count_flags();
647 if (flags_used
== 1) {
648 rb
->splash( HZ
*2, "You marked 1 field. There are %d mines.",
653 rb
->splash( HZ
*2, "You marked %d fields. There are %d mines.",
654 flags_used
, mine_num
);
659 if( rb
->default_event_handler( button
) == SYS_USB_CONNECTED
)
660 return MINESWEEPER_USB
;
663 if( button
!= BUTTON_NONE
)
669 /* plugin entry point */
670 enum plugin_status
plugin_start(struct plugin_api
* api
, void* parameter
)
677 rb
->lcd_set_backdrop(NULL
);
682 switch( minesweeper() )
684 case MINESWEEPER_WIN
:
685 rb
->splash( HZ
, "You Win!" );
686 rb
->lcd_clear_display();
690 case MINESWEEPER_LOSE
:
691 rb
->splash( HZ
, "You Lose!" );
692 rb
->lcd_clear_display();
696 case MINESWEEPER_USB
:
697 return PLUGIN_USB_CONNECTED
;
699 case MINESWEEPER_QUIT
: