summaryrefslogtreecommitdiffstats
path: root/apps/plugins/minesweeper.c
blob: 2500fed8ce7cd44345e89dc0225df8cc024d4b47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2004-2006 Antoine Cellerier <dionoea -at- videolan -dot- org>
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "plugin.h"

#ifdef HAVE_LCD_BITMAP

PLUGIN_HEADER

/* what the minesweeper() function can return */
enum minesweeper_status {
    MINESWEEPER_WIN,
    MINESWEEPER_LOSE,
    MINESWEEPER_QUIT,
    MINESWEEPER_USB
};

/* variable button definitions */
#if CONFIG_KEYPAD == RECORDER_PAD
#   define MINESWP_UP       BUTTON_UP
#   define MINESWP_DOWN     BUTTON_DOWN
#   define MINESWP_QUIT     BUTTON_OFF
#   define MINESWP_START    BUTTON_ON
#   define MINESWP_TOGGLE   BUTTON_PLAY
#   define MINESWP_TOGGLE2  BUTTON_F1
#   define MINESWP_DISCOVER BUTTON_ON
#   define MINESWP_DISCOVER2 BUTTON_F2
#   define MINESWP_INFO     BUTTON_F3
#   define MINESWP_RIGHT    (BUTTON_F1 | BUTTON_RIGHT)
#   define MINESWP_LEFT     (BUTTON_F1 | BUTTON_LEFT)

#elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
#   define MINESWP_UP       BUTTON_UP
#   define MINESWP_DOWN     BUTTON_DOWN
#   define MINESWP_QUIT     BUTTON_OFF
#   define MINESWP_START    BUTTON_ON
#   define MINESWP_TOGGLE   BUTTON_SELECT
#   define MINESWP_TOGGLE2  BUTTON_F1
#   define MINESWP_DISCOVER BUTTON_ON
#   define MINESWP_DISCOVER2 BUTTON_F2
#   define MINESWP_INFO     BUTTON_F3
#   define MINESWP_RIGHT    (BUTTON_F1 | BUTTON_RIGHT)
#   define MINESWP_LEFT     (BUTTON_F1 | BUTTON_LEFT)

#elif CONFIG_KEYPAD == ONDIO_PAD
#   define MINESWP_UP       BUTTON_UP
#   define MINESWP_DOWN     BUTTON_DOWN
#   define MINESWP_QUIT     BUTTON_OFF
#   define MINESWP_START    BUTTON_MENU
#   define MINESWP_TOGGLE_PRE BUTTON_MENU
#   define MINESWP_TOGGLE   (BUTTON_MENU | BUTTON_REL)
#   define MINESWP_DISCOVER (BUTTON_MENU | BUTTON_REPEAT)
#   define MINESWP_INFO     (BUTTON_MENU | BUTTON_OFF)
#   define MINESWP_RIGHT    (BUTTON_MENU | BUTTON_RIGHT)
#   define MINESWP_LEFT     (BUTTON_MENU | BUTTON_LEFT)

#elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
      (CONFIG_KEYPAD == IRIVER_H300_PAD)
#   define MINESWP_UP       BUTTON_UP
#   define MINESWP_DOWN     BUTTON_DOWN
#   define MINESWP_QUIT     BUTTON_OFF
#   define MINESWP_START    BUTTON_SELECT
#   define MINESWP_TOGGLE   BUTTON_ON
#   define MINESWP_DISCOVER BUTTON_SELECT
#   define MINESWP_INFO     BUTTON_MODE
#   define MINESWP_RIGHT    (BUTTON_ON | BUTTON_RIGHT)
#   define MINESWP_LEFT     (BUTTON_ON | BUTTON_LEFT)

#   define MINESWP_RC_QUIT  BUTTON_RC_STOP

#elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
      (CONFIG_KEYPAD == IPOD_3G_PAD)
#   define MINESWP_UP       BUTTON_SCROLL_BACK
#   define MINESWP_DOWN     BUTTON_SCROLL_FWD
#   define MINESWP_QUIT     BUTTON_MENU
#   define MINESWP_START    BUTTON_SELECT
#   define MINESWP_TOGGLE   BUTTON_PLAY
#   define MINESWP_DISCOVER (BUTTON_SELECT | BUTTON_PLAY)
#   define MINESWP_INFO     (BUTTON_SELECT | BUTTON_MENU)
#   define MINESWP_RIGHT    (BUTTON_SELECT | BUTTON_RIGHT)
#   define MINESWP_LEFT     (BUTTON_SELECT | BUTTON_LEFT)

