2005-09-22 08:53:04 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Dave Chapman
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/***
|
|
|
|
Sudoku by Dave Chapman
|
|
|
|
|
|
|
|
User instructions
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
Use the arrow keys to move cursor, and press SELECT/ON/F2 to increment
|
|
|
|
the number under the cursor.
|
|
|
|
|
|
|
|
At any time during the game, press On to bring up the game menu with
|
|
|
|
further options:
|
|
|
|
|
|
|
|
Save
|
|
|
|
Reload
|
|
|
|
Clear
|
|
|
|
Solve
|
|
|
|
|
|
|
|
Sudoku is implemented as a "viewer" for a ".ss" file, as generated by
|
|
|
|
Simple Sudoku and other applications - http://angusj.com/sudoku/
|
|
|
|
|
|
|
|
In-progress game positions are saved in the original .ss file, with
|
|
|
|
A-I used to indicate numbers entered by the user.
|
|
|
|
|
|
|
|
Example ".ss" file, and one with a saved state:
|
|
|
|
|
|
|
|
...|...|... ...|...|...
|
|
|
|
2..|8.4|9.1 2.C|8.4|9.1
|
|
|
|
...|1.6|32. E..|1.6|32.
|
|
|
|
----------- -----------
|
|
|
|
...|..5|.4. ...|..5|.4.
|
|
|
|
8..|423|..6 8..|423|..6
|
|
|
|
.3.|9..|... .3D|9..|A..
|
|
|
|
----------- -----------
|
|
|
|
.63|7.9|... .63|7.9|...
|
|
|
|
4.9|5.2|..8 4.9|5.2|.C8
|
|
|
|
...|...|... ...|...|...
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "plugin.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_LCD_BITMAP
|
|
|
|
|
2006-01-15 18:20:18 +00:00
|
|
|
PLUGIN_HEADER
|
|
|
|
|
2005-09-22 08:53:04 +00:00
|
|
|
#define STATE_FILE PLUGIN_DIR "/sudoku.state"
|
|
|
|
#define GAMES_FILE PLUGIN_DIR "/sudoku.levels"
|
|
|
|
|
|
|
|
/* variable button definitions */
|
|
|
|
#if CONFIG_KEYPAD == RECORDER_PAD
|
|
|
|
#define SUDOKU_BUTTON_QUIT BUTTON_OFF
|
2005-12-14 01:31:37 +00:00
|
|
|
#define SUDOKU_BUTTON_UP BUTTON_UP
|
|
|
|
#define SUDOKU_BUTTON_DOWN BUTTON_DOWN
|
2005-09-22 08:53:04 +00:00
|
|
|
#define SUDOKU_BUTTON_TOGGLE BUTTON_PLAY
|
2005-09-22 21:00:03 +00:00
|
|
|
#define SUDOKU_BUTTON_MENU BUTTON_F1
|
2005-11-29 00:02:00 +00:00
|
|
|
#define SUDOKU_BUTTON_POSSIBLE BUTTON_F2
|
2005-09-22 08:53:04 +00:00
|
|
|
|
|
|
|
#elif CONFIG_KEYPAD == ONDIO_PAD
|
|
|
|
#define SUDOKU_BUTTON_QUIT BUTTON_OFF
|
2005-12-14 01:31:37 +00:00
|
|
|
#define SUDOKU_BUTTON_UP BUTTON_UP
|
|
|
|
#define SUDOKU_BUTTON_DOWN BUTTON_DOWN
|
2005-09-22 21:00:03 +00:00
|
|
|
#define SUDOKU_BUTTON_ALTTOGGLE (BUTTON_MENU | BUTTON_DOWN)
|
|
|
|
#define SUDOKU_BUTTON_TOGGLE_PRE BUTTON_MENU
|
|
|
|
#define SUDOKU_BUTTON_TOGGLE (BUTTON_MENU | BUTTON_REL)
|
|
|
|
#define SUDOKU_BUTTON_MENU_PRE BUTTON_MENU
|
|
|
|
#define SUDOKU_BUTTON_MENU (BUTTON_MENU | BUTTON_REPEAT)
|
2005-11-29 00:02:00 +00:00
|
|
|
#define SUDOKU_BUTTON_POSSIBLE (BUTTON_MENU | BUTTON_LEFT)
|
2005-09-22 08:53:04 +00:00
|
|
|
|
|
|
|
#elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
|
|
|
|
(CONFIG_KEYPAD == IRIVER_H300_PAD)
|
|
|
|
#define SUDOKU_BUTTON_QUIT BUTTON_OFF
|
2005-12-14 01:31:37 +00:00
|
|
|
#define SUDOKU_BUTTON_UP BUTTON_UP
|
|
|
|
#define SUDOKU_BUTTON_DOWN BUTTON_DOWN
|
2005-09-22 20:17:21 +00:00
|
|
|
#define SUDOKU_BUTTON_ALTTOGGLE BUTTON_ON
|
2005-09-22 08:53:04 +00:00
|
|
|
#define SUDOKU_BUTTON_TOGGLE BUTTON_SELECT
|
|
|
|
#define SUDOKU_BUTTON_MENU BUTTON_MODE
|
2005-11-29 00:02:00 +00:00
|
|
|
#define SUDOKU_BUTTON_POSSIBLE BUTTON_REC
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2006-02-24 20:54:09 +00:00
|
|
|
#elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
|
|
|
|
(CONFIG_KEYPAD == IPOD_3G_PAD)
|
2005-12-14 01:31:37 +00:00
|
|
|
#define SUDOKU_BUTTON_QUIT (BUTTON_SELECT | BUTTON_MENU)
|
|
|
|
#define SUDOKU_BUTTON_UP BUTTON_SCROLL_BACK
|
|
|
|
#define SUDOKU_BUTTON_DOWN BUTTON_SCROLL_FWD
|
|
|
|
#define SUDOKU_BUTTON_TOGGLE BUTTON_SELECT
|
|
|
|
#define SUDOKU_BUTTON_MENU BUTTON_MENU
|
|
|
|
#define SUDOKU_BUTTON_POSSIBLE (BUTTON_SELECT | BUTTON_LEFT)
|
|
|
|
|
2006-01-18 11:09:06 +00:00
|
|
|
#elif (CONFIG_KEYPAD == IAUDIO_X5_PAD)
|
|
|
|
#define SUDOKU_BUTTON_QUIT BUTTON_POWER
|
|
|
|
#define SUDOKU_BUTTON_UP BUTTON_UP
|
|
|
|
#define SUDOKU_BUTTON_DOWN BUTTON_DOWN
|
2006-02-24 15:06:25 +00:00
|
|
|
#define SUDOKU_BUTTON_TOGGLE BUTTON_SELECT
|
2006-01-18 11:09:06 +00:00
|
|
|
#define SUDOKU_BUTTON_MENU BUTTON_PLAY
|
|
|
|
#define SUDOKU_BUTTON_POSSIBLE BUTTON_REC
|
2005-12-14 01:31:37 +00:00
|
|
|
|
2006-02-24 15:42:52 +00:00
|
|
|
#elif (CONFIG_KEYPAD == GIGABEAT_PAD)
|
|
|
|
#define SUDOKU_BUTTON_QUIT BUTTON_A
|
|
|
|
#define SUDOKU_BUTTON_UP BUTTON_UP
|
|
|
|
#define SUDOKU_BUTTON_DOWN BUTTON_DOWN
|
|
|
|
#define SUDOKU_BUTTON_TOGGLE BUTTON_SELECT
|
|
|
|
#define SUDOKU_BUTTON_MENU BUTTON_MENU
|
|
|
|
#define SUDOKU_BUTTON_POSSIBLE BUTTON_POWER
|
|
|
|
|
2005-09-22 08:53:04 +00:00
|
|
|
#elif
|
|
|
|
#error SUDOKU: Unsupported keypad
|
|
|
|
#endif
|
|
|
|
|
2006-01-22 21:30:32 +00:00
|
|
|
/* The bitmaps */
|
|
|
|
extern const fb_data sudoku_normal[];
|
|
|
|
extern const fb_data sudoku_start[];
|
|
|
|
extern const fb_data sudoku_inverse[];
|
|
|
|
|
2005-09-22 08:53:04 +00:00
|
|
|
#if (LCD_HEIGHT==128) && (LCD_WIDTH==160)
|
|
|
|
/* For iriver H1x0 - 160x128, 9 cells @ 12x12 with 14 border lines*/
|
|
|
|
|
|
|
|
/* Internal dimensions of a cell */
|
|
|
|
#define CELL_WIDTH 12
|
|
|
|
#define CELL_HEIGHT 12
|
|
|
|
|
|
|
|
#define BOARD_WIDTH (CELL_WIDTH*9+10+4)
|
|
|
|
#define BOARD_HEIGHT (CELL_HEIGHT*9+10+4)
|
|
|
|
|
2005-11-29 00:02:00 +00:00
|
|
|
#define XOFS (((LCD_WIDTH-BOARD_WIDTH)/2)+10)
|
2005-09-22 08:53:04 +00:00
|
|
|
#define YOFS ((LCD_HEIGHT-BOARD_HEIGHT)/2)
|
|
|
|
|
2005-11-29 00:02:00 +00:00
|
|
|
#define XOFSSCRATCHPAD 3
|
|
|
|
|
2005-09-22 08:53:04 +00:00
|
|
|
/* Locations of each cell */
|
|
|
|
static unsigned char cellxpos[9]={ 2, 15, 28, 42, 55, 68, 82, 95, 108 };
|
|
|
|
static unsigned char cellypos[9]={ 2, 15, 28, 42, 55, 68, 82, 95, 108 };
|
|
|
|
|
2006-01-22 21:30:32 +00:00
|
|
|
/* The height of one cell in the bitmap */
|
|
|
|
#define BITMAP_HEIGHT 12
|
2006-01-24 23:24:58 +00:00
|
|
|
#define BITMAP_STRIDE 12
|
2005-09-22 08:53:04 +00:00
|
|
|
|
|
|
|
#elif (LCD_HEIGHT==64) && (LCD_WIDTH==112)
|
|
|
|
/* For Archos Recorder, FM and Ondio (112x64):
|
|
|
|
9 cells @ 8x6 with 10 border lines
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Internal dimensions of a cell */
|
|
|
|
#define CELL_WIDTH 8
|
|
|
|
#define CELL_HEIGHT 6
|
|
|
|
|
|
|
|
#define BOARD_WIDTH (CELL_WIDTH*9+10)
|
|
|
|
#define BOARD_HEIGHT (CELL_HEIGHT*9+10)
|
|
|
|
|
2005-11-29 00:02:00 +00:00
|
|
|
#define XOFS (((LCD_WIDTH-BOARD_WIDTH)/2)+7)
|
2005-09-22 08:53:04 +00:00
|
|
|
#define YOFS ((LCD_HEIGHT-BOARD_HEIGHT)/2)
|
|
|
|
|
2005-11-29 00:02:00 +00:00
|
|
|
#define XOFSSCRATCHPAD 2
|
|
|
|
|
2005-09-22 08:53:04 +00:00
|
|
|
/* Locations of each cell */
|
|
|
|
static unsigned char cellxpos[9]={ 1, 10, 19, 28, 37, 46, 55, 64, 73 };
|
|
|
|
static unsigned char cellypos[9]={ 1, 8, 15, 22, 29, 36, 43, 50, 57 };
|
|
|
|
|
2006-01-22 21:30:32 +00:00
|
|
|
/* The height of one cell in the bitmap */
|
|
|
|
#define BITMAP_HEIGHT 8
|
2006-01-24 23:24:58 +00:00
|
|
|
#define BITMAP_STRIDE 8
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2005-12-18 13:04:00 +00:00
|
|
|
#elif (LCD_HEIGHT>=176) && (LCD_WIDTH>=220)
|
2005-11-27 23:59:05 +00:00
|
|
|
/* iriver h300 */
|
|
|
|
|
|
|
|
/* Internal dimensions of a cell */
|
|
|
|
#define CELL_WIDTH 16
|
|
|
|
#define CELL_HEIGHT 16
|
|
|
|
|
|
|
|
#define BOARD_WIDTH (CELL_WIDTH*9+10+4)
|
|
|
|
#define BOARD_HEIGHT (CELL_HEIGHT*9+10+4)
|
|
|
|
|
2005-11-29 00:02:00 +00:00
|
|
|
#define XOFS (((LCD_WIDTH-BOARD_WIDTH)/2)+15)
|
2005-11-27 23:59:05 +00:00
|
|
|
#define YOFS ((LCD_HEIGHT-BOARD_HEIGHT)/2)
|
|
|
|
|
2005-11-29 00:02:00 +00:00
|
|
|
#define XOFSSCRATCHPAD 10
|
|
|
|
|
2005-11-27 23:59:05 +00:00
|
|
|
/* Locations of each cell */
|
|
|
|
static unsigned char cellxpos[9]={ 2, 19, 36, 54, 71, 88, 106, 123, 140 };
|
|
|
|
static unsigned char cellypos[9]={ 2, 19, 36, 54, 71, 88, 106, 123, 140 };
|
|
|
|
|
2006-01-22 21:30:32 +00:00
|
|
|
/* The height of one cell in the bitmap */
|
|
|
|
#define BITMAP_HEIGHT 16
|
2006-01-24 23:24:58 +00:00
|
|
|
#define BITMAP_STRIDE 16
|
2005-11-27 23:59:05 +00:00
|
|
|
|
|
|
|
#else
|
2005-09-22 08:53:04 +00:00
|
|
|
#error SUDOKU: Unsupported LCD size
|
|
|
|
#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;
|
|
|
|
|
|
|
|
struct sudoku_state_t {
|
2005-10-04 23:36:03 +00:00
|
|
|
char filename[MAX_PATH]; /* Filename */
|
2005-09-22 08:53:04 +00:00
|
|
|
char startboard[9][9]; /* The initial state of the game */
|
|
|
|
char currentboard[9][9]; /* The current state of the game */
|
|
|
|
char savedboard[9][9]; /* Cached copy of saved state */
|
|
|
|
int x,y; /* Cursor position */
|
2005-10-04 23:36:03 +00:00
|
|
|
int editmode; /* We are editing the start board */
|
2005-11-29 00:02:00 +00:00
|
|
|
#ifdef SUDOKU_BUTTON_POSSIBLE
|
|
|
|
short possiblevals[9][9]; /* possible values a cell could be, user sets them */
|
|
|
|
#endif
|
2005-09-22 08:53:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/****** Solver routine by Tom Shackell <shackell@cs.york.ac.uk>
|
|
|
|
|
|
|
|
Downloaded from:
|
|
|
|
|
|
|
|
http://www-users.cs.york.ac.uk/~shackell/sudoku/Sudoku.html
|
|
|
|
|
|
|
|
Released under GPLv2
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef unsigned int Bitset;
|
|
|
|
|
|
|
|
#define BLOCK 3
|
|
|
|
#define SIZE (BLOCK*BLOCK)
|
|
|
|
|
|
|
|
#define true 1
|
|
|
|
#define false 0
|
|
|
|
|
|
|
|
typedef struct _Sudoku {
|
2006-03-03 08:14:44 +00:00
|
|
|
Bitset table[SIZE][SIZE];
|
2005-09-22 08:53:04 +00:00
|
|
|
}Sudoku;
|
|
|
|
|
|
|
|
typedef struct _Stats {
|
2006-03-03 08:14:44 +00:00
|
|
|
int numTries;
|
|
|
|
int backTracks;
|
|
|
|
int numEmpty;
|
|
|
|
bool solutionFound;
|
2005-09-22 08:53:04 +00:00
|
|
|
}Stats;
|
|
|
|
|
|
|
|
typedef struct _Options {
|
2006-03-03 08:14:44 +00:00
|
|
|
bool allSolutions;
|
|
|
|
bool uniquenessCheck;
|
2005-09-22 08:53:04 +00:00
|
|
|
}Options;
|
|
|
|
|
|
|
|
void sudoku_init(Sudoku* sud);
|
|
|
|
void sudoku_set(Sudoku* sud, int x, int y, int num, bool original);
|
|
|
|
int sudoku_get(Sudoku* sud, int x, int y, bool* original);
|
|
|
|
|
|
|
|
#define BIT(n) ((Bitset)(1<<(n)))
|
|
|
|
#define BIT_TEST(v,n) ((((Bitset)v) & BIT(n)) != 0)
|
|
|
|
#define BIT_CLEAR(v,n) (v) &= ~BIT(n)
|
|
|
|
#define MARK_BIT BIT(0)
|
|
|
|
#define ORIGINAL_BIT BIT(SIZE+1)
|
|
|
|
|
|
|
|
#define ALL_BITS (BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(5) | BIT(6) | BIT(7) | BIT(8) | BIT(9))
|
|
|
|
|
|
|
|
/* initialize a sudoku problem, should be called before using set or get */
|
2006-03-03 08:14:44 +00:00
|
|
|
void sudoku_init(Sudoku* sud)
|
|
|
|
{
|
|
|
|
int y, x;
|
|
|
|
for (y = 0; y < SIZE; y++){
|
|
|
|
for (x = 0; x < SIZE; x++){
|
|
|
|
sud->table[x][y] = ALL_BITS;
|
|
|
|
}
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set the number at a particular x and y column */
|
2006-03-03 08:14:44 +00:00
|
|
|
void sudoku_set(Sudoku* sud, int x, int y, int num, bool original)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
int bx, by;
|
|
|
|
Bitset orig;
|
|
|
|
|
|
|
|
/* clear the row and columns */
|
|
|
|
for (i = 0; i < SIZE; i++){
|
|
|
|
BIT_CLEAR(sud->table[i][y], num);
|
|
|
|
BIT_CLEAR(sud->table[x][i], num);
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
2006-03-03 08:14:44 +00:00
|
|
|
/* clear the block */
|
|
|
|
bx = x - (x % BLOCK);
|
|
|
|
by = y - (y % BLOCK);
|
|
|
|
for (i = 0; i < BLOCK; i++){
|
|
|
|
for (j = 0; j < BLOCK; j++){
|
|
|
|
BIT_CLEAR(sud->table[bx+j][by+i], num);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* mark the table */
|
|
|
|
orig = original ? ORIGINAL_BIT : 0;
|
|
|
|
sud->table[x][y] = BIT(num) | MARK_BIT | orig;
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get the number at a particular x and y column, if this
|
|
|
|
is not unique return 0 */
|
2006-03-03 08:14:44 +00:00
|
|
|
int sudoku_get(Sudoku* sud, int x, int y, bool* original)
|
|
|
|
{
|
|
|
|
Bitset val = sud->table[x][y];
|
|
|
|
int result = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (original) {
|
|
|
|
*original = val & ORIGINAL_BIT;
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
2006-03-03 08:14:44 +00:00
|
|
|
for (i = 1; i <= SIZE; i++){
|
|
|
|
if (BIT_TEST(val, i)){
|
|
|
|
if (result != 0){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
result = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* returns true if this is a valid problem, this is necessary because the input
|
|
|
|
problem might be degenerate which breaks the solver algorithm. */
|
2006-03-03 08:14:44 +00:00
|
|
|
static bool is_valid(const Sudoku* sud)
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
|
|
|
|
for (y = 0; y < SIZE; y++){
|
|
|
|
for (x = 0; x < SIZE; x++){
|
|
|
|
if ((sud->table[x][y] & ALL_BITS) == 0){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
2006-03-03 08:14:44 +00:00
|
|
|
return true;
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
/* scan the table for the most constrained item, giving all it's options, sets
|
|
|
|
the best x and y coordinates, the number of options and the options for
|
|
|
|
that coordinate and returns true if the puzzle is finished */
|
|
|
|
static bool scan(const Sudoku* sud, int* rX, int* rY, int *num, int* options)
|
|
|
|
{
|
|
|
|
int x, y, i, j;
|
|
|
|
int bestCount = SIZE+1;
|
|
|
|
Bitset val;
|
|
|
|
bool allMarked = true;
|
|
|
|
|
|
|
|
for (y = 0; y < SIZE; y++){
|
|
|
|
for (x = 0; x < SIZE; x++){
|
|
|
|
Bitset val = sud->table[x][y];
|
|
|
|
int i;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
if (val & MARK_BIT) {
|
|
|
|
/* already set */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
allMarked = false;
|
|
|
|
for (i = 1; i <= SIZE; i++){
|
|
|
|
if (BIT_TEST(val, i)){
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (count < bestCount){
|
|
|
|
bestCount = count;
|
|
|
|
*rX = x;
|
|
|
|
*rY = y;
|
|
|
|
if (count == 0){
|
|
|
|
/* can't possibly be beaten */
|
|
|
|
*num = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
|
|
|
}
|
2006-03-03 08:14:44 +00:00
|
|
|
/* now copy into options */
|
|
|
|
*num = bestCount;
|
|
|
|
val = sud->table[*rX][*rY];
|
|
|
|
for (i = 1, j = 0; i <= SIZE; i++){
|
|
|
|
if (BIT_TEST(val, i)){
|
|
|
|
options[j++] = i;
|
|
|
|
}
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
2006-03-03 08:14:44 +00:00
|
|
|
return allMarked;
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool solve(Sudoku* sud, Stats* stats, const Options* options);
|
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
/* try a particular option and return true if that gives a solution or false
|
|
|
|
if it doesn't, restores board on backtracking */
|
|
|
|
static bool spawn_option(Sudoku* sud, Stats* stats, const Options* options,
|
|
|
|
int x, int y, int num)
|
|
|
|
{
|
|
|
|
Sudoku copy;
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
rb->memcpy(©,sud,sizeof(Sudoku));
|
|
|
|
sudoku_set(©, x, y, num, false);
|
|
|
|
stats->numTries += 1;
|
|
|
|
if (solve(©, stats, options)){
|
|
|
|
if (!options->allSolutions && stats->solutionFound){
|
|
|
|
rb->memcpy(sud,©,sizeof(Sudoku));
|
|
|
|
}
|
2005-09-22 08:53:04 +00:00
|
|
|
return true;
|
2006-03-03 08:14:44 +00:00
|
|
|
}else{
|
|
|
|
stats->backTracks++;
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
2006-03-03 08:14:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* solve a sudoku problem, returns true if there is a solution and false
|
|
|
|
otherwise. stats is used to track statisticss */
|
|
|
|
static bool solve(Sudoku* sud, Stats* stats, const Options* options)
|
|
|
|
{
|
|
|
|
while (true){
|
|
|
|
int x, y, i, num;
|
|
|
|
int places[SIZE];
|
|
|
|
|
|
|
|
if (scan(sud, &x, &y, &num, places)){
|
|
|
|
/* a solution was found! */
|
|
|
|
if (options->uniquenessCheck && stats->solutionFound){
|
|
|
|
/*printf("\n\t... But the solution is not unique!\n"); */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
stats->solutionFound = true;
|
|
|
|
if (options->allSolutions || options->uniquenessCheck){
|
|
|
|
/*printf("\n\tSolution after %d iterations\n", stats->numTries); */
|
|
|
|
/*sudoku_print(sud); */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return true;
|
|
|
|
}
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
2006-03-03 08:14:44 +00:00
|
|
|
if (num == 0){
|
|
|
|
/* can't be satisfied */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/* try all the places (except the last one) */
|
|
|
|
for (i = 0; i < num-1; i++){
|
|
|
|
if (spawn_option(sud, stats, options, x, y, places[i])){
|
|
|
|
/* solution found! */
|
|
|
|
if (!options->allSolutions && stats->solutionFound){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* take the last place ourself */
|
|
|
|
stats->numTries += 1;
|
|
|
|
sudoku_set(sud, x, y, places[num-1], false);
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******** END OF IMPORTED CODE */
|
|
|
|
|
|
|
|
|
|
|
|
/* A wrapper function between the Sudoku plugin and the above solver code */
|
2006-03-03 08:14:44 +00:00
|
|
|
void sudoku_solve(struct sudoku_state_t* state)
|
|
|
|
{
|
|
|
|
bool ret;
|
|
|
|
Stats stats;
|
|
|
|
Options options;
|
|
|
|
Sudoku sud;
|
|
|
|
bool original;
|
|
|
|
int r,c;
|
|
|
|
|
|
|
|
/* Initialise the parameters */
|
|
|
|
sudoku_init(&sud);
|
|
|
|
rb->memset(&stats,0,sizeof(stats));
|
|
|
|
options.allSolutions=false;
|
|
|
|
options.uniquenessCheck=false;
|
|
|
|
|
|
|
|
/* Convert Rockbox format into format for solver */
|
|
|
|
for (r=0;r<9;r++) {
|
|
|
|
for (c=0;c<9;c++) {
|
|
|
|
if (state->startboard[r][c]!='0') {
|
|
|
|
sudoku_set(&sud, c, r, state->startboard[r][c]-'0', true);
|
|
|
|
}
|
|
|
|
}
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
/* need to check for degenerate input problems ... */
|
|
|
|
if (is_valid(&sud)){
|
|
|
|
ret = solve(&sud, &stats, &options);
|
|
|
|
} else {
|
|
|
|
ret = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
/* Populate the board with the solution. */
|
|
|
|
for (r=0;r<9;r++) {
|
|
|
|
for (c=0;c<9;c++) {
|
|
|
|
state->currentboard[r][c]='0'+
|
|
|
|
sudoku_get(&sud, c, r, &original);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rb->splash(HZ*2, true, "Solve failed");
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
return;
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void clear_state(struct sudoku_state_t* state)
|
|
|
|
{
|
2006-03-03 08:14:44 +00:00
|
|
|
int r,c;
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
state->filename[0]=0;
|
|
|
|
for (r=0;r<9;r++) {
|
|
|
|
for (c=0;c<9;c++) {
|
|
|
|
state->startboard[r][c]='0';
|
|
|
|
state->currentboard[r][c]='0';
|
2005-11-29 00:02:00 +00:00
|
|
|
#ifdef SUDOKU_BUTTON_POSSIBLE
|
2006-03-03 08:14:44 +00:00
|
|
|
state->possiblevals[r][c]=0;
|
2005-11-29 00:02:00 +00:00
|
|
|
#endif
|
2006-03-03 08:14:44 +00:00
|
|
|
}
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
state->x=0;
|
|
|
|
state->y=0;
|
|
|
|
state->editmode=0;
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Load game - only ".ss" is officially supported, but any sensible
|
|
|
|
text representation (one line per row) may load.
|
|
|
|
*/
|
2006-03-03 08:14:44 +00:00
|
|
|
bool load_sudoku(struct sudoku_state_t* state, char* filename)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
size_t n;
|
|
|
|
int r = 0, c = 0;
|
|
|
|
unsigned int i;
|
|
|
|
int valid=0;
|
|
|
|
char buf[300]; /* A buffer to read a sudoku board from */
|
|
|
|
|
|
|
|
fd=rb->open(filename, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
rb->splash(HZ*2, true, "Can not open");
|
|
|
|
LOGF("Invalid sudoku file: %s\n",filename);
|
2005-09-22 08:53:04 +00:00
|
|
|
return(false);
|
2006-03-03 08:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rb->strncpy(state->filename,filename,MAX_PATH);
|
|
|
|
n=rb->read(fd,buf,300);
|
|
|
|
if (n <= 0) {
|
|
|
|
return(false);
|
|
|
|
}
|
|
|
|
rb->close(fd);
|
|
|
|
|
|
|
|
r=0;
|
|
|
|
c=0;
|
|
|
|
i=0;
|
|
|
|
while ((i < n) && (r < 9)) {
|
|
|
|
switch (buf[i]){
|
|
|
|
case ' ': case '\t':
|
|
|
|
if (c > 0)
|
|
|
|
valid=1;
|
|
|
|
break;
|
|
|
|
case '|':
|
|
|
|
case '*':
|
|
|
|
case '-':
|
|
|
|
case '\r':
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
if (valid) {
|
|
|
|
r++;
|
|
|
|
valid=0;
|
|
|
|
}
|
|
|
|
c = 0;
|
|
|
|
break;
|
|
|
|
case '_': case '.':
|
|
|
|
valid=1;
|
|
|
|
if (c >= SIZE || r >= SIZE){
|
|
|
|
LOGF("ERROR: sudoku problem is the wrong size (%d,%d)\n",
|
|
|
|
c, r);
|
|
|
|
return(false);
|
|
|
|
}
|
|
|
|
c++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (((buf[i]>='A') && (buf[i]<='I')) ||
|
|
|
|
((buf[i]>='0') && (buf[i]<='9'))) {
|
|
|
|
valid=1;
|
|
|
|
if (r >= SIZE || c >= SIZE){
|
|
|
|
LOGF("ERROR: sudoku problem is the wrong size "
|
|
|
|
"(%d,%d)\n", c, r);
|
|
|
|
return(false);
|
|
|
|
}
|
|
|
|
if ((buf[i]>='0') && (buf[i]<='9')) {
|
|
|
|
state->startboard[r][c]=buf[i];
|
|
|
|
state->currentboard[r][c]=buf[i];
|
|
|
|
} else {
|
|
|
|
state->currentboard[r][c]='1'+(buf[i]-'A');
|
|
|
|
}
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
/* Ignore any other characters */
|
|
|
|
break;
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
2006-03-03 08:14:44 +00:00
|
|
|
i++;
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
/* Save a copy of the saved state - so we can reload without using the
|
|
|
|
disk */
|
|
|
|
rb->memcpy(state->savedboard,state->currentboard,81);
|
|
|
|
return(true);
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
bool save_sudoku(struct sudoku_state_t* state)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int r,c;
|
|
|
|
int i;
|
|
|
|
char line[16];
|
|
|
|
char sep[16];
|
2006-01-20 21:02:17 +00:00
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
rb->memcpy(line,"...|...|...\r\n",13);
|
|
|
|
rb->memcpy(sep,"-----------\r\n",13);
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
if (state->filename[0]==0) {
|
|
|
|
return false;
|
|
|
|
}
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
fd=rb->open(state->filename, O_WRONLY|O_CREAT);
|
|
|
|
if (fd >= 0) {
|
|
|
|
for (r=0;r<9;r++) {
|
|
|
|
i=0;
|
|
|
|
for (c=0;c<9;c++) {
|
|
|
|
if (state->startboard[r][c]!='0') {
|
|
|
|
line[i]=state->startboard[r][c];
|
|
|
|
} else if (state->currentboard[r][c]!='0') {
|
|
|
|
line[i]='A'+(state->currentboard[r][c]-'1');
|
|
|
|
} else {
|
|
|
|
line[i]='.';
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
if ((c==2) || (c==5)) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rb->write(fd,line,sizeof(line)-1);
|
|
|
|
if ((r==2) || (r==5)) {
|
|
|
|
rb->write(fd,sep,sizeof(sep)-1);
|
|
|
|
}
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
2006-03-03 08:14:44 +00:00
|
|
|
/* Add a blank line at end */
|
|
|
|
rb->write(fd,"\r\n",2);
|
|
|
|
rb->close(fd);
|
|
|
|
/* Save a copy of the saved state - so we can reload without
|
|
|
|
using the disk */
|
|
|
|
rb->memcpy(state->savedboard,state->currentboard,81);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void restore_state(struct sudoku_state_t* state)
|
|
|
|
{
|
2006-03-03 08:14:44 +00:00
|
|
|
rb->memcpy(state->currentboard,state->savedboard,81);
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void clear_board(struct sudoku_state_t* state)
|
|
|
|
{
|
2006-03-03 08:14:44 +00:00
|
|
|
int r,c;
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
for (r=0;r<9;r++) {
|
|
|
|
for (c=0;c<9;c++) {
|
|
|
|
state->currentboard[r][c]=state->startboard[r][c];
|
|
|
|
}
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
2006-03-03 08:14:44 +00:00
|
|
|
state->x=0;
|
|
|
|
state->y=0;
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void update_cell(struct sudoku_state_t* state, int r, int c)
|
|
|
|
{
|
2006-03-03 08:14:44 +00:00
|
|
|
/* We have four types of cell:
|
|
|
|
1) User-entered number
|
|
|
|
2) Starting number
|
|
|
|
3) Cursor in cell
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((r==state->y) && (c==state->x)) {
|
|
|
|
rb->lcd_bitmap_part(sudoku_inverse,0,
|
|
|
|
BITMAP_HEIGHT*(state->currentboard[r][c]-'0'),
|
|
|
|
BITMAP_STRIDE,
|
|
|
|
XOFS+cellxpos[c],YOFS+cellypos[r],CELL_WIDTH,
|
|
|
|
CELL_HEIGHT);
|
|
|
|
} else {
|
2005-09-22 08:53:04 +00:00
|
|
|
if (state->startboard[r][c]!='0') {
|
2006-03-03 08:14:44 +00:00
|
|
|
rb->lcd_bitmap_part(sudoku_start,0,
|
|
|
|
BITMAP_HEIGHT*(state->startboard[r][c]-'0'),
|
|
|
|
BITMAP_STRIDE,
|
|
|
|
XOFS+cellxpos[c],YOFS+cellypos[r],
|
|
|
|
CELL_WIDTH,CELL_HEIGHT);
|
2005-09-22 08:53:04 +00:00
|
|
|
} else {
|
2006-03-03 08:14:44 +00:00
|
|
|
rb->lcd_bitmap_part(sudoku_normal,0,
|
|
|
|
BITMAP_HEIGHT*(state->currentboard[r][c]-'0'),
|
|
|
|
BITMAP_STRIDE,
|
|
|
|
XOFS+cellxpos[c],YOFS+cellypos[r],
|
|
|
|
CELL_WIDTH,CELL_HEIGHT);
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
2006-03-03 08:14:44 +00:00
|
|
|
}
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
rb->lcd_update_rect(cellxpos[c],cellypos[r],CELL_WIDTH,CELL_HEIGHT);
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void display_board(struct sudoku_state_t* state)
|
|
|
|
{
|
2006-03-03 08:14:44 +00:00
|
|
|
int r,c;
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
/* Clear the display buffer */
|
|
|
|
rb->lcd_clear_display();
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
/* Draw the gridlines - differently for different targets */
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2005-11-27 23:59:05 +00:00
|
|
|
#if LCD_HEIGHT > 64
|
2006-03-03 08:14:44 +00:00
|
|
|
/* Large targets - draw single/double lines */
|
|
|
|
for (r=0;r<9;r++) {
|
|
|
|
rb->lcd_hline(XOFS,XOFS+BOARD_WIDTH-1,YOFS+cellypos[r]-1);
|
|
|
|
rb->lcd_vline(XOFS+cellxpos[r]-1,YOFS,YOFS+BOARD_HEIGHT-1);
|
|
|
|
if ((r % 3)==0) {
|
|
|
|
rb->lcd_hline(XOFS,XOFS+BOARD_WIDTH-1,YOFS+cellypos[r]-2);
|
|
|
|
rb->lcd_vline(XOFS+cellxpos[r]-2,YOFS,YOFS+BOARD_HEIGHT-1);
|
|
|
|
}
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
2006-03-03 08:14:44 +00:00
|
|
|
rb->lcd_hline(XOFS,XOFS+BOARD_WIDTH-1,YOFS+cellypos[8]+CELL_HEIGHT);
|
|
|
|
rb->lcd_hline(XOFS,XOFS+BOARD_WIDTH-1,YOFS+cellypos[8]+CELL_HEIGHT+1);
|
|
|
|
rb->lcd_vline(XOFS+cellxpos[8]+CELL_WIDTH,YOFS,YOFS+BOARD_HEIGHT-1);
|
|
|
|
rb->lcd_vline(XOFS+cellxpos[8]+CELL_WIDTH+1,YOFS,YOFS+BOARD_HEIGHT-1);
|
2005-09-22 08:53:04 +00:00
|
|
|
#elif (LCD_HEIGHT==64)
|
2006-03-03 08:14:44 +00:00
|
|
|
/* Small targets - draw dotted/single lines */
|
|
|
|
for (r=0;r<9;r++) {
|
|
|
|
if ((r % 3)==0) {
|
|
|
|
/* Solid Line */
|
|
|
|
rb->lcd_hline(XOFS,XOFS+BOARD_WIDTH-1,YOFS+cellypos[r]-1);
|
|
|
|
rb->lcd_vline(XOFS+cellxpos[r]-1,YOFS,YOFS+BOARD_HEIGHT-1);
|
|
|
|
} else {
|
|
|
|
/* Dotted line */
|
|
|
|
for (c=XOFS;c<XOFS+BOARD_WIDTH;c+=2) {
|
|
|
|
rb->lcd_drawpixel(c,YOFS+cellypos[r]-1);
|
|
|
|
}
|
|
|
|
for (c=YOFS;c<YOFS+BOARD_HEIGHT;c+=2) {
|
|
|
|
rb->lcd_drawpixel(XOFS+cellxpos[r]-1,c);
|
|
|
|
}
|
|
|
|
}
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
2006-03-03 08:14:44 +00:00
|
|
|
rb->lcd_hline(XOFS,XOFS+BOARD_WIDTH-1,YOFS+cellypos[8]+CELL_HEIGHT);
|
|
|
|
rb->lcd_vline(XOFS+cellxpos[8]+CELL_WIDTH,YOFS,YOFS+BOARD_HEIGHT-1);
|
2005-09-22 08:53:04 +00:00
|
|
|
#else
|
2006-03-03 08:14:44 +00:00
|
|
|
#error SUDOKU: Unsupported LCD height
|
2005-09-22 08:53:04 +00:00
|
|
|
#endif
|
|
|
|
|
2005-11-29 00:02:00 +00:00
|
|
|
#ifdef SUDOKU_BUTTON_POSSIBLE
|
2006-03-03 08:14:44 +00:00
|
|
|
rb->lcd_vline(XOFSSCRATCHPAD,YOFS,YOFS+BOARD_HEIGHT-1);
|
|
|
|
rb->lcd_vline(XOFSSCRATCHPAD+CELL_WIDTH+1,YOFS,YOFS+BOARD_HEIGHT-1);
|
|
|
|
for (r=0;r<9;r++) {
|
2006-01-20 00:45:38 +00:00
|
|
|
#if LCD_HEIGHT > 64
|
2006-03-03 08:14:44 +00:00
|
|
|
/* Large targets - draw single/double lines */
|
|
|
|
rb->lcd_hline(XOFSSCRATCHPAD,XOFSSCRATCHPAD+CELL_WIDTH+1,
|
|
|
|
YOFS+cellypos[r]-1);
|
|
|
|
if ((r % 3)==0)
|
|
|
|
rb->lcd_hline(XOFSSCRATCHPAD,XOFSSCRATCHPAD+CELL_WIDTH+1,
|
|
|
|
YOFS+cellypos[r]-2);
|
2006-01-20 00:45:38 +00:00
|
|
|
#elif LCD_HEIGHT == 64
|
2006-03-03 08:14:44 +00:00
|
|
|
/* Small targets - draw dotted/single lines */
|
|
|
|
if ((r % 3)==0) {
|
|
|
|
/* Solid Line */
|
|
|
|
rb->lcd_hline(XOFSSCRATCHPAD,XOFSSCRATCHPAD+CELL_WIDTH+1,
|
|
|
|
YOFS+cellypos[r]-1);
|
|
|
|
} else {
|
|
|
|
/* Dotted line */
|
|
|
|
for (c=XOFSSCRATCHPAD;c<XOFSSCRATCHPAD+CELL_WIDTH+1;c+=2) {
|
|
|
|
rb->lcd_drawpixel(c,YOFS+cellypos[r]-1);
|
|
|
|
}
|
|
|
|
}
|
2005-11-29 00:02:00 +00:00
|
|
|
#endif
|
2006-03-03 08:14:44 +00:00
|
|
|
if ((r>0) && state->possiblevals[state->y][state->x]&(1<<(r)))
|
|
|
|
rb->lcd_bitmap_part(sudoku_normal,0,BITMAP_HEIGHT*r,BITMAP_STRIDE,
|
|
|
|
XOFSSCRATCHPAD+1,YOFS+cellypos[r-1],
|
|
|
|
CELL_WIDTH,CELL_HEIGHT);
|
|
|
|
}
|
|
|
|
rb->lcd_hline(XOFSSCRATCHPAD,XOFSSCRATCHPAD+CELL_WIDTH+1,
|
|
|
|
YOFS+cellypos[8]+CELL_HEIGHT);
|
2006-01-20 00:45:38 +00:00
|
|
|
#if LCD_HEIGHT > 64
|
2006-03-03 08:14:44 +00:00
|
|
|
rb->lcd_hline(XOFSSCRATCHPAD,XOFSSCRATCHPAD+CELL_WIDTH+1,
|
|
|
|
YOFS+cellypos[8]+CELL_HEIGHT+1);
|
2006-01-20 00:45:38 +00:00
|
|
|
#endif
|
2006-03-03 08:14:44 +00:00
|
|
|
if (state->possiblevals[state->y][state->x]&(1<<(r)))
|
|
|
|
rb->lcd_bitmap_part(sudoku_normal,0,BITMAP_HEIGHT*r,BITMAP_STRIDE,
|
|
|
|
XOFSSCRATCHPAD+1,YOFS+cellypos[8],
|
|
|
|
CELL_WIDTH,CELL_HEIGHT);
|
2005-11-29 00:02:00 +00:00
|
|
|
#endif
|
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
/* Draw the numbers */
|
|
|
|
for (r=0;r<9;r++) {
|
|
|
|
for (c=0;c<9;c++) {
|
|
|
|
/* We have four types of cell:
|
|
|
|
1) User-entered number
|
|
|
|
2) Starting number
|
|
|
|
3) Cursor in cell
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((r==state->y) && (c==state->x)) {
|
|
|
|
rb->lcd_bitmap_part(sudoku_inverse,0,
|
|
|
|
BITMAP_HEIGHT*(state->currentboard[r][c]-
|
|
|
|
'0'),
|
|
|
|
BITMAP_STRIDE,
|
|
|
|
XOFS+cellxpos[c],YOFS+cellypos[r],
|
|
|
|
CELL_WIDTH,CELL_HEIGHT);
|
|
|
|
} else {
|
|
|
|
if (state->startboard[r][c]!='0') {
|
|
|
|
rb->lcd_bitmap_part(sudoku_start,0,
|
|
|
|
BITMAP_HEIGHT*(state->startboard[r][c]-
|
|
|
|
'0'),
|
|
|
|
BITMAP_STRIDE,
|
|
|
|
XOFS+cellxpos[c],YOFS+cellypos[r],
|
|
|
|
CELL_WIDTH,CELL_HEIGHT);
|
|
|
|
} else {
|
|
|
|
rb->lcd_bitmap_part(sudoku_normal,0,
|
|
|
|
BITMAP_HEIGHT*
|
|
|
|
(state->currentboard[r][c]-'0'),
|
|
|
|
BITMAP_STRIDE,
|
|
|
|
XOFS+cellxpos[c],YOFS+cellypos[r],
|
|
|
|
CELL_WIDTH,CELL_HEIGHT);
|
|
|
|
}
|
|
|
|
}
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
/* update the screen */
|
|
|
|
rb->lcd_update();
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the status of the board, assuming a change at the cursor location */
|
2006-03-03 08:14:44 +00:00
|
|
|
bool check_status(struct sudoku_state_t* state)
|
|
|
|
{
|
|
|
|
int check[9];
|
|
|
|
int r,c;
|
|
|
|
int r1,c1;
|
|
|
|
int cell;
|
|
|
|
|
|
|
|
/* First, check the column */
|
|
|
|
for (cell=0;cell<9;cell++) {
|
|
|
|
check[cell]=0;
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
2006-03-03 08:14:44 +00:00
|
|
|
for (r=0;r<9;r++) {
|
|
|
|
cell=state->currentboard[r][state->x];
|
|
|
|
if (cell!='0') {
|
|
|
|
if (check[cell-'1']==1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
check[cell-'1']=1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Second, check the row */
|
|
|
|
for (cell=0;cell<9;cell++) {
|
|
|
|
check[cell]=0;
|
|
|
|
}
|
|
|
|
for (c=0;c<9;c++) {
|
|
|
|
cell=state->currentboard[state->y][c];
|
|
|
|
if (cell!='0') {
|
|
|
|
if (check[cell-'1']==1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
check[cell-'1']=1;
|
|
|
|
}
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
2006-03-03 08:14:44 +00:00
|
|
|
|
|
|
|
/* Finally, check the 3x3 sub-grid */
|
|
|
|
for (cell=0;cell<9;cell++) {
|
|
|
|
check[cell]=0;
|
|
|
|
}
|
|
|
|
r1=(state->y/3)*3;
|
|
|
|
c1=(state->x/3)*3;
|
|
|
|
for (r=r1;r<r1+3;r++) {
|
|
|
|
for (c=c1;c<c1+3;c++) {
|
|
|
|
cell=state->currentboard[r][c];
|
|
|
|
if (cell!='0') {
|
|
|
|
if (check[cell-'1']==1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
check[cell-'1']=1;
|
|
|
|
}
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
/* We passed all the checks :) */
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
return false;
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int sudoku_menu_cb(int key, int m)
|
|
|
|
{
|
|
|
|
(void)m;
|
|
|
|
switch(key)
|
|
|
|
{
|
|
|
|
#ifdef MENU_ENTER2
|
|
|
|
case MENU_ENTER2:
|
|
|
|
#endif
|
|
|
|
case MENU_ENTER:
|
|
|
|
key = BUTTON_NONE; /* eat the downpress, next menu reacts on release */
|
|
|
|
break;
|
|
|
|
|
|
|
|
#ifdef MENU_ENTER2
|
|
|
|
case MENU_ENTER2 | BUTTON_REL:
|
|
|
|
#endif
|
|
|
|
case MENU_ENTER | BUTTON_REL:
|
|
|
|
key = MENU_ENTER; /* fake downpress, next menu doesn't like release */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool sudoku_menu(struct sudoku_state_t* state)
|
|
|
|
{
|
2006-03-03 08:14:44 +00:00
|
|
|
int m;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
static const struct menu_item items[] = {
|
|
|
|
{ "Save", NULL },
|
|
|
|
{ "Reload", NULL },
|
|
|
|
{ "Clear", NULL },
|
|
|
|
{ "Solve", NULL },
|
|
|
|
{ "New", NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
m = rb->menu_init(items, sizeof(items) / sizeof(*items),
|
|
|
|
sudoku_menu_cb, NULL, NULL, NULL);
|
|
|
|
|
|
|
|
result=rb->menu_show(m);
|
|
|
|
|
|
|
|
switch (result) {
|
|
|
|
case 0: /* Save state */
|
|
|
|
save_sudoku(state);
|
|
|
|
break;
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
case 1: /* Restore state */
|
|
|
|
restore_state(state);
|
|
|
|
break;
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
case 2: /* Clear all */
|
|
|
|
clear_board(state);
|
|
|
|
break;
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
case 3: /* Solve */
|
|
|
|
sudoku_solve(state);
|
|
|
|
break;
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
case 4: /* Create a new game manually */
|
|
|
|
clear_state(state);
|
|
|
|
state->editmode=1;
|
|
|
|
break;
|
2005-10-04 23:36:03 +00:00
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
rb->menu_exit(m);
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
return (result==MENU_ATTACHED_USB);
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
void move_cursor(struct sudoku_state_t* state, int newx, int newy)
|
|
|
|
{
|
|
|
|
int oldx, oldy;
|
|
|
|
|
|
|
|
/* Check that the character at the cursor position is legal */
|
|
|
|
if (check_status(state)) {
|
|
|
|
rb->splash(HZ*2, true, "Illegal move!");
|
|
|
|
/* Ignore any button presses during the splash */
|
|
|
|
rb->button_clear_queue();
|
|
|
|
return;
|
|
|
|
}
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
/* Move Cursor */
|
|
|
|
oldx=state->x;
|
|
|
|
oldy=state->y;
|
|
|
|
state->x=newx;
|
|
|
|
state->y=newy;
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
/* Redraw current and old cells */
|
|
|
|
update_cell(state,oldx,oldy);
|
|
|
|
update_cell(state,newx,newy);
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* plugin entry point */
|
|
|
|
enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
|
|
|
|
{
|
2006-03-03 08:14:44 +00:00
|
|
|
bool exit;
|
|
|
|
int button;
|
|
|
|
int lastbutton = BUTTON_NONE;
|
|
|
|
long ticks;
|
|
|
|
struct sudoku_state_t state;
|
|
|
|
|
|
|
|
/* plugin init */
|
|
|
|
rb = api;
|
|
|
|
/* end of plugin init */
|
|
|
|
|
|
|
|
clear_state(&state);
|
|
|
|
|
|
|
|
if (parameter==NULL) {
|
|
|
|
state.editmode=1;
|
|
|
|
} else {
|
|
|
|
if (!load_sudoku(&state,(char*)parameter)) {
|
|
|
|
rb->splash(HZ*2, true, "Load error");
|
|
|
|
return(PLUGIN_ERROR);
|
|
|
|
}
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
display_board(&state);
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
/* The main game loop */
|
|
|
|
exit=false;
|
|
|
|
ticks=0;
|
|
|
|
while(!exit) {
|
|
|
|
button = rb->button_get(true);
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
switch(button){
|
|
|
|
/* Exit game */
|
|
|
|
case SUDOKU_BUTTON_QUIT:
|
|
|
|
exit=1;
|
|
|
|
break;
|
2005-09-22 08:53:04 +00:00
|
|
|
|
|
|
|
/* Increment digit */
|
2005-09-22 20:17:21 +00:00
|
|
|
#ifdef SUDOKU_BUTTON_ALTTOGGLE
|
2006-03-03 08:14:44 +00:00
|
|
|
case SUDOKU_BUTTON_ALTTOGGLE | BUTTON_REPEAT:
|
2005-09-22 20:17:21 +00:00
|
|
|
#endif
|
2006-03-03 08:14:44 +00:00
|
|
|
case SUDOKU_BUTTON_TOGGLE | BUTTON_REPEAT:
|
|
|
|
/* Slow down the repeat speed to 1/3 second */
|
|
|
|
if ((*rb->current_tick-ticks) < (HZ/3)) {
|
|
|
|
break;
|
|
|
|
}
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2005-09-22 20:17:21 +00:00
|
|
|
#ifdef SUDOKU_BUTTON_ALTTOGGLE
|
2006-03-03 08:14:44 +00:00
|
|
|
case SUDOKU_BUTTON_ALTTOGGLE:
|
2005-09-22 20:17:21 +00:00
|
|
|
#endif
|
2006-03-03 08:14:44 +00:00
|
|
|
case SUDOKU_BUTTON_TOGGLE:
|
2005-09-22 21:00:03 +00:00
|
|
|
#ifdef SUDOKU_BUTTON_TOGGLE_PRE
|
2006-03-03 08:14:44 +00:00
|
|
|
if ((button == SUDOKU_BUTTON_TOGGLE)
|
|
|
|
&& (lastbutton != SUDOKU_BUTTON_TOGGLE_PRE))
|
|
|
|
break;
|
2005-09-22 21:00:03 +00:00
|
|
|
#endif
|
2006-03-03 08:14:44 +00:00
|
|
|
/* Increment digit */
|
|
|
|
ticks=*rb->current_tick;
|
|
|
|
if (state.editmode) {
|
|
|
|
if (state.startboard[state.y][state.x]=='9') {
|
|
|
|
state.startboard[state.y][state.x]='0';
|
|
|
|
state.currentboard[state.y][state.x]='0';
|
|
|
|
} else {
|
|
|
|
state.startboard[state.y][state.x]++;
|
|
|
|
state.currentboard[state.y][state.x]++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (state.startboard[state.y][state.x]=='0') {
|
|
|
|
if (state.currentboard[state.y][state.x]=='9') {
|
|
|
|
state.currentboard[state.y][state.x]='0';
|
|
|
|
} else {
|
2005-10-04 23:36:03 +00:00
|
|
|
state.currentboard[state.y][state.x]++;
|
2006-03-03 08:14:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
update_cell(&state,state.y,state.x);
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* move cursor left */
|
|
|
|
case BUTTON_LEFT:
|
|
|
|
case (BUTTON_LEFT | BUTTON_REPEAT):
|
|
|
|
if (state.x==0) {
|
|
|
|
move_cursor(&state,8,state.y);
|
|
|
|
} else {
|
|
|
|
move_cursor(&state,state.x-1,state.y);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* move cursor right */
|
|
|
|
case BUTTON_RIGHT:
|
|
|
|
case (BUTTON_RIGHT | BUTTON_REPEAT):
|
|
|
|
if (state.x==8) {
|
|
|
|
move_cursor(&state,0,state.y);
|
|
|
|
} else {
|
|
|
|
move_cursor(&state,state.x+1,state.y);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* move cursor up */
|
|
|
|
case SUDOKU_BUTTON_UP:
|
|
|
|
case (SUDOKU_BUTTON_UP | BUTTON_REPEAT):
|
|
|
|
if (state.y==0) {
|
|
|
|
move_cursor(&state,state.x,8);
|
|
|
|
} else {
|
|
|
|
move_cursor(&state,state.x,state.y-1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* move cursor down */
|
|
|
|
case SUDOKU_BUTTON_DOWN:
|
|
|
|
case (SUDOKU_BUTTON_DOWN | BUTTON_REPEAT):
|
|
|
|
if (state.y==8) {
|
|
|
|
move_cursor(&state,state.x,0);
|
|
|
|
} else {
|
|
|
|
move_cursor(&state,state.x,state.y+1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SUDOKU_BUTTON_MENU:
|
2005-09-22 21:00:03 +00:00
|
|
|
#ifdef SUDOKU_BUTTON_MENU_PRE
|
2006-03-03 08:14:44 +00:00
|
|
|
if (lastbutton != SUDOKU_BUTTON_MENU_PRE)
|
|
|
|
break;
|
2005-09-22 21:00:03 +00:00
|
|
|
#endif
|
2006-03-03 08:14:44 +00:00
|
|
|
/* Don't let the user leave a game in a bad state */
|
|
|
|
if (check_status(&state)) {
|
|
|
|
rb->splash(HZ*2, true, "Illegal move!");
|
|
|
|
/* Ignore any button presses during the splash */
|
|
|
|
rb->button_clear_queue();
|
|
|
|
} else {
|
|
|
|
if (state.editmode) {
|
|
|
|
rb->kbd_input(state.filename,MAX_PATH);
|
|
|
|
if (save_sudoku(&state)) {
|
|
|
|
state.editmode=0;
|
|
|
|
} else {
|
|
|
|
rb->splash(HZ*2, true, "Save failed");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (sudoku_menu(&state)) {
|
|
|
|
return PLUGIN_USB_CONNECTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2005-11-29 00:02:00 +00:00
|
|
|
#ifdef SUDOKU_BUTTON_POSSIBLE
|
2006-03-03 08:14:44 +00:00
|
|
|
case SUDOKU_BUTTON_POSSIBLE:
|
|
|
|
/* Toggle current number in the possiblevals structure */
|
|
|
|
if (state.currentboard[state.y][state.x]!='0') {
|
|
|
|
state.possiblevals[state.y][state.x]^=
|
|
|
|
(1 << (state.currentboard[state.y][state.x] - '0'));
|
|
|
|
}
|
|
|
|
break;
|
2005-11-29 00:02:00 +00:00
|
|
|
#endif
|
2006-03-03 08:14:44 +00:00
|
|
|
default:
|
|
|
|
if (rb->default_event_handler(button) == SYS_USB_CONNECTED) {
|
|
|
|
/* Quit if USB has been connected */
|
|
|
|
return PLUGIN_USB_CONNECTED;
|
|
|
|
}
|
|
|
|
break;
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
2006-03-03 08:14:44 +00:00
|
|
|
if (button != BUTTON_NONE)
|
|
|
|
lastbutton = button;
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
display_board(&state);
|
|
|
|
}
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
return PLUGIN_OK;
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|