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_LEFT BUTTON_LEFT
37 # define MINESWP_RIGHT BUTTON_RIGHT
38 # define MINESWP_UP BUTTON_UP
39 # define MINESWP_DOWN BUTTON_DOWN
40 # define MINESWP_QUIT BUTTON_OFF
41 # define MINESWP_TOGGLE BUTTON_ON
42 # define MINESWP_TOGGLE2 BUTTON_F1
43 # define MINESWP_DISCOVER BUTTON_PLAY
44 # define MINESWP_DISCOVER2 BUTTON_F2
45 # define MINESWP_INFO BUTTON_F3
47 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
48 # define MINESWP_LEFT BUTTON_LEFT
49 # define MINESWP_RIGHT BUTTON_RIGHT
50 # define MINESWP_UP BUTTON_UP
51 # define MINESWP_DOWN BUTTON_DOWN
52 # define MINESWP_QUIT BUTTON_OFF
53 # define MINESWP_TOGGLE BUTTON_ON
54 # define MINESWP_TOGGLE2 BUTTON_F1
55 # define MINESWP_DISCOVER BUTTON_SELECT
56 # define MINESWP_DISCOVER2 BUTTON_F2
57 # define MINESWP_INFO BUTTON_F3
59 #elif CONFIG_KEYPAD == ONDIO_PAD
60 # define MINESWP_LEFT BUTTON_LEFT
61 # define MINESWP_RIGHT BUTTON_RIGHT
62 # define MINESWP_UP BUTTON_UP
63 # define MINESWP_DOWN BUTTON_DOWN
64 # define MINESWP_QUIT BUTTON_OFF
65 # define MINESWP_TOGGLE_PRE BUTTON_MENU
66 # define MINESWP_TOGGLE (BUTTON_MENU | BUTTON_REL)
67 # define MINESWP_DISCOVER (BUTTON_MENU | BUTTON_REPEAT)
68 # define MINESWP_INFO (BUTTON_MENU | BUTTON_OFF)
70 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
71 (CONFIG_KEYPAD == IRIVER_H300_PAD)
72 # define MINESWP_LEFT BUTTON_LEFT
73 # define MINESWP_RIGHT BUTTON_RIGHT
74 # define MINESWP_UP BUTTON_UP
75 # define MINESWP_DOWN BUTTON_DOWN
76 # define MINESWP_QUIT BUTTON_OFF
77 # define MINESWP_TOGGLE BUTTON_ON
78 # define MINESWP_TOGGLE2 BUTTON_REC
79 # define MINESWP_DISCOVER BUTTON_SELECT
80 # define MINESWP_INFO BUTTON_MODE
82 # define MINESWP_RC_QUIT BUTTON_RC_STOP
84 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
85 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
86 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
87 # define MINESWP_SCROLLWHEEL
88 # define MINESWP_LEFT BUTTON_LEFT
89 # define MINESWP_RIGHT BUTTON_RIGHT
90 # define MINESWP_UP BUTTON_MENU
91 # define MINESWP_DOWN BUTTON_PLAY
92 # define MINESWP_NEXT BUTTON_SCROLL_FWD
93 # define MINESWP_PREV BUTTON_SCROLL_BACK
94 # define MINESWP_QUIT (BUTTON_SELECT | BUTTON_MENU)
95 # define MINESWP_TOGGLE_PRE BUTTON_SELECT
96 # define MINESWP_TOGGLE (BUTTON_SELECT | BUTTON_REL)
97 # define MINESWP_DISCOVER (BUTTON_SELECT | BUTTON_REPEAT)
98 # define MINESWP_INFO (BUTTON_SELECT | BUTTON_PLAY)
100 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
101 # define MINESWP_LEFT BUTTON_LEFT
102 # define MINESWP_RIGHT BUTTON_RIGHT
103 # define MINESWP_UP BUTTON_UP
104 # define MINESWP_DOWN BUTTON_DOWN
105 # define MINESWP_QUIT BUTTON_POWER
106 # define MINESWP_TOGGLE BUTTON_PLAY
107 # define MINESWP_DISCOVER BUTTON_SELECT
108 # define MINESWP_INFO BUTTON_REC
110 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
111 # define MINESWP_LEFT BUTTON_LEFT
112 # define MINESWP_RIGHT BUTTON_RIGHT
113 # define MINESWP_UP BUTTON_UP
114 # define MINESWP_DOWN BUTTON_DOWN
115 # define MINESWP_QUIT BUTTON_POWER
116 # define MINESWP_TOGGLE BUTTON_A
117 # define MINESWP_DISCOVER BUTTON_SELECT
118 # define MINESWP_INFO BUTTON_MENU
120 #elif (CONFIG_KEYPAD == SANSA_E200_PAD)
121 # define MINESWP_SCROLLWHEEL
122 # define MINESWP_LEFT BUTTON_LEFT
123 # define MINESWP_RIGHT BUTTON_RIGHT
124 # define MINESWP_UP BUTTON_UP
125 # define MINESWP_DOWN BUTTON_DOWN
126 # define MINESWP_QUIT BUTTON_POWER
127 # define MINESWP_NEXT BUTTON_SCROLL_FWD
128 # define MINESWP_PREV BUTTON_SCROLL_BACK
129 # define MINESWP_TOGGLE BUTTON_REC
130 # define MINESWP_DISCOVER BUTTON_SELECT
131 # define MINESWP_INFO (BUTTON_REC|BUTTON_REPEAT)
133 #elif (CONFIG_KEYPAD == SANSA_C200_PAD)
134 # define MINESWP_LEFT BUTTON_LEFT
135 # define MINESWP_RIGHT BUTTON_RIGHT
136 # define MINESWP_UP BUTTON_UP
137 # define MINESWP_DOWN BUTTON_DOWN
138 # define MINESWP_QUIT BUTTON_POWER
139 # define MINESWP_TOGGLE_PRE BUTTON_SELECT
140 # define MINESWP_TOGGLE (BUTTON_SELECT | BUTTON_REL)
141 # define MINESWP_TOGGLE2 BUTTON_VOL_DOWN
142 # define MINESWP_DISCOVER (BUTTON_SELECT | BUTTON_REPEAT)
143 # define MINESWP_DISCOVER2 BUTTON_VOL_UP
144 # define MINESWP_INFO BUTTON_REC
146 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
147 # define MINESWP_LEFT BUTTON_LEFT
148 # define MINESWP_RIGHT BUTTON_RIGHT
149 # define MINESWP_UP BUTTON_SCROLL_UP
150 # define MINESWP_DOWN BUTTON_SCROLL_DOWN
151 # define MINESWP_QUIT BUTTON_POWER
152 # define MINESWP_TOGGLE BUTTON_PLAY
153 # define MINESWP_DISCOVER BUTTON_REW
154 # define MINESWP_INFO (BUTTON_REW | BUTTON_PLAY)
156 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
157 # define MINESWP_LEFT BUTTON_LEFT
158 # define MINESWP_RIGHT BUTTON_RIGHT
159 # define MINESWP_UP BUTTON_UP
160 # define MINESWP_DOWN BUTTON_DOWN
161 # define MINESWP_QUIT BUTTON_BACK
162 # define MINESWP_TOGGLE BUTTON_PLAY
163 # define MINESWP_DISCOVER BUTTON_SELECT
164 # define MINESWP_INFO BUTTON_MENU
166 #elif (CONFIG_KEYPAD == MROBE100_PAD)
167 # define MINESWP_LEFT BUTTON_LEFT
168 # define MINESWP_RIGHT BUTTON_RIGHT
169 # define MINESWP_UP BUTTON_UP
170 # define MINESWP_DOWN BUTTON_DOWN
171 # define MINESWP_QUIT BUTTON_POWER
172 # define MINESWP_TOGGLE BUTTON_DISPLAY
173 # define MINESWP_DISCOVER BUTTON_SELECT
174 # define MINESWP_INFO BUTTON_MENU
176 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
177 # define MINESWP_LEFT BUTTON_RC_REW
178 # define MINESWP_RIGHT BUTTON_RC_FF
179 # define MINESWP_UP BUTTON_RC_VOL_UP
180 # define MINESWP_DOWN BUTTON_RC_VOL_DOWN
181 # define MINESWP_QUIT BUTTON_RC_REC
182 # define MINESWP_TOGGLE BUTTON_RC_MODE
183 # define MINESWP_DISCOVER BUTTON_RC_PLAY
184 # define MINESWP_INFO BUTTON_RC_MENU
187 #error No keymap defined!
190 /* here is a global api struct pointer. while not strictly necessary,
191 * it's nice not to have to pass the api pointer in all function calls
194 static struct plugin_api
*rb
;
196 extern const fb_data minesweeper_tiles
[];
198 #ifdef HAVE_LCD_COLOR
199 # if ( LCD_HEIGHT * LCD_WIDTH ) / ( 16 * 16 ) >= 130
200 /* We want to have at least 130 tiles on the screen */
202 # elif ( LCD_HEIGHT * LCD_WIDTH ) / ( 12 * 12 ) >= 130
207 # define BackgroundColor LCD_RGBPACK( 128, 128, 128 )
217 #define ExplodedMine 12
219 #define draw_tile( num, x, y ) \
220 rb->lcd_bitmap_part( minesweeper_tiles, 0, num * TileSize, \
221 TileSize, left+x*TileSize, top+y*TileSize, \
224 #define invert_tile( x, y ) \
225 rb->lcd_set_drawmode(DRMODE_COMPLEMENT); \
226 rb->lcd_fillrect( left+x*TileSize, top+y*TileSize, TileSize, TileSize ); \
227 rb->lcd_set_drawmode(DRMODE_SOLID);
231 * if there is a mine, mine is true
232 * if tile is known by player, known is true
233 * if tile has a flag, flag is true
234 * neighbors is the total number of mines arround tile
238 unsigned char mine
: 1;
239 unsigned char known
: 1;
240 unsigned char flag
: 1;
241 unsigned char neighbors
: 4;
244 /* the height and width of the field */
245 #define MAX_HEIGHT (LCD_HEIGHT/TileSize)
246 #define MAX_WIDTH (LCD_WIDTH/TileSize)
247 int height
= MAX_HEIGHT
;
248 int width
= MAX_WIDTH
;
252 /* The Minefield. Caution it is defined as Y, X! Not the opposite. */
253 tile minefield
[MAX_HEIGHT
][MAX_WIDTH
];
255 /* total number of mines on the game */
258 /* percentage of mines on minefield used during generation */
261 /* number of tiles left on the game */
264 /* number of used flags on the game */
267 /* Because mines are set after the first move... */
268 bool no_mines
= true;
270 /* We need a stack (created on discover()) for the cascade algorithm. */
273 /* a usefull string for snprintf */
277 void push( int *stack
, int y
, int x
)
279 if( stack_pos
<= height
*width
)
281 stack
[++stack_pos
] = y
;
282 stack
[++stack_pos
] = x
;
286 /* Unveil tiles and push them to stack if they are empty. */
287 void unveil( int *stack
, int y
, int x
)
289 if( x
< 0 || y
< 0 || x
> width
- 1 || y
> height
- 1
290 || minefield
[y
][x
].known
291 || minefield
[y
][x
].mine
|| minefield
[y
][x
].flag
) return;
293 minefield
[y
][x
].known
= 1;
295 if( minefield
[y
][x
].neighbors
== 0 )
299 void discover( int y
, int x
)
301 int stack
[height
*width
];
304 if( x
< 0 || y
< 0 || x
> width
- 1 || y
> height
- 1
305 || minefield
[y
][x
].known
306 || minefield
[y
][x
].mine
|| minefield
[y
][x
].flag
) return;
308 minefield
[y
][x
].known
= 1;
309 /* Exit if the tile is not empty. (no mines nearby) */
310 if( minefield
[y
][x
].neighbors
) return;
314 /* Scan all nearby tiles. If we meet a tile with a number we just unveil
315 * it. If we meet an empty tile, we push the location in stack. For each
316 * location in stack we do the same thing. (scan again all nearby tiles)
320 /* Pop x, y from stack. */
321 x
= stack
[stack_pos
--];
322 y
= stack
[stack_pos
--];
324 unveil( stack
, y
-1, x
-1 );
325 unveil( stack
, y
-1, x
);
326 unveil( stack
, y
-1, x
+1 );
327 unveil( stack
, y
, x
+1 );
328 unveil( stack
, y
+1, x
+1 );
329 unveil( stack
, y
+1, x
);
330 unveil( stack
, y
+1, x
-1 );
331 unveil( stack
, y
, x
-1 );
335 /* Reset the whole board for a new game. */
336 void minesweeper_init( void )
340 for( i
= 0; i
< MAX_HEIGHT
; i
++ )
342 for( j
= 0; j
< MAX_WIDTH
; j
++ )
344 minefield
[i
][j
].known
= 0;
345 minefield
[i
][j
].flag
= 0;
346 minefield
[i
][j
].mine
= 0;
347 minefield
[i
][j
].neighbors
= 0;
351 tiles_left
= width
*height
;
355 /* put mines on the mine field */
356 /* there is p% chance that a tile is a mine */
357 /* if the tile has coordinates (x,y), then it can't be a mine */
358 void minesweeper_putmines( int p
, int x
, int y
)
363 for( i
= 0; i
< height
; i
++ )
365 for( j
= 0; j
< width
; j
++ )
367 if( rb
->rand()%100 < p
&& !( y
==i
&& x
==j
) )
369 minefield
[i
][j
].mine
= 1;
374 minefield
[i
][j
].mine
= 0;
376 minefield
[i
][j
].neighbors
= 0;
380 /* we need to compute the neighbor element for each tile */
381 for( i
= 0; i
< height
; i
++ )
383 for( j
= 0; j
< width
; j
++ )
388 minefield
[i
][j
].neighbors
+= minefield
[i
-1][j
-1].mine
;
389 minefield
[i
][j
].neighbors
+= minefield
[i
-1][j
].mine
;
391 minefield
[i
][j
].neighbors
+= minefield
[i
-1][j
+1].mine
;
394 minefield
[i
][j
].neighbors
+= minefield
[i
][j
-1].mine
;
396 minefield
[i
][j
].neighbors
+= minefield
[i
][j
+1].mine
;
400 minefield
[i
][j
].neighbors
+= minefield
[i
+1][j
-1].mine
;
401 minefield
[i
][j
].neighbors
+= minefield
[i
+1][j
].mine
;
403 minefield
[i
][j
].neighbors
+= minefield
[i
+1][j
+1].mine
;
410 /* In case the user is lucky and there are no mines positioned. */
411 if( !mine_num
&& height
*width
!= 1 )
413 minesweeper_putmines(p
, x
, y
);
417 /* A function that will uncover all the board, when the user wins or loses.
418 can easily be expanded, (just a call assigned to a button) as a solver. */
419 void mine_show( void )
423 for( i
= 0; i
< height
; i
++ )
425 for( j
= 0; j
< width
; j
++ )
427 if( minefield
[i
][j
].mine
)
429 if( minefield
[i
][j
].known
)
431 draw_tile( ExplodedMine
, j
, i
);
435 draw_tile( Mine
, j
, i
);
440 draw_tile( minefield
[i
][j
].neighbors
, j
, i
);
447 button
= rb
->button_get(true);
448 while( ( button
== BUTTON_NONE
)
449 || ( button
& (BUTTON_REL
|BUTTON_REPEAT
) ) );
452 int count_tiles_left( void )
456 for( i
= 0; i
< height
; i
++ )
457 for( j
= 0; j
< width
; j
++ )
458 if( minefield
[i
][j
].known
== 0 )
463 int count_flags( void )
467 for( i
= 0; i
< height
; i
++ )
468 for( j
= 0; j
< width
; j
++ )
469 if( minefield
[i
][j
].flag
== 1 )
474 /* welcome screen where player can chose mine percentage */
475 enum minesweeper_status
menu( void )
477 int selection
, result
= MINESWEEPER_QUIT
;
478 bool menu_quit
= false;
480 MENUITEM_STRINGLIST( menu
, "Minesweeper Menu", NULL
, "Play Minesweeper",
481 "Mine Percentage", "Number of Rows",
482 "Number of Columns", "Quit" );
484 #ifdef HAVE_LCD_COLOR
485 rb
->lcd_set_foreground( rb
->global_settings
->fg_color
);
486 rb
->lcd_set_background( rb
->global_settings
->bg_color
);
491 switch( rb
->do_menu( &menu
, &selection
) )
494 result
= MINESWEEPER_WIN
; /* start playing */
499 rb
->set_int( "Mine Percentage", "%", UNIT_INT
, &p
, NULL
,
504 rb
->set_int( "Number of Rows", "", UNIT_INT
, &height
, NULL
,
505 1, 1, MAX_HEIGHT
, NULL
);
509 rb
->set_int( "Number of Columns", "", UNIT_INT
, &width
, NULL
,
510 1, 1, MAX_WIDTH
, NULL
);
514 result
= MINESWEEPER_QUIT
; /* quit program */
523 /* the big and ugly game function */
524 enum minesweeper_status
minesweeper( void )
528 int lastbutton
= BUTTON_NONE
;
530 /* the cursor coordinates */
536 if( ( i
= menu() ) != MINESWEEPER_WIN
) return i
;
541 top
= (LCD_HEIGHT
-height
*TileSize
)/2;
542 left
= (LCD_WIDTH
-width
*TileSize
)/2;
544 rb
->srand( *rb
->current_tick
);
555 /* clear the screen buffer */
556 #ifdef HAVE_LCD_COLOR
557 rb
->lcd_set_background( BackgroundColor
);
559 rb
->lcd_clear_display();
561 /* display the mine field */
562 for( i
= 0; i
< height
; i
++ )
564 for( j
= 0; j
< width
; j
++ )
566 if( minefield
[i
][j
].known
)
568 draw_tile( minefield
[i
][j
].neighbors
, j
, i
);
570 else if(minefield
[i
][j
].flag
)
572 draw_tile( Flag
, j
, i
);
576 draw_tile( Unknown
, j
, i
);
581 /* display the cursor */
584 /* update the screen */
587 switch( button
= rb
->button_get( true ) )
589 /* quit minesweeper (you really shouldn't use this button ...) */
590 #ifdef MINESWP_RC_QUIT
591 case MINESWP_RC_QUIT
:
594 return MINESWEEPER_QUIT
;
596 /* move cursor left */
598 case MINESWP_LEFT
|BUTTON_REPEAT
:
599 x
= ( x
+ width
- 1 )%width
;
602 /* move cursor right */
604 case MINESWP_RIGHT
|BUTTON_REPEAT
:
608 /* move cursor down */
610 case MINESWP_DOWN
|BUTTON_REPEAT
:
611 y
= ( y
+ 1 )%height
;
616 case MINESWP_UP
|BUTTON_REPEAT
:
617 y
= ( y
+ height
- 1 )%height
;
620 /*move cursor though the entire field*/
621 #ifdef MINESWP_SCROLLWHEEL
623 case MINESWP_NEXT
|BUTTON_REPEAT
:
624 if (x
== width
-1 ) {
625 y
= ( y
+ 1 )%height
;
631 case MINESWP_PREV
|BUTTON_REPEAT
:
633 y
= ( y
+ height
- 1 )%height
;
635 x
= ( x
+ width
- 1 )%width
;
638 /* discover a tile (and it's neighbors if .neighbors == 0) */
639 case MINESWP_DISCOVER
:
640 #ifdef MINESWP_DISCOVER2
641 case MINESWP_DISCOVER2
:
643 if( minefield
[y
][x
].flag
) break;
644 /* we put the mines on the first "click" so that you don't
645 * lose on the first "click" */
646 if( tiles_left
== width
*height
&& no_mines
)
647 minesweeper_putmines(p
,x
,y
);
651 if( minefield
[y
][x
].mine
)
653 minefield
[y
][x
].known
= 1;
654 return MINESWEEPER_LOSE
;
656 tiles_left
= count_tiles_left();
657 if( tiles_left
== mine_num
)
659 return MINESWEEPER_WIN
;
663 /* toggle flag under cursor */
665 #ifdef MINESWP_TOGGLE_PRE
666 if( lastbutton
!= MINESWP_TOGGLE_PRE
)
669 #ifdef MINESWP_TOGGLE2
670 case MINESWP_TOGGLE2
:
672 minefield
[y
][x
].flag
= ( minefield
[y
][x
].flag
+ 1 )%2;
675 /* show how many mines you think you have found and how many
676 * there really are on the game */
680 flags_used
= count_flags();
681 if (flags_used
== 1) {
682 rb
->splash( HZ
*2, "You marked 1 field. There are %d mines.",
687 rb
->splash( HZ
*2, "You marked %d fields. There are %d mines.",
688 flags_used
, mine_num
);
693 if( rb
->default_event_handler( button
) == SYS_USB_CONNECTED
)
694 return MINESWEEPER_USB
;
697 if( button
!= BUTTON_NONE
)
703 /* plugin entry point */
704 enum plugin_status
plugin_start(struct plugin_api
* api
, void* parameter
)
711 rb
->lcd_set_backdrop(NULL
);
716 switch( minesweeper() )
718 case MINESWEEPER_WIN
:
719 rb
->splash( HZ
, "You Win!" );
720 rb
->lcd_clear_display();
724 case MINESWEEPER_LOSE
:
725 rb
->splash( HZ
, "You Lose!" );
726 rb
->lcd_clear_display();
730 case MINESWEEPER_USB
:
731 return PLUGIN_USB_CONNECTED
;
733 case MINESWEEPER_QUIT
: