# 3x3x3 simulator in 745 bytes of javascript



## Kirjava (Sep 26, 2013)

View Sim / See Code

Up = U
Down = x'
Right = y
Left = Scramble

Bonus: 15 Puzzle in 467 bytes

jlaire helped me with some stuff


----------



## rj (Sep 26, 2013)

Kirjava said:


> View Sim / See Code
> 
> Up = U
> Down = x'
> ...



Genius. Pure genius.


----------



## Stefan (Sep 26, 2013)

The following layout allows shorter initialization as well as shorter indices (uses all of 0 to 9 instead of just 3 to 5):

YYYZZZAAACCC
YYYZZZAAACCC
YYYZZZAAACCC
BBB
BBB
BBB
XXX
XXX
XXX

Edit:

And you could replace
[noparse]if(q==37){for(i=0;i++<99d(0|Math.random()*3+38)}[/noparse]
with
if(i=q>99)while(i--)d(0|Math.random()*3+38)


----------



## Kirjava (Sep 26, 2013)

Changed a few things and got 680 (UFRBLD)

Can't seem to get your scramble code to work?


----------



## Stefan (Sep 26, 2013)

Kirjava said:


> Changed a few things and got 680 (UFRBLD)



Geez you went even further than I suggested. Might as well put it all on a single line 

Btw, why use XYZABC instead of UFRBLD?



Kirjava said:


> Can't seem to get your scramble code to work?



Did you try hitting the appropriate keys? The slash key for example should work.


----------



## Kirjava (Sep 26, 2013)

Stefan said:


> Did you try hitting the appropriate keys? The slash key for example should work.



Yes, but it seemed to only perform a single move? 

i appears to not be correctly assigned


----------



## Stefan (Sep 26, 2013)

Argh, messed that up and didn't test properly, either. Try this then:

[noparse]if(q>99)for(i=q;i--d(0|Math.random()*3+38)[/noparse]


----------



## rj (Sep 26, 2013)

Solve videos anyone?


----------



## Stefan (Sep 26, 2013)

626

I changed the ending like proposed above, changed the r function and how it's called, and assigned w when it's first used.



Spoiler: Not sure how permanent it's there, so I copy here (beware of extra spaces):



a=document.getElementById('a');h='XXXYYYZZZAAABBBCCC\n';a[w="innerHTML"]=h+h+h;function r(s){g=a[w].split("");for(j=0;++j<s.lengthif(j%4){f=g[x=s[j]];g[x]=g[y=s[j-1]];g[y]=f};a[w]=g.join("")}window.onkeydown=d;function d(e){q=e.keyCode||e;if(q==38)r([0,2,40,38,1,21,39,19,3,12,9,6,4,13,10,7,5,14,11,8]);if(q==39)d(38),r([23,32,29,26,22,31,28,25,24,33,30,27,41,50,47,44,42,51,48,45,43,52,49,46,15,53,55,17,16,34,54,36]);if(q==40)r([0,3,15,49,1,4,16,48,2,5,17,47,19,22,34,30,20,23,35,29,21,24,36,28,38,41,53,11,39,42,54,10,40,43,55,9,6,44,46,8,7,25,45,27,12,14,52,50,13,33,51,31]);if(q>99)for(i=q;i--d(0|Math.random()*3+38)}


----------



## joey (Sep 26, 2013)

I pasted a colour version here: http://jsfiddle.net/evgNF/
(note: I didn't try to make it smaller)


----------



## Kirjava (Sep 26, 2013)

Stefan said:


> changed the r function and how it's called,



Cool stuff, I did something similar in kSim but couldn't think how to do it succinctly here.

614


----------



## joey (Sep 26, 2013)

Pretty sure you can remove "this.".

Works on chrome. That'd be 5 chars saved.


----------



## Owen (Sep 27, 2013)

This is pretty awesome, love the fifteen puzzle.


----------



## qqwref (Sep 27, 2013)

This is pretty crazy stuff, although we are certainly pushing the limit of what we can call a sim 

I don't know if this counts as cheating, but I saved 23(?) characters by relaxing our definition of a scramble (and replacing a "40" with a "q"). Hold left (or any key other than up/right/down, really) to do random moves. http://jsfiddle.net/c8uNB/4/

I wonder if we could gain anything by changing the definition of a[w]/h to something like "h='XYZABC\n';h+=h+h;a[w="innerHTML"]=h+h+h;". We'd have to change the permutations of course.


EDIT: You're gonna hate me for this one. http://jsfiddle.net/c8uNB/5/
EDIT2: Saved another character by switching from ascii(whatever+60) to ascii(whatever+35). http://jsfiddle.net/c8uNB/6/


----------



## Stefan (Sep 27, 2013)

qqwref said:


> replacing a "40" with a "q"



Bloody brilliant, I love it! So obvious in hindsight


----------



## cubernya (Sep 27, 2013)

Has anybody done a solve on this yet? I'm sure I could, but it'd be interesting.


----------



## rj (Sep 27, 2013)

theZcuber said:


> Has anybody done a solve on this yet? I'm sure I could, but it'd be interesting.



And can a video be done?


----------



## Roman (Sep 27, 2013)

Very nice!


----------



## szalejot (Sep 27, 2013)

Such a brillant idea


----------



## bundat (Sep 27, 2013)

This is very impressive, reminds me of 96kb .kkrieger



qqwref said:


> EDIT2: Saved another character by switching from ascii(whatever+60) to ascii(whatever+35). http://jsfiddle.net/c8uNB/6/



I saved 3 characters by a cheap trick (just like the one used with innerHTML).
http://jsfiddle.net/c8uNB/10/


(I also have a fail attempt here, I cannot understand why it fails to work... can any js gurus enlighten me? I'm probably forgetting some basic js concept...)


----------



## Kirjava (Sep 27, 2013)

bundat said:


> (I also have a fail attempt here, I cannot understand why it fails to work... can any js gurus enlighten me? I'm probably forgetting some basic js concept...)



Because the return value of s.chatCodeAt is stored in c and is not a reference to the function.


----------



## bundat (Sep 27, 2013)

Weird, if I try to alert out c, I get a "native code" output that makes me think I have the proper function reference.

If I try something like:

```
javascript:c="abcde".charCodeAt;for(i=0;i<5;i++)alert(i+":"+c(i))
```

Firefox 3.6 and IE8 both give: 91, 111, 98, 106, 101 (instead of 97-101)
Chrome just errors off with "Uncaught TypeError: String.prototype.charCodeAt called on null or undefined"
Latest Firefox also errors off with "TypeError: can't convert undefined to object"

I guess charCodeAt is actually a static method (in String), but behaves like a member method (by accessing "this")... if that makes sense?
So c doesn't manage to retain a reference to "this" (s)... I think, I'm not really sure.

Well, just ignore me, back to the topic of the tiny 3x3 sim.


----------



## Kirjava (Sep 27, 2013)

never again


----------



## Stefan (Sep 27, 2013)

Kirjava said:


> never again



I wish you had turned the microphone on (not for key press sounds but for all the cursing I assume took place).


----------



## Renslay (Sep 27, 2013)

I would do this:
1) Do a SIM scramble
2) Re-create the scramble on a real cube
3) Following the real cube, solve the SIM

Would it be considered as cheating?


----------



## Stefan (Sep 27, 2013)

Btw, why don't we support let's say R, U and y instead? I think that would make it easier to use *and* save bytes.


----------



## Kirjava (Sep 27, 2013)

Stefan said:


> I wish you had turned the microphone on (not for key press sounds but for all the cursing I assume took place).



I recorded it at work and don't have a microphone here.



Renslay said:


> Would it be considered as cheating?



I think it would make it harder, so no 



Stefan said:


> Btw, why don't we support let's say R, U and y instead? I think that would make it easier to use *and* save bytes.



It'd be smaller at least - D moves would be a lot of keypresses


----------



## Renslay (Sep 27, 2013)

Kirjava said:


> It'd be smaller at least - D moves would be a lot of keypresses



But it's not hard to avoid D moves during the solving. Have to use a bit different algorithms.


----------



## Stefan (Sep 27, 2013)

Kirjava said:


> It'd be smaller at least - D moves would be a lot of keypresses



Pfft, who needs D moves. D moves are gay.

Supporting {R, U, D, y} instead of what we have now wouldn't add many bytes and make this thing fairly convenient. Or {R, U, F, y}. Or {L, R, F, B, U, D} actually. At some point, defining the keyCodes and their effects as a map should also become shorter than those repeated "q==XY?" tests.

Maybe we should have a standard, some kind of reasonable minimum functionality requirements for this game.


----------



## Kirjava (Sep 27, 2013)

Stefan said:


> Supporting {R, U, D, y} instead of what we have now wouldn't add many bytes and make this thing fairly convenient.



I don't know, I think the point is minimalism - adding D to the movegroup is bloat.



Stefan said:


> Maybe we should have a standard, some kind of minimum functionality requirements for this game.



Yeah, I was thinking about that when it came to scrambling. 

I mean, as long as you can simulate a full cube and it's solvable with some kind of scramble functionality - why would we need anything else?


----------



## Stefan (Sep 27, 2013)

Kirjava said:


> I don't know, I think the point is minimalism - adding D to the movegroup is bloat.



Three moves is bloat already. Remember [thread=19751]the entire cube is 2-gen![/thread]


----------



## Kirjava (Sep 27, 2013)

Stefan said:


> Three moves is bloat already. Remember [thread=19751]the entire cube is 2-gen![/thread]



haha, now solving a sim like *that* would be impressive


----------



## Stefan (Sep 27, 2013)

Kirjava said:


> haha, now solving a sim like *that* would be impressive



Depending on which two algs you use, it might actually be very simple, just cumbersome because repetitive. You can pretty much solve one piece at a time, independently of the others. Similar to old pochmann for BLD, and I described it a bit here: http://groups.yahoo.com/neo/groups/speedsolvingrubikscube/conversations/topics/13973?l=1

Also, [post=655002]Bruce said[/post] that <x,Uz> suffices, even for _"the group consisting of all supercube positions in all 24 orientations of the cube"_. Not sure if something even simpler (like <Rw,y> maybe?) suffices for the normal cube group, too lazy to check now...

Unfortunately, these kind of "moves" aren't just a bunch of 4-cycles anymore, so would require a different encoding/handling for us here.


----------



## Kirjava (Sep 27, 2013)

Stefan said:


> Depending on which two algs you use, it might actually be very simple, just cumbersome because repetitive. You can pretty much solve one piece at a time, independently of the others. Similar to old pochmann for BLD, and I described it a bit here: http://groups.yahoo.com/neo/groups/speedsolvingrubikscube/conversations/topics/13973?l=1



There's also this, but it probably uses more space.

Also if we put it all on one line you can save two bytes of the '\n' char and then we can save four by using document.documentElement instead of document.getElementById('a'). Then we can stop using HTML entirely (possibly?).


----------



## Stefan (Sep 27, 2013)

I think with so many people involved, we need some better kind of version control and merging now 

Anyway... I forked your http://jsfiddle.net/c8uNB/13/ and got it to 411 bytes by using "h" as the storage and only *writing* to the document: http://jsfiddle.net/SH5UX/1/

Has the disadvantage of not showing anything before the first move. If you don't like that, pay six bytes for *;r("")* at the end (or similar).


----------



## Stefan (Sep 27, 2013)

Save two more by going backwards through s (changes the direction of the turns, easy to fix if desired):

for(j=0;j++<s.length
becomes
for(j=s.length;j--

Save one more by updating the document more often than necessary (this will break my *;r("")* fix, though):
for(j=s.length;j--;document.getElementById('a').innerHTML=h=g.join(""))


----------



## Stefan (Sep 27, 2013)

- Use onkeyup instead of onkeydown.
- Since we don't have one-click-scramble functionality anymore anyway, we might as well just scramble ourselves rather than use Math.random. Just hack away on the entire keyboard.
- No need for separate r and d functions, one can do all the work.

=> 339


----------



## Kirjava (Sep 27, 2013)

wow

it's now less than half the size of the original


----------



## Stefan (Sep 27, 2013)

teamwork ftw

*Edit: *This btw doesn't use {R,U,y} yet, which should shave off another 32 bytes from one of the effect strings. Due to all the encoding, I'm just not sure which ones. Thanks, Obama!


----------



## Kirjava (Sep 27, 2013)

307

{R,U,y}

edit: can save 6 bytes by moving to one line


----------



## Stefan (Sep 27, 2013)

Less than 2/3 of the 15-puzzle sim now. I guess 3x3 is easier to implement.


----------



## Stefan (Sep 27, 2013)

Kirjava said:


> edit: can save 6 bytes by moving to one line



How? I'd only remove the one "\n".

The 15-puzzle's scramble function totally sucks, btw.


----------



## Kirjava (Sep 27, 2013)

would using document.documentElement instead of access the pre tag work? if so that's a saving of four

and after looking at this, I imagine the 15 puzzle could be made tiiiny.


----------



## bundat (Sep 27, 2013)

Can I go into the realm of "ugly hacks" and undocumented behavior?

Replaced doc.getbyId(a) with document.body.lastChild
Also removes the id property from the pre tag (is that counted?)

Replace for loop with "for (j in s)".
Seems to work on both Firefox and Chrome.
(I think this would easily break though? Like by adding to String.proto? No idea...)

-13 chars in script
-7 in html

http://jsfiddle.net/SH5UX/16/
(I edited 13, the last one linked... no idea what I missed)

*EDIT*: I guess you're gonna go with a one-liner and get rid of the pre tag completely...? If so try document.body instead of docElem, also seems to work


----------



## qqwref (Sep 27, 2013)

278 characters: http://jsfiddle.net/SH5UX/18/
- Got rid of g entirely inside our function by redefining h to be the .split("") version of the old h
- Moved definition of s
- Got rid of the html entirely in favor of document.body.innerHTML, because why the hell not
- Replaced \n with a space

EDIT: hey, if anything goes, why not just replace "document.body.innerHTML=h.join("")" with "alert(h)"? and while we're at it, remove the spaces from h too... that would bring it down to 251 or so.


----------



## Stefan (Sep 28, 2013)

qqwref said:


> EDIT: hey, if anything goes, why not just replace "document.body.innerHTML=h.join("")" with "alert(h)"? and while we're at it, remove the spaces from h too... that would bring it down to 251 or so.



Don't be silly, that would make it inconvenient to use.


----------



## bundat (Sep 28, 2013)

I just noticed, if you resize the window, you can get the 3 lines to wrap, and its similar to the original layout (minus the monospace font, so it can get a bit misaligned).

Just in case anyone wants to attempt a solve.


----------



## qqwref (Sep 28, 2013)

15 puzzle in 194 characters. http://jsfiddle.net/pq6VK/3/
Or 169 with alert(h) if you prefer


----------



## Stefan (Sep 28, 2013)

qqwref said:


> 15 puzzle in 194 characters. http://jsfiddle.net/pq6VK/3/
> Or 169 with alert(h) if you prefer



Good job, but... nah, not ok.

For the 15-puzzle I'd say there is a natural and straight-forward minimum-requirements standard and we should follow it:
- Proper 4x4 view (like Tom's original).
- Properly laid out up/down/left/right keys (doesn't have to be the arrow keys, could be i/n/a/f, for example).
- Proper scrambling function (single key press), because proper hand-scrambling is hard. I suggest random state or at least 9999 random moves (maybe more, we should actually analyze this (this could be an interesting side project)).


----------



## Renslay (Sep 28, 2013)

Stefan said:


> Good job, but... nah, not ok.
> 
> For the 15-puzzle I'd say there is a natural and straight-forward minimum-requirements standard and we should follow it:
> - Proper 4x4 view (like Tom's original).
> ...



Then you could also say for a 3x3x3 sim:
- Proper net view
- Proper movements for all 6 sides
- Proper scrambling function

My opinion, the lack of the second and the third option is _maybe_ accaptable, but I don't like these "show all sides in one row" sims. For me, only the first sim is accaptable.


----------



## Stefan (Sep 28, 2013)

Renslay said:


> - Proper net view



What exactly does that mean?


----------



## Renslay (Sep 28, 2013)

Stefan said:


> What exactly does that mean?



http://mathworld.wolfram.com/images/eps-gif/CubeNets_950.gif

Maybe the problem is at me, but I see the sim-cubes like this:
XXXYYYZZZAAABBBCCC
XXXYYYZZZAAABBBCCC
XXXYYYZZZAAABBBCCC

Six squares next to each other is not a net of a 3d cube.


----------



## Stefan (Sep 28, 2013)

Renslay said:


> http://mathworld.wolfram.com/images/eps-gif/CubeNets_950.gif



Ok, that's actually a decent suggestion. On the other hand, with a real cube, you don't see all six sides but just three (usually). And [thread=37821]qCube-v2[/thread] seems to be quite good as well, despite showing little more than two sides. So I'd say showing all six sides shouldn't be required, and I find it really hard to define a reasonable minimum standard.


----------



## qqwref (Sep 28, 2013)

Stefan said:


> ...I find it really hard to define a reasonable minimum standard.


Indeed, and that's why I think my 15 puzzle is acceptable  FWIW it does actually have properly laid out up/down/left/right keys (use the arrow keys) although of course any other key can do turns too. The "proper 4x4 view" and "proper scrambling function" are the same kind of concessions we already made on the 3x3x3 sim without much complaint. I could add the 4x4 view back easily enough (i.e. without wasting a huge number of characters) but the proper scrambling function would be difficult due to the way I use q.

Oh, and as people have already mentioned, the scrambling function in the original 15puzzle sim was already not very good. Adding an extra character (99 moves -> 999 moves) would help, but ultimately unless we have a very specific idea of what makes an acceptable scrambler (e.g. must be random state with randomly placed hole) it's only natural to keep saving bytes by making the scrambler worse or even absent.


----------



## Stefan (Sep 28, 2013)

qqwref said:


> Indeed, and that's why I think my 15 puzzle is acceptable



No, no, no... that sentence only referred to the cube.



qqwref said:


> The "proper 4x4 view" and "proper scrambling function" are the same kind of concessions we already made on the 3x3x3 sim without much complaint.



No. That's not the same.

- The cube *view* issue was doomed to begin with, as there simply is no "obvious/natural" way to do it (except maybe graphical projection of the 3D cube onto the screen).
- *Scrambling* in our cube sim still is proper! Holding a key for like five seconds or randomly hitting keys for five seconds is both reasonably convenient and provides reasonably good scrambles. Not the case for the 15-puzzle, where you need a lot more random moves for a good quality scramble.



qqwref said:


> unless we have a very specific idea of what makes an acceptable scrambler (e.g. must be random state with randomly placed hole) it's only natural to keep saving bytes by making the scrambler worse or even absent.



I did provide a very specific idea of what makes an acceptable scrambler (random state or 9999 random moves, and single key press (though I'd also accept any other convenient method resulting in good quality scrambles in about five seconds)). I disagree that bad or missing scrambling is acceptable (though if either, then I prefer missing).


----------



## rj (Sep 28, 2013)

There's an old saying among perl programmers. "Every program has one unnecessary line of code, and a bug."
According to that, you could reduce this program to a single line of code, with a bug.


----------



## Stefan (Sep 28, 2013)

Let me make this a proper challenge by defining the requirements:
- Must look like Tom's original or "equivalent" (you can for example make it graphical, or use 10..15 instead of A..F)
- Must have reasonably laid out up/down/left/right keys working like Tom's original (or use the mouse or whatever in a convenient way).
- Must have one-key/click random state scrambling.
- Optional: Timer, starting with the first move after scrambling, updating at least every 0.1 seconds during the attempt, stopping with a certain key/click, showing at least "seconds.centiseconds" (the time formatted like that, not that string) and "DNF" if stopped when puzzle not solved.
- Optional: Show a sub-10 solve (by anyone)


----------



## ben1996123 (Sep 28, 2013)

rj said:


> There's an old saying among perl programmers. "Every program has one unnecessary line of code, and a bug."
> According to that, you could reduce this program to a single line of code, with a bug.



1 line of code with a bug ok. heres my 3x3sim in c++

main(){}

bug: doesnt actually simulate a 3x3


----------



## rj (Sep 28, 2013)

Stefan said:


> Let me make this a proper challenge by defining the requirements:
> - Must look like Tom's original or "equivalent" (you can for example make it graphical, or use 10..15 instead of A..F)
> - Must have reasonably laid out up/down/left/right keys working like Tom's original (or use the mouse or whatever in a convenient way).
> - Must have one-key/click random state scrambling.
> ...



I don't know JS, sorry.  That was encouragement for the people who are making it shorter.



ben1996123 said:


> 1 line of code with a bug ok. heres my 3x3sim in c++
> 
> main(){}
> 
> bug: doesnt actually simulate a 3x3



Nice! Here's mine in Perl:

#!/usr/bin/perl -w


----------



## Stefan (Sep 28, 2013)

rj said:


> There's an old saying among perl programmers. "Every program has one unnecessary line of code, and a bug."
> According to that, you could reduce this program to a single line of code, with a bug.



According to that, that single line is unnecessary, but when you remove it (which is ok since it's unnecessary), you don't have any line anymore, and that old saying still claims there's an unnecessary line. So obviously that old saying is wrong.


----------



## Stefan (Sep 28, 2013)

rj said:


> I don't know JS, sorry.  That was encouragement for the people who are making it shorter.



Feel free to do it in any other language.


----------



## rj (Sep 28, 2013)

Stefan said:


> According to that, that single line is unnecessary, but when you remove it (which is ok since it's unnecessary), you don't have any line anymore, and that old saying still claims there's an unnecessary line of code. So obviously that old saying is wrong.



It's a half-joke. Almost all perl programs have a few unnecessary lines of code.

I did:
#!/usr/bin/perl -w

One line and a bug.


----------



## radmin (Sep 28, 2013)

http://jsfiddle.net/Yx8Wz/5/

I made it graphical with css sprites. Not shorter but more interesting than the work I'm supposed to be doing right now.
It just replaces the text with an image. It might make it easier for someone to actually solve.


----------



## Renslay (Sep 28, 2013)

radmin said:


> http://jsfiddle.net/Yx8Wz/5/
> 
> I made it graphical with css sprites. Not shorter but more interesting than the work I'm supposed to be doing right now.
> It just replaces the text with an image. It might make it easier for someone to actually solve.



Coooool. I like it!


----------



## rj (Sep 28, 2013)

Renslay said:


> Coooool. I like it!



I could solve this, maybe.


----------



## qqwref (Sep 28, 2013)

Stefan said:


> Let me make this a proper challenge by defining the requirements:
> - Must look like Tom's original or "equivalent" (you can for example make it graphical, or use 10..15 instead of A..F)
> - Must have reasonably laid out up/down/left/right keys working like Tom's original (or use the mouse or whatever in a convenient way).
> - Must have one-key/click random state scrambling.


This is certainly a stronger requirement than the original one. No sliding puzzle sim posted so far has even been close to random state scrambling.

I made one with a 9999-move one-key scrambler and the old visuals, but it's 271 characters now (plus <pre> tag): http://jsfiddle.net/pq6VK/4/



Stefan said:


> No. That's not the same. [...]
> - *Scrambling* in our cube sim still is proper! Holding a key for like five seconds or randomly hitting keys for five seconds is both reasonably convenient and provides reasonably good scrambles. Not the case for the 15-puzzle, where you need a lot more random moves for a good quality scramble.


Of course it's the same. They're both puzzles that don't scramble themselves. Real 15 puzzles don't scramble themselves either, you know. How can you call it a proper real puzzle if it doesn't even have random-state scrambling?!

Anyway, in the new cube sims, holding a key doesn't do anything useful anymore (you have to mash keys). While I agree a 15-puzzle takes longer to scramble than a 3x3x3, you can still scramble the puzzle completely by mashing random keys. It just might take longer. And I don't personally have any problem with that.




radmin said:


> http://jsfiddle.net/Yx8Wz/5/
> 
> I made it graphical with css sprites. Not shorter but more interesting than the work I'm supposed to be doing right now.
> It just replaces the text with an image. It might make it easier for someone to actually solve.


Looks pretty nice. I did a 3:58.95 solve on it.


----------



## Carson (Sep 28, 2013)

So, no requirement that the sim "really" simulate the cube? i.e., the ability to rotate any face...


----------



## Kirjava (Sep 28, 2013)

Carson said:


> So, no requirement that the sim "really" simulate the cube? i.e., the ability to rotate any face...



You can rotate any face with combinations of multiple keypresses in any of the existing sims presented.


----------



## Carson (Sep 28, 2013)

Kirjava said:


> You can rotate any face with combinations of multiple keypresses in any of the existing sims presented.



Can we get a definition of "sim" as it is being referenced in this thread?


----------



## Stefan (Sep 28, 2013)

qqwref said:


> This is certainly a stronger requirement than the original one.



I didn't call it a challenge for nothing 



qqwref said:


> No sliding puzzle sim posted so far has even been close to random state scrambling.



Really? That's rather surprising, given how easy it is. Basically just spam a random permutation and fix parity (or spam again until parity is ok, it's a 50/50 chance after all). Also, the one Ben has been using lately (this one) appears to be doing exactly that (see it's "scrambleStd" function).



qqwref said:


> Of course it's the same.



No it's not. For 15-puzzle there's an obvious view and for the cube there isn't, and for the cube we have reasonable scrambling while your 15-puzzle did not. But I'm just repeating myself, so I doubt it helps.

Oh, another major difference is that nobody really wanted to use that cube simulator, as it was terribly user-unfriendly from the start. The 15-puzzle on the contrary was very user-friendly.



qqwref said:


> Real 15 puzzles don't scramble themselves either, you know.



Of course I know, and the resulting poor-quality hand-scrambles are precisely the reason I never took this puzzle and its records seriously. The "real" one, that is. I do highly respect the recent simulator records (those by Ben, I mean, I actually don't know of others). The computer *does* allow us to scramble it properly, and trivially so, so we should do that.



qqwref said:


> While I agree a 15-puzzle takes longer to scramble than a 3x3x3, you can still scramble the puzzle completely by mashing random keys. It just might take longer.



"Might" take longer? It most certainly does. Much longer. And it's simply not reasonable at all, given that random state scrambling is trivial (it is fairly easy for 3x3 as well, btw, as we're not producing a short scramble alg to be applied to a real cube (which is indeed hard) but only produce a scramble *state*).



qqwref said:


> I made one with a 9999-move one-key scrambler and the old visuals, but it's 271 characters now (plus <pre> tag): http://jsfiddle.net/pq6VK/4/



Doesn't work for me. The result screen is empty and stays empty when I press keys (tested in Chrome and IE).


----------



## Kirjava (Sep 28, 2013)

Carson said:


> Can we get a definition of "sim" as it is being referenced in this thread?



Simulator for the Rubik's Cube.


----------



## Stefan (Sep 28, 2013)

Kirjava said:


> Simulator for the Rubik's Cube.



I think Carson deserves better than that. I actually agree with what I think he means. This cube simulator doesn't simulate the cube. It doesn't simulate an L turn, for example, which is a fundamental move on the real cube. *We* simulate that turn. But that's not the same. Essentially it's *not* a simulator for the Rubik's Cube. If it for example allowed <r,u,f> turning, we also wouldn't call it a Rubik's Cube simulator but an <r,u,f> solving simulator.


----------



## qqwref (Sep 28, 2013)

Stefan said:


> Really? That's rather surprising, given how easy it is. Basically just spam a random permutation and fix parity (or spam again until parity is ok, it's a 50/50 chance after all)


Not surprising at all, given that we are trying for as few characters as possible. Because of those pesky \ns I can't really think of any short way to actually generate a random state. You're free to try yourself, and I'd like to see your result.



Stefan said:


> Doesn't work for me. The result screen is empty and stays empty when I press keys (tested in Chrome and IE).


It looks like Chrome wasn't accepting function(e)d(e.keyCode%5) - it wanted some {}s. Here's a fixed version with 272 characters: http://jsfiddle.net/pq6VK/5/

PS: can I at least get a "nice job" for completely optimizing the 15 puzzle myself?


----------



## KongShou (Sep 28, 2013)

qqwref said:


> Not surprising at all, given that we are trying for as few characters as possible. Because of those pesky \ns I can't really think of any short way to actually generate a random state. You're free to try yourself, and I'd like to see your result.
> 
> 
> It looks like Chrome wasn't accepting function(e)d(e.keyCode%5) - it wanted some {}s. Here's a fixed version with 272 characters: http://jsfiddle.net/pq6VK/5/
> ...



nice job


----------



## Stefan (Sep 28, 2013)

qqwref said:


> Not surprising at all, given that we are trying for as few characters as possible.



Yes surprising. Judging by what you're saying now, I guess your _"No sliding puzzle sim posted so far"_ meant _"posted in this thread"_. That was not at all clear, as I think there just had been Thom's and yours and both were 15-puzzles while _"sliding puzzle"_ is more general. So I thought you meant in general (which should've been clear by me pointing to one outside?). And there it is surprising (or rather, wrong).



qqwref said:


> PS: can I at least get a "nice job" for completely optimizing the 15 puzzle myself?



Sure. I already did tell you [post=905415]"Good job"[/post] precisely because I do think you deserve praise, but if "nice" is preferable: Nice job.


----------



## qqwref (Sep 29, 2013)

Stefan said:


> Yes surprising. Judging by what you're saying now, I guess your _"No sliding puzzle sim posted so far"_ meant _"posted in this thread"_. That was not at all clear, as I think there just had been Thom's and yours and both were 15-puzzles while _"sliding puzzle"_ is more general. So I thought you meant in general (which should've been clear by me pointing to one outside?). And there it is surprising (or rather, wrong).


Nah, I was specifically talking about code-golfed ones (that is, ones in this thread). The ones people use for speedsolving are, unsurprisingly, random-state.


----------



## Kirjava (Sep 29, 2013)

Stefan said:


> I think Carson deserves better than that. I actually agree with what I think he means. This cube simulator doesn't simulate the cube. It doesn't simulate an L turn, for example, which is a fundamental move on the real cube. *We* simulate that turn. But that's not the same. Essentially it's *not* a simulator for the Rubik's Cube. If it for example allowed <r,u,f> turning, we also wouldn't call it a Rubik's Cube simulator but an <r,u,f> solving simulator.



I agree that /we/ are simulating the turn, but disagree that it is not a simulator.

For me, it's good enough that it reaches every position and can be fully scrambled and solved (also that is has some kind of visual output).


----------



## Carson (Sep 29, 2013)

Kirjava said:


> I agree that /we/ are simulating the turn, but disagree that it is not a simulator.
> 
> For me, it's good enough that it reaches every position and can be fully scrambled and solved (also that is has some kind of visual output).



User experience anyone? I can solve a rubik's cube. While that doesn't make me special among this community, I have been solving them for almost a decade now. While I'm not the fastest, I like to think that I have accumulated a fair amount of knowledge on the subject. Here is my issue with calling this a simulator: I can't solve it. With the knowledge of a decade of solving, I am unable to solve this "simulated cube". To do so, I would have to learn how to solve a cube with the same restrictions placed upon it as in this "sim". While this may be good enough for you... I can't justify calling it a sim under those circumstances. To me, it is just a programming experiment... a damn good one, but that is all it is.


----------



## qqwref (Sep 29, 2013)

You could solve cubes for a decade and not be able to solve qcube without quite a bit of time/practice (if you have never used any keyboard sim before). Is this really so different?


----------



## Carson (Sep 29, 2013)

I'm not arguing that any keyboard sim can replicate the exact experience, but this one requires additional knowledge to solve. Since certain moves are not possible (yes, I understand the same resulting states are possible, but the moves themselves are not) the method changes. If the algorithms used to solve a rubik's cube cannot be used to solve this, how can it be considered a sim? 

It certainly is MUCH more difficult for me to solve any keyboard sim. I find that I have to stop to think about the the individual movements contained within an algorithm. While I haven't tried this in quite some time, my best time is probably several minutes. So, yes, I am very bad at it... but I am still able to solve it using the same techniques and knowledge.


----------



## Kirjava (Sep 29, 2013)

Carson said:


> User experience anyone?



There are other sims with better usability if you need that. 

Just because this is hard to use does not mean that it is not a sim.

You don't have a valid complaint.



Carson said:


> If the algorithms used to solve a rubik's cube cannot be used to solve this, how can it be considered a sim?



You can use algorithms like normal? You're making out like it's a different puzzle or something.

U = Up
D = Down Down Up Down Down
F = Down Down Down Up Down
B = Down Up Down Down Down
R = Right Down Down Down Up Down Right Right Right
L = Right Right Right Down Down Down Up Down Right

Here are the controls, it supports every move.


----------



## Carson (Sep 29, 2013)

Kirjava said:


> There are other sims with better usability if you need that.
> 
> Just because this is hard to use does not mean that it is not a sim.
> 
> You don't have a valid complaint.



My argument is not the "difficulty", it is that because of the restrictions on movement, the user experience isn't remotely comparable with that of solving a cube. That I don't possess the knowledge to solve this "sim" isn't a reflection on it's difficulty, but instead that the knowledge required is different.


----------



## Kirjava (Sep 29, 2013)

Carson said:


> My argument is not the "difficulty", it is that because of the restrictions on movement, the user experience isn't remotely comparable with that of solving a cube.



Did you see my above post? You can even see me solve the damn thing with CFOP, busting out Sunes and **** in my video.



Carson said:


> That I don't possess the knowledge to solve this "sim" isn't a reflection on it's difficulty, but instead that the knowledge required is different.



What different knowledge is required? You just solve it the same way you solve any cube or sim, it just has a weird control scheme and a confusing display.


----------



## qqwref (Sep 29, 2013)

Yeah, having solved a version of this puzzle (the one with the graphical display) I can confirm that you do use the same techniques and algorithms as with a regular cube. It is just more difficult to get it to do the move you want to do (and to recognize stuff). It's not actually a restriction on the puzzle, just a very inefficient control scheme.


Back to Stefan's complaints, I think I feel like the best part of these sims is their small size. To me, a shorter sim is more impressive and more interesting, even if it doesn't have as many features as the longer one, and is further in functionality from a real program. A lot of the fun for me is seeing how much you can strip down and still be able to say that, yes, it is still an interactive program that simulates the puzzle. I guess I can see where Stefan is coming from but I feel like when you are adding features back in and increasing the size of the puzzle the activity changes from code golf to software-development. And if I'm going to be doing that I'd rather work on a good sim.


----------



## Stefan (Sep 30, 2013)

Kirjava said:


> I agree that /we/ are simulating the turn, but disagree that it is not a simulator.



I didn't say it's not a simulator, I said it's not a Rubik's Cube simulator. And I'm sticking to that. I dismiss your _"L is there it's just Right Right Right Down Down Down Up Down Right"_. Like I said before, it's _"a *fundamental* move on the real cube"_ and it simply isn't on the sim.



Kirjava said:


> For me, it's good enough that it reaches every position and can be fully scrambled and solved (also that is has some kind of visual output).



Let's represent each state with a number from 0 to 43252003274489855999 and on key up, increase that number by 1 modulo 43252003274489856000? Awesome?



Kirjava said:


> You can even see me solve the damn thing with CFOP, busting out Sunes and **** in my video.



I'm doubt we can. Sure, I can watch the video and I see letters jumping around, but it's probably impossible to follow. We can't even follow your skype teamblind instructions on cubecast! Fun stat: youtube stats say 196 views and 3 hours, so about 1 minute per view. I claim nobody actually tried to understand anything going on in that video 



qqwref said:


> Back to Stefan's complaints, I think I feel like the best part of these sims is their small size. To me, a shorter sim is more impressive and more interesting, even if it doesn't have as many features as the longer one, and is further in functionality from a real program. A lot of the fun for me is seeing how much you can strip down and still be able to say that, yes, it is still an interactive program that simulates the puzzle. I guess I can see where Stefan is coming from but I feel like when you are adding features back in and increasing the size of the puzzle the activity changes from code golf to software-development. And if I'm going to be doing that I'd rather work on a good sim.



I get your point as well, but in the case of the 15-puzzle, a version without good scrambling is in my opinion simply missing something. Exactly what the "real" version is missing and why I don't like it. Ok, it's a full simulation of the real one that way, but that's a full simulation of something bad. Also, it's not like we've never code-golfed scramblers, and I thought this one might be interesting as well.


----------



## cubizh (Sep 30, 2013)

Kirjava said:


> Spoiler: Example solve video
> 
> 
> 
> ...


This should be fun to reconstruct.


----------



## Kirjava (Sep 30, 2013)

Stefan said:


> I didn't say it's not a simulator, I said it's not a Rubik's Cube simulator. And I'm sticking to that. I dismiss your _"L is there it's just Right Right Right Down Down Down Up Down Right"_. Like I said before, it's _"a *fundamental* move on the real cube"_ and it simply isn't on the sim.



I disagree. The only problem with doing 'fundamental moves' is you have to move the side you want to turn to the top first.



Stefan said:


> Let's represent each state with a number from 0 to 43252003274489855999 and on key up, increase that number by 1 modulo 43252003274489856000? Awesome?



What if I want to use a method other than the devil's algorithm? Can you tell me the controls for U, F, R, B, L and D? 



Stefan said:


> I'm doubt we can. Sure, I can watch the video and I see letters jumping around, but it's probably impossible to follow.



Carson seemed to think that you needed to use a different method to solve the cube for some reason. I was pointing out that you just use normal methods to solve this.

Also, I can follow what is happening in the video. It's quite easy to visualise the 3x3x3 once you get used to it.


----------



## Stefan (Sep 30, 2013)

Kirjava said:


> I disagree. The only problem with doing 'fundamental moves' is *you have to move the side you want to turn to the top first*.



Which you don't have to do with the real one.



Kirjava said:


> What if I want to use a method other than the devil's algorithm? Can you tell me the controls for U, F, R, B, L and D?



Not sure. It's either something simple like "For L press space 482375920 times" or it might depend on the state like "For L press space N(S) times, where S is the current state", with N being some kind of table/function.

Anyway, you said _"For me, it's good enough that it reaches every position and can be fully scrambled and solved (also that is has some kind of visual output)"_ and I think my suggestion does fulfill those requirements and thus should be good enough for you.



Kirjava said:


> Carson seemed to think that you needed to use a different method to solve the cube for some reason.



Ah, ok. Didn't sound like that to me.



Kirjava said:


> Also, I can follow what is happening in the video. It's quite easy to visualise the 3x3x3 once you get used to it.



I don't think I'd want to get used to that . Also, you still have the advantage of knowing your algs/style.


----------



## Kirjava (Sep 30, 2013)

Stefan said:


> Which you don't have to do with the real one.



You don't have to press keys on a real cube either.



Stefan said:


> Not sure. It's either something simple like "For L press space 482375920 times" or it might depend on the state like "For L press space N(S) times, where S is the current state", with N being some kind of table/function.
> 
> Anyway, you said _"For me, it's good enough that it reaches every position and can be fully scrambled and solved (also that is has some kind of visual output)"_ and I think my suggestion does fulfill those requirements and thus should be good enough for you.



I don't know how to solve it until I know the controls. 



Stefan said:


> I don't think I'd want to get used to that . Also, you still have the advantage of knowing your algs/style.



You don't have to get used to it, it's not designed to be actually used. I'm also quite sure I could follow someone else's solve.


----------



## Renslay (Sep 30, 2013)

Stefan said:


> Let's represent each state with a number from 0 to 43252003274489855999 and on key up, increase that number by 1 modulo 43252003274489856000? Awesome?



I know a smiliar variant, yet easier to implement:
Cube = 123456789AB1234567000000000000000000
(In the order of EP-CP-EO-CO, 18 cubies)
And you can define <U,x,y> with permutations along the numbers, with orientations as additions modulo 3 or modulo 2. So, a scrambled cube for example:
Scrambled cube = 46432A1B9783254176010010010112102101

Here, a cube sim. Awesome.


----------



## StachuK1992 (Sep 30, 2013)

Renslay,
That would be a really cool human thistlewaite/kociemba sim! I'd love to play with that (if I had full controls, not this <U,x,y> stuff).

I'm definitely on the side that 1 keypress should be 1 move to be considered a /full/ sim. But these aren't full sims, so...


----------



## qqwref (Sep 30, 2013)

Here's a new challenge (for the 3x3x3 only) then:
- Sim must display all 54 stickers, with faces in a cube net and each face composed of a three by three array of stickers (although you can choose how to display each sticker)
- Each of the 6 turns must be able to be done with some single keypress or mouse click

PS: Stefan, if you want to write a random-state scrambler for the 15puzzle, go ahead, I'd like to see it. As I said, though, I had trouble thinking about how to work around those newlines. It might be easier to store h without newlines and then do something like this:

```
v="";g=h;while(g.length>0)v+=g.splice(0,4)+"<br/>";document.body.innerHTML=v
```


----------



## Stefan (Oct 1, 2013)

qqwref said:


> Stefan, if you want to write a random-state scrambler for the 15puzzle, go ahead, I'd like to see it. As I said, though, I had trouble thinking about how to work around those newlines. It might be easier to store h without newlines and then do something like this:
> 
> ```
> v="";g=h;while(g.length>0)v+=g.splice(0,4)+"<br/>";document.body.innerHTML=v
> ```



Can certainly be improved, but at least I think it works: 313

Also improved your ?:?:?:?: term by using & instead of && where possible and q>3 instead of q==4.


----------



## bundat (Oct 1, 2013)

qqwref said:


> Here's a new challenge (for the 3x3x3 only) then:
> - Sim must display all 54 stickers



I think just requiring at least 27 stickers is a more accurate "realistic simulation", since you can only normally see at most 3 sides of a cube simultaneously.
(Although seeing the other sides wouldn't hurt)

Back when I was first learning Java, I made a sim (with almost the same controls as hi-games) that only shows 3 sides, and I could still get solves on it relatively close to my times on a real cube. (if you download it, don't click on the cube/canvas, or the keyboard will stop working... I never did try to figure out why it did that)


----------



## Stefan (Oct 1, 2013)

Renslay said:


> I know a smiliar variant, *yet easier to implement*:
> Cube = 123456789AB1234567000000000000000000
> (In the order of EP-CP-EO-CO, 18 cubies)
> And you can define <U,x,y> with permutations along the numbers, with orientations as additions modulo 3 or modulo 2. So, a scrambled cube for example:
> Scrambled cube = 46432A1B9783254176010010010112102101



How is that easier to implement than basically
onkeyup=function(){innerHTML=i=(i+1)%43252003274489856000}
?


----------



## Renslay (Oct 1, 2013)

Stefan said:


> How is that easier to implement than basically
> onkeyup=function(){innerHTML=i=(i+1)%43252003274489856000}
> ?



I mean easier to implement a single key = single turn (U, R, F and the others). Of course the single key = next cube state is the most easiest implementation.


----------



## Kirjava (Oct 2, 2013)

1512

works in FF and IE (and any non-prefix-requiring browsers)

bonus points for 3D?


----------



## Renslay (Oct 2, 2013)

Kirjava said:


> 1512
> 
> works in FF and IE (and any non-prefix-requiring browsers)
> 
> bonus points for 3D?



Woaaaaah! Now that's a real (user-friendly) sim! Nice work!


----------



## bundat (Oct 2, 2013)

Very nice. I recommend changing FA0 to something like F50 (the current orange and yellow confuses me).


----------



## StachuK1992 (Oct 2, 2013)

That's super cool, Thom.
It's usable, has full controls, and still has a pretty small code base!


----------



## AvGalen (Oct 3, 2013)

Kirjava said:


> 1512
> 
> works in FF and IE (and any non-prefix-requiring browsers)
> 
> bonus points for 3D?



bonus points for being totally awesome!


----------



## Lchu613 (Oct 3, 2013)

Cool, now use a green and an orange that don't confuse my color-blind eyes plz .


----------



## rj (Oct 11, 2013)

Kirjava said:


> 1512
> 
> works in FF and IE (and any non-prefix-requiring browsers)
> 
> bonus points for 3D?



Yikes! You scare me. Now add a timer.


----------



## Kirjava (Oct 11, 2013)

rj said:


> Yikes! You scare me. Now add a timer.



ok


----------



## Kirjava (Jan 23, 2014)

Kirjava said:


> 1512



1297

*chomp*

1275

*chomp*


----------



## Kirjava (Jan 27, 2014)

Kirjava said:


> 1275



1233

om nom nom

getting under 1kb would be nice. anyone got any ideas?


----------



## imvelox (Jul 16, 2015)

molarmanful said:


> Superbump.
> 
> Just looked at this, and improved the 15-puzzle sim from the original down to 414 bytes. http://codepen.io/molarmanful/pen/OVEYEv
> 
> ...



it says 'undefined' after some moves


----------



## molarmanful (Jul 17, 2015)

Okay, I deleted my previous post because I finished redoing the code. But OH MY GOD! I got the 15 puzzle sim down to 372 bytes!

Demo here: http://codepen.io/molarmanful/pen/OVEYEv

I got some inspiration from the 219-byte game of Tron and used their key event handling. Did the customary golfing to minimize repetition, unnecessary code, and stuff like that. Also, I fixed the undefined error and made the puzzle scramble onload and onclick.


```
a=document.getElementById('a');a[w="innerHTML"]='1234\n5678\n9ABC\nDEF ';h=" ";function r(f,g){a[w]=a[w][s='substring'](0,f)+g+a[w][s](f+1)}onkeyup=d=function(e){f=(x=a[w]).indexOf(h);q=e?e.which&3:0|Math.random()*4;y=f+[-5,1,5,-1][q];if((q==2&&f<14)||(!q&&f>4))r(f,x[y]),r(y,h);if(q%2){g=x.charAt(y);if(g!="\n")r(f,g),r(y,h)}},onclick=onload=function(){for(i=99;i--;)d()}
```


----------



## qqwref (Jul 17, 2015)

279 bytes  http://codepen.io/anon/pen/LVBRbX


```
a=document.getElementById('a');a[w="innerHTML"]='1234\n5678\n9ABC\nDEF ';r=(f,g)=>a[w]=a[w].slice(0,f)+g+a[w].slice(f+1);onkeyup=d=e=>{f=(x=a[w]).search(" ");if((z=x[y=f+[-5,1,5,-1][e?e.which&3:0|Math.random()*4]])&&z!='\n')r(f,z),r(y," ")},onclick=onload=e=>{for(i=1e4;i--;)d()}
```

Changes:
- Removed a lot of the complicated logic with q and f and g to check if a move is valid, and simply replaced it with a check for whether x[y] is not undefined and not \n (that is: if(x[y]&&x[y]!='\n'))
- Used z for x[y] and inlined the definition of y
- Got rid of variable q now, since we only use it once
- Got rid of variable h to save 2 bytes
- slice instead of substring, and got rid of variable s
- Changed scramble length to 1e4 (=10000) instead of 99 - it loses a byte but provides scrambles which are much closer to random state
- search instead of indexOf
- (arguments)=>{function body} syntax



EDIT: I updated an old script from this topic: http://jsfiddle.net/pq6VK/9/ The javascript is only 217 (!!) bytes.

```
h=[...' 123\n5674\n9AB8\nDEFC'];y=0;onkeyup=e=>d(e.keyCode%5);d=q=>{if(q==6)for(i=1e4;i--;d(0|Math.random()*5));m=h[y],h[y]=h[f=h[w=y+[-5,,1,5,-1][q]]&&h[w]!='\n'?w:y],h[y=f]=m;document.body.innerHTML=h.join("")};d(6)
```

Tricks include:
- the new function syntax as above
- the valid-move logic as above
- [...string] instead of string.split("")
- starting from a non-solved state (it doesn't matter, we immediately scramble anyway) to save a character on y
- making the entire body a <pre> tag using CSS so I can just use document.body.innerHTML


----------



## molarmanful (Jul 17, 2015)

I decided to keep with the ES5 syntax for all y'all non-FF users:


```
a=document.getElementById('a');a[w="innerHTML"]='1234\n5678\n9ABC\nDEF ';function r(f,g){a[w]=a[w].slice(0,f)+g+a[w].slice(f+1)}onkeyup=d=function(e){f=(x=a[w]).search(" ");if((z=x[y=f+[-5,1,5,-1][e?e.which&3:0|Math.random()*4]])&&z!='\n')r(f,z),r(y," ")},onclick=function(e){for(i=1e4;i--;)d()}
```

295 bytes, which is still a big improvement over 372. And we're sub-300, so thanks qqwref!

One thing I like about the code is the [-5,1,5,-1][e?e.which&3:0 part, which I find pretty cool. I'm using it for sure in other projects when possible.


----------



## qqwref (Jul 17, 2015)

The other one is a working 15 puzzle sim too  Should still be pretty short (240 bytes by my count) even with ES5 syntax:

```
h=' 123\n5674\n9AB8\nDEFC'.split("");y=0;onkeyup=function(e){d(e.keyCode%5)};d=function(q){if(q==6)for(i=1e4;i--;d(0|Math.random()*5));m=h[y],h[y]=h[f=h[w=y+[-5,,1,5,-1][q]]&&h[w]!='\n'?w:y],h[y=f]=m;document.body.innerHTML=h.join("")};d(6)
```

I'm kinda thinking of trying the 3x3x3 sim challenge too... hmmm
EDIT: lol, never mind, I forgot how crazy small it got XD Might still be interesting to see what you can get with a reasonable view and control scheme though...


----------



## Stefan (Jul 17, 2015)

molarmanful said:


> OH MY GOD! I got the 15 puzzle sim down to 372 bytes!



I don't understand the excitement. If I'm not mistaken, [post=906615]we were at least down to 313[/post], including random-state scrambling (that's just the latest posted version, maybe earlier ones were even shorter). Does yours have more/better functionality?


----------



## Stefan (Jul 17, 2015)

qqwref said:


> EDIT: I updated an old script from this topic: http://jsfiddle.net/pq6VK/9/ The javascript is only 217 (!!) bytes.
> 
> ```
> h=[...' 123\n5674\n9AB8\nDEFC'];y=0;onkeyup=e=>d(e.keyCode%5);d=q=>{if(q==6)for(i=1e4;i--;d(0|Math.random()*5));m=h[y],h[y]=h[f=h[w=y+[-5,,1,5,-1][q]]&&h[w]!='\n'?w:y],h[y=f]=m;document.body.innerHTML=h.join("")};d(6)
> ```



I'm not set up to test it myself, but q==6 should be equivalent to q>5, right?

And maybe d(1e4) instead of d(6) and use that to get rid of the if?


----------



## molarmanful (Jul 17, 2015)

Stefan said:


> I don't understand the excitement. If I'm not mistaken, [post=906615]we were at least down to 313[/post], including random-state scrambling (that's just the latest posted version, maybe earlier ones were even shorter). Does yours have more/better functionality?


I didn't read the previous posts, just thought, " this could be better" and edited the code.


ES5 code, with my edits (224 bytes):

```
h=' 123\n5674\n9AB8\nDEFC'.split("");y=0;onkeyup=function(e){d(e.which%5)};function d(q){if(q>5)for(i=1e4;i--;d(0|Math.random()*5));m=h[y],h[y]=h[f=h[w=y+[-5,,1,5,-1][q]]&&h[w]!='\n'?w:y],h[y=f]=m;a.innerHTML=h.join("")}d(6)
```

- Keep pre tag with ID a and call it using a.innerHTML (actually works for some reason).
- Change keyCode to which.
- Change d=function to function d so that we can eliminate the semicolon right after the closing bracket.
- Change q==6 to q>5, as Stefan suggested.

I'm working on improving the variable swap inside function d so we can eliminate variable m.


----------



## qqwref (Jul 17, 2015)

molarmanful said:


> - Change 0|Math.random()*5 to new Date%5. This isn't as great, but it works.


I don't think this is valid. The date just doesn't move fast enough to be used as a random number inside such a tight loop, so you will get a lot of UUUU... moves and it won't be a decent scramble anymore.

Also - and this is a problem with my 217 too - I noticed the script now only provides scrambles when you load the scrambler, which means you can't press a key (e.g. v) to do it anymore. Oh well.

209 bytes ES5 (http://jsfiddle.net/pq6VK/14/):

```
h=' 123\n5674\n9AB8\nDEFC'.split("");y=0;onkeyup=function(e){d(e.which)};function d(q){m=h[y],h[y]=h[f=h[w=y+[-5,1,5,-1][q&3]]&&h[w]!='\n'?w:y],h[y=f]=m;a.innerHTML=h.join("")}for(i=1e4;i--;)d(Math.random()*4)
```
I moved the for loop outside of d, so we don't need to check if(q>5) anymore. And switched back to modulo 4 (and saved two characters by using q&3 inside d, which means we don't have to floor the random number).

(ES6: 187 (http://jsfiddle.net/pq6VK/17/))

```
h=[...' 123\n5674\n9AB8\nDEFC'];y=0;onkeyup=e=>d(e.which);d=q=>{m=h[y],h[y]=h[f=h[w=y+[-5,1,5,-1][q&3]]&&h[w]!='\n'?w:y],h[y=f]=m,a.innerHTML=h.join("")};for(i=1e4;i--;)d(Math.random()*4)
```


----------



## Stefan (Jul 18, 2015)

qqwref said:


> I noticed the script now only provides scrambles when you load the scrambler, which means *you can't press a key (e.g. v) to do it anymore.*



What about the F5 key? 

Save one byte with *h[w]>'\n'* instead of *h[w]!='\n'*.

Edit: 179, by replacing *&&h[w]!=* with *>* and using the loop for *onkeyup=e=>d(e.which)* to save a semicolon.


```
h=[...' 123\n5674\n9AB8\nDEFC'];y=0;d=q=>{m=h[y],h[y]=h[f=h[w=y+[-5,1,5,-1][q&3]]>'\n'?w:y],h[y=f]=m,a.innerHTML=h.join("")};for(i=1e4;i--;onkeyup=e=>d(e.which))d(Math.random()*4)
```


----------



## qqwref (Jul 18, 2015)

Ah, nice  If we're doing that, we can also change >'\n' to >' ' for 178 (since y is the space, the other piece is always 0-9A-F):

```
h=[...' 123\n5674\n9AB8\nDEFC'];y=0;d=q=>{m=h[y],h[y]=h[f=h[w=y+[-5,1,5,-1][q&3]]>' '?w:y],h[y=f]=m,a.innerHTML=h.join("")};for(i=1e4;i--;onkeyup=e=>d(e.which))d(Math.random()*4)
```


----------



## Stefan (Jul 18, 2015)

qqwref said:


> Ah, nice  If we're doing that, we can also change >'\n' to >' ' for 178 (since y is the space, the other piece is always 0-9A-F)



Haha, I only considered *>=' '*. That's what I get for not looking at the big picture 

Edit: Lol. 173.


```
h=[...' 123\n5674\n9AB8\nDEFC'];y=0;d=q=>{h[y]=h[f=h[w=y+[-5,1,5,-1][q&3]]>' '?w:y],h[y=f]=' ',a.innerHTML=h.join("")};for(i=1e4;i--;onkeyup=e=>d(e.which))d(Math.random()*4)
```


----------



## molarmanful (Jul 18, 2015)

Huh, my edits to the previous post didn't save! I saw that the date trick didn't work right after I posted and tried to edit.

ES5 code (down to 196):

```
onkeyup=function(e){d(e.which)};function d(q){h[y]=h[f=h[w=y+[-5,1,5,-1][q&3]=]=>' '?w:y],h[y=f]=' ',a.innerHTML=h.join("")}for(h=' 123\n5674\n9AB8\nDEFC'.split(""),y=0,i=1e4;i--;)d(Math.random()*4)
```

- I guess Stefan figured out how to eliminate m, so kudos to him.
- Put variable initialization in for loop, which did nothing -- but I'll keep it there anyway  .

I think we need to shorten that string defined in variable h. No idea how to do that (or if it's even possible). Anyone have a clue?


----------



## qqwref (Jul 18, 2015)

Stefan said:


> Edit: Lol. 173.
> 
> 
> ```
> ...


Haha, of course... Well, that's what I get for not looking at the big picture


----------



## molarmanful (Dec 15, 2015)

Well, it's been awhile since this happened. Since then, ES6 has obviously progressed much more, along with my code golfing skills.

170 bytes:

```
h=[...` 1235674
9AB8
DEFC`];y=0;d=q=>{h[y]=h[f=h[w=y+[-5,1,5,-1][q&3]=]=>' '?w:y],h[y=f]=' ',a.innerHTML=h.join``};for(i=1e4;i--;onkeyup=e=>d(e.which))d(Math.random()*4)
```
Really easy edits.


----------

