diff options
Diffstat (limited to 'apps')
-rw-r--r-- | apps/plugins/sudoku.c | 50 |
1 files changed, 38 insertions, 12 deletions
diff --git a/apps/plugins/sudoku.c b/apps/plugins/sudoku.c index bbf0cec249..526416d11e 100644 --- a/apps/plugins/sudoku.c +++ b/apps/plugins/sudoku.c | |||
@@ -378,11 +378,12 @@ static unsigned char num_inverse[10][8]= { | |||
378 | static struct plugin_api* rb; | 378 | static struct plugin_api* rb; |
379 | 379 | ||
380 | struct sudoku_state_t { | 380 | struct sudoku_state_t { |
381 | char* filename; /* Filename */ | 381 | char filename[MAX_PATH]; /* Filename */ |
382 | char startboard[9][9]; /* The initial state of the game */ | 382 | char startboard[9][9]; /* The initial state of the game */ |
383 | char currentboard[9][9]; /* The current state of the game */ | 383 | char currentboard[9][9]; /* The current state of the game */ |
384 | char savedboard[9][9]; /* Cached copy of saved state */ | 384 | char savedboard[9][9]; /* Cached copy of saved state */ |
385 | int x,y; /* Cursor position */ | 385 | int x,y; /* Cursor position */ |
386 | int editmode; /* We are editing the start board */ | ||
386 | }; | 387 | }; |
387 | 388 | ||
388 | /****** Solver routine by Tom Shackell <shackell@cs.york.ac.uk> | 389 | /****** Solver routine by Tom Shackell <shackell@cs.york.ac.uk> |
@@ -664,6 +665,7 @@ void clear_state(struct sudoku_state_t* state) | |||
664 | { | 665 | { |
665 | int r,c; | 666 | int r,c; |
666 | 667 | ||
668 | state->filename[0]=0; | ||
667 | for (r=0;r<9;r++) { | 669 | for (r=0;r<9;r++) { |
668 | for (c=0;c<9;c++) { | 670 | for (c=0;c<9;c++) { |
669 | state->startboard[r][c]='0'; | 671 | state->startboard[r][c]='0'; |
@@ -673,6 +675,7 @@ void clear_state(struct sudoku_state_t* state) | |||
673 | 675 | ||
674 | state->x=0; | 676 | state->x=0; |
675 | state->y=0; | 677 | state->y=0; |
678 | state->editmode=0; | ||
676 | } | 679 | } |
677 | 680 | ||
678 | /* Load game - only ".ss" is officially supported, but any sensible | 681 | /* Load game - only ".ss" is officially supported, but any sensible |
@@ -693,15 +696,13 @@ bool load_sudoku(struct sudoku_state_t* state, char* filename) { | |||
693 | return(false); | 696 | return(false); |
694 | } | 697 | } |
695 | 698 | ||
696 | state->filename=filename; | 699 | rb->strncpy(state->filename,filename,MAX_PATH); |
697 | n=rb->read(fd,buf,300); | 700 | n=rb->read(fd,buf,300); |
698 | if (n <= 0) { | 701 | if (n <= 0) { |
699 | return(false); | 702 | return(false); |
700 | } | 703 | } |
701 | rb->close(fd); | 704 | rb->close(fd); |
702 | 705 | ||
703 | clear_state(state); | ||
704 | |||
705 | r=0; | 706 | r=0; |
706 | c=0; | 707 | c=0; |
707 | i=0; | 708 | i=0; |
@@ -765,7 +766,7 @@ bool save_sudoku(struct sudoku_state_t* state) { | |||
765 | char line[]="...|...|...\r\n"; | 766 | char line[]="...|...|...\r\n"; |
766 | char sep[]="-----------\r\n"; | 767 | char sep[]="-----------\r\n"; |
767 | 768 | ||
768 | if (state->filename==NULL) { | 769 | if (state->filename[0]==0) { |
769 | return false; | 770 | return false; |
770 | } | 771 | } |
771 | 772 | ||
@@ -1003,6 +1004,7 @@ bool sudoku_menu(struct sudoku_state_t* state) | |||
1003 | { "Reload", NULL }, | 1004 | { "Reload", NULL }, |
1004 | { "Clear", NULL }, | 1005 | { "Clear", NULL }, |
1005 | { "Solve", NULL }, | 1006 | { "Solve", NULL }, |
1007 | { "New", NULL }, | ||
1006 | }; | 1008 | }; |
1007 | 1009 | ||
1008 | m = rb->menu_init(items, sizeof(items) / sizeof(*items), | 1010 | m = rb->menu_init(items, sizeof(items) / sizeof(*items), |
@@ -1027,6 +1029,11 @@ bool sudoku_menu(struct sudoku_state_t* state) | |||
1027 | sudoku_solve(state); | 1029 | sudoku_solve(state); |
1028 | break; | 1030 | break; |
1029 | 1031 | ||
1032 | case 4: /* Create a new game manually */ | ||
1033 | clear_state(state); | ||
1034 | state->editmode=1; | ||
1035 | break; | ||
1036 | |||
1030 | default: | 1037 | default: |
1031 | break; | 1038 | break; |
1032 | } | 1039 | } |
@@ -1072,8 +1079,10 @@ enum plugin_status plugin_start(struct plugin_api* api, void* parameter) | |||
1072 | rb = api; | 1079 | rb = api; |
1073 | /* end of plugin init */ | 1080 | /* end of plugin init */ |
1074 | 1081 | ||
1082 | clear_state(&state); | ||
1083 | |||
1075 | if (parameter==NULL) { | 1084 | if (parameter==NULL) { |
1076 | return(PLUGIN_ERROR); | 1085 | state.editmode=1; |
1077 | } else { | 1086 | } else { |
1078 | if (!load_sudoku(&state,(char*)parameter)) { | 1087 | if (!load_sudoku(&state,(char*)parameter)) { |
1079 | rb->splash(HZ*2, true, "Load error"); | 1088 | rb->splash(HZ*2, true, "Load error"); |
@@ -1116,14 +1125,22 @@ enum plugin_status plugin_start(struct plugin_api* api, void* parameter) | |||
1116 | #endif | 1125 | #endif |
1117 | /* Increment digit */ | 1126 | /* Increment digit */ |
1118 | ticks=*rb->current_tick; | 1127 | ticks=*rb->current_tick; |
1119 | if (state.startboard[state.y][state.x]=='0') { | 1128 | if (state.editmode) { |
1120 | if (state.currentboard[state.y][state.x]=='0') { | 1129 | if (state.startboard[state.y][state.x]=='9') { |
1121 | state.currentboard[state.y][state.x]='1'; | 1130 | state.startboard[state.y][state.x]='0'; |
1122 | } else if (state.currentboard[state.y][state.x]=='9') { | ||
1123 | state.currentboard[state.y][state.x]='0'; | 1131 | state.currentboard[state.y][state.x]='0'; |
1124 | } else { | 1132 | } else { |
1133 | state.startboard[state.y][state.x]++; | ||
1125 | state.currentboard[state.y][state.x]++; | 1134 | state.currentboard[state.y][state.x]++; |
1126 | } | 1135 | } |
1136 | } else { | ||
1137 | if (state.startboard[state.y][state.x]=='0') { | ||
1138 | if (state.currentboard[state.y][state.x]=='9') { | ||
1139 | state.currentboard[state.y][state.x]='0'; | ||
1140 | } else { | ||
1141 | state.currentboard[state.y][state.x]++; | ||
1142 | } | ||
1143 | } | ||
1127 | } | 1144 | } |
1128 | update_cell(&state,state.y,state.x); | 1145 | update_cell(&state,state.y,state.x); |
1129 | break; | 1146 | break; |
@@ -1179,8 +1196,17 @@ enum plugin_status plugin_start(struct plugin_api* api, void* parameter) | |||
1179 | /* Ignore any button presses during the splash */ | 1196 | /* Ignore any button presses during the splash */ |
1180 | rb->button_clear_queue(); | 1197 | rb->button_clear_queue(); |
1181 | } else { | 1198 | } else { |
1182 | if (sudoku_menu(&state)) { | 1199 | if (state.editmode) { |
1183 | return PLUGIN_USB_CONNECTED; | 1200 | rb->kbd_input(state.filename,MAX_PATH); |
1201 | if (save_sudoku(&state)) { | ||
1202 | state.editmode=0; | ||
1203 | } else { | ||
1204 | rb->splash(HZ*2, true, "Save failed"); | ||
1205 | } | ||
1206 | } else { | ||
1207 | if (sudoku_menu(&state)) { | ||
1208 | return PLUGIN_USB_CONNECTED; | ||
1209 | } | ||
1184 | } | 1210 | } |
1185 | } | 1211 | } |
1186 | break; | 1212 | break; |