diff --git a/lib/src/kociemba_solver.dart b/lib/src/kociemba_solver.dart index 4230653..bf8fe4c 100644 --- a/lib/src/kociemba_solver.dart +++ b/lib/src/kociemba_solver.dart @@ -103,7 +103,7 @@ class KociembaSolver extends Solver { // increment axis if (++search.ax[n] > 5) { if (n == 0) { - if (sw.elapsedMilliseconds > timeout.inMilliseconds) { + if (sw.elapsed > timeout) { return null; } diff --git a/lib/src/solver.dart b/lib/src/solver.dart index a37c85c..0920482 100644 --- a/lib/src/solver.dart +++ b/lib/src/solver.dart @@ -1,5 +1,3 @@ -import 'dart:isolate'; - import 'package:cuber/src/cube.dart'; import 'package:cuber/src/solution.dart'; @@ -31,54 +29,30 @@ abstract class Solver { Cube cube, { Duration timeout = defaultTimeout, }) async* { - final receiver = ReceivePort(); - - await Isolate.spawn(_solveDeeply, [ - receiver.sendPort, - cube, - timeout?.inMilliseconds, - this, - ]); - - await for (final data in receiver) { - if (data is Solution) { - yield data; + timeout ??= Solver.defaultTimeout; + + var maxDepth = Solver.defaultMaxDepth; + final solutions = {}; + final sw = Stopwatch()..start(); + + while (sw.elapsed < timeout) { + final s = solve( + cube, + maxDepth: maxDepth, + timeout: timeout - sw.elapsed, + ); + + if (maxDepth > 0 && s != null && s.isNotEmpty) { + if (!solutions.contains(s)) { + solutions.add(s); + yield s; + maxDepth = s.length - 1; + } else { + maxDepth--; + } } else { break; } } } } - -void _solveDeeply(List data) { - final SendPort sender = data[0]; - final Cube cube = data[1]; - final int timeout = data[2] ?? Solver.defaultTimeout.inMilliseconds; - final Solver solver = data[3]; - - var maxDepth = Solver.defaultMaxDepth; - final solutions = {}; - final sw = Stopwatch()..start(); - - while (sw.elapsedMilliseconds < timeout) { - final s = solver.solve( - cube, - maxDepth: maxDepth, - timeout: Duration(milliseconds: timeout - sw.elapsedMilliseconds), - ); - - if (maxDepth > 0 && s != null && s.isNotEmpty) { - if (!solutions.contains(s)) { - solutions.add(s); - sender.send(s); - maxDepth = s.length - 1; - } else { - maxDepth--; - } - } else { - break; - } - } - - sender.send(null); -}