Skip to content

Commit 109add6

Browse files
committed
Add a friendly sudoku interface.
1 parent 5576725 commit 109add6

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

examples/engine.mjs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* jshint esversion:11 */
22

3+
import path from "node:path";
34
import {
45
readFile,
56
} from "node:fs/promises";
@@ -18,11 +19,7 @@ import {
1819
}
1920
from "../jsds/jsds.mjs";
2021

21-
function default_callback(candidate) {
22-
console.log(candidate.toString());
23-
}
24-
25-
function* search(input_strings, buffer_limit = 1024, callback = default_callback) {
22+
function* search(input_strings, buffer_limit, callback) {
2623
buffer_size(buffer_limit);
2724

2825
let rules = {};
@@ -100,45 +97,54 @@ async function read_file_to_string_array(file_path) {
10097
return results;
10198
}
10299

100+
function default_callback(candidate) {
101+
console.log(candidate.toString());
102+
}
103+
103104
async function main() {
104-
const help_message = "Usage: node engine.mjs <file_path> <buffer_limit> [callback_module]";
105+
const help_message = "Usage: node engine.mjs <file_path> <buffer_limit> [config_module]";
105106
if (argv.length != 4 && argv.length != 5) {
106107
console.error(help_message);
107108
exit(1);
108109
}
109110
const file_path = argv[2];
110111
const buffer_limit = parseInt(argv[3], 10);
111112
let callback;
113+
let await_each_step;
112114
if (argv.length == 5) {
113115
try {
114-
const callback_module = await import(argv[4]);
115-
if (typeof callback_module.default === "function") {
116-
callback = callback_module.default;
117-
} else {
118-
console.error("Callback module must export a default function.");
119-
exit(1);
120-
}
116+
const config_module = await import(path.resolve(argv[4]));
117+
callback = config_module.callback;
118+
await_each_step = config_module.await_each_step;
121119
} catch (error) {
122120
console.error(`Error importing callback module: ${error.message}`);
123121
exit(1);
124122
}
125123
} else {
126124
callback = default_callback;
125+
await_each_step = true;
127126
}
128127

129128
const data = await read_file_to_string_array(file_path);
129+
for (const input of data) {
130+
callback(new rule_t(input));
131+
}
130132
const generator = search(data, buffer_limit, callback);
131133
const handle = createInterface({
132134
input: stdin,
133135
output: stdout
134136
});
137+
console.log("Search starting...");
135138
while (true) {
136139
if (generator.next().done) {
137140
console.log("Search completed.");
138141
break;
139142
}
140-
await handle.question("Press Enter to continue...");
143+
if (await_each_step) {
144+
await handle.question("Press Enter to continue...");
145+
}
141146
}
147+
exit(0);
142148
}
143149

144150
await main(); // jshint ignore:line

examples/sudoku_callback.mjs renamed to examples/sudoku.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ let array = Array.from({
66
length: 9
77
}, () => " "));
88

9-
export default function callback(candidate) {
9+
export function callback(candidate) {
1010
if (candidate.length() === 0) {
1111
const text = candidate.toString();
1212
const match = text.match(/\(\(Cell (\d) (\d)\) = \(Literal (\d)\)\)/);
@@ -22,3 +22,5 @@ export default function callback(candidate) {
2222
}
2323
}
2424
}
25+
26+
export const await_each_step = false;

0 commit comments

Comments
 (0)