Javascript example: CubeTimer. Reviews?

eval vs function: JavaScript has anonymous functions and they are often used for this: setTimeout( function () { document.keyup = startTime; }, 1000 );. The keyword is a bit long, but at least better than defining and naming a function somewhere else in the code.

I'm not comfortable with floating point arithmetic and not sure what works and what doesn't. When I need integers, I often use Math.floor or Math.round almost everywhere and just hope for the best... Need to read this some day.

What about replacing
window.setTimeout( 'document.onkeyup = startTimer', 1000 );
with
document.onkeyup = function () { document.onkeyup = startTimer; };
? Seems to work here.
 
But yeah, actually I intend to implement this, though maybe not in Javascript at first as I'm not yet comfortable enough with it.

Nice to hear :)

Javascript is clearly not the natural choice for implementing a computer solver, but I think building a good one in js would be of great benefit to the cubing community. A key challenge is Javascript's data storage efficiency, especially if the algorithm needs to store large lookup/pruning tables. Implementing the Kociemba lookup tables using a naive cube-state representation turned out to be infeasible, but if/when I find the time I'm going to modify the implementation to store cube-state in the lookup tables as strings. A single cube can be stored in a minimum of 7 characters in javascript. Even if this works, the implementation will still be very sluggish to initialise. I think a more memory efficient implementation needs to be found for Javascript.

Another project I've been working on is finding a heuristic which provides an accurate measure of a cube state's distance from the solved state. A perfect heuristic should lead to an optimal solver which runs in linear time. I've had some ideas and limited success, but I've as yet found nothing good enough for a direct route to the solution.

Maybe I'll put Herbert's stuff in there again, but another issue is that I'd have to find out how I'm allowed to do it.

As far as I'm aware, using Herbert's Java implementation of the Two Phase algorithm is fine provide credit is given. This is what I did for my random-state scrambler, but the code is used as a library and not modified directly.
 
Last edited:
eval vs function: JavaScript has anonymous functions and they are often used for this: setTimeout( function () { document.keyup = startTime; }, 1000 );.
Ah, very nice! I'm familiar with that in general, but didn't think of it here.

Need to read this some day.
Ugh. I've seen that before. Probably when you mentioned it before. I really don't want to go through that. My understanding is that working with integers in floats is exact as long as they're relatively small (fit in the significand part) and there's no rounding.

What about replacing
window.setTimeout( 'document.onkeyup = startTimer', 1000 );
with
document.onkeyup = function () { document.onkeyup = startTimer; };
? Seems to work here.
Whoa, wait... <thinks.../>... ok, that is cool, but it leads to accidental timer restarts by hitting and releasing two keys (or the same key twice). I really want the one second delay. I'm considering this:
document.onkeyup = function () { window.setTimeout( function () { document.onkeyup = startTimer; }, 1000 ); };
But ugh... this is not for the beginnerz.
 
Last edited:
Javascript is clearly not the natural choice for implementing a computer solver, but I think building a good one in js would be of great benefit to the cubing community.
I agree, it would be very nice. I'm thinking of using Thistlethwaite with tables for bi-directional search for each step, so eight tables of rather small size (a few thousand entries). Then just combine those tables.
 
Booyah!

document.onkeyup = function () { document.onkeyup = null; window.setTimeout( function () { document.onkeyup = startTimer; }, 1000 ); };

That first waits for a keyup, then waits another second to allow starting again. But... it's not that much better than the original and it's a hell of a lot more complicated. Though... seems oddly straight-forward in hindsight.
 
Cride5 and Stefan would probably be interested in this, and I hope Jeremy doesn't mind me linking to it.

Jeremy wrote a 3x3x3 RSS hack at the beginning of October. Problem is, he never got it to be fully cross-browser (essentially Chrome, FF, Safari, IE - both on Windows and Mac).
(This was supposed to work better, but fails for me.)
 
He connects Javascript to Herbert's solver through a small applet? Not bad, but also, well... still somewhat unsatisfying. Both versions produce good scrambles for me, btw, but the first shows the cubes unscrambled.
 
... I'm going to modify the implementation to store cube-state in the lookup tables as strings. A single cube can be stored in a minimum of 7 characters in javascript.
Humm, you're doing it wrong. The cube states are supposed to take zero space. This is the whole point of indexing; you only store the values in an array, and find the one that corresponds to a given cube state using table[cubeToIndex(cubeState)].

I did some tests using spidermonkey, and an array of 10^7 values uses about 70 bytes (!) per number. It's probably using a hash table internally. I know some implementations are smarter and use a real array when initializing with new Array(size), but not all do that.

A string with 10^7 characters (all '.'), on the other hand, uses only one byte per character. This is what I meant earlier, to store the table, not cube states, in a string. I haven't tried other JavaScript engines and huge strings might cause problems in some of them, but it might work.