#elif (CONFIG_KEYPAD == IAUDIO_X5_PAD)
#   define MINESWP_UP       BUTTON_UP
#   define MINESWP_DOWN     BUTTON_DOWN
#   define MINESWP_QUIT     BUTTON_POWER
#   define MINESWP_START    BUTTON_REC
#   define MINESWP_TOGGLE   BUTTON_PLAY
#   define MINESWP_DISCOVER BUTTON_SELECT
#   define MINESWP_INFO     (BUTTON_REC | BUTTON_PLAY)
#   define MINESWP_RIGHT    (BUTTON_PLAY | BUTTON_RIGHT)
#   define MINESWP_LEFT     (BUTTON_PLAY | BUTTON_LEFT)

#elif (CONFIG_KEYPAD == GIGABEAT_PAD)
#   define MINESWP_UP       BUTTON_UP
#   define MINESWP_DOWN     BUTTON_DOWN
#   define MINESWP_QUIT     BUTTON_A
#   define MINESWP_START    BUTTON_SELECT
#   define MINESWP_TOGGLE   BUTTON_POWER
#   define MINESWP_DISCOVER BUTTON_SELECT
#   define MINESWP_INFO     BUTTON_MENU
#   define MINESWP_RIGHT    (BUTTON_SELECT | BUTTON_RIGHT)
#   define MINESWP_LEFT     (BUTTON_SELECT | BUTTON_LEFT)

#elif (CONFIG_KEYPAD == SANSA_E200_PAD)
#   define MINESWP_UP       BUTTON_UP
#   define MINESWP_DOWN     BUTTON_DOWN
#   define MINESWP_QUIT     BUTTON_POWER
#   define MINESWP_START    BUTTON_SELECT
#   define MINESWP_TOGGLE   BUTTON_REC
#   define MINESWP_DISCOVER BUTTON_SELECT
#   define MINESWP_INFO     (BUTTON_REC|BUTTON_REPEAT)
#   define MINESWP_RIGHT    (BUTTON_SELECT | BUTTON_RIGHT)
#   define MINESWP_LEFT     (BUTTON_SELECT | BUTTON_LEFT)

#elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
#   define MINESWP_UP       BUTTON_SCROLL_UP
#   define MINESWP_DOWN     BUTTON_SCROLL_DOWN
#   define MINESWP_QUIT     BUTTON_POWER
#   define MINESWP_START    BUTTON_FF
#   define MINESWP_TOGGLE   BUTTON_PLAY
#   define MINESWP_DISCOVER BUTTON_REW
#   define MINESWP_INFO     (BUTTON_REW | BUTTON_PLAY)
#   define MINESWP_RIGHT    (BUTTON_RIGHT | BUTTON_PLAY)
#   define MINESWP_LEFT     (BUTTON_LEFT | BUTTON_PLAY)

#else
#   warning Missing key definitions for this keypad
#endif

/* here is a global api struct pointer. while not strictly necessary,
 * it's nice not to have to pass the api pointer in all function calls
 * in the plugin
 */
static struct plugin_api *rb;

extern const fb_data minesweeper_tiles[];

#ifdef HAVE_LCD_COLOR
#   if ( LCD_HEIGHT * LCD_WIDTH ) / ( 16 * 16 ) >= 130
        /* We want to have at least 130 tiles on the screen */
#       define TileSize 16
#   else
#       define TileSize 12
#   endif
#   define BackgroundColor LCD_RGBPACK( 128, 128, 128 )
#elif LCD_DEPTH > 1
#   define TileSize 12
#else
#   define TileSize 8
#endif

#define Mine         9
#define Flag         10
#define Unknown      11
#define ExplodedMine 12

#define draw_tile( num, x, y ) \
    rb->lcd_bitmap_part( minesweeper_tiles, 0, num * TileSize, \
                         TileSize, left+x*TileSize, top+y*TileSize, \
                         TileSize, TileSize )

#define invert_tile( x, y ) \
    rb->lcd_set_drawmode(DRMODE_COMPLEMENT); \
    rb->lcd_fillrect( left+x*TileSize, top+y*TileSize, TileSize, TileSize ); \
    rb->lcd_set_drawmode(DRMODE_SOLID);


/* the tile struct
 * if there is a mine, mine is true
 * if tile is known by player, known is true
 * if tile has a flag, flag is true
 * neighbors is the total number of mines arround tile
 */
typedef struct tile
{
    unsigned char mine : 1;
    unsigned char known : 1;
    unsigned char flag : 1;
    unsigned char neighbors : 4;
} tile;

/* the height and width of the field */
#define MAX_HEIGHT (LCD_HEIGHT/TileSize)
#define MAX_WIDTH  (LCD_WIDTH/TileSize)
int height = MAX_HEIGHT;
int width = MAX_WIDTH;
int top;
int left;

