summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apps/plugins/minesweeper.c73
1 files changed, 33 insertions, 40 deletions
diff --git a/apps/plugins/minesweeper.c b/apps/plugins/minesweeper.c
index dbf3271353..58e56de159 100644
--- a/apps/plugins/minesweeper.c
+++ b/apps/plugins/minesweeper.c
@@ -365,23 +365,17 @@ tile minefield[MAX_HEIGHT][MAX_WIDTH];
365int mine_num = 0; 365int mine_num = 0;
366 366
367/* percentage of mines on minefield used during generation */ 367/* percentage of mines on minefield used during generation */
368int p = 16; 368int percent = 16;
369 369
370/* number of tiles left on the game */ 370/* number of tiles left on the game */
371int tiles_left; 371int tiles_left;
372 372
373/* number of used flags on the game */
374int flags_used;
375
376/* Because mines are set after the first move... */ 373/* Because mines are set after the first move... */
377bool no_mines = true; 374bool no_mines = true;
378 375
379/* We need a stack (created on discover()) for the cascade algorithm. */ 376/* We need a stack (created on discover()) for the cascade algorithm. */
380int stack_pos = 0; 377int stack_pos = 0;
381 378
382/* a usefull string for snprintf */
383char str[30];
384
385#ifdef HAVE_TOUCHSCREEN 379#ifdef HAVE_TOUCHSCREEN
386 380
387#include "lib/pluginlib_touchscreen.h" 381#include "lib/pluginlib_touchscreen.h"
@@ -434,9 +428,10 @@ int neighbors_flagged( int y, int x )
434bool discover( int y, int x, bool explore_neighbors ) 428bool discover( int y, int x, bool explore_neighbors )
435{ 429{
436 /* Selected tile. */ 430 /* Selected tile. */
437 if( x < 0 || y < 0 || x > width - 1 || y > height - 1 431 if( x < 0 || y < 0 || x > width - 1 || y > height - 1)
438 || minefield[y][x].known 432 return false;
439 || minefield[y][x].mine || minefield[y][x].flag ) 433
434 if( minefield[y][x].known || minefield[y][x].mine || minefield[y][x].flag )
440 { 435 {
441 if( !minefield[y][x].flag && minefield[y][x].mine ) 436 if( !minefield[y][x].flag && minefield[y][x].mine )
442 return true; 437 return true;
@@ -492,18 +487,7 @@ bool discover( int y, int x, bool explore_neighbors )
492/* Reset the whole board for a new game. */ 487/* Reset the whole board for a new game. */
493void minesweeper_init( void ) 488void minesweeper_init( void )
494{ 489{
495 int i,j; 490 rb->memset(minefield, 0, sizeof(minefield));
496
497 for( i = 0; i < MAX_HEIGHT; i++ )
498 {
499 for( j = 0; j < MAX_WIDTH; j++ )
500 {
501 minefield[i][j].known = 0;
502 minefield[i][j].flag = 0;
503 minefield[i][j].mine = 0;
504 minefield[i][j].neighbors = 0;
505 }
506 }
507 no_mines = true; 491 no_mines = true;
508 tiles_left = width*height; 492 tiles_left = width*height;
509} 493}
@@ -612,24 +596,24 @@ void mine_show( void )
612 596
613int count_tiles_left( void ) 597int count_tiles_left( void )
614{ 598{
615 int tiles_left = 0; 599 int tiles = 0;
616 int i, j; 600 int i, j;
617 for( i = 0; i < height; i++ ) 601 for( i = 0; i < height; i++ )
618 for( j = 0; j < width; j++ ) 602 for( j = 0; j < width; j++ )
619 if( minefield[i][j].known == 0 ) 603 if( minefield[i][j].known == 0 )
620 tiles_left++; 604 tiles++;
621 return tiles_left; 605 return tiles;
622} 606}
623 607
624int count_flags( void ) 608int count_flags( void )
625{ 609{
626 int flags_used = 0; 610 int flags = 0;
627 int i, j; 611 int i, j;
628 for( i = 0; i < height; i++ ) 612 for( i = 0; i < height; i++ )
629 for( j = 0; j < width; j++ ) 613 for( j = 0; j < width; j++ )
630 if( minefield[i][j].flag == 1 ) 614 if( minefield[i][j].flag == 1 )
631 flags_used++; 615 flags++;
632 return flags_used; 616 return flags;
633} 617}
634 618
635/* welcome screen where player can chose mine percentage */ 619/* welcome screen where player can chose mine percentage */
@@ -657,7 +641,7 @@ enum minesweeper_status menu( void )
657 break; 641 break;
658 642
659 case 1: 643 case 1:
660 rb->set_int( "Mine Percentage", "%", UNIT_INT, &p, NULL, 644 rb->set_int( "Mine Percentage", "%", UNIT_INT, &percent, NULL,
661 1, 2, 98, NULL ); 645 1, 2, 98, NULL );
662 break; 646 break;
663 647
@@ -790,25 +774,29 @@ enum minesweeper_status minesweeper( void )
790 /* move cursor left */ 774 /* move cursor left */
791 case MINESWP_LEFT: 775 case MINESWP_LEFT:
792 case MINESWP_LEFT|BUTTON_REPEAT: 776 case MINESWP_LEFT|BUTTON_REPEAT:
793 x = ( x + width - 1 )%width; 777 if( --x < 0)
778 x += width;
794 break; 779 break;
795 780
796 /* move cursor right */ 781 /* move cursor right */
797 case MINESWP_RIGHT: 782 case MINESWP_RIGHT:
798 case MINESWP_RIGHT|BUTTON_REPEAT: 783 case MINESWP_RIGHT|BUTTON_REPEAT:
799 x = ( x + 1 )%width; 784 if( ++x >= width )
785 x -= width;
800 break; 786 break;
801 787
802 /* move cursor down */ 788 /* move cursor down */
803 case MINESWP_DOWN: 789 case MINESWP_DOWN:
804 case MINESWP_DOWN|BUTTON_REPEAT: 790 case MINESWP_DOWN|BUTTON_REPEAT:
805 y = ( y + 1 )%height; 791 if( ++y >= height )
792 y -= height;
806 break; 793 break;
807 794
808 /* move cursor up */ 795 /* move cursor up */
809 case MINESWP_UP: 796 case MINESWP_UP:
810 case MINESWP_UP|BUTTON_REPEAT: 797 case MINESWP_UP|BUTTON_REPEAT:
811 y = ( y + height - 1 )%height; 798 if( --y < 0 )
799 y += height;
812 break; 800 break;
813 801
814 /*move cursor though the entire field*/ 802 /*move cursor though the entire field*/
@@ -816,17 +804,21 @@ enum minesweeper_status minesweeper( void )
816 case MINESWP_NEXT: 804 case MINESWP_NEXT:
817 case MINESWP_NEXT|BUTTON_REPEAT: 805 case MINESWP_NEXT|BUTTON_REPEAT:
818 if (x == width -1 ) { 806 if (x == width -1 ) {
819 y = ( y + 1 )%height; 807 if( ++y >= height )
808 y -= height;
820 } 809 }
821 x = ( x + 1 )%width; 810 if( ++x >= width )
811 x -= width;
822 break; 812 break;
823 813
824 case MINESWP_PREV: 814 case MINESWP_PREV:
825 case MINESWP_PREV|BUTTON_REPEAT: 815 case MINESWP_PREV|BUTTON_REPEAT:
826 if (x == 0) { 816 if (x == 0) {
827 y = ( y + height - 1 )%height; 817 if( --y < 0 )
818 y += height;
828 } 819 }
829 x = ( x + width - 1 )%width; 820 if( --x < 0 )
821 x += width;
830 break; 822 break;
831#endif 823#endif
832 /* discover a tile (and it's neighbors if .neighbors == 0) */ 824 /* discover a tile (and it's neighbors if .neighbors == 0) */
@@ -838,7 +830,7 @@ enum minesweeper_status minesweeper( void )
838 /* we put the mines on the first "click" so that you don't 830 /* we put the mines on the first "click" so that you don't
839 * lose on the first "click" */ 831 * lose on the first "click" */
840 if( tiles_left == width*height && no_mines ) 832 if( tiles_left == width*height && no_mines )
841 minesweeper_putmines(p,x,y); 833 minesweeper_putmines(percent,x,y);
842 834
843 if( discover( y, x, true ) ) 835 if( discover( y, x, true ) )
844 { 836 {
@@ -862,7 +854,8 @@ enum minesweeper_status minesweeper( void )
862#ifdef MINESWP_TOGGLE2 854#ifdef MINESWP_TOGGLE2
863 case MINESWP_TOGGLE2: 855 case MINESWP_TOGGLE2:
864#endif 856#endif
865 minefield[y][x].flag = ( minefield[y][x].flag + 1 )%2; 857 if( !minefield[y][x].known )
858 minefield[y][x].flag = !minefield[y][x].flag;
866 break; 859 break;
867 860
868 /* show how many mines you think you have found and how many 861 /* show how many mines you think you have found and how many
@@ -870,7 +863,7 @@ enum minesweeper_status minesweeper( void )
870 case MINESWP_INFO: 863 case MINESWP_INFO:
871 if( no_mines ) 864 if( no_mines )
872 break; 865 break;
873 flags_used = count_flags(); 866 int flags_used = count_flags();
874 if (flags_used == 1) { 867 if (flags_used == 1) {
875 rb->splashf( HZ*2, "You marked 1 field. There are %d mines.", 868 rb->splashf( HZ*2, "You marked 1 field. There are %d mines.",
876 mine_num ); 869 mine_num );