Another project I've been working on is finding a heuristic which provides an accurate measure of a cube state's distance from the solved state. A perfect heuristic should lead to an optimal solver which runs in linear time. I've had some ideas and limited success, but I've as yet found nothing good enough for a direct route to the solution.
I'll be really impressed if you find anything good that doesn't rely on precomputed tables. I haven't given it much thought, but it just seems really hard/impossible.
 
Last edited:
I'm thinking of using Thistlethwaite with tables for bi-directional search for each step, so eight tables of rather small size (a few thousand entries). Then just combine those tables.
That was stupid. My old Thisthethwaite implementation did do four bi-directional BFS searches, but of course I can only precompute tables for the backwards searches, as the goal is always the same but the start isn't.
 
... I'm going to modify the implementation to store cube-state in the lookup tables as strings. A single cube can be stored in a minimum of 7 characters in javascript.
Humm, you're doing it wrong. The cube states are supposed to take zero space. This is the whole point of indexing; you only store the values in an array, and find the one that corresponds to a given cube state using table[cubeToIndex(cubeState)].
... good point, move tables store cube state in coordinate form :o
Still, the values stored in the move tables are natural numbers with ranges significantly lower than is justified by using 64-bit numbers. From my understanding, the largest move tables are URFtoDLF and URtoDF. Each need to store values up to 8!/(8-6)!-1 = 20159 (15-bits). Pruning tables only need to store values up to 18-1 (so 5-bits per entry).

A string with 10^7 characters (all '.'), on the other hand, uses only one byte per character.

In js I'm quite sure 16-bits are used for each character, to allow for unicode. I'm not sure if this is standard across all implementations, but I'd be surprised if it wasn't. This would mean that move tables would conveniently be able to store 1 entry per character, and pruning tables would be able to store 3 entries per character.

The result would be that the largest move tables would require strings of length 20160 (~40Kb) and the largest pruning table (541284 entries) would be storable in 180428 characters (~361Kb). Further compression could be achieved by allowing entries to be stored between two characters, but the extra overhead involved with lookups probably wouldn't be worth the small saving in space.

Another project I've been working on is finding a heuristic which provides an accurate measure of a cube state's distance from the solved state. A perfect heuristic should lead to an optimal solver which runs in linear time. I've had some ideas and limited success, but I've as yet found nothing good enough for a direct route to the solution.
I'll be really impressed if you find anything good that doesn't rely on precomputed tables. I haven't given it much thought, but it just seems really hard/impossible.

A heuristic I found was able to solve some scrambles up to about 10 moves linearly. Larger scrambles often hit local maxima, so I wasn't able to get it to work perfectly. Just to give you an example of where I got with it, here is an optimal random-state scramble from cube explorer along with the measured heuristic distance of each state:

12: B
43: B L
190: B L F'
310: B L F' D
356: B L F' D U'
968: B L F' D U' B'
1480: B L F' D U' B' F'
1937: B L F' D U' B' F' L
2392: B L F' D U' B' F' L B
2576: B L F' D U' B' F' L B R
2192: B L F' D U' B' F' L B R U'
2321: B L F' D U' B' F' L B R U' R
2952: B L F' D U' B' F' L B R U' R D'
2600: B L F' D U' B' F' L B R U' R D' L2
3393: B L F' D U' B' F' L B R U' R D' L2 B2
2616: B L F' D U' B' F' L B R U' R D' L2 B2 F'
3384: B L F' D U' B' F' L B R U' R D' L2 B2 F' L'

This heuristic certainly provides a 'rough' estimate of a cube's state from the solved state, but isn't perfect. I think a perfect one does exist and I'm going to see if I can find it..


Cride5 and Stefan would probably be interested in this, and I hope Jeremy doesn't mind me linking to it.

Jeremy wrote a 3x3x3 RSS hack at the beginning of October. Problem is, he never got it to be fully cross-browser (essentially Chrome, FF, Safari, IE - both on Windows and Mac).
(This was supposed to work better, but fails for me.)

Hmm, I wonder if cross-platform/browser compatibility is always going to be a problem for js calling java applets. It certainly didn't work in my browser :/
 
Last edited:
Still, the values stored in the move tables are natural numbers with ranges significantly lower than is justified by using 64-bit numbers.
And, once again, the actual memory usage can be more than 10 times that. Try it yourself.

In js I'm quite sure 16-bits are used for each character, to allow for unicode.
You're right, from the ECMAScript standard: "A string value is a member of the type String and is a finite ordered sequence of zero or more 16-bit unsigned integer values.". The implementation I tested is apparently from 2007, but it definitely used 8 bits.
 
Still, the values stored in the move tables are natural numbers with ranges significantly lower than is justified by using 64-bit numbers.
And, once again, the actual memory usage can be more than 10 times that. Try it yourself.

Don't worry I do believe you. It wouldn't surprise me if all engines implement arrays using hashtables. Theres some discussion on it here..
http://stackoverflow.com/questions/1460427/does-the-array-size-matter-in-javascript-in-this-case
 
Back
Top