/* The Minefield. Caution it is defined as Y, X! Not the opposite. */
tile minefield[MAX_HEIGHT][MAX_WIDTH];

/* total number of mines on the game */
int mine_num = 0;

/* percentage of mines on minefield used during generation */
int p = 16;

/* number of tiles left on the game */
int tiles_left;

/* Because mines are set after the first move... */
bool no_mines = true;

/* We need a stack (created on discover()) for the cascade algorithm. */
int stack_pos = 0;

/* a usefull string for snprintf */
char str[30];


void push( int *stack, int y, int x )
{
    if( stack_pos <= height*width )
    {
        stack[++stack_pos] = y;
        stack[++stack_pos] = x;
    }
}

/* Unveil tiles and push them to stack if they are empty. */
void unveil( int *stack, int y, int x )
{
    if( x < 0 || y < 0 || x > width - 1 || y > height - 1
       || minefield[y][x].known
       || minefield[y][x].mine || minefield[y][x].flag ) return;

    minefield[y][x].known = 1;

    if( minefield[y][x].neighbors == 0 )
        push( stack, y, x );
}

void discover( int y, int x )
{
    int stack[height*width];

    /* Selected tile. */
    if( x < 0 || y < 0 || x > width - 1 || y > height - 1
       || minefield[y][x].known
       || minefield[y][x].mine || minefield[y][x].flag ) return;

    minefield[y][x].known = 1;
    /* Exit if the tile is not empty. (no mines nearby) */
    if( minefield[y][x].neighbors ) return;

    push( stack, y, x );

    /* Scan all nearby tiles. If we meet a tile with a number we just unveil
     * it. If we meet an empty tile, we push the location in stack. For each
     * location in stack we do the same thing. (scan again all nearby tiles)
     */
    while( stack_pos )
    {
        /* Pop x, y from stack. */
        x = stack[stack_pos--];
        y = stack[stack_pos--];

        unveil( stack, y-1, x-1 );
        unveil( stack, y-1, x   );
        unveil( stack, y-1, x+1 );
        unveil( stack, y,   x+1 );
        unveil( stack, y+1, x+1 );
        unveil( stack, y+1, x   );
        unveil( stack, y+1, x-1 );
        unveil( stack, y,   x-1 );
    }
}

/* Reset the whole board for a new game. */
void minesweeper_init( void )
{
    int i,j;

    for( i = 0; i < MAX_HEIGHT; i++ )
    {
        for( j = 0; j < MAX_WIDTH; j++ )
        {
            minefield[i][j].known = 0;
            minefield[i][j].flag = 0;
            minefield[i][j].mine = 0;
            minefield[i][j].neighbors = 0;
        }
    }
    no_mines = true;
    tiles_left = width*height;
}


/* put mines on the mine field */
/* there is p% chance that a tile is a mine */
/* if the tile has coordinates (x,y), then it can't be a mine */
void minesweeper_putmines( int p, int x, int y )
{
    int i,j;

    mine_num = 0;
    for( i = 0; i < height; i++ )
    {
        for( j = 0; j < width; j++ )
        {
            if( rb->rand()%100 < p && !( y==i && x==j ) )
            {
                minefield[i][j].mine = 1;
                mine_num++;
            }
            else
            {
                minefield[i][j].mine = 0;
            }
            minefield[i][j].neighbors = 0;
        }
    }

    /* we need to compute the neighbor element for each tile */
    for( i = 0; i < height; i++ )
    {
        for( j = 0; j < width; j++ )
        {
            if( i > 0 )
            {
                if( j > 0 )
                    minefield[i][j].neighbors += minefield[i-1][j-1].mine;
                minefield[i][j].neighbors += minefield[i-1][j].mine;
                if( j < width - 1 )
                    minefield[i][j].neighbors += minefield[i-1][j+1].mine;
            }
            if( j > 0 )
                minefield[i][j].neighbors += minefield[i][j-1].mine;
            if( j < width - 1 )
                minefield[i][j].neighbors += minefield[i][j+1].mine;
            if( i < height - 1 )
            {
                if( j > 0 )
                    minefield[i][j].neighbors += minefield[i+1][j-1].mine;
                minefield[i][j].neighbors += minefield[i+1][j].mine;
                if( j < width - 1 )
                    minefield[i][j].neighbors += minefield[i+1][j+1].mine;
            }
        }
    }

    no_mines = false;

    /* In case the user is lucky and there are no mines positioned. */
    if( !mine_num && height*width != 1 )
    {
        minesweeper_putmines(p, x, y);
    }
}

