2004-08-17 06:50:14 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2004 dionoea (Antoine Cellerier)
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
Mine Sweeper by dionoea
|
|
|
|
|
|
|
|
use arrow keys to move cursor
|
|
|
|
use ON or F2 to clear a tile
|
|
|
|
use PLAY or F1 to put a flag on a tile
|
|
|
|
use F3 to see how many mines are left (supposing all your flags are correct)
|
|
|
|
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#include "plugin.h"
|
|
|
|
#include "button.h"
|
|
|
|
#include "lcd.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_LCD_BITMAP
|
|
|
|
|
2005-06-26 20:42:53 +00:00
|
|
|
//what the minesweeper() function can return
|
2004-10-18 21:45:00 +00:00
|
|
|
#define MINESWEEPER_USB 3
|
2004-08-17 06:50:14 +00:00
|
|
|
#define MINESWEEPER_QUIT 2
|
|
|
|
#define MINESWEEPER_LOSE 1
|
|
|
|
#define MINESWEEPER_WIN 0
|
|
|
|
|
2004-10-18 21:45:00 +00:00
|
|
|
/* variable button definitions */
|
|
|
|
#if CONFIG_KEYPAD == RECORDER_PAD
|
2005-12-14 01:31:37 +00:00
|
|
|
#define MINESWP_UP BUTTON_UP
|
|
|
|
#define MINESWP_DOWN BUTTON_DOWN
|
2004-10-18 21:45:00 +00:00
|
|
|
#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
|
|
|
|
|
|
|
|
#elif CONFIG_KEYPAD == ONDIO_PAD
|
2005-12-14 01:31:37 +00:00
|
|
|
#define MINESWP_UP BUTTON_UP
|
|
|
|
#define MINESWP_DOWN BUTTON_DOWN
|
2004-10-18 21:45:00 +00:00
|
|
|
#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)
|
|
|
|
|
2005-06-29 12:47:24 +00:00
|
|
|
#elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
|
|
|
|
(CONFIG_KEYPAD == IRIVER_H300_PAD)
|
2005-12-14 01:31:37 +00:00
|
|
|
#define MINESWP_UP BUTTON_UP
|
|
|
|
#define MINESWP_DOWN BUTTON_DOWN
|
2005-02-04 12:41:09 +00:00
|
|
|
#define MINESWP_QUIT BUTTON_OFF
|
2005-02-10 23:55:24 +00:00
|
|
|
#define MINESWP_START BUTTON_SELECT
|
|
|
|
#define MINESWP_TOGGLE BUTTON_SELECT
|
|
|
|
#define MINESWP_DISCOVER BUTTON_ON
|
|
|
|
#define MINESWP_INFO BUTTON_MODE
|
2005-02-04 12:41:09 +00:00
|
|
|
|
2005-12-19 00:11:28 +00:00
|
|
|
#elif (CONFIG_KEYPAD == IPOD_4G_PAD)
|
2005-12-14 01:31:37 +00:00
|
|
|
#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)
|
|
|
|
|
2004-10-18 21:45:00 +00:00
|
|
|
#endif
|
|
|
|
|
2004-08-17 06:50:14 +00:00
|
|
|
/* 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;
|
|
|
|
|
|
|
|
|
2004-08-18 12:06:10 +00:00
|
|
|
/* define how numbers are displayed (that way we don't have to */
|
|
|
|
/* worry about fonts) */
|
2004-08-17 06:50:14 +00:00
|
|
|
static unsigned char num[9][8] = {
|
|
|
|
/*reading the sprites:
|
|
|
|
on screen f123
|
|
|
|
4567
|
|
|
|
890a
|
|
|
|
bcde
|
2005-06-26 20:42:53 +00:00
|
|
|
|
2004-08-17 06:50:14 +00:00
|
|
|
in binary b84f
|
|
|
|
c951
|
|
|
|
d062
|
|
|
|
ea73
|
|
|
|
*/
|
2005-06-26 20:42:53 +00:00
|
|
|
|
2004-08-18 12:06:10 +00:00
|
|
|
/* 0 */
|
|
|
|
{0x00, /* ........ */
|
|
|
|
0x00, /* ........ */
|
|
|
|
0x00, /* ........ */
|
|
|
|
0x00, /* ........ */
|
|
|
|
0x00, /* ........ */
|
|
|
|
0x00, /* ........ */
|
|
|
|
0x00, /* ........ */
|
|
|
|
0x00},/* ........ */
|
|
|
|
/* 1 */
|
|
|
|
{0x00, /* ........ */
|
|
|
|
0x00, /* ........ */
|
|
|
|
0x00, /* ...OO... */
|
|
|
|
0x44, /* ....O... */
|
|
|
|
0x7c, /* ....O... */
|
|
|
|
0x40, /* ....O... */
|
|
|
|
0x00, /* ...OOO.. */
|
|
|
|
0x00},/* ........ */
|
|
|
|
/* 2 */
|
|
|
|
{0x00, /* ........ */
|
|
|
|
0x00, /* ........ */
|
|
|
|
0x48, /* ...OO... */
|
|
|
|
0x64, /* ..O..O.. */
|
|
|
|
0x54, /* ....O... */
|
|
|
|
0x48, /* ...O.... */
|
|
|
|
0x00, /* ..OOOO.. */
|
|
|
|
0x00},/* ........ */
|
|
|
|
/* 3 */
|
|
|
|
{0x00, /* ........ */
|
|
|
|
0x00, /* ........ */
|
|
|
|
0x44, /* ..OOO... */
|
|
|
|
0x54, /* .....O.. */
|
|
|
|
0x54, /* ...OO... */
|
|
|
|
0x28, /* .....O.. */
|
|
|
|
0x00, /* ..OOO... */
|
|
|
|
0x00},/* ........ */
|
|
|
|
/* 4 */
|
|
|
|
{0x00, /* ........ */
|
|
|
|
0x00, /* ........ */
|
|
|
|
0x1c, /* ..O..... */
|
|
|
|
0x10, /* ..O..... */
|
|
|
|
0x70, /* ..OOOO.. */
|
|
|
|
0x10, /* ....O... */
|
|
|
|
0x00, /* ....O... */
|
|
|
|
0x00},/* ........ */
|
|
|
|
/* 5 */
|
|
|
|
{0x00, /* ........ */
|
|
|
|
0x00, /* ........ */
|
|
|
|
0x5c, /* ..OOOO.. */
|
|
|
|
0x54, /* ..O..... */
|
|
|
|
0x54, /* ..OOO... */
|
|
|
|
0x24, /* .....O.. */
|
|
|
|
0x00, /* ..OOO... */
|
|
|
|
0x00},/* ........ */
|
|
|
|
/* 6 */
|
|
|
|
{0x00, /* ........ */
|
|
|
|
0x00, /* ........ */
|
|
|
|
0x38, /* ...OOO.. */
|
|
|
|
0x54, /* ..O..... */
|
|
|
|
0x54, /* ..OOO... */
|
|
|
|
0x24, /* ..O..O.. */
|
|
|
|
0x00, /* ...OO... */
|
|
|
|
0x00},/* ........ */
|
|
|
|
/* 7 */
|
|
|
|
{0x00, /* ........ */
|
|
|
|
0x00, /* ........ */
|
|
|
|
0x44, /* ..OOOO.. */
|
|
|
|
0x24, /* .....O.. */
|
|
|
|
0x14, /* ....O... */
|
|
|
|
0x0c, /* ...O.... */
|
|
|
|
0x00, /* ..O..... */
|
|
|
|
0x00},/* ........ */
|
|
|
|
/* 8 */
|
|
|
|
{0x00, /* ........ */
|
|
|
|
0x00, /* ........ */
|
|
|
|
0x28, /* ...OO... */
|
|
|
|
0x54, /* ..O..O.. */
|
|
|
|
0x54, /* ...OO... */
|
|
|
|
0x28, /* ..O..O.. */
|
|
|
|
0x00, /* ...OO... */
|
|
|
|
0x00},/* ........ */
|
2004-08-17 06:50:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
2004-08-18 12:06:10 +00:00
|
|
|
/* the height and width of the field */
|
2005-06-26 20:42:53 +00:00
|
|
|
int height = LCD_HEIGHT/8;
|
|
|
|
int width = LCD_WIDTH/8;
|
2004-08-17 06:50:14 +00:00
|
|
|
|
2004-08-18 12:06:10 +00:00
|
|
|
/* the minefield */
|
2004-08-17 06:50:14 +00:00
|
|
|
tile minefield[LCD_HEIGHT/8][LCD_WIDTH/8];
|
|
|
|
|
2004-08-18 12:06:10 +00:00
|
|
|
/* total number of mines on the game */
|
2004-08-17 06:50:14 +00:00
|
|
|
int mine_num = 0;
|
|
|
|
|
2004-08-18 12:06:10 +00:00
|
|
|
/* discovers the tile when player clears one of them */
|
|
|
|
/* a chain reaction (of discovery) occurs if tile has no mines */
|
|
|
|
/* as neighbors */
|
2004-08-17 06:50:14 +00:00
|
|
|
void discover(int, int);
|
|
|
|
void discover(int x, int y){
|
2005-06-26 20:42:53 +00:00
|
|
|
|
2004-08-17 06:50:14 +00:00
|
|
|
if(x<0) return;
|
|
|
|
if(y<0) return;
|
|
|
|
if(x>width-1) return;
|
|
|
|
if(y>height-1) return;
|
|
|
|
if(minefield[y][x].known) return;
|
2005-06-26 20:42:53 +00:00
|
|
|
|
2004-08-17 06:50:14 +00:00
|
|
|
minefield[y][x].known = 1;
|
|
|
|
if(minefield[y][x].neighbors == 0){
|
|
|
|
discover(x-1,y-1);
|
|
|
|
discover(x,y-1);
|
|
|
|
discover(x+1,y-1);
|
|
|
|
discover(x+1,y);
|
|
|
|
discover(x+1,y+1);
|
|
|
|
discover(x,y+1);
|
|
|
|
discover(x-1,y+1);
|
|
|
|
discover(x-1,y);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-18 12:06:10 +00:00
|
|
|
/* init not mine related elements of the mine field */
|
2004-08-17 06:50:14 +00:00
|
|
|
void minesweeper_init(void){
|
|
|
|
int i,j;
|
2005-06-26 20:42:53 +00:00
|
|
|
|
2004-08-17 06:50:14 +00:00
|
|
|
for(i=0;i<height;i++){
|
|
|
|
for(j=0;j<width;j++){
|
|
|
|
minefield[i][j].known = 0;
|
|
|
|
minefield[i][j].flag = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-18 12:06:10 +00:00
|
|
|
/* 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 */
|
2004-08-17 06:50:14 +00:00
|
|
|
void minesweeper_putmines(int p, int x, int y){
|
|
|
|
int i,j;
|
|
|
|
|
2004-10-25 12:28:57 +00:00
|
|
|
mine_num = 0;
|
2004-08-17 06:50:14 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2005-06-26 20:42:53 +00:00
|
|
|
|
2004-08-18 12:06:10 +00:00
|
|
|
/* we need to compute the neighbor element for each tile */
|
2004-08-17 06:50:14 +00:00
|
|
|
for(i=0;i<height;i++){
|
|
|
|
for(j=0;j<width;j++){
|
|
|
|
if(i>0){
|
2004-08-18 12:06:10 +00:00
|
|
|
if(j>0)
|
|
|
|
minefield[i][j].neighbors += minefield[i-1][j-1].mine;
|
2004-08-17 06:50:14 +00:00
|
|
|
minefield[i][j].neighbors += minefield[i-1][j].mine;
|
2004-08-18 12:06:10 +00:00
|
|
|
if(j<width-1)
|
|
|
|
minefield[i][j].neighbors += minefield[i-1][j+1].mine;
|
2004-08-17 06:50:14 +00:00
|
|
|
}
|
2004-08-18 12:06:10 +00:00
|
|
|
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;
|
2004-08-17 06:50:14 +00:00
|
|
|
if(i<height-1){
|
2004-08-18 12:06:10 +00:00
|
|
|
if(j>0)
|
|
|
|
minefield[i][j].neighbors += minefield[i+1][j-1].mine;
|
2004-08-17 06:50:14 +00:00
|
|
|
minefield[i][j].neighbors += minefield[i+1][j].mine;
|
2004-08-18 12:06:10 +00:00
|
|
|
if(j<width-1)
|
|
|
|
minefield[i][j].neighbors += minefield[i+1][j+1].mine;
|
2004-08-17 06:50:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-18 12:06:10 +00:00
|
|
|
/* the big and ugly function that is the game */
|
|
|
|
int minesweeper(void)
|
|
|
|
{
|
2004-08-17 06:50:14 +00:00
|
|
|
int i,j;
|
2004-10-18 21:45:00 +00:00
|
|
|
int button;
|
|
|
|
int lastbutton = BUTTON_NONE;
|
2005-06-26 20:42:53 +00:00
|
|
|
|
2004-08-18 12:06:10 +00:00
|
|
|
/* the cursor coordinates */
|
2004-08-17 06:50:14 +00:00
|
|
|
int x=0,y=0;
|
2005-06-26 20:42:53 +00:00
|
|
|
|
2004-08-18 12:06:10 +00:00
|
|
|
/* number of tiles left on the game */
|
2004-08-17 06:50:14 +00:00
|
|
|
int tiles_left=width*height;
|
|
|
|
|
2004-08-18 12:06:10 +00:00
|
|
|
/* percentage of mines on minefield used durring generation */
|
2004-08-17 06:50:14 +00:00
|
|
|
int p=16;
|
|
|
|
|
2004-08-18 12:06:10 +00:00
|
|
|
/* a usefull string for snprintf */
|
2004-08-17 06:50:14 +00:00
|
|
|
char str[30];
|
|
|
|
|
2004-08-18 12:06:10 +00:00
|
|
|
/* welcome screen where player can chose mine percentage */
|
2004-08-17 06:50:14 +00:00
|
|
|
i = 0;
|
|
|
|
while(true){
|
|
|
|
rb->lcd_clear_display();
|
2005-06-26 20:42:53 +00:00
|
|
|
|
2005-02-10 23:55:24 +00:00
|
|
|
rb->lcd_puts(0,0,"Mine Sweeper");
|
2004-08-17 06:50:14 +00:00
|
|
|
|
|
|
|
rb->snprintf(str, 20, "%d%% mines", p);
|
2005-06-26 20:42:53 +00:00
|
|
|
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 ");
|
2004-10-18 21:45:00 +00:00
|
|
|
#if CONFIG_KEYPAD == RECORDER_PAD
|
2005-06-26 20:42:53 +00:00
|
|
|
rb->lcd_puts(0,6,"ON to start");
|
2004-10-18 21:45:00 +00:00
|
|
|
#elif CONFIG_KEYPAD == ONDIO_PAD
|
2005-06-26 20:42:53 +00:00
|
|
|
rb->lcd_puts(0,6,"MODE to start");
|
2005-02-10 23:55:24 +00:00
|
|
|
#elif CONFIG_KEYPAD == IRIVER_H100_PAD
|
2005-06-26 20:42:53 +00:00
|
|
|
rb->lcd_puts(0,6,"SELECT to start");
|
2004-10-18 21:45:00 +00:00
|
|
|
#endif
|
2004-08-17 06:50:14 +00:00
|
|
|
|
|
|
|
rb->lcd_update();
|
|
|
|
|
|
|
|
|
2004-10-18 21:45:00 +00:00
|
|
|
button = rb->button_get(true);
|
|
|
|
switch(button){
|
2005-12-14 01:31:37 +00:00
|
|
|
case MINESWP_DOWN:
|
2004-08-17 06:50:14 +00:00
|
|
|
p = (p + 98)%100;
|
|
|
|
break;
|
2005-06-26 20:42:53 +00:00
|
|
|
|
2005-12-14 01:31:37 +00:00
|
|
|
case MINESWP_UP:
|
2004-08-17 06:50:14 +00:00
|
|
|
p = (p + 2)%100;
|
|
|
|
break;
|
|
|
|
|
2005-06-26 20:42:53 +00:00
|
|
|
case BUTTON_RIGHT:
|
|
|
|
height = height%(LCD_HEIGHT/8)+1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BUTTON_LEFT:
|
|
|
|
width = width%(LCD_WIDTH/8)+1;
|
|
|
|
break;
|
|
|
|
|
2004-10-18 21:45:00 +00:00
|
|
|
case MINESWP_START:/* start playing */
|
2004-08-17 06:50:14 +00:00
|
|
|
i = 1;
|
|
|
|
break;
|
|
|
|
|
2004-10-18 21:45:00 +00:00
|
|
|
case MINESWP_QUIT:/* quit program */
|
2004-08-17 06:50:14 +00:00
|
|
|
return MINESWEEPER_QUIT;
|
2004-10-18 21:45:00 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
|
|
|
|
return MINESWEEPER_USB;
|
|
|
|
break;
|
2004-08-17 06:50:14 +00:00
|
|
|
}
|
2004-08-18 12:06:10 +00:00
|
|
|
if(i==1)
|
|
|
|
break;
|
2004-08-17 06:50:14 +00:00
|
|
|
}
|
2005-06-26 20:42:53 +00:00
|
|
|
|
2004-08-17 06:50:14 +00:00
|
|
|
|
|
|
|
/********************
|
|
|
|
* init *
|
|
|
|
********************/
|
|
|
|
|
|
|
|
minesweeper_init();
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* play *
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
while(true){
|
2005-06-26 20:42:53 +00:00
|
|
|
|
2004-08-17 06:50:14 +00:00
|
|
|
//clear the screen buffer
|
|
|
|
rb->lcd_clear_display();
|
2005-06-26 20:42:53 +00:00
|
|
|
|
2004-08-17 06:50:14 +00:00
|
|
|
//display the mine field
|
|
|
|
for(i=0;i<height;i++){
|
|
|
|
for(j=0;j<width;j++){
|
2005-07-19 15:39:54 +00:00
|
|
|
#if LCD_DEPTH > 1
|
2005-11-16 21:09:23 +00:00
|
|
|
rb->lcd_set_foreground(LCD_DARKGRAY);
|
2005-07-19 15:39:54 +00:00
|
|
|
rb->lcd_drawrect(j*8,i*8,8,8);
|
|
|
|
rb->lcd_set_foreground(LCD_BLACK);
|
|
|
|
#else
|
2004-08-17 06:50:14 +00:00
|
|
|
rb->lcd_drawrect(j*8,i*8,8,8);
|
2005-07-19 15:39:54 +00:00
|
|
|
#endif
|
2004-08-17 06:50:14 +00:00
|
|
|
if(minefield[i][j].known){
|
|
|
|
if(minefield[i][j].mine){
|
|
|
|
rb->lcd_putsxy(j*8+1,i*8+1,"b");
|
|
|
|
} else if(minefield[i][j].neighbors){
|
2005-06-28 23:15:47 +00:00
|
|
|
rb->lcd_set_drawmode(DRMODE_FG);
|
2005-07-06 22:58:02 +00:00
|
|
|
rb->lcd_mono_bitmap(num[minefield[i][j].neighbors],j*8,i*8,8,8);
|
2005-06-28 23:15:47 +00:00
|
|
|
rb->lcd_set_drawmode(DRMODE_SOLID);
|
2005-06-26 20:42:53 +00:00
|
|
|
}
|
2004-08-17 06:50:14 +00:00
|
|
|
} else if(minefield[i][j].flag) {
|
|
|
|
rb->lcd_drawline(j*8+2,i*8+2,j*8+5,i*8+5);
|
|
|
|
rb->lcd_drawline(j*8+2,i*8+5,j*8+5,i*8+2);
|
|
|
|
} else {
|
2005-07-19 15:39:54 +00:00
|
|
|
#if LCD_DEPTH > 1
|
2005-11-16 21:09:23 +00:00
|
|
|
rb->lcd_set_foreground(LCD_LIGHTGRAY);
|
2005-07-19 15:39:54 +00:00
|
|
|
rb->lcd_fillrect(j*8+1,i*8+1,6,6);
|
|
|
|
rb->lcd_set_foreground(LCD_BLACK);
|
|
|
|
#else
|
2004-08-17 06:50:14 +00:00
|
|
|
rb->lcd_fillrect(j*8+2,i*8+2,4,4);
|
2005-07-19 15:39:54 +00:00
|
|
|
#endif
|
2004-08-17 06:50:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-18 12:06:10 +00:00
|
|
|
/* display the cursor */
|
2005-06-24 22:33:21 +00:00
|
|
|
rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
|
|
|
|
rb->lcd_fillrect(x*8,y*8,8,8);
|
|
|
|
rb->lcd_set_drawmode(DRMODE_SOLID);
|
|
|
|
|
2004-08-18 12:06:10 +00:00
|
|
|
/* update the screen */
|
2004-08-17 06:50:14 +00:00
|
|
|
rb->lcd_update();
|
2005-06-26 20:42:53 +00:00
|
|
|
|
2004-10-18 21:45:00 +00:00
|
|
|
button = rb->button_get(true);
|
|
|
|
switch(button){
|
2004-08-18 12:06:10 +00:00
|
|
|
/* quit minesweeper (you really shouldn't use this button ...) */
|
2004-10-18 21:45:00 +00:00
|
|
|
case MINESWP_QUIT:
|
2004-08-17 06:50:14 +00:00
|
|
|
return MINESWEEPER_QUIT;
|
2005-06-26 20:42:53 +00:00
|
|
|
|
2004-08-18 12:06:10 +00:00
|
|
|
/* move cursor left */
|
2004-08-17 06:50:14 +00:00
|
|
|
case BUTTON_LEFT:
|
2004-10-18 21:45:00 +00:00
|
|
|
case (BUTTON_LEFT | BUTTON_REPEAT):
|
2004-08-17 06:50:14 +00:00
|
|
|
x = (x + width - 1)%width;
|
|
|
|
break;
|
2005-06-26 20:42:53 +00:00
|
|
|
|
2004-08-18 12:06:10 +00:00
|
|
|
/* move cursor right */
|
2004-08-17 06:50:14 +00:00
|
|
|
case BUTTON_RIGHT:
|
2004-10-18 21:45:00 +00:00
|
|
|
case (BUTTON_RIGHT | BUTTON_REPEAT):
|
2004-08-17 06:50:14 +00:00
|
|
|
x = (x + 1)%width;
|
|
|
|
break;
|
2005-06-26 20:42:53 +00:00
|
|
|
|
2004-08-18 12:06:10 +00:00
|
|
|
/* move cursor down */
|
2005-12-14 01:31:37 +00:00
|
|
|
case MINESWP_DOWN:
|
|
|
|
case (MINESWP_DOWN | BUTTON_REPEAT):
|
2004-08-17 06:50:14 +00:00
|
|
|
y = (y + 1)%height;
|
|
|
|
break;
|
2005-06-26 20:42:53 +00:00
|
|
|
|
2004-08-18 12:06:10 +00:00
|
|
|
/* move cursor up */
|
2005-12-14 01:31:37 +00:00
|
|
|
case MINESWP_UP:
|
|
|
|
case (MINESWP_UP | BUTTON_REPEAT):
|
2004-08-17 06:50:14 +00:00
|
|
|
y = (y + height - 1)%height;
|
|
|
|
break;
|
|
|
|
|
2004-08-18 12:06:10 +00:00
|
|
|
/* discover a tile (and it's neighbors if .neighbors == 0) */
|
2004-10-18 21:45:00 +00:00
|
|
|
case MINESWP_DISCOVER:
|
|
|
|
#ifdef MINESWP_DISCOVER2
|
|
|
|
case MINESWP_DISCOVER2:
|
|
|
|
#endif
|
2004-08-17 06:50:14 +00:00
|
|
|
if(minefield[y][x].flag) break;
|
2004-08-18 12:06:10 +00:00
|
|
|
/* we put the mines on the first "click" so that you don't */
|
|
|
|
/* lose on the first "click" */
|
2004-08-17 06:50:14 +00:00
|
|
|
if(tiles_left == width*height) minesweeper_putmines(p,x,y);
|
|
|
|
discover(x,y);
|
|
|
|
if(minefield[y][x].mine){
|
|
|
|
return MINESWEEPER_LOSE;
|
|
|
|
}
|
|
|
|
tiles_left = 0;
|
|
|
|
for(i=0;i<height;i++){
|
|
|
|
for(j=0;j<width;j++){
|
|
|
|
if(minefield[i][j].known == 0) tiles_left++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(tiles_left == mine_num){
|
|
|
|
return MINESWEEPER_WIN;
|
|
|
|
}
|
2005-06-26 20:42:53 +00:00
|
|
|
break;
|
|
|
|
|
2004-08-18 12:06:10 +00:00
|
|
|
/* toggle flag under cursor */
|
2004-10-18 21:45:00 +00:00
|
|
|
case MINESWP_TOGGLE:
|
|
|
|
#ifdef MINESWP_TOGGLE_PRE
|
|
|
|
if (lastbutton != MINESWP_TOGGLE_PRE)
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef MINESWP_TOGGLE2
|
|
|
|
case MINESWP_TOGGLE2:
|
|
|
|
#endif
|
2004-08-17 06:50:14 +00:00
|
|
|
minefield[y][x].flag = (minefield[y][x].flag + 1)%2;
|
|
|
|
break;
|
|
|
|
|
2004-08-18 12:06:10 +00:00
|
|
|
/* show how many mines you think you have found and how many */
|
|
|
|
/* there really are on the game */
|
2004-10-18 21:45:00 +00:00
|
|
|
case MINESWP_INFO:
|
2004-08-17 06:50:14 +00:00
|
|
|
tiles_left = 0;
|
|
|
|
for(i=0;i<height;i++){
|
|
|
|
for(j=0;j<width;j++){
|
|
|
|
if(minefield[i][j].flag) tiles_left++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rb->splash(HZ*2, true, "You found %d mines out of %d", tiles_left, mine_num);
|
|
|
|
break;
|
2004-10-18 21:45:00 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
|
|
|
|
return MINESWEEPER_USB;
|
|
|
|
break;
|
2004-08-17 06:50:14 +00:00
|
|
|
}
|
2004-10-18 21:45:00 +00:00
|
|
|
if (button != BUTTON_NONE)
|
|
|
|
lastbutton = button;
|
2004-08-17 06:50:14 +00:00
|
|
|
}
|
2005-06-26 20:42:53 +00:00
|
|
|
|
2004-08-17 06:50:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-18 12:06:10 +00:00
|
|
|
/* plugin entry point */
|
2004-08-17 06:50:14 +00:00
|
|
|
enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
|
|
|
|
{
|
2004-10-25 12:28:57 +00:00
|
|
|
bool exit = false;
|
2004-08-18 12:06:10 +00:00
|
|
|
/* plugin init */
|
2004-08-17 06:50:14 +00:00
|
|
|
TEST_PLUGIN_API(api);
|
|
|
|
(void)parameter;
|
|
|
|
rb = api;
|
2004-08-18 12:06:10 +00:00
|
|
|
/* end of plugin init */
|
2004-08-17 06:50:14 +00:00
|
|
|
|
2004-10-25 12:28:57 +00:00
|
|
|
while(!exit) {
|
|
|
|
switch(minesweeper()){
|
|
|
|
case MINESWEEPER_WIN:
|
|
|
|
rb->splash(HZ*2, true, "You Win :)");
|
|
|
|
break;
|
2005-06-26 20:42:53 +00:00
|
|
|
|
2004-10-25 12:28:57 +00:00
|
|
|
case MINESWEEPER_LOSE:
|
|
|
|
rb->splash(HZ*2, true, "You Lost :(");
|
|
|
|
break;
|
2005-06-26 20:42:53 +00:00
|
|
|
|
2004-10-25 12:28:57 +00:00
|
|
|
case MINESWEEPER_USB:
|
|
|
|
return PLUGIN_USB_CONNECTED;
|
2005-06-26 20:42:53 +00:00
|
|
|
|
2004-10-25 12:28:57 +00:00
|
|
|
case MINESWEEPER_QUIT:
|
|
|
|
exit = true;
|
|
|
|
break;
|
2005-06-26 20:42:53 +00:00
|
|
|
|
2004-10-25 12:28:57 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2004-08-17 06:50:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return PLUGIN_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|