summaryrefslogtreecommitdiff
path: root/apps/plugins/sudoku/generator.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/sudoku/generator.c')
-rw-r--r--apps/plugins/sudoku/generator.c28
1 files changed, 26 insertions, 2 deletions
diff --git a/apps/plugins/sudoku/generator.c b/apps/plugins/sudoku/generator.c
index 037798d761..eaef8c0f17 100644
--- a/apps/plugins/sudoku/generator.c
+++ b/apps/plugins/sudoku/generator.c
@@ -1056,9 +1056,14 @@ select_template( void )
1056 init_template( i ); 1056 init_template( i );
1057} 1057}
1058 1058
1059static bool check_buttons(void)
1060{
1061 int button = rb->button_get(false);
1062 return (button && (!(button & BUTTON_REL)) && (!(button & BUTTON_REPEAT)));
1063}
1059 1064
1060static 1065static
1061void 1066bool
1062generate( void ) 1067generate( void )
1063{ 1068{
1064 static int digits[ 9 ]; 1069 static int digits[ 9 ];
@@ -1066,6 +1071,10 @@ generate( void )
1066 int i; 1071 int i;
1067 1072
1068start: 1073start:
1074 /* Allow the user to abort generation by pressing any button */
1075 if (check_buttons())
1076 return false;
1077
1069 for( i = 0 ; i < 9 ; ++i ) 1078 for( i = 0 ; i < 9 ; ++i )
1070 digits[ i ] = i + 1; 1079 digits[ i ] = i + 1;
1071 1080
@@ -1081,11 +1090,19 @@ start:
1081 for( i = 0 ; i < len_tmplt ; ++i ) 1090 for( i = 0 ; i < len_tmplt ; ++i )
1082 fill( tmplt[ i ], digits[ i % 9 ] ); 1091 fill( tmplt[ i ], digits[ i % 9 ] );
1083 1092
1093 /* Allow the user to abort generation by pressing any button */
1094 if (check_buttons())
1095 return false;
1096
1084 rb->yield(); 1097 rb->yield();
1085 1098
1086 if( 0 != solve( ) || idx_history < 81 ) 1099 if( 0 != solve( ) || idx_history < 81 )
1087 goto start; 1100 goto start;
1088 1101
1102 /* Allow the user to abort generation by pressing any button */
1103 if (check_buttons())
1104 return false;
1105
1089 rb->yield(); 1106 rb->yield();
1090 1107
1091 for( i = 0 ; i < len_tmplt ; ++i ) 1108 for( i = 0 ; i < len_tmplt ; ++i )
@@ -1105,6 +1122,8 @@ start:
1105 goto start; 1122 goto start;
1106 1123
1107 clear_moves( ); 1124 clear_moves( );
1125
1126 return true;
1108} 1127}
1109 1128
1110bool sudoku_generate_board(struct sudoku_state_t* state, char** difficulty) 1129bool sudoku_generate_board(struct sudoku_state_t* state, char** difficulty)
@@ -1113,7 +1132,12 @@ bool sudoku_generate_board(struct sudoku_state_t* state, char** difficulty)
1113 1132
1114 rb->srand(*rb->current_tick); 1133 rb->srand(*rb->current_tick);
1115 1134
1116 generate(); 1135 rb->button_clear_queue();
1136
1137 if (!generate()) {
1138 /* User has aborted with a button press */
1139 return false;
1140 }
1117 1141
1118 i=0; 1142 i=0;
1119 for (r=0;r<9;r++) { 1143 for (r=0;r<9;r++) {