/* A function that will uncover all the board, when the user wins or loses.
   can easily be expanded, (just a call assigned to a button) as a solver. */
void mine_show( void )
{
    int i, j, button;

    for( i = 0; i < height; i++ )
    {
        for( j = 0; j < width; j++ )
        {
            if( minefield[i][j].mine )
            {
                if( minefield[i][j].known )
                {
                    draw_tile( ExplodedMine, j, i );
                }
                else
                {
                    draw_tile( Mine, j, i );
                }
            }
            else
            {
                draw_tile( minefield[i][j].neighbors, j, i );
            }
        }
    }
    rb->lcd_update();

    do
        button = rb->button_get(true);
    while( ( button == BUTTON_NONE )
           || ( button & (BUTTON_REL|BUTTON_REPEAT) ) );
}

int count_tiles_left( void )
{
    int tiles_left = 0;
    int i, j;
    for( i = 0; i < height; i++ )
        for( j = 0; j < width; j++ )
             if( minefield[i][j].known == 0 )
                tiles_left++;
    return tiles_left;
}

/* welcome screen where player can chose mine percentage */
enum minesweeper_status menu( void )
{
    int button;

    while( true )
    {
#ifdef HAVE_LCD_COLOR
        rb->lcd_set_background( LCD_WHITE );
        rb->lcd_set_foreground( LCD_BLACK );
#endif
        rb->lcd_clear_display();

        rb->lcd_puts( 0, 0, "Mine Sweeper" );

        rb->snprintf( str, 20, "%d%% mines", p );
        rb->lcd_puts( 0, 2, str );
        rb->lcd_puts( 0, 3, "down / up" );
        rb->snprintf( str, 20, "%d cols x %d rows", width, height );
        rb->lcd_puts( 0, 4, str );
        rb->lcd_puts( 0, 5, "left x right" );
        rb->lcd_puts( 0, 6,
#if CONFIG_KEYPAD == RECORDER_PAD
            "ON to start"
#elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
            "ON to start"
#elif CONFIG_KEYPAD == ONDIO_PAD
            "MODE to start"
#elif (CONFIG_KEYPAD == IRIVER_H100_PAD) \
      || (CONFIG_KEYPAD == IRIVER_H300_PAD ) \
      || (CONFIG_KEYPAD == IPOD_4G_PAD) \
      || (CONFIG_KEYPAD == IPOD_3G_PAD) \
      || (CONFIG_KEYPAD == GIGABEAT_PAD)
            "SELECT to start"
#elif CONFIG_KEYPAD == IAUDIO_X5_PAD
            "REC to start"
#elif CONFIG_KEYPAD == IRIVER_H10_PAD
            "FF to start"
#elif CONFIG_KEYPAD == SANSA_E200_PAD
            "SELECT to start"
#else
            ""
#       warning Please define help string for this keypad.
#endif
        );
        rb->lcd_update();

        switch( button = rb->button_get( true ) )
        {
            case MINESWP_DOWN:
            case MINESWP_DOWN|BUTTON_REPEAT:
                p = (p + 94)%98 + 2;
                break;

            case MINESWP_UP:
            case MINESWP_UP|BUTTON_REPEAT:
                p = p%98 + 2;
                break;

            case BUTTON_RIGHT:
            case BUTTON_RIGHT|BUTTON_REPEAT:
                height = height%MAX_HEIGHT + 1;
                break;

            case BUTTON_LEFT:
            case BUTTON_LEFT|BUTTON_REPEAT:
                width = width%MAX_WIDTH + 1;
                break;

            case MINESWP_RIGHT:
            case MINESWP_RIGHT|BUTTON_REPEAT:
                height--;
                if( height < 1 ) height = MAX_HEIGHT;
                break;

            case MINESWP_LEFT:
            case MINESWP_LEFT|BUTTON_REPEAT:
                width--;
                if( width < 1 ) width = MAX_WIDTH;
                break;

            case MINESWP_START:/* start playing */
                return MINESWEEPER_WIN;

#ifdef MINESWP_RC_QUIT
            case MINESWP_RC_QUIT:
#endif
            case MINESWP_QUIT:/* quit program */
                return MINESWEEPER_QUIT;

            default:
                if( rb->default_event_handler(button) == SYS_USB_CONNECTED )
                    return MINESWEEPER_USB;
                break;
        }
    }
}

/* the big and ugly game function */
enum minesweeper_status minesweeper( void )
{
    int i, j;
    int button;
    int lastbutton = BUTTON_NONE;

    /* the cursor coordinates */
    int x=0, y=0;

