forked from len0rd/rockbox
minesweeper: bugfixes / code improvements
dont make out of array accesses to the minefield dont flag positions already known avoid using the same name for local & global variables don't use modulo but only 1 conditional add/sub git-svn-id: svn://svn.rockbox.org/rockbox/trunk@26956 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
parent
1da9b71992
commit
694bc2a093
1 changed files with 33 additions and 40 deletions
|
@ -365,23 +365,17 @@ tile minefield[MAX_HEIGHT][MAX_WIDTH];
|
||||||
int mine_num = 0;
|
int mine_num = 0;
|
||||||
|
|
||||||
/* percentage of mines on minefield used during generation */
|
/* percentage of mines on minefield used during generation */
|
||||||
int p = 16;
|
int percent = 16;
|
||||||
|
|
||||||
/* number of tiles left on the game */
|
/* number of tiles left on the game */
|
||||||
int tiles_left;
|
int tiles_left;
|
||||||
|
|
||||||
/* number of used flags on the game */
|
|
||||||
int flags_used;
|
|
||||||
|
|
||||||
/* Because mines are set after the first move... */
|
/* Because mines are set after the first move... */
|
||||||
bool no_mines = true;
|
bool no_mines = true;
|
||||||
|
|
||||||
/* We need a stack (created on discover()) for the cascade algorithm. */
|
/* We need a stack (created on discover()) for the cascade algorithm. */
|
||||||
int stack_pos = 0;
|
int stack_pos = 0;
|
||||||
|
|
||||||
/* a usefull string for snprintf */
|
|
||||||
char str[30];
|
|
||||||
|
|
||||||
#ifdef HAVE_TOUCHSCREEN
|
#ifdef HAVE_TOUCHSCREEN
|
||||||
|
|
||||||
#include "lib/pluginlib_touchscreen.h"
|
#include "lib/pluginlib_touchscreen.h"
|
||||||
|
@ -434,9 +428,10 @@ int neighbors_flagged( int y, int x )
|
||||||
bool discover( int y, int x, bool explore_neighbors )
|
bool discover( int y, int x, bool explore_neighbors )
|
||||||
{
|
{
|
||||||
/* Selected tile. */
|
/* Selected tile. */
|
||||||
if( x < 0 || y < 0 || x > width - 1 || y > height - 1
|
if( x < 0 || y < 0 || x > width - 1 || y > height - 1)
|
||||||
|| minefield[y][x].known
|
return false;
|
||||||
|| minefield[y][x].mine || minefield[y][x].flag )
|
|
||||||
|
if( minefield[y][x].known || minefield[y][x].mine || minefield[y][x].flag )
|
||||||
{
|
{
|
||||||
if( !minefield[y][x].flag && minefield[y][x].mine )
|
if( !minefield[y][x].flag && minefield[y][x].mine )
|
||||||
return true;
|
return true;
|
||||||
|
@ -492,18 +487,7 @@ bool discover( int y, int x, bool explore_neighbors )
|
||||||
/* Reset the whole board for a new game. */
|
/* Reset the whole board for a new game. */
|
||||||
void minesweeper_init( void )
|
void minesweeper_init( void )
|
||||||
{
|
{
|
||||||
int i,j;
|
rb->memset(minefield, 0, sizeof(minefield));
|
||||||
|
|
||||||
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;
|
no_mines = true;
|
||||||
tiles_left = width*height;
|
tiles_left = width*height;
|
||||||
}
|
}
|
||||||
|
@ -612,24 +596,24 @@ void mine_show( void )
|
||||||
|
|
||||||
int count_tiles_left( void )
|
int count_tiles_left( void )
|
||||||
{
|
{
|
||||||
int tiles_left = 0;
|
int tiles = 0;
|
||||||
int i, j;
|
int i, j;
|
||||||
for( i = 0; i < height; i++ )
|
for( i = 0; i < height; i++ )
|
||||||
for( j = 0; j < width; j++ )
|
for( j = 0; j < width; j++ )
|
||||||
if( minefield[i][j].known == 0 )
|
if( minefield[i][j].known == 0 )
|
||||||
tiles_left++;
|
tiles++;
|
||||||
return tiles_left;
|
return tiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
int count_flags( void )
|
int count_flags( void )
|
||||||
{
|
{
|
||||||
int flags_used = 0;
|
int flags = 0;
|
||||||
int i, j;
|
int i, j;
|
||||||
for( i = 0; i < height; i++ )
|
for( i = 0; i < height; i++ )
|
||||||
for( j = 0; j < width; j++ )
|
for( j = 0; j < width; j++ )
|
||||||
if( minefield[i][j].flag == 1 )
|
if( minefield[i][j].flag == 1 )
|
||||||
flags_used++;
|
flags++;
|
||||||
return flags_used;
|
return flags;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* welcome screen where player can chose mine percentage */
|
/* welcome screen where player can chose mine percentage */
|
||||||
|
@ -657,7 +641,7 @@ enum minesweeper_status menu( void )
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 1:
|
case 1:
|
||||||
rb->set_int( "Mine Percentage", "%", UNIT_INT, &p, NULL,
|
rb->set_int( "Mine Percentage", "%", UNIT_INT, &percent, NULL,
|
||||||
1, 2, 98, NULL );
|
1, 2, 98, NULL );
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -790,25 +774,29 @@ enum minesweeper_status minesweeper( void )
|
||||||
/* move cursor left */
|
/* move cursor left */
|
||||||
case MINESWP_LEFT:
|
case MINESWP_LEFT:
|
||||||
case MINESWP_LEFT|BUTTON_REPEAT:
|
case MINESWP_LEFT|BUTTON_REPEAT:
|
||||||
x = ( x + width - 1 )%width;
|
if( --x < 0)
|
||||||
|
x += width;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* move cursor right */
|
/* move cursor right */
|
||||||
case MINESWP_RIGHT:
|
case MINESWP_RIGHT:
|
||||||
case MINESWP_RIGHT|BUTTON_REPEAT:
|
case MINESWP_RIGHT|BUTTON_REPEAT:
|
||||||
x = ( x + 1 )%width;
|
if( ++x >= width )
|
||||||
|
x -= width;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* move cursor down */
|
/* move cursor down */
|
||||||
case MINESWP_DOWN:
|
case MINESWP_DOWN:
|
||||||
case MINESWP_DOWN|BUTTON_REPEAT:
|
case MINESWP_DOWN|BUTTON_REPEAT:
|
||||||
y = ( y + 1 )%height;
|
if( ++y >= height )
|
||||||
|
y -= height;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* move cursor up */
|
/* move cursor up */
|
||||||
case MINESWP_UP:
|
case MINESWP_UP:
|
||||||
case MINESWP_UP|BUTTON_REPEAT:
|
case MINESWP_UP|BUTTON_REPEAT:
|
||||||
y = ( y + height - 1 )%height;
|
if( --y < 0 )
|
||||||
|
y += height;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/*move cursor though the entire field*/
|
/*move cursor though the entire field*/
|
||||||
|
@ -816,17 +804,21 @@ enum minesweeper_status minesweeper( void )
|
||||||
case MINESWP_NEXT:
|
case MINESWP_NEXT:
|
||||||
case MINESWP_NEXT|BUTTON_REPEAT:
|
case MINESWP_NEXT|BUTTON_REPEAT:
|
||||||
if (x == width -1 ) {
|
if (x == width -1 ) {
|
||||||
y = ( y + 1 )%height;
|
if( ++y >= height )
|
||||||
|
y -= height;
|
||||||
}
|
}
|
||||||
x = ( x + 1 )%width;
|
if( ++x >= width )
|
||||||
|
x -= width;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MINESWP_PREV:
|
case MINESWP_PREV:
|
||||||
case MINESWP_PREV|BUTTON_REPEAT:
|
case MINESWP_PREV|BUTTON_REPEAT:
|
||||||
if (x == 0) {
|
if (x == 0) {
|
||||||
y = ( y + height - 1 )%height;
|
if( --y < 0 )
|
||||||
|
y += height;
|
||||||
}
|
}
|
||||||
x = ( x + width - 1 )%width;
|
if( --x < 0 )
|
||||||
|
x += width;
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
/* discover a tile (and it's neighbors if .neighbors == 0) */
|
/* discover a tile (and it's neighbors if .neighbors == 0) */
|
||||||
|
@ -838,7 +830,7 @@ enum minesweeper_status minesweeper( void )
|
||||||
/* we put the mines on the first "click" so that you don't
|
/* we put the mines on the first "click" so that you don't
|
||||||
* lose on the first "click" */
|
* lose on the first "click" */
|
||||||
if( tiles_left == width*height && no_mines )
|
if( tiles_left == width*height && no_mines )
|
||||||
minesweeper_putmines(p,x,y);
|
minesweeper_putmines(percent,x,y);
|
||||||
|
|
||||||
if( discover( y, x, true ) )
|
if( discover( y, x, true ) )
|
||||||
{
|
{
|
||||||
|
@ -862,7 +854,8 @@ enum minesweeper_status minesweeper( void )
|
||||||
#ifdef MINESWP_TOGGLE2
|
#ifdef MINESWP_TOGGLE2
|
||||||
case MINESWP_TOGGLE2:
|
case MINESWP_TOGGLE2:
|
||||||
#endif
|
#endif
|
||||||
minefield[y][x].flag = ( minefield[y][x].flag + 1 )%2;
|
if( !minefield[y][x].known )
|
||||||
|
minefield[y][x].flag = !minefield[y][x].flag;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* show how many mines you think you have found and how many
|
/* show how many mines you think you have found and how many
|
||||||
|
@ -870,7 +863,7 @@ enum minesweeper_status minesweeper( void )
|
||||||
case MINESWP_INFO:
|
case MINESWP_INFO:
|
||||||
if( no_mines )
|
if( no_mines )
|
||||||
break;
|
break;
|
||||||
flags_used = count_flags();
|
int flags_used = count_flags();
|
||||||
if (flags_used == 1) {
|
if (flags_used == 1) {
|
||||||
rb->splashf( HZ*2, "You marked 1 field. There are %d mines.",
|
rb->splashf( HZ*2, "You marked 1 field. There are %d mines.",
|
||||||
mine_num );
|
mine_num );
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue