2005-09-22 08:53:04 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Dave Chapman
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2005-09-22 08:53:04 +00:00
|
|
|
*
|
|
|
|
* 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"
|
2007-09-16 18:46:39 +00:00
|
|
|
#include "lib/configfile.h"
|
2005-09-22 08:53:04 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_LCD_BITMAP
|
|
|
|
|
2006-04-01 23:23:07 +00:00
|
|
|
#include <lib/playback_control.h>
|
2006-04-01 18:38:34 +00:00
|
|
|
#include "sudoku.h"
|
|
|
|
#include "generator.h"
|
|
|
|
|
2006-08-09 20:31:38 +00:00
|
|
|
/* The bitmaps */
|
2008-11-20 11:27:31 +00:00
|
|
|
#include "pluginbitmaps/sudoku_normal.h"
|
|
|
|
#include "pluginbitmaps/sudoku_inverse.h"
|
|
|
|
#include "pluginbitmaps/sudoku_start.h"
|
2006-08-09 20:31:38 +00:00
|
|
|
|
|
|
|
#define BITMAP_HEIGHT (BMPHEIGHT_sudoku_normal/10)
|
2009-09-04 00:46:24 +00:00
|
|
|
#define BITMAP_STRIDE STRIDE(SCREEN_MAIN, BMPWIDTH_sudoku_normal, BMPHEIGHT_sudoku_normal)
|
2006-08-09 20:31:38 +00:00
|
|
|
|
2009-07-27 04:33:52 +00:00
|
|
|
#if (LCD_DEPTH>2)
|
|
|
|
#define BITMAP_WIDTH (BMPWIDTH_sudoku_normal/2)
|
|
|
|
#else
|
|
|
|
#define BITMAP_WIDTH BMPWIDTH_sudoku_normal
|
|
|
|
#endif
|
|
|
|
|
2006-01-15 18:20:18 +00:00
|
|
|
PLUGIN_HEADER
|
|
|
|
|
2006-05-15 20:08:37 +00:00
|
|
|
/* Default game - used to initialise sudoku.ss if it doesn't exist. */
|
|
|
|
static const char default_game[9][9] =
|
|
|
|
{
|
|
|
|
{ '0','1','0', '3','0','7', '0','0','4' },
|
|
|
|
{ '0','0','0', '0','6','0', '1','0','2' },
|
|
|
|
{ '0','0','0', '0','8','0', '5','6','0' },
|
|
|
|
|
|
|
|
{ '0','6','0', '0','0','0', '0','2','9' },
|
|
|
|
{ '0','0','0', '5','0','3', '0','0','0' },
|
|
|
|
{ '7','9','0', '0','0','0', '0','3','0' },
|
|
|
|
|
|
|
|
{ '0','8','5', '0','3','0', '0','0','0' },
|
|
|
|
{ '1','0','2', '0','7','0', '0','0','0' },
|
|
|
|
{ '0','0','0', '4','0','8', '0','5','0' },
|
|
|
|
};
|
|
|
|
|
2006-08-09 20:31:38 +00:00
|
|
|
#if LCD_HEIGHT <= LCD_WIDTH /* Horizontal layout, scratchpad at the left */
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2008-11-28 00:37:28 +00:00
|
|
|
#if (LCD_HEIGHT==64) && (LCD_WIDTH==112 || LCD_WIDTH==128)
|
2006-08-09 20:31:38 +00:00
|
|
|
/* Archos Recorders and Ondios - 112x64, 9 cells @ 8x6 with 10 border lines */
|
|
|
|
#define SMALL_BOARD
|
2008-10-25 21:22:00 +00:00
|
|
|
#define MARK_OFFS 1 /* Pixels between border and mark */
|
|
|
|
#define MARK_SPACE 1 /* Pixels between two marks */
|
|
|
|
#define MARK_SIZE 1 /* Mark width and height */
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2007-09-20 10:49:48 +00:00
|
|
|
#elif ((LCD_HEIGHT==80) && (LCD_WIDTH==132))
|
|
|
|
/* C200, 9 cells @ 8x8 with 8 border lines */
|
|
|
|
#define SMALL_BOARD
|
2008-10-25 21:22:00 +00:00
|
|
|
#define MARK_OFFS 1 /* Pixels between border and mark */
|
|
|
|
#define MARK_SPACE 1 /* Pixels between two marks */
|
|
|
|
#define MARK_SIZE 1 /* Mark width and height */
|
2007-09-20 10:49:48 +00:00
|
|
|
|
2008-03-22 14:20:04 +00:00
|
|
|
#elif ((LCD_HEIGHT==96) && (LCD_WIDTH==128))
|
|
|
|
/* iAudio M3, 9 cells @ 9x9 with 14 border lines */
|
2008-10-25 21:22:00 +00:00
|
|
|
#define MARK_OFFS 1 /* Pixels between border and mark */
|
|
|
|
#define MARK_SPACE 2 /* Pixels between two marks */
|
|
|
|
#define MARK_SIZE 1 /* Mark width and height */
|
2005-11-29 00:02:00 +00:00
|
|
|
|
2008-03-22 14:20:04 +00:00
|
|
|
#elif (LCD_HEIGHT==110) && (LCD_WIDTH==138) \
|
2009-12-04 11:41:31 +00:00
|
|
|
|| (LCD_HEIGHT==128) && (LCD_WIDTH==128)
|
2008-03-22 14:20:04 +00:00
|
|
|
/* iPod Mini - 138x110, 9 cells @ 10x10 with 14 border lines */
|
2006-09-18 21:44:24 +00:00
|
|
|
/* iriver H10 5-6GB - 128x128, 9 cells @ 10x10 with 14 border lines */
|
2008-10-25 21:22:00 +00:00
|
|
|
#define MARK_OFFS 1 /* Pixels between border and mark */
|
|
|
|
#define MARK_SPACE 1 /* Pixels between two marks */
|
|
|
|
#define MARK_SIZE 2 /* Mark width and height */
|
2006-09-18 21:44:24 +00:00
|
|
|
|
2008-03-22 14:20:04 +00:00
|
|
|
#elif ((LCD_HEIGHT==128) && (LCD_WIDTH==160)) \
|
|
|
|
|| ((LCD_HEIGHT==132) && (LCD_WIDTH==176))
|
2006-08-09 20:31:38 +00:00
|
|
|
/* iAudio X5, Iriver H1x0, iPod G3, G4 - 160x128; */
|
|
|
|
/* iPod Nano - 176x132, 9 cells @ 12x12 with 14 border lines */
|
2008-10-25 21:22:00 +00:00
|
|
|
#define MARK_OFFS 1 /* Pixels between border and mark */
|
|
|
|
#define MARK_SPACE 2 /* Pixels between two marks */
|
|
|
|
#define MARK_SIZE 2 /* Mark width and height */
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2007-01-15 20:40:48 +00:00
|
|
|
#elif ((LCD_HEIGHT==176) && (LCD_WIDTH==220))
|
2006-08-09 20:31:38 +00:00
|
|
|
/* Iriver h300, iPod Color/Photo - 220x176, 9 cells @ 16x16 with 14 border lines */
|
2008-10-25 21:22:00 +00:00
|
|
|
#define MARK_OFFS 1 /* Pixels between border and mark */
|
|
|
|
#define MARK_SPACE 1 /* Pixels between two marks */
|
|
|
|
#define MARK_SIZE 4 /* Mark width and height */
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2009-07-27 04:33:52 +00:00
|
|
|
#elif (LCD_HEIGHT==240) && (LCD_WIDTH==320)
|
2006-08-10 11:14:26 +00:00
|
|
|
/* iPod Video - 320x240, 9 cells @ 24x24 with 14 border lines */
|
2009-07-27 04:33:52 +00:00
|
|
|
#define MARK_OFFS 1 /* Pixels between border and mark */
|
|
|
|
#define MARK_SPACE 2 /* Pixels between two marks */
|
|
|
|
#define MARK_SIZE 6 /* Mark width and height */
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2009-07-27 04:33:52 +00:00
|
|
|
#elif (LCD_HEIGHT==480) && (LCD_WIDTH==640)
|
|
|
|
/* M:Robe 500 - 640x480, 9 cells @ 48x48 with 14 border lines */
|
2008-10-25 21:22:00 +00:00
|
|
|
#define MARK_OFFS 1 /* Pixels between border and mark */
|
|
|
|
#define MARK_SPACE 2 /* Pixels between two marks */
|
|
|
|
#define MARK_SIZE 6 /* Mark width and height */
|
2005-11-29 00:02:00 +00:00
|
|
|
|
2006-08-09 20:31:38 +00:00
|
|
|
#else
|
|
|
|
#error SUDOKU: Unsupported LCD size
|
|
|
|
#endif
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2006-08-09 20:31:38 +00:00
|
|
|
#else /* Vertical layout, scratchpad at the bottom */
|
|
|
|
#define VERTICAL_LAYOUT
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2007-01-15 20:40:48 +00:00
|
|
|
#if ((LCD_HEIGHT==220) && (LCD_WIDTH==176))
|
|
|
|
/* e200, 9 cells @ 16x16 with 14 border lines */
|
2008-10-25 21:22:00 +00:00
|
|
|
#define MARK_OFFS 1 /* Pixels between border and mark */
|
|
|
|
#define MARK_SPACE 1 /* Pixels between two marks */
|
|
|
|
#define MARK_SIZE 4 /* Mark width and height */
|
2007-01-15 20:40:48 +00:00
|
|
|
|
|
|
|
#elif (LCD_HEIGHT>=320) && (LCD_WIDTH>=240)
|
2006-08-10 11:14:26 +00:00
|
|
|
/* Gigabeat - 240x320, 9 cells @ 24x24 with 14 border lines */
|
2008-10-25 21:22:00 +00:00
|
|
|
#define MARK_OFFS 1 /* Pixels between border and mark */
|
|
|
|
#define MARK_SPACE 2 /* Pixels between two marks */
|
|
|
|
#define MARK_SIZE 6 /* Mark width and height */
|
2005-11-27 23:59:05 +00:00
|
|
|
|
2009-12-04 11:41:31 +00:00
|
|
|
#elif ((LCD_HEIGHT==160) && (LCD_WIDTH==128))
|
|
|
|
/* Philips GoGear SA9200 - 128x160, 9 cells @ 10x10 with 14 border tiles */
|
|
|
|
#define MARK_OFFS 1 /* Pixels between border and mark */
|
|
|
|
#define MARK_SPACE 1 /* Pixels between two marks */
|
|
|
|
#define MARK_SIZE 2 /* Mark width and height */
|
|
|
|
|
2006-08-09 20:31:38 +00:00
|
|
|
#else
|
|
|
|
#error SUDOKU: Unsupported LCD size
|
|
|
|
#endif
|
2005-11-29 00:02:00 +00:00
|
|
|
|
2006-08-09 20:31:38 +00:00
|
|
|
#endif /* Layout */
|
2005-11-27 23:59:05 +00:00
|
|
|
|
2009-07-27 04:33:52 +00:00
|
|
|
#define CELL_WIDTH BITMAP_WIDTH
|
|
|
|
#define CELL_HEIGHT BITMAP_HEIGHT
|
|
|
|
|
2008-11-27 20:26:50 +00:00
|
|
|
#ifdef SUDOKU_BUTTON_CHANGEDIR
|
|
|
|
int invertdir=0;
|
|
|
|
#else
|
|
|
|
#define invertdir 0
|
|
|
|
#endif
|
|
|
|
|
2007-09-16 18:46:39 +00:00
|
|
|
#define CFGFILE_VERSION 0 /* Current config file version */
|
|
|
|
#define CFGFILE_MINVERSION 0 /* Minimum config file version to accept */
|
|
|
|
|
2008-10-25 21:22:00 +00:00
|
|
|
#if defined(HAVE_LCD_COLOR) || defined(SUDOKU_BUTTON_POSSIBLE)
|
2007-09-16 18:46:39 +00:00
|
|
|
/* settings */
|
|
|
|
struct sudoku_config {
|
2008-10-25 21:22:00 +00:00
|
|
|
#ifdef HAVE_LCD_COLOR
|
2007-09-16 18:46:39 +00:00
|
|
|
int number_display;
|
2008-10-25 21:22:00 +00:00
|
|
|
#endif
|
|
|
|
#ifdef SUDOKU_BUTTON_POSSIBLE
|
|
|
|
int show_markings;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
struct sudoku_config sudcfg_disk = {
|
|
|
|
#ifdef HAVE_LCD_COLOR
|
|
|
|
0,
|
|
|
|
#endif
|
|
|
|
#ifdef SUDOKU_BUTTON_POSSIBLE
|
|
|
|
1,
|
|
|
|
#endif
|
2007-09-16 18:46:39 +00:00
|
|
|
};
|
|
|
|
struct sudoku_config sudcfg;
|
|
|
|
|
|
|
|
static const char cfg_filename[] = "sudoku.cfg";
|
2008-10-25 21:22:00 +00:00
|
|
|
#ifdef HAVE_LCD_COLOR
|
2007-09-16 18:46:39 +00:00
|
|
|
static char *number_str[2] = { "black", "coloured" };
|
2008-10-25 21:22:00 +00:00
|
|
|
#endif
|
|
|
|
#ifdef SUDOKU_BUTTON_POSSIBLE
|
|
|
|
static char *mark_str[2] = { "hide", "show" };
|
|
|
|
#endif
|
2007-09-16 18:46:39 +00:00
|
|
|
|
|
|
|
struct configdata disk_config[] = {
|
2008-10-25 21:22:00 +00:00
|
|
|
#ifdef HAVE_LCD_COLOR
|
2009-01-17 22:53:12 +00:00
|
|
|
{ TYPE_ENUM, 0, 2, { .int_p = &sudcfg_disk.number_display }, "numbers",
|
|
|
|
number_str },
|
2008-10-25 21:22:00 +00:00
|
|
|
#endif
|
|
|
|
#ifdef SUDOKU_BUTTON_POSSIBLE
|
2009-01-17 22:53:12 +00:00
|
|
|
{ TYPE_ENUM, 0, 2, { .int_p = &sudcfg_disk.show_markings }, "markings",
|
|
|
|
mark_str },
|
2008-10-25 21:22:00 +00:00
|
|
|
#endif
|
2007-09-16 18:46:39 +00:00
|
|
|
};
|
2008-10-25 21:22:00 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_LCD_COLOR
|
2007-09-16 18:46:39 +00:00
|
|
|
#define NUMBER_TYPE (sudcfg.number_display*CELL_WIDTH)
|
|
|
|
#else
|
|
|
|
#define NUMBER_TYPE 0
|
|
|
|
#endif
|
|
|
|
|
2006-08-09 20:31:38 +00:00
|
|
|
/* Size dependent build-time calculations */
|
|
|
|
#ifdef SMALL_BOARD
|
|
|
|
#define BOARD_WIDTH (CELL_WIDTH*9+10)
|
|
|
|
#define BOARD_HEIGHT (CELL_HEIGHT*9+10)
|
2009-07-27 04:33:52 +00:00
|
|
|
static unsigned int cellxpos[9]={
|
2006-08-09 20:31:38 +00:00
|
|
|
1, (CELL_WIDTH+2), (2*CELL_WIDTH+3),
|
|
|
|
(3*CELL_WIDTH+4), (4*CELL_WIDTH+5), (5*CELL_WIDTH+6),
|
|
|
|
(6*CELL_WIDTH+7), (7*CELL_WIDTH+8), (8*CELL_WIDTH+9)
|
|
|
|
};
|
2009-07-27 04:33:52 +00:00
|
|
|
static unsigned int cellypos[9]={
|
2006-08-09 20:31:38 +00:00
|
|
|
1, (CELL_HEIGHT+2), (2*CELL_HEIGHT+3),
|
|
|
|
(3*CELL_HEIGHT+4), (4*CELL_HEIGHT+5), (5*CELL_HEIGHT+6),
|
|
|
|
(6*CELL_HEIGHT+7), (7*CELL_HEIGHT+8), (8*CELL_HEIGHT+9)
|
|
|
|
};
|
|
|
|
#else /* !SMALL_BOARD */
|
|
|
|
#define BOARD_WIDTH (CELL_WIDTH*9+10+4)
|
|
|
|
#define BOARD_HEIGHT (CELL_HEIGHT*9+10+4)
|
2009-07-27 04:33:52 +00:00
|
|
|
static unsigned int cellxpos[9]={
|
2006-08-09 20:31:38 +00:00
|
|
|
2, (CELL_WIDTH +3), (2*CELL_WIDTH +4),
|
|
|
|
(3*CELL_WIDTH +6), (4*CELL_WIDTH +7), (5*CELL_WIDTH +8),
|
|
|
|
(6*CELL_WIDTH+10), (7*CELL_WIDTH+11), (8*CELL_WIDTH+12)
|
|
|
|
};
|
2009-07-27 04:33:52 +00:00
|
|
|
static unsigned int cellypos[9]={
|
2006-08-09 20:31:38 +00:00
|
|
|
2, (CELL_HEIGHT +3), (2*CELL_HEIGHT +4),
|
|
|
|
(3*CELL_HEIGHT +6), (4*CELL_HEIGHT +7), (5*CELL_HEIGHT +8),
|
|
|
|
(6*CELL_HEIGHT+10), (7*CELL_HEIGHT+11), (8*CELL_HEIGHT+12)
|
|
|
|
};
|
|
|
|
#endif
|
2005-11-27 23:59:05 +00:00
|
|
|
|
2006-08-09 20:31:38 +00:00
|
|
|
#ifdef VERTICAL_LAYOUT
|
|
|
|
#define XOFS ((LCD_WIDTH-BOARD_WIDTH)/2)
|
|
|
|
#define YOFS ((LCD_HEIGHT-(BOARD_HEIGHT+CELL_HEIGHT*2+2))/2)
|
|
|
|
#define YOFSSCRATCHPAD (YOFS+BOARD_HEIGHT+CELL_WIDTH)
|
2005-11-27 23:59:05 +00:00
|
|
|
#else
|
2006-08-09 20:31:38 +00:00
|
|
|
#define XOFSSCRATCHPAD ((LCD_WIDTH-(BOARD_WIDTH+CELL_WIDTH*2+2))/2)
|
|
|
|
#define XOFS (XOFSSCRATCHPAD+CELL_WIDTH*2+2)
|
|
|
|
#define YOFS ((LCD_HEIGHT-BOARD_HEIGHT)/2)
|
2005-09-22 08:53:04 +00:00
|
|
|
#endif
|
|
|
|
|
2009-12-16 11:53:36 +00:00
|
|
|
#define BLOCK 3
|
|
|
|
#define SIZE (BLOCK*BLOCK)
|
|
|
|
|
|
|
|
void sudoku_solve(struct sudoku_state_t* state)
|
|
|
|
{
|
|
|
|
bool ret = sudoku_solve_board(state);
|
|
|
|
|
|
|
|
if (!ret) {
|
|
|
|
rb->splash(HZ*2, "Solve failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2008-10-30 21:00:17 +00:00
|
|
|
/* Copies the current to the saved board */
|
|
|
|
static void save_state(struct sudoku_state_t *state)
|
|
|
|
{
|
|
|
|
rb->memcpy(state->savedboard, state->currentboard,
|
|
|
|
sizeof(state->savedboard));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copies the saved to the current board */
|
|
|
|
static void restore_state(struct sudoku_state_t *state)
|
|
|
|
{
|
|
|
|
rb->memcpy(state->currentboard, state->savedboard,
|
|
|
|
sizeof(state->savedboard));
|
|
|
|
}
|
|
|
|
|
2006-05-15 20:08:37 +00:00
|
|
|
void default_state(struct sudoku_state_t* state)
|
|
|
|
{
|
|
|
|
int r,c;
|
|
|
|
|
2009-07-14 13:57:45 +00:00
|
|
|
rb->strlcpy(state->filename,GAME_FILE,MAX_PATH);
|
2006-05-15 20:08:37 +00:00
|
|
|
for (r=0;r<9;r++) {
|
|
|
|
for (c=0;c<9;c++) {
|
|
|
|
state->startboard[r][c]=default_game[r][c];
|
|
|
|
state->currentboard[r][c]=default_game[r][c];
|
|
|
|
#ifdef SUDOKU_BUTTON_POSSIBLE
|
|
|
|
state->possiblevals[r][c]=0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-28 22:39:54 +00:00
|
|
|
/* initialize the saved board so reload function works */
|
2008-10-30 21:00:17 +00:00
|
|
|
save_state(state);
|
2008-10-28 22:39:54 +00:00
|
|
|
|
2006-05-15 20:08:37 +00:00
|
|
|
state->x=0;
|
|
|
|
state->y=0;
|
|
|
|
state->editmode=0;
|
|
|
|
}
|
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
|
|
|
|
2009-07-14 13:57:45 +00:00
|
|
|
rb->strlcpy(state->filename,GAME_FILE,MAX_PATH);
|
2006-03-03 08:14:44 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2006-05-15 20:08:37 +00:00
|
|
|
/* Check the status of the board, assuming a change at the cursor location */
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We passed all the checks :) */
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-07-14 13:57:45 +00:00
|
|
|
rb->strlcpy(state->filename,filename,MAX_PATH);
|
2006-03-03 08:14:44 +00:00
|
|
|
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-05-15 20:08:37 +00:00
|
|
|
/* Check that the board is valid - we need to check every row/column
|
2009-11-15 11:19:58 +00:00
|
|
|
and block individually */
|
|
|
|
for (state->y = 0; state->y < 9; state->y++) {
|
|
|
|
state->x = (state->y%3)*3 + (state->y/3);
|
2006-05-15 20:08:37 +00:00
|
|
|
if (check_status(state)) return false;
|
|
|
|
}
|
|
|
|
state->x = 0;
|
|
|
|
state->y = 0;
|
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
/* Save a copy of the saved state - so we can reload without using the
|
|
|
|
disk */
|
2008-10-30 21:00:17 +00:00
|
|
|
save_state(state);
|
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
|
|
|
bool save_sudoku(struct sudoku_state_t* state)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int r,c;
|
|
|
|
int i;
|
2006-04-01 18:38:34 +00:00
|
|
|
char line[13];
|
|
|
|
char sep[13];
|
2006-01-20 21:02:17 +00:00
|
|
|
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(0, "Saving...");
|
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
|
|
|
|
2010-05-06 17:35:13 +00:00
|
|
|
fd=rb->open(state->filename, O_WRONLY|O_CREAT, 0666);
|
2006-03-03 08:14:44 +00:00
|
|
|
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++;
|
|
|
|
}
|
|
|
|
}
|
2006-04-01 18:38:34 +00:00
|
|
|
rb->write(fd,line,sizeof(line));
|
2006-03-03 08:14:44 +00:00
|
|
|
if ((r==2) || (r==5)) {
|
2006-04-01 18:38:34 +00:00
|
|
|
rb->write(fd,sep,sizeof(sep));
|
2006-03-03 08:14:44 +00:00
|
|
|
}
|
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);
|
2006-06-04 16:36:49 +00:00
|
|
|
rb->reload_directory();
|
2006-03-03 08:14:44 +00:00
|
|
|
/* Save a copy of the saved state - so we can reload without
|
|
|
|
using the disk */
|
2008-10-30 21:00:17 +00:00
|
|
|
save_state(state);
|
2006-03-03 08:14:44 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
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)) {
|
2007-09-16 18:46:39 +00:00
|
|
|
rb->lcd_bitmap_part(sudoku_inverse,NUMBER_TYPE,
|
2006-03-03 08:14:44 +00:00
|
|
|
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') {
|
2007-09-16 18:46:39 +00:00
|
|
|
rb->lcd_bitmap_part(sudoku_start,NUMBER_TYPE,
|
2006-03-03 08:14:44 +00:00
|
|
|
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 {
|
2007-09-16 18:46:39 +00:00
|
|
|
rb->lcd_bitmap_part(sudoku_normal,NUMBER_TYPE,
|
2006-03-03 08:14:44 +00:00
|
|
|
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;
|
2008-10-25 21:22:00 +00:00
|
|
|
#ifdef SUDOKU_BUTTON_POSSIBLE
|
|
|
|
int i;
|
|
|
|
#endif
|
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
|
|
|
|
2006-08-09 20:31:38 +00:00
|
|
|
#ifdef SMALL_BOARD
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
#else
|
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);
|
2006-08-09 20:31:38 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SUDOKU_BUTTON_POSSIBLE
|
|
|
|
#ifdef VERTICAL_LAYOUT
|
|
|
|
rb->lcd_hline(XOFS,XOFS+BOARD_WIDTH-1,YOFSSCRATCHPAD);
|
|
|
|
rb->lcd_hline(XOFS,XOFS+BOARD_WIDTH-1,YOFSSCRATCHPAD+CELL_HEIGHT+1);
|
2006-03-03 08:14:44 +00:00
|
|
|
for (r=0;r<9;r++) {
|
2006-08-09 20:31:38 +00:00
|
|
|
#ifdef SMALL_BOARD
|
|
|
|
/* Small targets - draw dotted/single lines */
|
2006-03-03 08:14:44 +00:00
|
|
|
if ((r % 3)==0) {
|
|
|
|
/* Solid Line */
|
2006-08-09 20:31:38 +00:00
|
|
|
rb->lcd_vline(XOFS+cellxpos[r]-1,YOFSSCRATCHPAD,
|
|
|
|
YOFSSCRATCHPAD+CELL_HEIGHT+1);
|
2006-03-03 08:14:44 +00:00
|
|
|
} else {
|
|
|
|
/* Dotted line */
|
2006-08-09 20:31:38 +00:00
|
|
|
for (c=YOFSSCRATCHPAD;c<YOFSSCRATCHPAD+CELL_HEIGHT+1;c+=2) {
|
2006-03-03 08:14:44 +00:00
|
|
|
rb->lcd_drawpixel(XOFS+cellxpos[r]-1,c);
|
|
|
|
}
|
|
|
|
}
|
2005-09-22 08:53:04 +00:00
|
|
|
#else
|
2006-08-09 20:31:38 +00:00
|
|
|
/* Large targets - draw single/double lines */
|
|
|
|
rb->lcd_vline(XOFS+cellxpos[r]-1,YOFSSCRATCHPAD,
|
|
|
|
YOFSSCRATCHPAD+CELL_HEIGHT+1);
|
|
|
|
if ((r % 3)==0)
|
|
|
|
rb->lcd_vline(XOFS+cellxpos[r]-2,YOFSSCRATCHPAD,
|
|
|
|
YOFSSCRATCHPAD+CELL_HEIGHT+1);
|
2005-09-22 08:53:04 +00:00
|
|
|
#endif
|
2009-06-07 21:27:05 +00:00
|
|
|
if ((r>0) && state->possiblevals[state->y][state->x]&BIT_N(r))
|
2007-09-16 18:46:39 +00:00
|
|
|
rb->lcd_bitmap_part(sudoku_normal,NUMBER_TYPE,BITMAP_HEIGHT*r,
|
|
|
|
BITMAP_STRIDE,XOFS+cellxpos[r-1],
|
|
|
|
YOFSSCRATCHPAD+1,CELL_WIDTH,CELL_HEIGHT);
|
2006-08-09 20:31:38 +00:00
|
|
|
}
|
|
|
|
rb->lcd_vline(XOFS+cellxpos[8]+CELL_WIDTH,YOFSSCRATCHPAD,
|
|
|
|
YOFSSCRATCHPAD+CELL_HEIGHT+1);
|
|
|
|
#ifndef SMALL_BOARD
|
|
|
|
rb->lcd_vline(XOFS+cellxpos[8]+CELL_WIDTH+1,YOFSSCRATCHPAD,
|
|
|
|
YOFSSCRATCHPAD+CELL_HEIGHT+1);
|
|
|
|
#endif
|
2009-06-07 21:27:05 +00:00
|
|
|
if (state->possiblevals[state->y][state->x]&BIT_N(r))
|
2007-09-16 18:46:39 +00:00
|
|
|
rb->lcd_bitmap_part(sudoku_normal,NUMBER_TYPE,BITMAP_HEIGHT*r,
|
|
|
|
BITMAP_STRIDE,XOFS+cellxpos[8],YOFSSCRATCHPAD+1,
|
2006-08-09 20:31:38 +00:00
|
|
|
CELL_WIDTH,CELL_HEIGHT);
|
|
|
|
#else /* Horizontal layout */
|
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-08-09 20:31:38 +00:00
|
|
|
#ifdef SMALL_BOARD
|
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);
|
|
|
|
}
|
|
|
|
}
|
2006-08-09 20:31:38 +00:00
|
|
|
#else
|
|
|
|
/* 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);
|
2005-11-29 00:02:00 +00:00
|
|
|
#endif
|
2009-06-07 21:27:05 +00:00
|
|
|
if ((r>0) && state->possiblevals[state->y][state->x]&BIT_N(r))
|
2007-09-16 18:46:39 +00:00
|
|
|
rb->lcd_bitmap_part(sudoku_normal,NUMBER_TYPE,BITMAP_HEIGHT*r,
|
|
|
|
BITMAP_STRIDE,XOFSSCRATCHPAD+1,
|
|
|
|
YOFS+cellypos[r-1],CELL_WIDTH,CELL_HEIGHT);
|
2006-03-03 08:14:44 +00:00
|
|
|
}
|
|
|
|
rb->lcd_hline(XOFSSCRATCHPAD,XOFSSCRATCHPAD+CELL_WIDTH+1,
|
|
|
|
YOFS+cellypos[8]+CELL_HEIGHT);
|
2006-08-09 20:31:38 +00:00
|
|
|
#ifndef SMALL_BOARD
|
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
|
2009-06-07 21:27:05 +00:00
|
|
|
if (state->possiblevals[state->y][state->x]&BIT_N(r))
|
2007-09-16 18:46:39 +00:00
|
|
|
rb->lcd_bitmap_part(sudoku_normal,NUMBER_TYPE,BITMAP_HEIGHT*r,
|
|
|
|
BITMAP_STRIDE,XOFSSCRATCHPAD+1,YOFS+cellypos[8],
|
2006-03-03 08:14:44 +00:00
|
|
|
CELL_WIDTH,CELL_HEIGHT);
|
2006-08-09 20:31:38 +00:00
|
|
|
#endif /* Layout */
|
|
|
|
#endif /* SUDOKU_BUTTON_POSSIBLE */
|
2005-11-29 00:02:00 +00:00
|
|
|
|
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
|
|
|
|
*/
|
2008-10-25 21:22:00 +00:00
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
if ((r==state->y) && (c==state->x)) {
|
2007-09-16 18:46:39 +00:00
|
|
|
rb->lcd_bitmap_part(sudoku_inverse,NUMBER_TYPE,
|
2006-03-03 08:14:44 +00:00
|
|
|
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') {
|
2007-09-16 18:46:39 +00:00
|
|
|
rb->lcd_bitmap_part(sudoku_start,NUMBER_TYPE,
|
2006-03-03 08:14:44 +00:00
|
|
|
BITMAP_HEIGHT*(state->startboard[r][c]-
|
|
|
|
'0'),
|
|
|
|
BITMAP_STRIDE,
|
|
|
|
XOFS+cellxpos[c],YOFS+cellypos[r],
|
|
|
|
CELL_WIDTH,CELL_HEIGHT);
|
|
|
|
} else {
|
2007-09-16 18:46:39 +00:00
|
|
|
rb->lcd_bitmap_part(sudoku_normal,NUMBER_TYPE,
|
2006-03-03 08:14:44 +00:00
|
|
|
BITMAP_HEIGHT*
|
|
|
|
(state->currentboard[r][c]-'0'),
|
|
|
|
BITMAP_STRIDE,
|
|
|
|
XOFS+cellxpos[c],YOFS+cellypos[r],
|
|
|
|
CELL_WIDTH,CELL_HEIGHT);
|
|
|
|
}
|
2008-10-25 21:22:00 +00:00
|
|
|
|
|
|
|
#ifdef SUDOKU_BUTTON_POSSIBLE
|
|
|
|
/* Draw the possible number markings on the board */
|
|
|
|
if(sudcfg.show_markings && state->startboard[r][c]=='0'
|
|
|
|
&& state->currentboard[r][c]=='0') {
|
|
|
|
for(i=0;i<9;i++) {
|
|
|
|
if(state->possiblevals[r][c]&(2<<i)) {
|
|
|
|
#if LCD_DEPTH > 1
|
|
|
|
/* draw markings in dark grey */
|
|
|
|
rb->lcd_set_foreground(LCD_DARKGRAY);
|
|
|
|
#endif
|
|
|
|
rb->lcd_fillrect(XOFS+cellxpos[c]+MARK_OFFS
|
|
|
|
+(i%3)*(MARK_SIZE+MARK_SPACE),
|
|
|
|
YOFS+cellypos[r]+MARK_OFFS
|
|
|
|
+(i/3)*(MARK_SIZE+MARK_SPACE),
|
|
|
|
MARK_SIZE,
|
|
|
|
MARK_SIZE);
|
|
|
|
#if LCD_DEPTH > 1
|
|
|
|
rb->lcd_set_foreground(LCD_BLACK);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* SUDOKU_BUTTON_POSSIBLE */
|
2006-03-03 08:14:44 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2006-05-15 20:08:37 +00:00
|
|
|
bool sudoku_generate(struct sudoku_state_t* state)
|
2006-04-01 18:38:34 +00:00
|
|
|
{
|
|
|
|
char* difficulty;
|
|
|
|
char str[80];
|
2006-05-15 20:08:37 +00:00
|
|
|
bool res;
|
|
|
|
struct sudoku_state_t new_state;
|
2006-04-01 18:38:34 +00:00
|
|
|
|
2006-05-15 20:08:37 +00:00
|
|
|
clear_state(&new_state);
|
|
|
|
display_board(&new_state);
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(0, "Generating...");
|
2006-04-01 18:38:34 +00:00
|
|
|
|
2006-04-09 14:20:14 +00:00
|
|
|
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
|
2006-04-09 13:47:28 +00:00
|
|
|
rb->cpu_boost(true);
|
|
|
|
#endif
|
2006-04-09 14:20:14 +00:00
|
|
|
|
2006-05-15 20:08:37 +00:00
|
|
|
res = sudoku_generate_board(&new_state,&difficulty);
|
2006-04-09 14:20:14 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
|
2006-04-09 13:47:28 +00:00
|
|
|
rb->cpu_boost(false);
|
|
|
|
#endif
|
2006-04-01 18:38:34 +00:00
|
|
|
|
2006-05-15 20:08:37 +00:00
|
|
|
if (res) {
|
|
|
|
rb->memcpy(state,&new_state,sizeof(new_state));
|
|
|
|
rb->snprintf(str,sizeof(str),"Difficulty: %s",difficulty);
|
|
|
|
display_board(state);
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(HZ*3, str);
|
2009-07-14 13:57:45 +00:00
|
|
|
rb->strlcpy(state->filename,GAME_FILE,MAX_PATH);
|
2006-05-15 20:08:37 +00:00
|
|
|
} else {
|
|
|
|
display_board(&new_state);
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(HZ*2, "Aborted");
|
2006-05-15 20:08:37 +00:00
|
|
|
}
|
2008-10-28 22:39:54 +00:00
|
|
|
/* initialize the saved board so reload function works */
|
2008-10-30 21:00:17 +00:00
|
|
|
save_state(state);
|
2006-05-15 20:08:37 +00:00
|
|
|
return res;
|
2006-04-01 18:38:34 +00:00
|
|
|
}
|
|
|
|
|
2007-09-16 18:46:39 +00:00
|
|
|
#ifdef HAVE_LCD_COLOR
|
|
|
|
static bool numdisplay_setting(void)
|
|
|
|
{
|
|
|
|
static const struct opt_items names[] = {
|
|
|
|
{"Black", -1},
|
|
|
|
{"Coloured", -1},
|
|
|
|
};
|
|
|
|
|
|
|
|
return rb->set_option("Number Display", &sudcfg.number_display, INT, names,
|
|
|
|
sizeof(names) / sizeof(names[0]), NULL);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-10-25 21:22:00 +00:00
|
|
|
#ifdef SUDOKU_BUTTON_POSSIBLE
|
|
|
|
static bool showmarkings_setting(void)
|
|
|
|
{
|
|
|
|
static const struct opt_items names[] = {
|
|
|
|
{"Hide", -1},
|
|
|
|
{"Show", -1},
|
|
|
|
};
|
|
|
|
|
|
|
|
return rb->set_option("Show Markings", &sudcfg.show_markings, INT, names,
|
|
|
|
sizeof(names) / sizeof(names[0]), NULL);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-09-16 18:46:39 +00:00
|
|
|
enum {
|
|
|
|
SM_AUDIO_PLAYBACK = 0,
|
|
|
|
#ifdef HAVE_LCD_COLOR
|
|
|
|
SM_NUMBER_DISPLAY,
|
2008-10-25 21:22:00 +00:00
|
|
|
#endif
|
|
|
|
#ifdef SUDOKU_BUTTON_POSSIBLE
|
|
|
|
SM_SHOW_MARKINGS,
|
2007-09-16 18:46:39 +00:00
|
|
|
#endif
|
|
|
|
SM_SAVE,
|
|
|
|
SM_RELOAD,
|
|
|
|
SM_CLEAR,
|
|
|
|
SM_SOLVE,
|
|
|
|
SM_GENERATE,
|
|
|
|
SM_NEW,
|
|
|
|
SM_QUIT,
|
|
|
|
};
|
|
|
|
|
2009-07-11 13:35:08 +00:00
|
|
|
int sudoku_menu(struct sudoku_state_t* state)
|
2005-09-22 08:53:04 +00:00
|
|
|
{
|
2006-03-03 08:14:44 +00:00
|
|
|
int result;
|
|
|
|
|
2009-06-16 04:25:21 +00:00
|
|
|
MENUITEM_STRINGLIST(menu, "Sudoku Menu", NULL,
|
|
|
|
"Audio Playback",
|
2007-09-16 18:46:39 +00:00
|
|
|
#ifdef HAVE_LCD_COLOR
|
2009-06-16 04:25:21 +00:00
|
|
|
"Number Display",
|
2008-10-25 21:22:00 +00:00
|
|
|
#endif
|
|
|
|
#ifdef SUDOKU_BUTTON_POSSIBLE
|
2009-06-16 04:25:21 +00:00
|
|
|
"Show Markings",
|
2007-09-16 18:46:39 +00:00
|
|
|
#endif
|
2009-06-16 04:25:21 +00:00
|
|
|
"Save", "Reload", "Clear", "Solve",
|
|
|
|
"Generate", "New", "Quit");
|
2006-03-03 08:14:44 +00:00
|
|
|
|
2009-06-16 04:25:21 +00:00
|
|
|
result = rb->do_menu(&menu, NULL, NULL, false);
|
2006-03-03 08:14:44 +00:00
|
|
|
|
|
|
|
switch (result) {
|
2007-09-16 18:46:39 +00:00
|
|
|
case SM_AUDIO_PLAYBACK:
|
2009-01-16 10:34:40 +00:00
|
|
|
playback_control(NULL);
|
2006-04-01 23:23:07 +00:00
|
|
|
break;
|
|
|
|
|
2007-09-16 18:46:39 +00:00
|
|
|
#ifdef HAVE_LCD_COLOR
|
|
|
|
case SM_NUMBER_DISPLAY:
|
|
|
|
numdisplay_setting();
|
|
|
|
break;
|
|
|
|
#endif
|
2008-10-25 21:22:00 +00:00
|
|
|
|
|
|
|
#ifdef SUDOKU_BUTTON_POSSIBLE
|
|
|
|
case SM_SHOW_MARKINGS:
|
|
|
|
showmarkings_setting();
|
|
|
|
break;
|
|
|
|
#endif
|
2007-09-16 18:46:39 +00:00
|
|
|
case SM_SAVE:
|
2006-03-03 08:14:44 +00:00
|
|
|
save_sudoku(state);
|
|
|
|
break;
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2007-09-16 18:46:39 +00:00
|
|
|
case SM_RELOAD:
|
2006-03-03 08:14:44 +00:00
|
|
|
restore_state(state);
|
|
|
|
break;
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2007-09-16 18:46:39 +00:00
|
|
|
case SM_CLEAR:
|
2006-03-03 08:14:44 +00:00
|
|
|
clear_board(state);
|
|
|
|
break;
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2007-09-16 18:46:39 +00:00
|
|
|
case SM_SOLVE:
|
2006-03-03 08:14:44 +00:00
|
|
|
sudoku_solve(state);
|
|
|
|
break;
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2007-09-16 18:46:39 +00:00
|
|
|
case SM_GENERATE:
|
2006-04-01 18:38:34 +00:00
|
|
|
sudoku_generate(state);
|
|
|
|
break;
|
|
|
|
|
2007-09-16 18:46:39 +00:00
|
|
|
case SM_NEW:
|
2006-03-03 08:14:44 +00:00
|
|
|
clear_state(state);
|
|
|
|
state->editmode=1;
|
|
|
|
break;
|
2005-10-04 23:36:03 +00:00
|
|
|
|
2007-09-16 18:46:39 +00:00
|
|
|
case SM_QUIT:
|
2006-04-01 18:38:34 +00:00
|
|
|
save_sudoku(state);
|
|
|
|
break;
|
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2005-09-22 08:53:04 +00:00
|
|
|
|
2009-07-11 13:35:08 +00:00
|
|
|
return result;
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
|
|
|
|
2006-05-15 20:08:37 +00:00
|
|
|
/* Menu used when user is in edit mode - i.e. creating a new game manually */
|
|
|
|
int sudoku_edit_menu(struct sudoku_state_t* state)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
2009-06-16 04:25:21 +00:00
|
|
|
MENUITEM_STRINGLIST(menu, "Edit Menu", NULL,
|
|
|
|
"Save as", "Quit");
|
2006-05-15 20:08:37 +00:00
|
|
|
|
2009-06-16 04:25:21 +00:00
|
|
|
result = rb->do_menu(&menu, NULL, NULL, false);
|
2006-05-15 20:08:37 +00:00
|
|
|
|
|
|
|
switch (result) {
|
|
|
|
case 0: /* Save new game */
|
|
|
|
rb->kbd_input(state->filename,MAX_PATH);
|
|
|
|
if (save_sudoku(state)) {
|
|
|
|
state->editmode=0;
|
|
|
|
} else {
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(HZ*2, "Save failed");
|
2006-05-15 20:08:37 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: /* Quit */
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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)) {
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(HZ*2, "Illegal move!");
|
2006-03-03 08:14:44 +00:00
|
|
|
/* 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 */
|
2009-01-16 10:34:40 +00:00
|
|
|
enum plugin_status plugin_start(const void* parameter)
|
2005-09-22 08:53:04 +00:00
|
|
|
{
|
2006-03-03 08:14:44 +00:00
|
|
|
bool exit;
|
|
|
|
int button;
|
|
|
|
int lastbutton = BUTTON_NONE;
|
2006-05-15 20:08:37 +00:00
|
|
|
int res;
|
2007-09-16 18:46:39 +00:00
|
|
|
int rc = PLUGIN_OK;
|
2006-03-03 08:14:44 +00:00
|
|
|
long ticks;
|
|
|
|
struct sudoku_state_t state;
|
2007-09-16 18:46:39 +00:00
|
|
|
|
2008-10-25 21:22:00 +00:00
|
|
|
#if defined(HAVE_LCD_COLOR) || defined(SUDOKU_BUTTON_POSSIBLE)
|
2007-09-16 18:46:39 +00:00
|
|
|
configfile_load(cfg_filename, disk_config,
|
|
|
|
sizeof(disk_config) / sizeof(disk_config[0]),
|
|
|
|
CFGFILE_MINVERSION);
|
|
|
|
rb->memcpy(&sudcfg, &sudcfg_disk, sizeof(sudcfg)); /* copy to running config */
|
|
|
|
#endif
|
2006-11-15 06:46:35 +00:00
|
|
|
|
|
|
|
#if LCD_DEPTH > 1
|
2006-11-15 06:14:27 +00:00
|
|
|
rb->lcd_set_backdrop(NULL);
|
2006-12-18 02:33:39 +00:00
|
|
|
rb->lcd_set_foreground(LCD_BLACK);
|
|
|
|
rb->lcd_set_background(LCD_WHITE);
|
2006-11-15 06:46:35 +00:00
|
|
|
#endif
|
2006-03-03 08:14:44 +00:00
|
|
|
|
|
|
|
clear_state(&state);
|
|
|
|
|
|
|
|
if (parameter==NULL) {
|
2006-04-01 18:38:34 +00:00
|
|
|
/* We have been started as a plugin - try default sudoku.ss */
|
|
|
|
if (!load_sudoku(&state,GAME_FILE)) {
|
2006-05-15 20:08:37 +00:00
|
|
|
/* No previous game saved, use the default */
|
|
|
|
default_state(&state);
|
2006-04-01 18:38:34 +00:00
|
|
|
}
|
2006-03-03 08:14:44 +00:00
|
|
|
} else {
|
|
|
|
if (!load_sudoku(&state,(char*)parameter)) {
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(HZ*2, "Load error");
|
2006-03-03 08:14:44 +00:00
|
|
|
return(PLUGIN_ERROR);
|
|
|
|
}
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
|
|
|
|
2006-12-18 02:33:39 +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){
|
2006-04-02 00:09:33 +00:00
|
|
|
#ifdef SUDOKU_BUTTON_QUIT
|
2006-03-03 08:14:44 +00:00
|
|
|
/* Exit game */
|
|
|
|
case SUDOKU_BUTTON_QUIT:
|
2006-05-15 20:08:37 +00:00
|
|
|
if (check_status(&state)) {
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(HZ*2, "Illegal move!");
|
2006-05-15 20:08:37 +00:00
|
|
|
/* Ignore any button presses during the splash */
|
|
|
|
rb->button_clear_queue();
|
|
|
|
} else {
|
|
|
|
save_sudoku(&state);
|
2007-09-16 18:46:39 +00:00
|
|
|
exit=true;
|
2006-05-15 20:08:37 +00:00
|
|
|
}
|
2006-03-03 08:14:44 +00:00
|
|
|
break;
|
2006-04-02 00:09:33 +00:00
|
|
|
#endif
|
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 {
|
2006-05-15 20:08:37 +00:00
|
|
|
state.currentboard[state.y][state.x]++;
|
2006-03-03 08:14:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
update_cell(&state,state.y,state.x);
|
|
|
|
break;
|
|
|
|
|
2006-04-02 00:09:33 +00:00
|
|
|
#ifdef SUDOKU_BUTTON_TOGGLEBACK
|
|
|
|
case SUDOKU_BUTTON_TOGGLEBACK | BUTTON_REPEAT:
|
|
|
|
/* Slow down the repeat speed to 1/3 second */
|
|
|
|
if ((*rb->current_tick-ticks) < (HZ/3)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SUDOKU_BUTTON_TOGGLEBACK:
|
|
|
|
/* Decrement digit */
|
|
|
|
ticks=*rb->current_tick;
|
|
|
|
if (state.editmode) {
|
|
|
|
if (state.startboard[state.y][state.x]=='0') {
|
|
|
|
state.startboard[state.y][state.x]='9';
|
|
|
|
state.currentboard[state.y][state.x]='9';
|
|
|
|
} 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]=='0') {
|
|
|
|
state.currentboard[state.y][state.x]='9';
|
|
|
|
} else {
|
|
|
|
state.currentboard[state.y][state.x]--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
update_cell(&state,state.y,state.x);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2006-03-03 08:14:44 +00:00
|
|
|
/* move cursor left */
|
2006-04-02 00:09:33 +00:00
|
|
|
case SUDOKU_BUTTON_LEFT:
|
|
|
|
case (SUDOKU_BUTTON_LEFT | BUTTON_REPEAT):
|
2008-11-27 20:26:50 +00:00
|
|
|
if ( (state.x==0&&invertdir==0) || (state.y==0&&invertdir==1) ) {
|
2006-04-02 00:09:33 +00:00
|
|
|
#ifndef SUDOKU_BUTTON_UP
|
2008-11-27 20:26:50 +00:00
|
|
|
if ( (state.y==0&&invertdir==0) || (state.x==0&&invertdir==1)) {
|
2006-04-02 00:09:33 +00:00
|
|
|
move_cursor(&state,8,8);
|
2008-11-27 20:26:50 +00:00
|
|
|
} else {
|
|
|
|
if (invertdir==0) {
|
|
|
|
move_cursor(&state,8,state.y-1);
|
|
|
|
} else {
|
|
|
|
move_cursor(&state,state.x-1,8);
|
|
|
|
}
|
2006-04-02 00:09:33 +00:00
|
|
|
}
|
|
|
|
#else
|
2006-03-03 08:14:44 +00:00
|
|
|
move_cursor(&state,8,state.y);
|
2006-04-02 00:09:33 +00:00
|
|
|
#endif
|
2008-11-27 20:26:50 +00:00
|
|
|
} else {
|
|
|
|
if (invertdir==0) {
|
|
|
|
move_cursor(&state,state.x-1,state.y);
|
|
|
|
} else {
|
|
|
|
move_cursor(&state,state.x,state.y-1);
|
|
|
|
}
|
2006-03-03 08:14:44 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* move cursor right */
|
2006-04-02 00:09:33 +00:00
|
|
|
case SUDOKU_BUTTON_RIGHT:
|
|
|
|
case (SUDOKU_BUTTON_RIGHT | BUTTON_REPEAT):
|
2008-11-27 20:26:50 +00:00
|
|
|
if ( (state.x==8&&invertdir==0) || (state.y==8&&invertdir==1) ) {
|
2006-04-02 00:09:33 +00:00
|
|
|
#ifndef SUDOKU_BUTTON_DOWN
|
2008-11-27 20:26:50 +00:00
|
|
|
if ( (state.y==8&&invertdir==0) || (state.x==8&&invertdir==1) ) {
|
2006-04-02 00:09:33 +00:00
|
|
|
move_cursor(&state,0,0);
|
2008-11-27 20:26:50 +00:00
|
|
|
} else {
|
|
|
|
if (invertdir==0) {
|
|
|
|
move_cursor(&state,0,state.y+1);
|
|
|
|
} else {
|
|
|
|
move_cursor(&state,state.x+1,0);
|
|
|
|
}
|
2006-04-02 00:09:33 +00:00
|
|
|
}
|
|
|
|
#else
|
2006-03-03 08:14:44 +00:00
|
|
|
move_cursor(&state,0,state.y);
|
2006-04-02 00:09:33 +00:00
|
|
|
#endif
|
2008-11-27 20:26:50 +00:00
|
|
|
} else {
|
|
|
|
if (invertdir==0) {
|
|
|
|
move_cursor(&state,state.x+1,state.y);
|
|
|
|
} else {
|
|
|
|
move_cursor(&state,state.x,state.y+1);
|
|
|
|
}
|
2006-03-03 08:14:44 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2006-04-02 00:09:33 +00:00
|
|
|
#ifdef SUDOKU_BUTTON_UP
|
2006-03-03 08:14:44 +00:00
|
|
|
/* 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;
|
2006-04-02 00:09:33 +00:00
|
|
|
#endif
|
2006-03-03 08:14:44 +00:00
|
|
|
|
2006-04-02 00:09:33 +00:00
|
|
|
#ifdef SUDOKU_BUTTON_DOWN
|
2006-03-03 08:14:44 +00:00
|
|
|
/* 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;
|
2006-04-02 00:09:33 +00:00
|
|
|
#endif
|
2006-03-03 08:14:44 +00:00
|
|
|
|
|
|
|
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)) {
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(HZ*2, "Illegal move!");
|
2006-03-03 08:14:44 +00:00
|
|
|
/* Ignore any button presses during the splash */
|
|
|
|
rb->button_clear_queue();
|
|
|
|
} else {
|
|
|
|
if (state.editmode) {
|
2006-05-15 20:08:37 +00:00
|
|
|
res = sudoku_edit_menu(&state);
|
|
|
|
if (res == MENU_ATTACHED_USB) {
|
2007-09-16 18:46:39 +00:00
|
|
|
rc = PLUGIN_USB_CONNECTED;
|
|
|
|
exit = true;
|
2006-05-15 20:08:37 +00:00
|
|
|
} else if (res == 1) { /* Quit */
|
2007-09-16 18:46:39 +00:00
|
|
|
exit = true;
|
2006-03-03 08:14:44 +00:00
|
|
|
}
|
|
|
|
} else {
|
2009-07-11 13:35:08 +00:00
|
|
|
res = sudoku_menu(&state);
|
|
|
|
if (res == MENU_ATTACHED_USB) {
|
2007-09-16 18:46:39 +00:00
|
|
|
rc = PLUGIN_USB_CONNECTED;
|
|
|
|
exit = true;
|
2009-07-11 13:35:08 +00:00
|
|
|
} else if (res == SM_QUIT) {
|
|
|
|
exit = true;
|
2006-03-03 08:14:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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]^=
|
2009-06-07 21:27:05 +00:00
|
|
|
BIT_N(state.currentboard[state.y][state.x] - '0');
|
2006-03-03 08:14:44 +00:00
|
|
|
}
|
|
|
|
break;
|
2005-11-29 00:02:00 +00:00
|
|
|
#endif
|
2008-11-27 20:26:50 +00:00
|
|
|
|
|
|
|
#ifdef SUDOKU_BUTTON_CHANGEDIR
|
|
|
|
case SUDOKU_BUTTON_CHANGEDIR:
|
|
|
|
/* Change scroll wheel direction */
|
|
|
|
invertdir=!invertdir;
|
|
|
|
break;
|
|
|
|
#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 */
|
2007-09-16 18:46:39 +00:00
|
|
|
rc = PLUGIN_USB_CONNECTED;
|
|
|
|
exit = true;
|
2006-03-03 08:14:44 +00:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
2008-10-25 21:22:00 +00:00
|
|
|
#if defined(HAVE_LCD_COLOR) || defined(SUDOKU_BUTTON_POSSIBLE)
|
2007-09-16 18:46:39 +00:00
|
|
|
if (rb->memcmp(&sudcfg, &sudcfg_disk, sizeof(sudcfg))) /* save settings if changed */
|
|
|
|
{
|
|
|
|
rb->memcpy(&sudcfg_disk, &sudcfg, sizeof(sudcfg));
|
|
|
|
configfile_save(cfg_filename, disk_config,
|
|
|
|
sizeof(disk_config) / sizeof(disk_config[0]),
|
|
|
|
CFGFILE_VERSION);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return rc;
|
2005-09-22 08:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|