    /**
     * Show the menu
     */
    if( ( i = menu() ) != MINESWEEPER_WIN ) return i;

    /**
     * Init game
     */
    top = (LCD_HEIGHT-height*TileSize)/2;
    left = (LCD_WIDTH-width*TileSize)/2;

    rb->srand( *rb->current_tick );
    minesweeper_init();
    x = 0;
    y = 0;

    /**
     * Play
     */
    while( true )
    {

        /* clear the screen buffer */
#ifdef HAVE_LCD_COLOR
        rb->lcd_set_background( BackgroundColor );
#endif
        rb->lcd_clear_display();

        /* display the mine field */
        for( i = 0; i < height; i++ )
        {
            for( j = 0; j < width; j++ )
            {
                if( minefield[i][j].known )
                {
                    draw_tile( minefield[i][j].neighbors, j, i );
                }
                else if(minefield[i][j].flag)
                {
                    draw_tile( Flag, j, i );
                }
                else
                {
                    draw_tile( Unknown, j, i );
                }
            }
        }

        /* display the cursor */
        invert_tile( x, y );

        /* update the screen */
        rb->lcd_update();

        switch( button = rb->button_get( true ) )
        {
            /* quit minesweeper (you really shouldn't use this button ...) */
#ifdef MINESWP_RC_QUIT
            case MINESWP_RC_QUIT:
#endif
            case MINESWP_QUIT:
                return MINESWEEPER_QUIT;

            /* move cursor left */
            case BUTTON_LEFT:
            case BUTTON_LEFT|BUTTON_REPEAT:
                x = ( x + width - 1 )%width;
                break;

            /* move cursor right */
            case BUTTON_RIGHT:
            case BUTTON_RIGHT|BUTTON_REPEAT:
                x = ( x + 1 )%width;
                break;

            /* move cursor down */
            case MINESWP_DOWN:
            case MINESWP_DOWN|BUTTON_REPEAT:
                y = ( y + 1 )%height;
                break;

            /* move cursor up */
            case MINESWP_UP:
            case MINESWP_UP|BUTTON_REPEAT:
                y = ( y + height - 1 )%height;
                break;

            /* discover a tile (and it's neighbors if .neighbors == 0) */
            case MINESWP_DISCOVER:
#ifdef MINESWP_DISCOVER2
            case MINESWP_DISCOVER2:
#endif
                if( minefield[y][x].flag ) break;
                /* we put the mines on the first "click" so that you don't
                 * lose on the first "click" */
                if( tiles_left == width*height && no_mines )
                    minesweeper_putmines(p,x,y);

                discover(y, x);

                if( minefield[y][x].mine )
                {
                    minefield[y][x].known = 1;
                    return MINESWEEPER_LOSE;
                }
                tiles_left = count_tiles_left();
                if( tiles_left == mine_num )
                {
                    return MINESWEEPER_WIN;
                }
                break;

            /* toggle flag under cursor */
            case MINESWP_TOGGLE:
#ifdef MINESWP_TOGGLE_PRE
                if( lastbutton != MINESWP_TOGGLE_PRE )
                    break;
#endif
#ifdef MINESWP_TOGGLE2
            case MINESWP_TOGGLE2:
#endif
                minefield[y][x].flag = ( minefield[y][x].flag + 1 )%2;
                break;

            /* show how many mines you think you have found and how many
             * there really are on the game */
            case MINESWP_INFO:
                if( no_mines )
                    break;
                tiles_left = count_tiles_left();
                rb->splash( HZ*2, true, "You found %d mines out of %d",
                            tiles_left, mine_num );
                break;

            default:
                if( rb->default_event_handler( button ) == SYS_USB_CONNECTED )
                    return MINESWEEPER_USB;
                break;
        }
        if( button != BUTTON_NONE )
            lastbutton = button;
    }

}

/* plugin entry point */
enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
{
    bool exit = false;

    (void)parameter;
    rb = api;
#if LCD_DEPTH > 1
    rb->lcd_set_backdrop(NULL);
#endif

    while( !exit )
    {
        switch( minesweeper() )
        {
            case MINESWEEPER_WIN:
                rb->splash( HZ, true, "You Win!" );
                rb->lcd_clear_display();
                mine_show();
                break;

            case MINESWEEPER_LOSE:
                rb->splash( HZ, true, "You Lose!" );
                rb->lcd_clear_display();
                mine_show();
                break;

            case MINESWEEPER_USB:
                return PLUGIN_USB_CONNECTED;

            case MINESWEEPER_QUIT:
                exit = true;
                break;

            default:
                break;
        }
    }

    return PLUGIN_OK;
}

#endif