Index: sudoku.c =================================================================== RCS file: /cvsroot/rockbox/apps/plugins/sudoku.c,v retrieving revision 1.2 diff -u -r1.2 sudoku.c --- sudoku.c 22 Sep 2005 09:01:50 -0000 1.2 +++ sudoku.c 22 Sep 2005 13:34:50 -0000 @@ -70,17 +70,20 @@ #define SUDOKU_BUTTON_QUIT BUTTON_OFF #define SUDOKU_BUTTON_TOGGLE BUTTON_PLAY #define SUDOKU_BUTTON_MENU BUTTON_F3 +#define SUDOKU_BUTTON_EDITMODE BUTTON_MENU #elif CONFIG_KEYPAD == ONDIO_PAD #define SUDOKU_BUTTON_QUIT BUTTON_OFF #define SUDOKU_BUTTON_TOGGLE BUTTON_MENU #define SUDOKU_BUTTON_MENU (BUTTON_MENU | BUTTON_OFF) +#define SUDOKU_BUTTON_EDITMODE 0x1000 /* xxx:not sure how to do this right */ #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \ (CONFIG_KEYPAD == IRIVER_H300_PAD) #define SUDOKU_BUTTON_QUIT BUTTON_OFF #define SUDOKU_BUTTON_TOGGLE BUTTON_SELECT #define SUDOKU_BUTTON_MENU BUTTON_MODE +#define SUDOKU_BUTTON_EDITMODE BUTTON_REC #elif #error SUDOKU: Unsupported keypad @@ -379,6 +382,7 @@ char currentboard[9][9]; /* The current state of the game */ char savedboard[9][9]; /* Cached copy of saved state */ int x,y; /* Cursor position */ + bool editmode; /* Editmode state */ }; /****** Solver routine by Tom Shackell @@ -1052,6 +1056,32 @@ update_cell(state,newx,newy); } +void change_digit(int direction, struct sudoku_state_t* state) { + if (direction < 0) { + if (state->startboard[state->y][state->x]=='0') { + if (state->currentboard[state->y][state->x]=='0') { + state->currentboard[state->y][state->x]='9'; + } else if (state->currentboard[state->y][state->x]=='1') { + state->currentboard[state->y][state->x]='0'; + } else { + 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]='1'; + } else if (state->currentboard[state->y][state->x]=='9') { + state->currentboard[state->y][state->x]='0'; + } else { + state->currentboard[state->y][state->x]++; + } + } + } + update_cell(state,state->y,state->x); +} + /* plugin entry point */ enum plugin_status plugin_start(struct plugin_api* api, void* parameter) { @@ -1079,6 +1109,7 @@ /* The main game loop */ exit=false; ticks=0; + state.editmode=false; while(!exit) { button = rb->button_get(true); @@ -1088,65 +1119,102 @@ exit=1; break; + case SUDOKU_BUTTON_EDITMODE: + state.editmode = !state.editmode; + break; + /* Increment digit */ case SUDOKU_BUTTON_TOGGLE | BUTTON_REPEAT: + if (state.editmode) { + break; + } /* Slow down the repeat speed to 1/3 second */ if ((*rb->current_tick-ticks) < (HZ/3)) { break; } case SUDOKU_BUTTON_TOGGLE: - /* Increment digit */ - ticks=*rb->current_tick; - if (state.startboard[state.y][state.x]=='0') { - if (state.currentboard[state.y][state.x]=='0') { - state.currentboard[state.y][state.x]='1'; - } else if (state.currentboard[state.y][state.x]=='9') { - state.currentboard[state.y][state.x]='0'; - } else { - state.currentboard[state.y][state.x]++; - } + if (state.editmode) { + break; } - update_cell(&state,state.y,state.x); + /* Increment digit */ + ticks = *rb->current_tick; + change_digit(1, &state); break; - /* move cursor left */ + /* move cursor left or decrement digit */ 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); + if (state.editmode) { + if (button == BUTTON_LEFT || (*rb->current_tick-ticks) > (HZ/3)) { + /* decrement digit */ + ticks = *rb->current_tick; + change_digit(-1, &state); + } + } else { + /* move cursor left */ + if (state.x==0) { + move_cursor(&state,8,state.y); + } else { + move_cursor(&state,state.x-1,state.y); + } } break; - /* move cursor right */ + /* move cursor right or increment digit*/ 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); + if (state.editmode) { + if (button == BUTTON_RIGHT || (*rb->current_tick-ticks) > (HZ/3)) { + /* increment digit */ + ticks = *rb->current_tick; + change_digit(1, &state); + } + } else { + /* move cursor right */ + if (state.x==8) { + move_cursor(&state,0,state.y); + } else { + move_cursor(&state,state.x+1,state.y); + } } break; - /* move cursor up */ + /* move cursor up or incrment digit */ case BUTTON_UP: case (BUTTON_UP | BUTTON_REPEAT): - if (state.y==0) { - move_cursor(&state,state.x,8); - } else { - move_cursor(&state,state.x,state.y-1); + if (state.editmode) { + if (button == BUTTON_UP || (*rb->current_tick-ticks) > (HZ/3)) { + /* increment digit */ + ticks = *rb->current_tick; + change_digit(1, &state); + } + } else { + /* move cursor up */ + if (state.y==0) { + move_cursor(&state,state.x,8); + } else { + move_cursor(&state,state.x,state.y-1); + } } break; - /* move cursor down */ + /* move cursor down or decrement digit */ case BUTTON_DOWN: case (BUTTON_DOWN | BUTTON_REPEAT): - if (state.y==8) { - move_cursor(&state,state.x,0); - } else { - move_cursor(&state,state.x,state.y+1); + if (state.editmode) { + if (button == BUTTON_DOWN || (*rb->current_tick-ticks) > (HZ/3)) { + /* decrement digit */ + ticks = *rb->current_tick; + change_digit(-1, &state); + } + } else { + /* move cursor down */ + if (state.y==8) { + move_cursor(&state,state.x,0); + } else { + move_cursor(&state,state.x,state.y+1); + } } break;