├── .gitignore ├── favicon.ico ├── favicon-16x16.png ├── favicon-32x32.png ├── js ├── README.md ├── keymaps.js ├── controls.js ├── giiker.js ├── cube.js ├── alg.js ├── solve.js └── alg_jison.js ├── LICENSE ├── README.md ├── scramblingNote.html ├── style.css ├── mobile.css ├── controls.html ├── mobile.html ├── 1LLL.html └── index.html /.gitignore: -------------------------------------------------------------------------------- 1 | *.csv 2 | *.pdf 3 | *.xlsx 4 | *.zip 5 | *.py 6 | misc/ 7 | -------------------------------------------------------------------------------- /favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tao-yu/Alg-Trainer/HEAD/favicon.ico -------------------------------------------------------------------------------- /favicon-16x16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tao-yu/Alg-Trainer/HEAD/favicon-16x16.png -------------------------------------------------------------------------------- /favicon-32x32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tao-yu/Alg-Trainer/HEAD/favicon-32x32.png -------------------------------------------------------------------------------- /js/README.md: -------------------------------------------------------------------------------- 1 | | File | From Repository | License | 2 | |-----------------------------------------|--------------------------------------------------------|--------------------| 3 | | cube.js and solve.js | [ldez/cubejs](https://github.com/ldez/cubejs) | MIT | 4 | | alg.js and alg_jison.js (older version) | [cubing/alg.js](https://github.com/cubing/alg.js) | MIT | 5 | | giiker.js (renamed and modified) | [hakatashi/giiker](https://github.com/hakatashi/giiker)| MIT | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017-2021 Tao Yu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [tao-yu.github.io/Alg-Trainer/](https://tao-yu.github.io/Alg-Trainer/) 2 | 3 | Alg Trainer is a website that makes it easy to memorize Rubik's Cube algorithms. 4 | 5 | In July 2017, I used this trainer to memorize full ZBLL (493 algorithms) in only 58 days, a feat which I describe in [this video](https://www.youtube.com/watch?v=5TEtHB5eoZw). 6 | 7 | ## Features 8 | 9 | - [Giiker Smart Rubik's Cube support](https://www.youtube.com/watch?v=2PWErrApqWQ) 10 | - Supports PLL, OLL, F2L, COLL, WV, ZBLL, 2GLL, ZZLL, ZBLS, CLS, TTLL, CMLL, TOLS, CLL, CPEOLL, OLLCP, and much, much more. 11 | - Train algsets by their subsets - for example, you can train the T, U, L, Pi, H, S and AS subsets of ZBLL separately. 12 | - Features a timer for timing your algorithms. 13 | - Real scrambles - scrambles are not just the reverse of the algorithm. It won't be possible to guess the algorithm just by looking at the scramble! 14 | - Pressing spacebar shows you the algorithm you should use. No more searching for algs in pdf documents! 15 | - Virtual cube. Learning algs with a virtual cube saves a ton of time because you don't need to scramble your cube. I used this when I was learning full ZBLL. 16 | - Customizable controls: The default keyboard controls for turning the virtual cube are based on a ergonomic layout designed by Ryan Heise. However it is possible to customize the controls to whatever you want! 17 | - User defined algset - Have a list of algs you want to train? No problem, just paste them into the "User defined algset" box. 18 | -------------------------------------------------------------------------------- /js/keymaps.js: -------------------------------------------------------------------------------- 1 | var defaultKeymaps = [ 2 | [new KeyCombo("KeyI"), "R"], 3 | [new KeyCombo("KeyK") , "R'"], 4 | [new KeyCombo("KeyJ") , "U"], 5 | [new KeyCombo("KeyF") , "U'"], 6 | [new KeyCombo("KeyH") , "F"], 7 | [new KeyCombo("KeyG") , "F'"], 8 | [new KeyCombo("KeyW") , "B"], 9 | [new KeyCombo("KeyO") , "B'"], 10 | [new KeyCombo("KeyD") , "L"], 11 | [new KeyCombo("KeyE") , "L'"], 12 | [new KeyCombo("KeyS") , "D"], 13 | [new KeyCombo("KeyL") , "D'"], 14 | [new KeyCombo("KeyU") , "r"], 15 | [new KeyCombo("KeyM") , "r'"], 16 | [new KeyCombo("KeyV") , "l"], 17 | [new KeyCombo("KeyR") , "l'"], 18 | [new KeyCombo("Quote") , "M"], 19 | [new KeyCombo("Backslash") , "M"], 20 | [new KeyCombo("BracketLeft") , "M'"], 21 | [new KeyCombo("KeyT") , "x"], 22 | [new KeyCombo("KeyN") , "x'"], 23 | [new KeyCombo("Semicolon") , "y"], 24 | [new KeyCombo("KeyP") , "z"], 25 | [new KeyCombo("KeyQ") , "z'"], 26 | [new KeyCombo("KeyA") , "y'"], 27 | [new KeyCombo("KeyH", {"shift": true}), "S"], 28 | [new KeyCombo("KeyG", {"shift": true}), "S'"], 29 | [new KeyCombo("KeyX"), "E"], 30 | [new KeyCombo("Period"), "E'"]]; 31 | 32 | function getKeyMaps() { 33 | if (localStorage.getItem("keymaps") === null) { 34 | localStorage.setItem("keymaps", JSON.stringify(defaultKeymaps)); 35 | } 36 | let km = JSON.parse(localStorage.getItem("keymaps")); 37 | // turn all objects into KeyCombo objects 38 | for (var i = 0; i < km.length; i++) { 39 | let kc = new KeyCombo(""); // ghost object 40 | Object.assign(kc, km[i][0]); 41 | km[i][0] = kc; 42 | } 43 | return km; 44 | } 45 | -------------------------------------------------------------------------------- /js/controls.js: -------------------------------------------------------------------------------- 1 | class KeyCombo { 2 | constructor(code, modifiers={}) { 3 | this.shift = modifiers.shift || false; 4 | this.ctrl = modifiers.ctrl || false; 5 | this.alt = modifiers.alt || false; 6 | 7 | this.code = code; 8 | } 9 | 10 | matches(evt) { 11 | return this.code == evt.code && evt.shiftKey == this.shift && evt.altKey == this.alt && evt.ctrlKey == this.ctrl; 12 | } 13 | 14 | toString() { 15 | let out = this.code.replace("Key", ""); 16 | if (this.shift) { 17 | out = "shift-" + out; 18 | } 19 | if (this.alt) { 20 | out = "alt-" + out; 21 | } 22 | if (this.ctrl) { 23 | out = "ctrl-" + out; 24 | } 25 | return out; 26 | } 27 | } 28 | 29 | function keyEventToKeyCombo(evt, force) { 30 | let code = evt.code; 31 | if (evt.key === "Shift" || evt.key === "Control" || evt.key === "Meta" || evt.key == "Alt") { 32 | if (force) { 33 | code = ""; 34 | } else { 35 | return false; 36 | } 37 | } 38 | return new KeyCombo(code, {"shift": evt.shiftKey, "alt": evt.altKey, "ctrl": evt.ctrlKey}); 39 | } 40 | 41 | class Listener { 42 | constructor() { 43 | let self = this; 44 | this.combos = []; // [[combo, fn]] 45 | document.body.addEventListener("keydown", e => self.keydown(e)); 46 | } 47 | 48 | keydown(e) { 49 | if (e.target !== document.body) { return; } 50 | for (let [combo, fn] of this.combos) { 51 | if (combo.matches(e)) { 52 | fn(e); 53 | e.preventDefault(); 54 | return true; 55 | } 56 | } 57 | return false; 58 | } 59 | 60 | register(combo, action) { 61 | this.combos.push([combo, action]); 62 | } 63 | 64 | reset() { 65 | this.combos = []; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /scramblingNote.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Algorithm trainer 7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | If one has the following preorientation settings:


16 | 17 | Starting from WCA orientation:
18 | First do one 19 | 20 | rotation
21 | 22 | then do a random number of 23 | 24 | rotations,
25 | 26 | and finally do a random number of 27 | 28 | rotations.
29 |

30 | 31 | In order to ensure that the orientation of one's scrambled cube matches that of the one displayed on screen to the right of the trainer, one should apply the rotation from the first box, z2 in this case, to their cube, starting from default orientation (that is, white top, green front, or the custom color scheme being used), before applying the scramble. 32 | 33 |

34 | If one uses a cube with the standard western colour scheme, our example here simply means starting every scramble with yellow top, green front, instead of white top, green front. Thus, the first box allows you to define a different default orientation. For example, if you are a CFOP solver who only solves cross on white, putting a z2 in the first box, and leaving other boxes empty, ensures that you will get the correct scramble orientations by ensuring your cube is in the yellow top, green front orientation before every scramble. 35 | 36 |

37 | 38 | The second and third boxes let you define your type of colour neutrality. In this case, a random number of y rotations and a random number of x2 rotations will be applied before each scramble. For Roux solvers, for example, this corresponds to x2/y colour neutrality. 39 | 40 |

41 | 42 | For many algsets, applying rotations before the alg does not significantly affect the difficulty of recognition. As such, it is often enough to leave the last two boxes boxes blank and simply train the algs from one orientation. The random orientation is perhaps more useful for virtual cube training, where all matters of preorientation are handled automatically. 43 | 44 | 45 | 46 |
47 | 48 | -------------------------------------------------------------------------------- /style.css: -------------------------------------------------------------------------------- 1 | body { 2 | background: black; 3 | font-family: Arial; 4 | color: white; 5 | } 6 | 7 | p { 8 | font-family: Arial; 9 | font-size: 20px; 10 | color: white; 11 | } 12 | 13 | hr { 14 | display: block; 15 | margin-top: 0.5em; 16 | margin-bottom: 0.5em; 17 | margin-left: auto; 18 | margin-right: auto; 19 | border-style: inset; 20 | border-width: 0.5px; 21 | border-color:#303030; 22 | } 23 | 24 | #timer { 25 | font-family: Arial; 26 | font-size: 30px; 27 | color: white; 28 | } 29 | 30 | #statistics { 31 | font-size: 20px; 32 | } 33 | 34 | p.scramble { 35 | vertical-align: middle; 36 | text-align: center; 37 | } 38 | 39 | 40 | table { 41 | border-collapse: collapse; 42 | } 43 | 44 | th, td, tr{ 45 | border: none; 46 | } 47 | 48 | .column_left { 49 | float: left; 50 | width: 30%; 51 | } 52 | 53 | .column_middle { 54 | text-align: center; 55 | float: left; 56 | width: 40%; 57 | } 58 | 59 | 60 | .column_right { 61 | text-align: center; 62 | float: left; 63 | width: 30%; 64 | } 65 | 66 | .algsetpicker{ 67 | width:70%; 68 | } 69 | 70 | a:link { 71 | color: cornflowerblue 72 | } 73 | 74 | a:visited { 75 | color: darkslateblue 76 | } 77 | 78 | 79 | #loader { 80 | position: absolute; 81 | left: 50%; 82 | top: 50%; 83 | z-index: 1; 84 | width: 150px; 85 | height: 150px; 86 | margin: -75px 0 0 -75px; 87 | border: 16px solid #f3f3f3; 88 | border-radius: 50%; 89 | border-top: 16px solid white; 90 | border-right: 16px solid red; 91 | border-bottom: 16px solid green; 92 | border-left: 16px solid orange; 93 | width: 120px; 94 | height: 120px; 95 | -webkit-animation: spin 2s linear infinite; 96 | animation: spin 2s linear infinite; 97 | } 98 | 99 | @-webkit-keyframes spin { 100 | 0% { -webkit-transform: rotate(0deg); } 101 | 100% { -webkit-transform: rotate(360deg); } 102 | } 103 | 104 | @keyframes spin { 105 | 0% { transform: rotate(0deg); } 106 | 100% { transform: rotate(360deg); } 107 | } 108 | 109 | /* Add animation to "page content" */ 110 | .animate-bottom { 111 | position: relative; 112 | -webkit-animation-name: animatebottom; 113 | -webkit-animation-duration: 1s; 114 | animation-name: animatebottom; 115 | animation-duration: 1s 116 | } 117 | 118 | @-webkit-keyframes animatebottom { 119 | from { bottom:-100px; opacity:0 } 120 | to { bottom:0px; opacity:1 } 121 | } 122 | 123 | @keyframes animatebottom { 124 | from{ bottom:-100px; opacity:0 } 125 | to{ bottom:0; opacity:1 } 126 | } 127 | 128 | #page { 129 | display: none; 130 | 131 | } 132 | 133 | #controls { 134 | position: fixed; 135 | left: 50%; 136 | bottom: 0; 137 | transform: translate(-50%, 0); 138 | margin: 0 auto; 139 | } 140 | 141 | #controls > button { 142 | background-color: grey; 143 | border: none; 144 | color: white; 145 | padding: 15px 32px; 146 | text-align: center; 147 | text-decoration: none; 148 | display: inline-block; 149 | font-size: 16px; 150 | } 151 | 152 | #visual-cube-container { 153 | display: flex; 154 | flex-direction: column; 155 | height: 300px; 156 | width: 100%; 157 | background: black; 158 | align-items: center; 159 | } 160 | -------------------------------------------------------------------------------- /mobile.css: -------------------------------------------------------------------------------- 1 | html { 2 | height: 100%; 3 | 4 | overflow: hidden; 5 | } 6 | 7 | body { 8 | 9 | font-family: Arial; 10 | background: black; 11 | min-height: 100vh; 12 | overflow: hidden; 13 | display: flex; 14 | margin: 3px; 15 | } 16 | 17 | p { 18 | font-family: Arial; 19 | font-size: 20px; 20 | color: white; 21 | } 22 | 23 | hr { 24 | display: block; 25 | margin-top: 0.5em; 26 | margin-bottom: 0.5em; 27 | margin-left: auto; 28 | margin-right: auto; 29 | border-style: inset; 30 | border-width: 0.5px; 31 | border-color: #303030; 32 | width: 100%; 33 | } 34 | 35 | #timer { 36 | font-family: Arial; 37 | font-size: 30px; 38 | color: white; 39 | } 40 | 41 | #statistics { 42 | font-size: 20px; 43 | } 44 | 45 | #flexbox { 46 | display: flex; 47 | flex-direction: column; 48 | min-height: 100vh 49 | } 50 | 51 | p.scramble { 52 | height: 10%; 53 | vertical-align: middle; 54 | text-align: center; 55 | } 56 | 57 | table { 58 | border-collapse: collapse; 59 | } 60 | 61 | th, 62 | td, 63 | tr { 64 | border: none; 65 | } 66 | 67 | #touchStartArea { 68 | margin: 0; 69 | padding: 0; 70 | width: 100%; 71 | height: 90%; 72 | flex: 1; 73 | 74 | } 75 | 76 | #page { 77 | 78 | margin: 0; 79 | padding: 0; 80 | 81 | display: none; 82 | height: 100%; 83 | width: 100%; 84 | min-height: 100vh; 85 | 86 | } 87 | 88 | #left_popup { 89 | display: none; 90 | position: fixed; 91 | top: 10%; 92 | left: 5%; 93 | right: 5%; 94 | width: 90%; 95 | height: 80vh; 96 | z-index: 9999; 97 | background-color: rgb(90, 90, 90); 98 | } 99 | 100 | #right_popup { 101 | display: none; 102 | position: fixed; 103 | top: 10%; 104 | left: 5%; 105 | right: 5%; 106 | width: 90%; 107 | height: 80vh; 108 | z-index: 9999; 109 | background-color: rgb(90, 90, 90); 110 | } 111 | 112 | .column_left { 113 | position: absolute; 114 | height: 70vh; 115 | overflow: auto; 116 | padding: 20px; 117 | background-color: rgb(90, 90, 90); 118 | color: white; 119 | font-size: 18px; 120 | } 121 | 122 | .column_right { 123 | position: absolute; 124 | height: 70vh; 125 | overflow: auto; 126 | padding: 20px; 127 | background-color: rgb(90, 90, 90); 128 | color: white; 129 | font-size: 18px; 130 | } 131 | 132 | .column_middle { 133 | text-align: center; 134 | width: 100%; 135 | height: 100%; 136 | } 137 | 138 | 139 | 140 | .algsetpicker { 141 | width: 70%; 142 | } 143 | 144 | a:link { 145 | color: cornflowerblue 146 | } 147 | 148 | a:visited { 149 | color: darkslateblue 150 | } 151 | 152 | 153 | #loader { 154 | position: absolute; 155 | left: 50%; 156 | top: 50%; 157 | z-index: 1; 158 | margin: -75px 0 0 -75px; 159 | border: 16px solid #f3f3f3; 160 | border-radius: 50%; 161 | border-top: 16px solid white; 162 | border-right: 16px solid red; 163 | border-bottom: 16px solid green; 164 | border-left: 16px solid orange; 165 | width: 120px; 166 | height: 120px; 167 | -webkit-animation: spin 2s linear infinite; 168 | animation: spin 2s linear infinite; 169 | } 170 | 171 | @-webkit-keyframes spin { 172 | 0% { 173 | -webkit-transform: rotate(0deg); 174 | } 175 | 176 | 100% { 177 | -webkit-transform: rotate(360deg); 178 | } 179 | } 180 | 181 | @keyframes spin { 182 | 0% { 183 | transform: rotate(0deg); 184 | } 185 | 186 | 100% { 187 | transform: rotate(360deg); 188 | } 189 | } 190 | 191 | /* Add animation to "page content" */ 192 | /* 193 | .animate-bottom { 194 | position: relative; 195 | -webkit-animation-name: animatebottom; 196 | -webkit-animation-duration: 1s; 197 | animation-name: animatebottom; 198 | animation-duration: 1s 199 | } 200 | 201 | @-webkit-keyframes animatebottom { 202 | from { 203 | bottom: -100px; 204 | opacity: 0 205 | } 206 | 207 | to { 208 | bottom: 0px; 209 | opacity: 1 210 | } 211 | } 212 | 213 | @keyframes animatebottom { 214 | from { 215 | bottom: -100px; 216 | opacity: 0 217 | } 218 | 219 | to { 220 | bottom: 0; 221 | opacity: 1 222 | } 223 | }*/ 224 | 225 | 226 | 227 | .big_button { 228 | background-color: grey; 229 | border: none; 230 | color: white; 231 | margin: 3px; 232 | font-size: 18px; 233 | } 234 | 235 | .arrow_button { 236 | flex: 1; 237 | padding: 10px; 238 | } 239 | 240 | 241 | .on_screen_controls { 242 | flex: 0.05; 243 | display: flex; 244 | 245 | } 246 | 247 | .sidebar_buttons { 248 | flex: 0.025; 249 | display: flex; 250 | } 251 | 252 | .sidebar_button { 253 | flex: 1; 254 | margin: 3px; 255 | background-color: grey; 256 | color: white; 257 | border: none; 258 | font-size: 18px; 259 | } 260 | 261 | .flex-item { 262 | flex: 2; 263 | padding: 10px; 264 | } 265 | 266 | #visual-cube-container { 267 | display: flex; 268 | flex-direction: column; 269 | height: 300px; 270 | width: 100%; 271 | background: rgb(90, 90, 90); 272 | align-items: center; 273 | } -------------------------------------------------------------------------------- /controls.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Algorithm trainer 6 | 7 | 8 | 9 | 10 | 11 | 34 | 35 | 36 |

Control Config

37 | 38 |

39 | Click a key binding to change it! The "Move" column specifies the move(s) to make upon that 40 | key combo. 41 |

42 | 43 |
44 | 45 | 46 |
47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 |
KeyMove
57 |
58 | 59 |
60 |
61 | 62 | 63 | 64 | 65 | 66 | 193 | 194 | -------------------------------------------------------------------------------- /mobile.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Alg Trainer Mobile (beta) 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 |
18 |
19 | 20 | 22 |
23 |
Leave all checkboxes unchecked to test on the full algset. 24 | 25 |
26 | 27 |

Settings

28 |
29 | 30 | Use Virtual Cube
31 | Hide timer
32 | Show Scramble
33 | Use Real Scrambles
34 | Random AUF
35 | Scramble subsequent steps
36 | Go in order
37 | Don't show alg after stopping timer
38 | Mirror across M Randomize
39 | Mirror across S Randomize
40 | Auto-fix algorithm rotation
41 | Line style
48 | Virtual cube size:

52 |
53 | 54 | 55 | Use user defined algset
57 | 60 |
61 | 62 | Preorientation:
63 | 64 | Full colour neutrality
65 | or
66 | Starting from WCA orientation:
67 | First do one 68 | 70 | rotation
71 | 72 | then do a random number of 73 | 75 | rotations,
76 | 77 | and finally do a random number of 78 | 80 | rotations.
81 |
82 | Note on preorientation settings 83 |

84 | 85 | 86 |
89 | Enter colour names, or RGB values as #RRGGBB.
90 | 91 | 92 | 93 | 96 | 97 | 98 | 99 | 102 | 103 | 104 | 105 | 108 | 109 | 110 | 111 | 114 | 115 | 116 | 117 | 120 | 121 | 122 | 123 | 126 | 127 |
95 |
101 |
107 |
113 |
119 |
125 |
128 | 129 | 130 |
131 | 132 | 133 | 134 |
135 |
136 | 137 |
138 | 139 |
140 |
141 | 142 |
143 |

Click the above cube to switch between flat, 3D view and hidden

144 | 145 |
146 |

 

147 |
148 |

 

149 | 150 | 151 |
152 | 153 | 154 |

155 | 156 |
157 |

Instructions

158 | Enter/Tab/Space: New scramble.
159 | Space: Stop timer and show algorithm.
160 | Left and right arrow keys: View previously tested algs.
161 | Backspace: Show algorithm without starting timer. 162 | 163 |

Virtual cube controls

164 | Space: Stop timer, show algorithm and reset case.
165 | Timer will automatically stop if the cube is solved.
166 | Esc: Reset case without showing algorithm.
167 |

View/Edit Keyboard Controls here.

168 | 169 |
170 | 171 |
172 | 173 | 174 |
175 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | -------------------------------------------------------------------------------- /1LLL.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Full 1LLL Trainer 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Alg Trainer 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | -------------------------------------------------------------------------------- /js/giiker.js: -------------------------------------------------------------------------------- 1 | const SERVICE_UUID = '0000aadb-0000-1000-8000-00805f9b34fb'; 2 | const CHARACTERISTIC_UUID = '0000aadc-0000-1000-8000-00805f9b34fb'; 3 | 4 | const SYSTEM_SERVICE_UUID = '0000aaaa-0000-1000-8000-00805f9b34fb'; 5 | const SYSTEM_READ_UUID = '0000aaab-0000-1000-8000-00805f9b34fb'; 6 | const SYSTEM_WRITE_UUID = '0000aaac-0000-1000-8000-00805f9b34fb'; 7 | 8 | // face indices 9 | const B = 0; 10 | const D = 1; 11 | const L = 2; 12 | const U = 3; 13 | const R = 4; 14 | const F = 5; 15 | 16 | const faces = ['B', 'D', 'L', 'U', 'R', 'F']; 17 | 18 | // color indices 19 | const b = 0; 20 | const y = 1; 21 | const o = 2; 22 | const w = 3; 23 | const r = 4; 24 | const g = 5; 25 | 26 | const colors = ['blue', 'yellow', 'orange', 'white', 'red', 'green']; 27 | 28 | const turns = { 29 | 0: 1, 30 | 1: 2, 31 | 2: -1, 32 | 8: -2, 33 | }; 34 | 35 | const cornerColors = [ 36 | [y, r, g], 37 | [r, w, g], 38 | [w, o, g], 39 | [o, y, g], 40 | [r, y, b], 41 | [w, r, b], 42 | [o, w, b], 43 | [y, o, b] 44 | ]; 45 | 46 | const cornerLocations = [ 47 | [D, R, F], 48 | [R, U, F], 49 | [U, L, F], 50 | [L, D, F], 51 | [R, D, B], 52 | [U, R, B], 53 | [L, U, B], 54 | [D, L, B] 55 | ]; 56 | 57 | const edgeLocations = [ 58 | [F, D], 59 | [F, R], 60 | [F, U], 61 | [F, L], 62 | [D, R], 63 | [U, R], 64 | [U, L], 65 | [D, L], 66 | [B, D], 67 | [B, R], 68 | [B, U], 69 | [B, L] 70 | ]; 71 | 72 | const edgeColors = [ 73 | [g, y], 74 | [g, r], 75 | [g, w], 76 | [g, o], 77 | [y, r], 78 | [w, r], 79 | [w, o], 80 | [y, o], 81 | [b, y], 82 | [b, r], 83 | [b, w], 84 | [b, o] 85 | ]; 86 | 87 | class EventEmitter { 88 | constructor() { 89 | this.listeners = {}; 90 | } 91 | 92 | on(label, callback) { 93 | if (!this.listeners[label]) { 94 | this.listeners[label] = []; 95 | } 96 | this.listeners[label].push(callback); 97 | } 98 | 99 | off(label, callback) { 100 | let listeners = this.listeners[label]; 101 | 102 | if (listeners && listeners.length > 0) { 103 | let index = listeners.indexOf(callback); 104 | if (index > -1) { 105 | listeners.splice(index, 1); 106 | this.listeners[label] = listeners; 107 | return true; 108 | } 109 | } 110 | return false; 111 | } 112 | 113 | emit(label, ...args) { 114 | let listeners = this.listeners[label]; 115 | 116 | if (listeners && listeners.length > 0) { 117 | listeners.forEach((listener) => { 118 | listener(...args); 119 | }); 120 | return true; 121 | } 122 | return false; 123 | } 124 | } 125 | 126 | class Giiker extends EventEmitter { 127 | constructor() { 128 | super(); 129 | this._onCharacteristicValueChanged = this._onCharacteristicValueChanged.bind(this); 130 | this._onDisconnected = this._onDisconnected.bind(this); 131 | } 132 | 133 | async connect() { 134 | if (!window.navigator) { 135 | throw new Error('window.navigator is not accesible. Maybe you\'re running Node.js?'); 136 | } 137 | 138 | if (!window.navigator.bluetooth) { 139 | throw new Error('Web Bluetooth API is not accesible'); 140 | } 141 | 142 | const device = await window.navigator.bluetooth.requestDevice({ 143 | filters: [{ 144 | namePrefix: 'Gi', 145 | }], 146 | optionalServices: [SERVICE_UUID, SYSTEM_SERVICE_UUID], 147 | }); 148 | 149 | const server = await device.gatt.connect(); 150 | const service = await server.getPrimaryService(SERVICE_UUID); 151 | const characteristic = await service.getCharacteristic(CHARACTERISTIC_UUID); 152 | await characteristic.startNotifications(); 153 | const value = await characteristic.readValue(); 154 | this._state = this._parseCubeValue(value).state; 155 | characteristic.addEventListener('characteristicvaluechanged', this._onCharacteristicValueChanged); 156 | 157 | this._systemService = await server.getPrimaryService(SYSTEM_SERVICE_UUID); 158 | 159 | device.addEventListener('gattserverdisconnected', this._onDisconnected); 160 | 161 | this._device = device; 162 | } 163 | 164 | /** 165 | * Disconnects from the GiiKER cube. Will fire the `disconnected` event once done. 166 | */ 167 | disconnect() { 168 | if (!this._device) { 169 | return; 170 | } 171 | this._device.gatt.disconnect(); 172 | } 173 | 174 | _onDisconnected() { 175 | this._device = null; 176 | this.emit('disconnected'); 177 | } 178 | 179 | /** 180 | * Returns a promise that will resolve to the battery level 181 | */ 182 | async getBatteryLevel () { 183 | const readCharacteristic = await this._systemService.getCharacteristic(SYSTEM_READ_UUID); 184 | const writeCharacteristic = await this._systemService.getCharacteristic(SYSTEM_WRITE_UUID); 185 | await readCharacteristic.startNotifications(); 186 | const data = new Uint8Array([0xb5]).buffer; 187 | writeCharacteristic.writeValue(data); 188 | 189 | return new Promise((resolve) => { 190 | const listener = (event) => { 191 | const value = event.target.value; 192 | readCharacteristic.removeEventListener('characteristicvaluechanged', listener); 193 | readCharacteristic.stopNotifications(); 194 | resolve(value.getUint8(1)); 195 | }; 196 | readCharacteristic.addEventListener('characteristicvaluechanged', listener); 197 | }); 198 | } 199 | 200 | /** 201 | * Returns the current state of the cube as arrays of corners and edges. 202 | * 203 | * Example how to interpret the state: 204 | * 205 | * Corner: 206 | * ``` 207 | * { 208 | * position: ['D', 'R', 'F'], 209 | * colors: ['yellow', 'red', 'green'] 210 | * } 211 | * ``` 212 | * The corner in position DRF has the colors yellow on D, red on R and green ON F. 213 | * 214 | * Edge: 215 | * ``` 216 | * { 217 | * position: ['F', 'U'], 218 | * colors: ['green', 'white'] 219 | * } 220 | * ``` 221 | * The edge in position FU has the colors green on F and white on U. 222 | */ 223 | get state() { 224 | const state = { 225 | corners: [], 226 | edges: [] 227 | }; 228 | this._state.cornerPositions.forEach((cp, index) => { 229 | const mappedColors = this._mapCornerColors( 230 | cornerColors[cp - 1], 231 | this._state.cornerOrientations[index], 232 | index 233 | ); 234 | state.corners.push({ 235 | position: cornerLocations[index].map((f) => faces[f]), 236 | colors: mappedColors.map((c) => colors[c]) 237 | }); 238 | }); 239 | this._state.edgePositions.forEach((ep, index) => { 240 | const mappedColors = this._mapEdgeColors( 241 | edgeColors[ep - 1], 242 | this._state.edgeOrientations[index] 243 | ); 244 | state.edges.push({ 245 | position: edgeLocations[index].map((f) => faces[f]), 246 | colors: mappedColors.map((c) => colors[c]) 247 | }); 248 | }); 249 | return state; 250 | } 251 | 252 | /** 253 | * Returns the current state of the cube as a string compatible with cubejs. 254 | * 255 | * See https://github.com/ldez/cubejs#cubefromstringstr 256 | */ 257 | get stateString() { 258 | const cornerFaceIndices = [ 259 | [29, 15, 26], 260 | [9, 8, 20], 261 | [6, 38, 18], 262 | [44, 27, 24], 263 | [17, 35, 51], 264 | [2, 11, 45], 265 | [36, 0, 47], 266 | [33, 42, 53] 267 | ]; 268 | 269 | const edgeFaceIndices = [ 270 | [25, 28], 271 | [23, 12], 272 | [19, 7], 273 | [21, 41], 274 | [32, 16], 275 | [5, 10], 276 | [3, 37], 277 | [30, 43], 278 | [52, 34], 279 | [48, 14], 280 | [46, 1], 281 | [50, 39] 282 | ]; 283 | 284 | const colorFaceMapping = { 285 | blue: 'B', 286 | yellow: 'D', 287 | orange: 'L', 288 | white: 'U', 289 | red: 'R', 290 | green: 'F' 291 | }; 292 | 293 | const state = this.state; 294 | const faces = []; 295 | 296 | state.corners.forEach((corner, cornerIndex) => { 297 | corner.position.forEach((face, faceIndex) => { 298 | faces[cornerFaceIndices[cornerIndex][faceIndex]] = colorFaceMapping[corner.colors[faceIndex]]; 299 | }); 300 | }); 301 | 302 | state.edges.forEach((edge, edgeIndex) => { 303 | edge.position.forEach((face, faceIndex) => { 304 | faces[edgeFaceIndices[edgeIndex][faceIndex]] = colorFaceMapping[edge.colors[faceIndex]]; 305 | }); 306 | }); 307 | 308 | faces[4] = 'U'; 309 | faces[13] = 'R'; 310 | faces[22] = 'F'; 311 | faces[31] = 'D'; 312 | faces[40] = 'L'; 313 | faces[49] = 'B'; 314 | 315 | return faces.join(''); 316 | } 317 | 318 | _onCharacteristicValueChanged(event) { 319 | const value = event.target.value; 320 | const {state, moves} = this._parseCubeValue(value); 321 | this._state = state; 322 | this.emit('move', moves[0]); 323 | } 324 | 325 | _parseCubeValue (value) { 326 | const state = { 327 | cornerPositions: [], 328 | cornerOrientations: [], 329 | edgePositions: [], 330 | edgeOrientations: [] 331 | }; 332 | const moves = []; 333 | if (value.getUint8(18) == 0xa7) { // decrypt 334 | var key = [176, 81, 104, 224, 86, 137, 237, 119, 38, 26, 193, 161, 210, 126, 150, 81, 93, 13, 236, 249, 89, 235, 88, 24, 113, 81, 214, 131, 130, 199, 2, 169, 39, 165, 171, 41]; 335 | var k = value.getUint8(19); 336 | var k1 = k >> 4 & 0xf; 337 | var k2 = k & 0xf; 338 | for (let i = 0; i < value.byteLength; i++) { 339 | const move = (value.getUint8(i) + key[i + k1] + key[i + k2]) & 0xff; 340 | const highNibble = move >> 4; 341 | const lowNibble = move & 0b1111; 342 | if (i < 4) { 343 | state.cornerPositions.push(highNibble, lowNibble); 344 | } else if (i < 8) { 345 | state.cornerOrientations.push(highNibble, lowNibble); 346 | } else if (i < 14) { 347 | state.edgePositions.push(highNibble, lowNibble); 348 | } else if (i < 16) { 349 | state.edgeOrientations.push(!!(move & 0b10000000)); 350 | state.edgeOrientations.push(!!(move & 0b01000000)); 351 | state.edgeOrientations.push(!!(move & 0b00100000)); 352 | state.edgeOrientations.push(!!(move & 0b00010000)); 353 | if (i === 14) { 354 | state.edgeOrientations.push(!!(move & 0b00001000)); 355 | state.edgeOrientations.push(!!(move & 0b00000100)); 356 | state.edgeOrientations.push(!!(move & 0b00000010)); 357 | state.edgeOrientations.push(!!(move & 0b00000001)); 358 | } 359 | } else { 360 | moves.push(this._parseMove(highNibble, lowNibble)); 361 | } 362 | } 363 | } 364 | else { // not encrypted 365 | for (let i = 0; i < value.byteLength; i++) { 366 | const move = value.getUint8(i) 367 | const highNibble = move >> 4; 368 | const lowNibble = move & 0b1111; 369 | if (i < 4) { 370 | state.cornerPositions.push(highNibble, lowNibble); 371 | } else if (i < 8) { 372 | state.cornerOrientations.push(highNibble, lowNibble); 373 | } else if (i < 14) { 374 | state.edgePositions.push(highNibble, lowNibble); 375 | } else if (i < 16) { 376 | state.edgeOrientations.push(!!(move & 0b10000000)); 377 | state.edgeOrientations.push(!!(move & 0b01000000)); 378 | state.edgeOrientations.push(!!(move & 0b00100000)); 379 | state.edgeOrientations.push(!!(move & 0b00010000)); 380 | if (i === 14) { 381 | state.edgeOrientations.push(!!(move & 0b00001000)); 382 | state.edgeOrientations.push(!!(move & 0b00000100)); 383 | state.edgeOrientations.push(!!(move & 0b00000010)); 384 | state.edgeOrientations.push(!!(move & 0b00000001)); 385 | } 386 | } else { 387 | moves.push(this._parseMove(highNibble, lowNibble)); 388 | } 389 | } 390 | } 391 | 392 | return {state, moves}; 393 | } 394 | 395 | _parseMove (faceIndex, turnIndex) { 396 | const face = faces[faceIndex - 1]; 397 | const amount = turns[turnIndex - 1]; 398 | let notation = face; 399 | 400 | switch (amount) { 401 | case 2: notation = `${face}2`; break; 402 | case -1: notation = `${face}'`; break; 403 | case -2: notation = `${face}2'`; break; 404 | } 405 | 406 | return {face, amount, notation}; 407 | } 408 | 409 | _mapCornerColors(colors, orientation, position) { 410 | const actualColors = []; 411 | 412 | if (orientation !== 3) { 413 | if (position === 0 || position === 2 || position === 5 || position === 7) { 414 | orientation = 3 - orientation; 415 | } 416 | } 417 | 418 | switch (orientation) { 419 | case 1: 420 | actualColors[0] = colors[1]; 421 | actualColors[1] = colors[2]; 422 | actualColors[2] = colors[0]; 423 | break; 424 | case 2: 425 | actualColors[0] = colors[2]; 426 | actualColors[1] = colors[0]; 427 | actualColors[2] = colors[1]; 428 | break; 429 | case 3: 430 | actualColors[0] = colors[0]; 431 | actualColors[1] = colors[1]; 432 | actualColors[2] = colors[2]; 433 | break; 434 | } 435 | 436 | return actualColors; 437 | } 438 | 439 | _mapEdgeColors (colors, orientation) { 440 | const actualColors = [...colors]; 441 | if (orientation) { 442 | actualColors.reverse(); 443 | } 444 | return actualColors; 445 | } 446 | } 447 | 448 | const connect = async () => { 449 | const giiker = new Giiker(); 450 | await giiker.connect(); 451 | return giiker; 452 | }; 453 | -------------------------------------------------------------------------------- /js/cube.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright (c) 2013-2017 Petri Lehtinen 4 | Copyright (c) 2018 Ludovic Fernandez 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | 26 | 27 | (function() { 28 | var BL, BR, Cube, DB, DBL, DF, DFR, DL, DLF, DR, DRB, FL, FR, UB, UBR, UF, UFL, UL, ULB, UR, URF, cornerColor, cornerFacelet, edgeColor, edgeFacelet, ref, ref1, ref2; 29 | 30 | ref = [0, 1, 2, 3, 4, 5, 6, 7], URF = ref[0], UFL = ref[1], ULB = ref[2], UBR = ref[3], DFR = ref[4], DLF = ref[5], DBL = ref[6], DRB = ref[7]; 31 | 32 | ref1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], UR = ref1[0], UF = ref1[1], UL = ref1[2], UB = ref1[3], DR = ref1[4], DF = ref1[5], DL = ref1[6], DB = ref1[7], FR = ref1[8], FL = ref1[9], BL = ref1[10], BR = ref1[11]; 33 | 34 | ref2 = (function() { 35 | var B, D, F, L, R, U; 36 | U = function(x) { 37 | return x - 1; 38 | }; 39 | R = function(x) { 40 | return U(9) + x; 41 | }; 42 | F = function(x) { 43 | return R(9) + x; 44 | }; 45 | D = function(x) { 46 | return F(9) + x; 47 | }; 48 | L = function(x) { 49 | return D(9) + x; 50 | }; 51 | B = function(x) { 52 | return L(9) + x; 53 | }; 54 | return [[[U(9), R(1), F(3)], [U(7), F(1), L(3)], [U(1), L(1), B(3)], [U(3), B(1), R(3)], [D(3), F(9), R(7)], [D(1), L(9), F(7)], [D(7), B(9), L(7)], [D(9), R(9), B(7)]], [[U(6), R(2)], [U(8), F(2)], [U(4), L(2)], [U(2), B(2)], [D(6), R(8)], [D(2), F(8)], [D(4), L(8)], [D(8), B(8)], [F(6), R(4)], [F(4), L(6)], [B(6), L(4)], [B(4), R(6)]]]; 55 | })(), cornerFacelet = ref2[0], edgeFacelet = ref2[1]; 56 | 57 | cornerColor = [['U', 'R', 'F'], ['U', 'F', 'L'], ['U', 'L', 'B'], ['U', 'B', 'R'], ['D', 'F', 'R'], ['D', 'L', 'F'], ['D', 'B', 'L'], ['D', 'R', 'B']]; 58 | 59 | edgeColor = [['U', 'R'], ['U', 'F'], ['U', 'L'], ['U', 'B'], ['D', 'R'], ['D', 'F'], ['D', 'L'], ['D', 'B'], ['F', 'R'], ['F', 'L'], ['B', 'L'], ['B', 'R']]; 60 | 61 | Cube = (function() { 62 | var faceNames, faceNums, parseAlg; 63 | 64 | function Cube(other) { 65 | var x; 66 | if (other != null) { 67 | this.init(other); 68 | } else { 69 | this.identity(); 70 | } 71 | this.newCp = (function() { 72 | var k, results; 73 | results = []; 74 | for (x = k = 0; k <= 7; x = ++k) { 75 | results.push(0); 76 | } 77 | return results; 78 | })(); 79 | this.newEp = (function() { 80 | var k, results; 81 | results = []; 82 | for (x = k = 0; k <= 11; x = ++k) { 83 | results.push(0); 84 | } 85 | return results; 86 | })(); 87 | this.newCo = (function() { 88 | var k, results; 89 | results = []; 90 | for (x = k = 0; k <= 7; x = ++k) { 91 | results.push(0); 92 | } 93 | return results; 94 | })(); 95 | this.newEo = (function() { 96 | var k, results; 97 | results = []; 98 | for (x = k = 0; k <= 11; x = ++k) { 99 | results.push(0); 100 | } 101 | return results; 102 | })(); 103 | } 104 | 105 | Cube.prototype.init = function(state) { 106 | this.co = state.co.slice(0); 107 | this.ep = state.ep.slice(0); 108 | this.cp = state.cp.slice(0); 109 | return this.eo = state.eo.slice(0); 110 | }; 111 | 112 | Cube.prototype.identity = function() { 113 | var x; 114 | this.cp = [0, 1, 2, 3, 4, 5, 6, 7]; 115 | this.co = (function() { 116 | var k, results; 117 | results = []; 118 | for (x = k = 0; k <= 7; x = ++k) { 119 | results.push(0); 120 | } 121 | return results; 122 | })(); 123 | this.ep = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]; 124 | return this.eo = (function() { 125 | var k, results; 126 | results = []; 127 | for (x = k = 0; k <= 11; x = ++k) { 128 | results.push(0); 129 | } 130 | return results; 131 | })(); 132 | }; 133 | 134 | Cube.prototype.toJSON = function() { 135 | return { 136 | cp: this.cp, 137 | co: this.co, 138 | ep: this.ep, 139 | eo: this.eo 140 | }; 141 | }; 142 | 143 | Cube.prototype.asString = function() { 144 | var c, corner, edge, i, k, l, len, m, n, o, ori, p, ref3, ref4, result; 145 | result = []; 146 | ref3 = [[4, 'U'], [13, 'R'], [22, 'F'], [31, 'D'], [40, 'L'], [49, 'B']]; 147 | for (k = 0, len = ref3.length; k < len; k++) { 148 | ref4 = ref3[k], i = ref4[0], c = ref4[1]; 149 | result[i] = c; 150 | } 151 | for (i = l = 0; l <= 7; i = ++l) { 152 | corner = this.cp[i]; 153 | ori = this.co[i]; 154 | for (n = m = 0; m <= 2; n = ++m) { 155 | result[cornerFacelet[i][(n + ori) % 3]] = cornerColor[corner][n]; 156 | } 157 | } 158 | for (i = o = 0; o <= 11; i = ++o) { 159 | edge = this.ep[i]; 160 | ori = this.eo[i]; 161 | for (n = p = 0; p <= 1; n = ++p) { 162 | result[edgeFacelet[i][(n + ori) % 2]] = edgeColor[edge][n]; 163 | } 164 | } 165 | return result.join(''); 166 | }; 167 | 168 | Cube.fromString = function(str) { 169 | var col1, col2, cube, i, j, k, l, m, o, ori, p, ref3; 170 | cube = new Cube; 171 | for (i = k = 0; k <= 7; i = ++k) { 172 | for (ori = l = 0; l <= 2; ori = ++l) { 173 | if ((ref3 = str[cornerFacelet[i][ori]]) === 'U' || ref3 === 'D') { 174 | break; 175 | } 176 | } 177 | col1 = str[cornerFacelet[i][(ori + 1) % 3]]; 178 | col2 = str[cornerFacelet[i][(ori + 2) % 3]]; 179 | for (j = m = 0; m <= 7; j = ++m) { 180 | if (col1 === cornerColor[j][1] && col2 === cornerColor[j][2]) { 181 | cube.cp[i] = j; 182 | cube.co[i] = ori % 3; 183 | } 184 | } 185 | } 186 | for (i = o = 0; o <= 11; i = ++o) { 187 | for (j = p = 0; p <= 11; j = ++p) { 188 | if (str[edgeFacelet[i][0]] === edgeColor[j][0] && str[edgeFacelet[i][1]] === edgeColor[j][1]) { 189 | cube.ep[i] = j; 190 | cube.eo[i] = 0; 191 | break; 192 | } 193 | if (str[edgeFacelet[i][0]] === edgeColor[j][1] && str[edgeFacelet[i][1]] === edgeColor[j][0]) { 194 | cube.ep[i] = j; 195 | cube.eo[i] = 1; 196 | break; 197 | } 198 | } 199 | } 200 | return cube; 201 | }; 202 | 203 | Cube.prototype.clone = function() { 204 | return new Cube(this.toJSON()); 205 | }; 206 | 207 | Cube.prototype.randomize = (function() { 208 | var mixPerm, randOri, randint, result; 209 | randint = function(min, max) { 210 | return min + (Math.random() * (max - min + 1) | 0); 211 | }; 212 | mixPerm = function(arr) { 213 | var i, k, max, r, ref3, ref4, ref5, results; 214 | max = arr.length - 1; 215 | results = []; 216 | for (i = k = 0, ref3 = max - 2; 0 <= ref3 ? k <= ref3 : k >= ref3; i = 0 <= ref3 ? ++k : --k) { 217 | r = randint(i, max); 218 | if (i !== r) { 219 | ref4 = [arr[r], arr[i]], arr[i] = ref4[0], arr[r] = ref4[1]; 220 | results.push((ref5 = [arr[max - 1], arr[max]], arr[max] = ref5[0], arr[max - 1] = ref5[1], ref5)); 221 | } else { 222 | results.push(void 0); 223 | } 224 | } 225 | return results; 226 | }; 227 | randOri = function(arr, max) { 228 | var i, k, ori, ref3; 229 | ori = 0; 230 | for (i = k = 0, ref3 = arr.length - 2; 0 <= ref3 ? k <= ref3 : k >= ref3; i = 0 <= ref3 ? ++k : --k) { 231 | ori += (arr[i] = randint(0, max - 1)); 232 | } 233 | return arr[arr.length - 1] = (max - ori % max) % max; 234 | }; 235 | result = function() { 236 | mixPerm(this.cp); 237 | mixPerm(this.ep); 238 | randOri(this.co, 3); 239 | randOri(this.eo, 2); 240 | return this; 241 | }; 242 | return result; 243 | })(); 244 | 245 | Cube.random = function() { 246 | return new Cube().randomize(); 247 | }; 248 | 249 | Cube.prototype.isSolved = function() { 250 | var c, e, k, l; 251 | for (c = k = 0; k <= 7; c = ++k) { 252 | if (this.cp[c] !== c) { 253 | return false; 254 | } 255 | if (this.co[c] !== 0) { 256 | return false; 257 | } 258 | } 259 | for (e = l = 0; l <= 11; e = ++l) { 260 | if (this.ep[e] !== e) { 261 | return false; 262 | } 263 | if (this.eo[e] !== 0) { 264 | return false; 265 | } 266 | } 267 | return true; 268 | }; 269 | 270 | Cube.prototype.cornerMultiply = function(other) { 271 | var from, k, ref3, ref4, to; 272 | for (to = k = 0; k <= 7; to = ++k) { 273 | from = other.cp[to]; 274 | this.newCp[to] = this.cp[from]; 275 | this.newCo[to] = (this.co[from] + other.co[to]) % 3; 276 | } 277 | ref3 = [this.newCp, this.cp], this.cp = ref3[0], this.newCp = ref3[1]; 278 | ref4 = [this.newCo, this.co], this.co = ref4[0], this.newCo = ref4[1]; 279 | return this; 280 | }; 281 | 282 | Cube.prototype.edgeMultiply = function(other) { 283 | var from, k, ref3, ref4, to; 284 | for (to = k = 0; k <= 11; to = ++k) { 285 | from = other.ep[to]; 286 | this.newEp[to] = this.ep[from]; 287 | this.newEo[to] = (this.eo[from] + other.eo[to]) % 2; 288 | } 289 | ref3 = [this.newEp, this.ep], this.ep = ref3[0], this.newEp = ref3[1]; 290 | ref4 = [this.newEo, this.eo], this.eo = ref4[0], this.newEo = ref4[1]; 291 | return this; 292 | }; 293 | 294 | Cube.prototype.multiply = function(other) { 295 | this.cornerMultiply(other); 296 | this.edgeMultiply(other); 297 | return this; 298 | }; 299 | 300 | Cube.moves = [ 301 | { 302 | cp: [UBR, URF, UFL, ULB, DFR, DLF, DBL, DRB], 303 | co: [0, 0, 0, 0, 0, 0, 0, 0], 304 | ep: [UB, UR, UF, UL, DR, DF, DL, DB, FR, FL, BL, BR], 305 | eo: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 306 | }, { 307 | cp: [DFR, UFL, ULB, URF, DRB, DLF, DBL, UBR], 308 | co: [2, 0, 0, 1, 1, 0, 0, 2], 309 | ep: [FR, UF, UL, UB, BR, DF, DL, DB, DR, FL, BL, UR], 310 | eo: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 311 | }, { 312 | cp: [UFL, DLF, ULB, UBR, URF, DFR, DBL, DRB], 313 | co: [1, 2, 0, 0, 2, 1, 0, 0], 314 | ep: [UR, FL, UL, UB, DR, FR, DL, DB, UF, DF, BL, BR], 315 | eo: [0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0] 316 | }, { 317 | cp: [URF, UFL, ULB, UBR, DLF, DBL, DRB, DFR], 318 | co: [0, 0, 0, 0, 0, 0, 0, 0], 319 | ep: [UR, UF, UL, UB, DF, DL, DB, DR, FR, FL, BL, BR], 320 | eo: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 321 | }, { 322 | cp: [URF, ULB, DBL, UBR, DFR, UFL, DLF, DRB], 323 | co: [0, 1, 2, 0, 0, 2, 1, 0], 324 | ep: [UR, UF, BL, UB, DR, DF, FL, DB, FR, UL, DL, BR], 325 | eo: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 326 | }, { 327 | cp: [URF, UFL, UBR, DRB, DFR, DLF, ULB, DBL], 328 | co: [0, 0, 1, 2, 0, 0, 2, 1], 329 | ep: [UR, UF, UL, BR, DR, DF, DL, BL, FR, FL, UB, DB], 330 | eo: [0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1] 331 | } 332 | ]; 333 | 334 | faceNums = { 335 | U: 0, 336 | R: 1, 337 | F: 2, 338 | D: 3, 339 | L: 4, 340 | B: 5 341 | }; 342 | 343 | faceNames = { 344 | 0: 'U', 345 | 1: 'R', 346 | 2: 'F', 347 | 3: 'D', 348 | 4: 'L', 349 | 5: 'B' 350 | }; 351 | 352 | parseAlg = function(arg) { 353 | var k, len, move, part, power, ref3, results; 354 | if (typeof arg === 'string') { 355 | ref3 = arg.split(/\s+/); 356 | results = []; 357 | for (k = 0, len = ref3.length; k < len; k++) { 358 | part = ref3[k]; 359 | if (part.length === 0) { 360 | continue; 361 | } 362 | if (part.length > 2) { 363 | throw new Error("Invalid move: " + part); 364 | } 365 | move = faceNums[part[0]]; 366 | if (move === void 0) { 367 | throw new Error("Invalid move: " + part); 368 | } 369 | if (part.length === 1) { 370 | power = 0; 371 | } else { 372 | if (part[1] === '2') { 373 | power = 1; 374 | } else if (part[1] === "'") { 375 | power = 2; 376 | } else { 377 | throw new Error("Invalid move: " + part); 378 | } 379 | } 380 | results.push(move * 3 + power); 381 | } 382 | return results; 383 | } else if (arg.length != null) { 384 | return arg; 385 | } else { 386 | return [arg]; 387 | } 388 | }; 389 | 390 | Cube.prototype.move = function(arg) { 391 | var face, k, l, len, move, power, ref3, ref4, x; 392 | ref3 = parseAlg(arg); 393 | for (k = 0, len = ref3.length; k < len; k++) { 394 | move = ref3[k]; 395 | face = move / 3 | 0; 396 | power = move % 3; 397 | for (x = l = 0, ref4 = power; 0 <= ref4 ? l <= ref4 : l >= ref4; x = 0 <= ref4 ? ++l : --l) { 398 | this.multiply(Cube.moves[face]); 399 | } 400 | } 401 | return this; 402 | }; 403 | 404 | Cube.inverse = function(arg) { 405 | var face, k, len, move, power, result, str; 406 | result = (function() { 407 | var k, len, ref3, results; 408 | ref3 = parseAlg(arg); 409 | results = []; 410 | for (k = 0, len = ref3.length; k < len; k++) { 411 | move = ref3[k]; 412 | face = move / 3 | 0; 413 | power = move % 3; 414 | results.push(face * 3 + -(power - 1) + 1); 415 | } 416 | return results; 417 | })(); 418 | result.reverse(); 419 | if (typeof arg === 'string') { 420 | str = ''; 421 | for (k = 0, len = result.length; k < len; k++) { 422 | move = result[k]; 423 | face = move / 3 | 0; 424 | power = move % 3; 425 | str += faceNames[face]; 426 | if (power === 1) { 427 | str += '2'; 428 | } else if (power === 2) { 429 | str += "'"; 430 | } 431 | str += ' '; 432 | } 433 | return str.substring(0, str.length - 1); 434 | } else if (arg.length != null) { 435 | return result; 436 | } else { 437 | return result[0]; 438 | } 439 | }; 440 | 441 | return Cube; 442 | 443 | })(); 444 | 445 | if (typeof module !== "undefined" && module !== null) { 446 | module.exports = Cube; 447 | } else { 448 | this.Cube = Cube; 449 | } 450 | 451 | }).call(this); -------------------------------------------------------------------------------- /js/alg.js: -------------------------------------------------------------------------------- 1 | /* 2 | # License 3 | 4 | The MIT License (MIT) 5 | 6 | Copyright (c) 2014 Lucas Garron 7 | 8 | Permission is hereby granted, free of charge, to any person obtaining a copy 9 | of this software and associated documentation files (the "Software"), to deal 10 | in the Software without restriction, including without limitation the rights 11 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | copies of the Software, and to permit persons to whom the Software is 13 | furnished to do so, subject to the following conditions: 14 | 15 | The above copyright notice and this permission notice shall be included in 16 | all copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | THE SOFTWARE. 25 | */ 26 | 27 | 28 | // Compatibility shim to work both in browers and node.js 29 | // Based on on https://gist.github.com/rpflorence/1198466 30 | (function (name, definition){ 31 | if (typeof module !== "undefined" && module.exports) { // Node.js 32 | module.exports = definition(require("./alg_jison")); 33 | } else { // Browser 34 | window[name] = definition(alg_jison); 35 | } 36 | })("alg", function (alg_jison) { 37 | 38 | var debug = false; 39 | 40 | var patterns = { 41 | single: /^[UFRBLD]$/, 42 | wide: /^([ufrbld])|([UFRBLD]w)$/, 43 | singleSlice: /^[MES]$/, 44 | wideSlice: /^[mes]$/, 45 | rotation: /^[xyz]$/, 46 | pause: /^\.$/ 47 | }; 48 | 49 | // function moveKind(moveString) { 50 | // for (s in patterns) { 51 | // if (patterns[s].test(moveString)) { 52 | // return s; 53 | // } 54 | // } 55 | // return "UNKNOWN"; 56 | // } 57 | 58 | function moveKind(move) { 59 | for (i in patterns) { 60 | if (patterns[i].test(move.base)) { 61 | return i; 62 | } 63 | } 64 | } 65 | 66 | var directionMap = { 67 | "U": "U", "Uw": "U", "u": "U", "y": "U", 68 | "F": "F", "Fw": "F", "f": "F", "S": "F", "s": "F", "z": "F", 69 | "R": "R", "Rw": "R", "r": "R" , "x": "R", 70 | "B": "B", "Bw": "B", "b": "B", 71 | "L": "L", "Lw": "L", "l": "L", "M": "L", "m": "L", 72 | "D": "D", "Dw": "D", "d": "D", "E": "D", "e": "D", 73 | ".": "." 74 | }; 75 | 76 | function canonicalizeMove(orig, dimension) { 77 | var move = {}; 78 | 79 | move.amount = orig.amount; 80 | move.base = directionMap[orig.base]; 81 | 82 | var mKind = moveKind(orig); 83 | 84 | if (mKind == "single") { 85 | move.startLayer = orig.layer || 1; 86 | move.endLayer = move.startLayer; 87 | } else if (mKind == "wide") { 88 | move.startLayer = orig.startLayer || 1; 89 | move.endLayer = orig.endLayer || 2; 90 | } else if (mKind == "wideSlice") { 91 | move.startLayer = 2; 92 | move.endLayer = dimension - 1; 93 | } else if (mKind == "singleSlice") { 94 | if (dimension % 2 == 1) { 95 | move.startLayer = (dimension + 1)/2; 96 | move.endLayer = (dimension + 1)/2; 97 | } else { 98 | // Hack: Make the end layer larger than the start layer, so nothing moves. 99 | move.startLayer = (dimension)/2 + 1; 100 | move.endLayer = (dimension)/2; 101 | } 102 | } else if (mKind == "rotation") { 103 | move.startLayer = 1; 104 | move.endLayer = dimension; 105 | } 106 | 107 | return move; 108 | } 109 | 110 | var cube = (function(){ 111 | 112 | 113 | 114 | var types = { 115 | sequence: {repeatable: false}, 116 | move: {repeatable: true }, 117 | commutator: {repeatable: true }, 118 | conjugate: {repeatable: true }, 119 | group: {repeatable: true }, 120 | pause: {repeatable: false}, 121 | newline: {repeatable: false}, 122 | comment_short: {repeatable: false}, 123 | comment_long: {repeatable: false}, 124 | timestamp: {repeatable: false} 125 | } 126 | 127 | 128 | 129 | /************************************************************************************************/ 130 | 131 | 132 | 133 | function fromString(algString) { 134 | return alg_jison.parse(algString); 135 | } 136 | 137 | 138 | 139 | // TODO: Document that it is not safe to mutate algs, because they may share moves. 140 | function cloneMove(move) { 141 | var newMove = {}; 142 | for (i in move) { 143 | newMove[i] = move[i] 144 | } 145 | return newMove; 146 | } 147 | 148 | 149 | 150 | /************************************************************************************************/ 151 | 152 | 153 | function suffix(repeated) { 154 | 155 | if (typeof repeated.amount === "undefined") { 156 | throw "Amount not defined for repeatable: ", repeated 157 | } 158 | 159 | var amount = Math.abs(repeated.amount); 160 | var amountDir = (repeated.amount > 0) ? 1 : -1; // Mutable 161 | 162 | var suffix = "" 163 | // Suffix Logic 164 | if (amount > 1) { 165 | suffix += "" + amount; 166 | } 167 | 168 | if (amountDir === -1) { 169 | suffix += "'"; 170 | } 171 | return suffix; 172 | } 173 | 174 | 175 | /****************************************************************/ 176 | 177 | 178 | function toString(alg, dimension) { 179 | 180 | var moveStrings = []; 181 | for (var i = 0; i < alg.length; i++) { 182 | var type = alg[i].type; 183 | var moveString = toString[type](alg[i]); 184 | if (types[type].repeatable) { 185 | moveString += suffix(alg[i]); 186 | } 187 | moveStrings.push(moveString); 188 | 189 | var lastMove = (i == alg.length - 1); 190 | var afterNewline = (alg[i].type === "newline"); 191 | var beforeNewline = ((i + 1) in alg && alg[i + 1].type === "newline"); 192 | var betweenPauses = ((i + 1) in alg && alg[i].type === "pause" && alg[i + 1].type === "pause"); 193 | 194 | if (!lastMove && !afterNewline && !beforeNewline && !betweenPauses) { 195 | moveStrings.push(" "); 196 | } 197 | } 198 | return moveStrings.join(""); 199 | } 200 | 201 | toString.move = function(move) { 202 | var tL = move.layer; 203 | var sL = move.startLayer; 204 | var oL = move.endLayer; 205 | 206 | var prefix = ""; 207 | 208 | // Prefix logic 209 | if (patterns.single.test(move.base)) { 210 | if (move.layer) { 211 | prefix = move.layer.toString(); 212 | } 213 | } else if (patterns.wide.test(move.base)) { 214 | if (move.endLayer) { 215 | prefix = move.endLayer.toString(); 216 | if (move.startLayer) { 217 | prefix = move.startLayer.toString() + "-" + prefix; 218 | } 219 | } 220 | } 221 | 222 | return prefix + move.base; 223 | } 224 | 225 | toString.commutator = function(commutator) { 226 | return "[" + toString(commutator.A) + ", " + toString(commutator.B) + "]"; 227 | } 228 | 229 | toString.conjugate = function(conjugate) { 230 | return "[" + toString(conjugate.A) + ": " + toString(conjugate.B) + "]"; 231 | } 232 | 233 | toString.group = function(group) { 234 | return "(" + toString(group.A) + ")"; 235 | } 236 | 237 | toString.timestamp = function(timestamp) { 238 | return "@" + timestamp.time + "s"; 239 | } 240 | 241 | toString.comment_short = function(comment_short) { 242 | return comment_short.comment; 243 | } 244 | 245 | toString.comment_long = function(comment_long) { 246 | return comment_long.comment; 247 | } 248 | 249 | toString.pause = function(pause) { 250 | return "."; 251 | } 252 | 253 | toString.newline = function(newline) { 254 | return "\n"; 255 | } 256 | 257 | 258 | 259 | /************************************************************************************************/ 260 | 261 | 262 | 263 | // From twisty.js. 264 | function getOptions(input, defaults) { 265 | var output = {}; 266 | for (var key in defaults) { 267 | output[key] = (key in input) ? input[key] : defaults[key]; 268 | } 269 | return output; 270 | } 271 | 272 | 273 | /****************************************************************/ 274 | 275 | 276 | // Dispatch mechanism constructor. 277 | function makeAlgTraversal(options) { 278 | 279 | options = getOptions(options || {}, { 280 | outputIsAlg: true, 281 | inputValidator: function(){return true;} 282 | }); 283 | 284 | var fn = function(alg, data) { 285 | var stringInput = (typeof alg === "string"); 286 | if (stringInput) {alg = fromString(alg);} 287 | 288 | if (!options.inputValidator(alg, data)) { 289 | throw "Validation failed." 290 | } 291 | 292 | var output = fn.sequence(alg, data); 293 | if (stringInput && options.outputIsAlg) {output = toString(output);} 294 | 295 | return output; 296 | } 297 | 298 | fn.sequence = function(algIn, data) { 299 | var moves = []; 300 | for (var i = 0; i < algIn.length; i++) { 301 | moves = moves.concat(fn[algIn[i].type](algIn[i], data)); 302 | } 303 | return moves; 304 | }; 305 | 306 | fn.move = function(move, data) { 307 | return move; 308 | } 309 | 310 | fn.commutator = function(commutator, data) { 311 | return { 312 | "type": "commutator", 313 | "A": fn(commutator.A, data), 314 | "B": fn(commutator.B, data), 315 | "amount": commutator.amount 316 | }; 317 | } 318 | 319 | fn.conjugate = function(conjugate, data) { 320 | return { 321 | "type": "conjugate", 322 | "A": fn(conjugate.A, data), 323 | "B": fn(conjugate.B, data), 324 | "amount": conjugate.amount 325 | }; 326 | } 327 | 328 | fn.group = function(group, data) { 329 | return { 330 | "type": "group", 331 | "A": fn(group.A, data), 332 | "amount": group.amount 333 | }; 334 | } 335 | 336 | var id = function(x) {return x;}; 337 | 338 | fn.pause = id; 339 | fn.newline = id; 340 | fn.comment_short = id; 341 | fn.comment_long = id; 342 | fn.timestamp = id; 343 | 344 | // Make the defaults available to overrides. 345 | // TODO: Use prototypes? 346 | for (i in fn) { 347 | fn["_" + i] = fn[i]; 348 | } 349 | 350 | return fn; 351 | } 352 | 353 | 354 | 355 | /************************************************************************************************/ 356 | 357 | 358 | function round(x) { 359 | // We want to round: 360 | // 2.6 to 3 361 | // 2.5 to 2 362 | // -2.5 to -2 363 | var antiSignish = x < 0 ? 1 : -1; // When can we haz ES6? 364 | return Math.round(-Math.abs(x)) * antiSignish; 365 | } 366 | 367 | function propertySameOrBothMissing(x, y, prop) { 368 | if (prop in x && prop in y) { 369 | return x[prop] == y[prop]; 370 | } 371 | else { 372 | return !(prop in x) && !(prop in y); 373 | } 374 | } 375 | 376 | function sameBlock(moveA, moveB) { 377 | 378 | if (moveA.type !== "move" || moveB.type !== "move") { 379 | throw new Error("Something other than a move was passed into sameBlock()."); 380 | } 381 | 382 | // TODO: semantic comparison. 383 | // e.g. only compare "startLayer" if the base is BASE_WIDE. 384 | 385 | return propertySameOrBothMissing(moveA, moveB, "base") && 386 | propertySameOrBothMissing(moveA, moveB, "layer") && 387 | propertySameOrBothMissing(moveA, moveB, "startLayer") && 388 | propertySameOrBothMissing(moveA, moveB, "endLayer"); 389 | } 390 | 391 | 392 | /****************************************************************/ 393 | 394 | 395 | var simplify = makeAlgTraversal(); 396 | 397 | simplify.sequence = function(sequence) { 398 | var algOut = []; 399 | for (var i = 0; i < sequence.length; i++) { 400 | var move = sequence[i]; 401 | if (move.type !== "move") { 402 | algOut.push(simplify[move.type](move)); 403 | } 404 | else if ( 405 | algOut.length > 0 && 406 | algOut[algOut.length-1].type == "move" && 407 | sameBlock(algOut[algOut.length-1], move) 408 | ) { 409 | var amount = algOut[algOut.length-1].amount + move.amount; 410 | // Mod to [-2, -1, 0, 1, 2] 411 | // x | 0 truncates x towards 0. 412 | amount = amount - 4 * round(amount / 4); 413 | if (amount == 0) { 414 | algOut.pop(); 415 | } 416 | else { 417 | algOut[algOut.length-1].amount = amount; 418 | } 419 | } 420 | else { 421 | algOut.push(cloneMove(move)); 422 | } 423 | //console.log(JSON.stringify(algOut)); 424 | } 425 | return algOut; 426 | } 427 | 428 | 429 | 430 | /************************************************************************************************/ 431 | 432 | 433 | 434 | function repeatMoves(movesIn, accordingTo) { 435 | 436 | var movesOnce = movesIn; 437 | 438 | var amount = Math.abs(accordingTo.amount); 439 | var amountDir = (accordingTo.amount > 0) ? 1 : -1; // Mutable 440 | 441 | if (amountDir == -1) { 442 | movesOnce = invert(movesOnce); 443 | } 444 | 445 | var movesOut = []; 446 | for (var i = 0; i < amount; i++) { 447 | movesOut = movesOut.concat(movesOnce); 448 | } 449 | 450 | return movesOut; 451 | } 452 | 453 | 454 | /****************************************************************/ 455 | 456 | 457 | var expand = makeAlgTraversal(); 458 | 459 | expand.commutator = function(commutator) { 460 | var once = [].concat( 461 | expand(commutator.A), 462 | expand(commutator.B), 463 | invert(expand(commutator.A)), 464 | invert(expand(commutator.B)) 465 | ); 466 | return repeatMoves(once, commutator); 467 | } 468 | 469 | expand.conjugate = function(conjugate) { 470 | var once = [].concat( 471 | expand(conjugate.A), 472 | expand(conjugate.B), 473 | invert(expand(conjugate.A)) 474 | ); 475 | return repeatMoves(once, conjugate); 476 | } 477 | 478 | expand.group = function(group) { 479 | var once = toMoves(group.A); 480 | return repeatMoves(once, group); 481 | } 482 | 483 | 484 | /****************************************************************/ 485 | 486 | 487 | 488 | var toMoves = makeAlgTraversal(); 489 | 490 | toMoves.commutator = expand.commutator; 491 | toMoves.conjugate = expand.conjugate; 492 | toMoves.group = expand.group; 493 | 494 | var emptySequence = function(timestamp) {return [];} 495 | 496 | // TODO: Allow handling semantic data in addition to pure moves during animation. 497 | toMoves.pause = function(pause) { 498 | return { 499 | "type": "move", 500 | "base": ".", 501 | "amount": 1, 502 | "location": pause.location 503 | }; 504 | }; 505 | toMoves.newline = toMoves.pause; 506 | toMoves.comment_short = emptySequence; 507 | toMoves.comment_long = emptySequence; 508 | toMoves.timestamp = emptySequence; 509 | 510 | 511 | 512 | /************************************************************************************************/ 513 | 514 | 515 | 516 | var invert = makeAlgTraversal(); 517 | 518 | invert.sequence = function(sequence) { 519 | var currentLine; 520 | var lines = [currentLine = []]; 521 | for (var i = 0; i < sequence.length; i++) { 522 | if (sequence[i].type == "newline") { 523 | lines.push(currentLine = []); 524 | } 525 | else { 526 | currentLine.push(invert[sequence[i].type](sequence[i])); 527 | } 528 | } 529 | var out = []; 530 | for (var i = lines.length - 1; i >= 0; i--) { 531 | lines[i].reverse() 532 | if (lines[i].length > 0 && lines[i][0].type == "comment_short") { 533 | var comment = lines[i].splice(0, 1)[0]; 534 | lines[i].push(comment); 535 | } 536 | if (i > 0) { 537 | lines[i].push({type: "newline"}); 538 | } 539 | out = out.concat(lines[i]); 540 | } 541 | return out; 542 | } 543 | 544 | invert.move = function(move) { 545 | var invertedMove = cloneMove(move); 546 | if (move.base !== ".") { 547 | invertedMove.amount = -invertedMove.amount; 548 | } 549 | return invertedMove; 550 | } 551 | 552 | invert.commutator = function(commutator) { 553 | return { 554 | "type": "commutator", 555 | "A": commutator.B, 556 | "B": commutator.A, 557 | "amount": commutator.amount 558 | }; 559 | } 560 | 561 | invert.conjugate = function(conjugate) { 562 | return { 563 | "type": "conjugate", 564 | "A": conjugate.A, 565 | "B": invert(conjugate.B), 566 | "amount": conjugate.amount 567 | }; 568 | } 569 | 570 | invert.group = function(group) { 571 | return { 572 | "type": "group", 573 | "A": invert(group.A), 574 | "amount": group.amount 575 | }; 576 | } 577 | 578 | // TODO: Reversing timestamps properly takes more work. 579 | toMoves.timestamp = function(timestamp) { 580 | return []; 581 | } 582 | 583 | 584 | 585 | /************************************************************************************************/ 586 | 587 | 588 | 589 | var removeComments = makeAlgTraversal(); 590 | 591 | removeComments.comment_short = function() { 592 | return []; 593 | } 594 | 595 | removeComments.comment_long = function() { 596 | return []; 597 | } 598 | 599 | 600 | 601 | /************************************************************************************************/ 602 | 603 | 604 | 605 | var mirrorM = { 606 | fixed: ["x", "M", "m"], 607 | sliceMap: { 608 | "U": "U", "Uw": "Uw", "u": "u", "y": "y", 609 | "F": "F", "Fw": "Fw", "f": "f", "S": "S", "s": "s", "z": "z", 610 | "R": "L", "Rw": "Lw", "r": "l", "x": "x", 611 | "B": "B", "Bw": "Bw", "b": "b", 612 | "L": "R", "Lw": "Rw", "l": "r", "M": "M", 613 | "D": "D", "Dw": "Dw", "d": "d", "E": "E", "e": "e" 614 | } 615 | }; 616 | 617 | 618 | var mirrorS = { 619 | fixed: ["z", "S", "s"], 620 | sliceMap: { 621 | "U": "U", "Uw": "Uw", "u": "u", "y": "y", 622 | "F": "B", "Fw": "Bw", "f": "b", "S": "S", "z": "z", 623 | "R": "R", "Rw": "Rw", "r": "r", "x": "x", 624 | "B": "F", "Bw": "Fw", "b": "f", 625 | "L": "L", "Lw": "Lw", "l": "l", "M": "M", "m": "m", 626 | "D": "D", "Dw": "Dw", "d": "d", "E": "E", "e": "e" 627 | } 628 | }; 629 | 630 | 631 | /****************************************************************/ 632 | 633 | 634 | var mirrorAcrossM = makeAlgTraversal(); 635 | 636 | mirrorAcrossM.move = function(move) { 637 | var mirroredMove = cloneMove(move); 638 | if (mirrorM.fixed.indexOf(mirroredMove.base) === -1) { 639 | mirroredMove.base = mirrorM.sliceMap[mirroredMove.base]; 640 | mirroredMove.amount = -mirroredMove.amount; 641 | } 642 | return mirroredMove; 643 | } 644 | 645 | 646 | var mirrorAcrossS = makeAlgTraversal(); 647 | 648 | mirrorAcrossS.move = function(move) { 649 | var mirroredMove = cloneMove(move); 650 | if (mirrorS.fixed.indexOf(mirroredMove.base) === -1) { 651 | mirroredMove.base = mirrorS.sliceMap[mirroredMove.base]; 652 | mirroredMove.amount = -mirroredMove.amount; 653 | } 654 | return mirroredMove; 655 | } 656 | 657 | 658 | 659 | 660 | /************************************************************************************************/ 661 | 662 | // Metrics 663 | 664 | 665 | /* 666 | [a, b] means: 667 | If the cost is constant based on doing the move at all, count it as `a` moves. 668 | If the cost depends on abs(amount), use `b` as a multiplier. 669 | 670 | Note: An amount of 0 will always have a cost of 0. 671 | */ 672 | var moveCountScalars = { 673 | "obtm": {rotation: [0, 0], outer: [1, 0], inner: [2, 0]}, 674 | "btm": {rotation: [0, 0], outer: [1, 0], inner: [1, 0]}, 675 | "obqtm": {rotation: [0, 0], outer: [0, 1], inner: [0, 2]}, 676 | "bqtm": {rotation: [0, 0], outer: [0, 1], inner: [0, 1]}, 677 | "etm": {rotation: [1, 0], outer: [1, 0], inner: [1, 0]} 678 | } 679 | 680 | function moveScale(amount, scalars) { 681 | if (amount == 0) { 682 | return 0; //TODO: ETM? 683 | } 684 | return scalars[0] + Math.abs(amount) * scalars[1]; 685 | } 686 | 687 | var add = function(a, b) { 688 | return a + b; 689 | }; 690 | 691 | var arraySum = function(arr) { 692 | return arr.reduce(add, 0); 693 | } 694 | 695 | 696 | function countMovesValidator(alg, data) { 697 | if (!data.metric) { 698 | console.error("No metric given. Valid options: " + Object.keys(moveCountScalars).join(", ")); 699 | return false; 700 | } 701 | if (!(data.metric in moveCountScalars)) { 702 | console.error("Invalid metric. Valid options: " + Object.keys(moveCountScalars).join(", ")); 703 | return false; 704 | } 705 | return true; 706 | } 707 | 708 | 709 | 710 | /****************************************************************/ 711 | 712 | 713 | 714 | // Example: alg.cube.countMoves("R", {metric: "obtm"}) 715 | // TODO: Default to obtm and 3x3x3. 716 | // TODO: Dimension independence? 717 | 718 | var countMoves = makeAlgTraversal({ 719 | outputIsAlg: false, 720 | inputValidator: countMovesValidator 721 | }); 722 | 723 | countMoves.sequence = function(move, data) { 724 | var counts = countMoves._sequence(move, data); 725 | return arraySum(counts); 726 | } 727 | 728 | countMoves.move = function(move, data) { 729 | // TODO: Get layer info without dummy number. 730 | var can = canonicalizeMove(move, 10000); 731 | 732 | var scalarKind; 733 | var mKind = moveKind(move); 734 | if (mKind == "rotation") { 735 | scalarKind = "rotation"; 736 | } else if (can.startLayer === 1) { 737 | scalarKind = "outer"; 738 | } else if (can.startLayer > 1) { 739 | scalarKind = "inner"; 740 | } 741 | var scalars = moveCountScalars[data.metric][scalarKind]; 742 | return moveScale(can.amount, scalars); 743 | } 744 | 745 | countMoves.commutator = function(commutator, data) { 746 | // TODO: map/reduce framework for structural recursion? 747 | var counts = countMoves._commutator(commutator, data); 748 | return (counts.A * 2 + counts.B * 2) * Math.abs(counts.amount); 749 | } 750 | 751 | countMoves.conjugate = function(conjugate, data) { 752 | var counts = countMoves._conjugate(conjugate, data); 753 | return (counts.A * 2 + counts.B * 1) * Math.abs(counts.amount); 754 | } 755 | 756 | countMoves.group = function(group, data) { 757 | var counts = countMoves._group(group, data); 758 | return (counts.A) * Math.abs(counts.amount); 759 | } 760 | 761 | var zero = function(group, data) { 762 | return 0; 763 | } 764 | 765 | countMoves.pause = zero; 766 | countMoves.newline = zero; 767 | countMoves.comment_short = zero; 768 | countMoves.comment_long = zero; 769 | countMoves.timestamp = zero; 770 | 771 | 772 | /************************************************************************************************/ 773 | 774 | // Exports 775 | 776 | return { 777 | toString: toString, 778 | simplify: simplify, 779 | fromString: fromString, 780 | cloneMove: cloneMove, 781 | makeAlgTraversal: makeAlgTraversal, 782 | invert: invert, 783 | mirrorAcrossM: mirrorAcrossM, 784 | mirrorAcrossS: mirrorAcrossS, 785 | canonicalizeMove: canonicalizeMove, 786 | removeComments: removeComments, 787 | toMoves: toMoves, 788 | expand: expand, 789 | countMoves: countMoves 790 | } 791 | })(); 792 | 793 | return { 794 | cube: cube 795 | } 796 | 797 | }); 798 | 799 | // var c = alg.cube; -------------------------------------------------------------------------------- /js/solve.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright (c) 2013-2017 Petri Lehtinen 4 | Copyright (c) 2018 Ludovic Fernandez 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | 26 | (function() { 27 | var BL, BR, Cnk, Cube, DB, DBL, DF, DFR, DL, DLF, DR, DRB, FL, FR, Include, N_FLIP, N_FRtoBR, N_PARITY, N_SLICE1, N_SLICE2, N_TWIST, N_UBtoDF, N_URFtoDLF, N_URtoDF, N_URtoUL, UB, UBR, UF, UFL, UL, ULB, UR, URF, allMoves1, allMoves2, computeMoveTable, computePruningTable, factorial, key, max, mergeURtoDF, moveTableParams, nextMoves1, nextMoves2, permutationIndex, pruning, pruningTableParams, ref, ref1, rotateLeft, rotateRight, value, 28 | slice1 = [].slice, 29 | indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; 30 | 31 | Cube = this.Cube || require('./cube'); 32 | 33 | ref = [0, 1, 2, 3, 4, 5, 6, 7], URF = ref[0], UFL = ref[1], ULB = ref[2], UBR = ref[3], DFR = ref[4], DLF = ref[5], DBL = ref[6], DRB = ref[7]; 34 | 35 | ref1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], UR = ref1[0], UF = ref1[1], UL = ref1[2], UB = ref1[3], DR = ref1[4], DF = ref1[5], DL = ref1[6], DB = ref1[7], FR = ref1[8], FL = ref1[9], BL = ref1[10], BR = ref1[11]; 36 | 37 | Cnk = function(n, k) { 38 | var i, j, s; 39 | if (n < k) { 40 | return 0; 41 | } 42 | if (k > n / 2) { 43 | k = n - k; 44 | } 45 | s = 1; 46 | i = n; 47 | j = 1; 48 | while (i !== n - k) { 49 | s *= i; 50 | s /= j; 51 | i--; 52 | j++; 53 | } 54 | return s; 55 | }; 56 | 57 | factorial = function(n) { 58 | var f, i, m, ref2; 59 | f = 1; 60 | for (i = m = 2, ref2 = n; 2 <= ref2 ? m <= ref2 : m >= ref2; i = 2 <= ref2 ? ++m : --m) { 61 | f *= i; 62 | } 63 | return f; 64 | }; 65 | 66 | max = function(a, b) { 67 | if (a > b) { 68 | return a; 69 | } else { 70 | return b; 71 | } 72 | }; 73 | 74 | rotateLeft = function(array, l, r) { 75 | var i, m, ref2, ref3, tmp; 76 | tmp = array[l]; 77 | for (i = m = ref2 = l, ref3 = r - 1; ref2 <= ref3 ? m <= ref3 : m >= ref3; i = ref2 <= ref3 ? ++m : --m) { 78 | array[i] = array[i + 1]; 79 | } 80 | return array[r] = tmp; 81 | }; 82 | 83 | rotateRight = function(array, l, r) { 84 | var i, m, ref2, ref3, tmp; 85 | tmp = array[r]; 86 | for (i = m = ref2 = r, ref3 = l + 1; ref2 <= ref3 ? m <= ref3 : m >= ref3; i = ref2 <= ref3 ? ++m : --m) { 87 | array[i] = array[i - 1]; 88 | } 89 | return array[l] = tmp; 90 | }; 91 | 92 | permutationIndex = function(context, start, end, fromEnd) { 93 | var i, maxAll, maxB, maxOur, our, permName; 94 | if (fromEnd == null) { 95 | fromEnd = false; 96 | } 97 | maxOur = end - start; 98 | maxB = factorial(maxOur + 1); 99 | if (context === 'corners') { 100 | maxAll = 7; 101 | permName = 'cp'; 102 | } else { 103 | maxAll = 11; 104 | permName = 'ep'; 105 | } 106 | our = (function() { 107 | var m, ref2, results; 108 | results = []; 109 | for (i = m = 0, ref2 = maxOur; 0 <= ref2 ? m <= ref2 : m >= ref2; i = 0 <= ref2 ? ++m : --m) { 110 | results.push(0); 111 | } 112 | return results; 113 | })(); 114 | return function(index) { 115 | var a, b, c, j, k, m, o, p, perm, q, ref10, ref11, ref12, ref2, ref3, ref4, ref5, ref6, ref7, ref8, ref9, t, u, w, x, y, z; 116 | if (index != null) { 117 | for (i = m = 0, ref2 = maxOur; 0 <= ref2 ? m <= ref2 : m >= ref2; i = 0 <= ref2 ? ++m : --m) { 118 | our[i] = i + start; 119 | } 120 | b = index % maxB; 121 | a = index / maxB | 0; 122 | perm = this[permName]; 123 | for (i = o = 0, ref3 = maxAll; 0 <= ref3 ? o <= ref3 : o >= ref3; i = 0 <= ref3 ? ++o : --o) { 124 | perm[i] = -1; 125 | } 126 | for (j = p = 1, ref4 = maxOur; 1 <= ref4 ? p <= ref4 : p >= ref4; j = 1 <= ref4 ? ++p : --p) { 127 | k = b % (j + 1); 128 | b = b / (j + 1) | 0; 129 | while (k > 0) { 130 | rotateRight(our, 0, j); 131 | k--; 132 | } 133 | } 134 | x = maxOur; 135 | if (fromEnd) { 136 | for (j = q = 0, ref5 = maxAll; 0 <= ref5 ? q <= ref5 : q >= ref5; j = 0 <= ref5 ? ++q : --q) { 137 | c = Cnk(maxAll - j, x + 1); 138 | if (a - c >= 0) { 139 | perm[j] = our[maxOur - x]; 140 | a -= c; 141 | x--; 142 | } 143 | } 144 | } else { 145 | for (j = t = ref6 = maxAll; ref6 <= 0 ? t <= 0 : t >= 0; j = ref6 <= 0 ? ++t : --t) { 146 | c = Cnk(j, x + 1); 147 | if (a - c >= 0) { 148 | perm[j] = our[x]; 149 | a -= c; 150 | x--; 151 | } 152 | } 153 | } 154 | return this; 155 | } else { 156 | perm = this[permName]; 157 | for (i = u = 0, ref7 = maxOur; 0 <= ref7 ? u <= ref7 : u >= ref7; i = 0 <= ref7 ? ++u : --u) { 158 | our[i] = -1; 159 | } 160 | a = b = x = 0; 161 | if (fromEnd) { 162 | for (j = w = ref8 = maxAll; ref8 <= 0 ? w <= 0 : w >= 0; j = ref8 <= 0 ? ++w : --w) { 163 | if ((start <= (ref9 = perm[j]) && ref9 <= end)) { 164 | a += Cnk(maxAll - j, x + 1); 165 | our[maxOur - x] = perm[j]; 166 | x++; 167 | } 168 | } 169 | } else { 170 | for (j = y = 0, ref10 = maxAll; 0 <= ref10 ? y <= ref10 : y >= ref10; j = 0 <= ref10 ? ++y : --y) { 171 | if ((start <= (ref11 = perm[j]) && ref11 <= end)) { 172 | a += Cnk(j, x + 1); 173 | our[x] = perm[j]; 174 | x++; 175 | } 176 | } 177 | } 178 | for (j = z = ref12 = maxOur; ref12 <= 0 ? z <= 0 : z >= 0; j = ref12 <= 0 ? ++z : --z) { 179 | k = 0; 180 | while (our[j] !== start + j) { 181 | rotateLeft(our, 0, j); 182 | k++; 183 | } 184 | b = (j + 1) * b + k; 185 | } 186 | return a * maxB + b; 187 | } 188 | }; 189 | }; 190 | 191 | Include = { 192 | twist: function(twist) { 193 | var i, m, o, ori, parity, v; 194 | if (twist != null) { 195 | parity = 0; 196 | for (i = m = 6; m >= 0; i = --m) { 197 | ori = twist % 3; 198 | twist = (twist / 3) | 0; 199 | this.co[i] = ori; 200 | parity += ori; 201 | } 202 | this.co[7] = (3 - parity % 3) % 3; 203 | return this; 204 | } else { 205 | v = 0; 206 | for (i = o = 0; o <= 6; i = ++o) { 207 | v = 3 * v + this.co[i]; 208 | } 209 | return v; 210 | } 211 | }, 212 | flip: function(flip) { 213 | var i, m, o, ori, parity, v; 214 | if (flip != null) { 215 | parity = 0; 216 | for (i = m = 10; m >= 0; i = --m) { 217 | ori = flip % 2; 218 | flip = flip / 2 | 0; 219 | this.eo[i] = ori; 220 | parity += ori; 221 | } 222 | this.eo[11] = (2 - parity % 2) % 2; 223 | return this; 224 | } else { 225 | v = 0; 226 | for (i = o = 0; o <= 10; i = ++o) { 227 | v = 2 * v + this.eo[i]; 228 | } 229 | return v; 230 | } 231 | }, 232 | cornerParity: function() { 233 | var i, j, m, o, ref2, ref3, ref4, ref5, s; 234 | s = 0; 235 | for (i = m = ref2 = DRB, ref3 = URF + 1; ref2 <= ref3 ? m <= ref3 : m >= ref3; i = ref2 <= ref3 ? ++m : --m) { 236 | for (j = o = ref4 = i - 1, ref5 = URF; ref4 <= ref5 ? o <= ref5 : o >= ref5; j = ref4 <= ref5 ? ++o : --o) { 237 | if (this.cp[j] > this.cp[i]) { 238 | s++; 239 | } 240 | } 241 | } 242 | return s % 2; 243 | }, 244 | edgeParity: function() { 245 | var i, j, m, o, ref2, ref3, ref4, ref5, s; 246 | s = 0; 247 | for (i = m = ref2 = BR, ref3 = UR + 1; ref2 <= ref3 ? m <= ref3 : m >= ref3; i = ref2 <= ref3 ? ++m : --m) { 248 | for (j = o = ref4 = i - 1, ref5 = UR; ref4 <= ref5 ? o <= ref5 : o >= ref5; j = ref4 <= ref5 ? ++o : --o) { 249 | if (this.ep[j] > this.ep[i]) { 250 | s++; 251 | } 252 | } 253 | } 254 | return s % 2; 255 | }, 256 | URFtoDLF: permutationIndex('corners', URF, DLF), 257 | URtoUL: permutationIndex('edges', UR, UL), 258 | UBtoDF: permutationIndex('edges', UB, DF), 259 | URtoDF: permutationIndex('edges', UR, DF), 260 | FRtoBR: permutationIndex('edges', FR, BR, true) 261 | }; 262 | 263 | for (key in Include) { 264 | value = Include[key]; 265 | Cube.prototype[key] = value; 266 | } 267 | 268 | computeMoveTable = function(context, coord, size) { 269 | var apply, cube, i, inner, j, k, m, move, o, p, ref2, results; 270 | apply = context === 'corners' ? 'cornerMultiply' : 'edgeMultiply'; 271 | cube = new Cube; 272 | results = []; 273 | for (i = m = 0, ref2 = size - 1; 0 <= ref2 ? m <= ref2 : m >= ref2; i = 0 <= ref2 ? ++m : --m) { 274 | cube[coord](i); 275 | inner = []; 276 | for (j = o = 0; o <= 5; j = ++o) { 277 | move = Cube.moves[j]; 278 | for (k = p = 0; p <= 2; k = ++p) { 279 | cube[apply](move); 280 | inner.push(cube[coord]()); 281 | } 282 | cube[apply](move); 283 | } 284 | results.push(inner); 285 | } 286 | return results; 287 | }; 288 | 289 | mergeURtoDF = (function() { 290 | var a, b; 291 | a = new Cube; 292 | b = new Cube; 293 | return function(URtoUL, UBtoDF) { 294 | var i, m; 295 | a.URtoUL(URtoUL); 296 | b.UBtoDF(UBtoDF); 297 | for (i = m = 0; m <= 7; i = ++m) { 298 | if (a.ep[i] !== -1) { 299 | if (b.ep[i] !== -1) { 300 | return -1; 301 | } else { 302 | b.ep[i] = a.ep[i]; 303 | } 304 | } 305 | } 306 | return b.URtoDF(); 307 | }; 308 | })(); 309 | 310 | N_TWIST = 2187; 311 | 312 | N_FLIP = 2048; 313 | 314 | N_PARITY = 2; 315 | 316 | N_FRtoBR = 11880; 317 | 318 | N_SLICE1 = 495; 319 | 320 | N_SLICE2 = 24; 321 | 322 | N_URFtoDLF = 20160; 323 | 324 | N_URtoDF = 20160; 325 | 326 | N_URtoUL = 1320; 327 | 328 | N_UBtoDF = 1320; 329 | 330 | Cube.moveTables = { 331 | parity: [[1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1], [0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0]], 332 | twist: null, 333 | flip: null, 334 | FRtoBR: null, 335 | URFtoDLF: null, 336 | URtoDF: null, 337 | URtoUL: null, 338 | UBtoDF: null, 339 | mergeURtoDF: null 340 | }; 341 | 342 | moveTableParams = { 343 | twist: ['corners', N_TWIST], 344 | flip: ['edges', N_FLIP], 345 | FRtoBR: ['edges', N_FRtoBR], 346 | URFtoDLF: ['corners', N_URFtoDLF], 347 | URtoDF: ['edges', N_URtoDF], 348 | URtoUL: ['edges', N_URtoUL], 349 | UBtoDF: ['edges', N_UBtoDF], 350 | mergeURtoDF: [] 351 | }; 352 | 353 | Cube.computeMoveTables = function() { 354 | var len, m, name, ref2, scope, size, tableName, tables; 355 | tables = 1 <= arguments.length ? slice1.call(arguments, 0) : []; 356 | if (tables.length === 0) { 357 | tables = (function() { 358 | var results; 359 | results = []; 360 | for (name in moveTableParams) { 361 | results.push(name); 362 | } 363 | return results; 364 | })(); 365 | } 366 | for (m = 0, len = tables.length; m < len; m++) { 367 | tableName = tables[m]; 368 | if (this.moveTables[tableName] !== null) { 369 | continue; 370 | } 371 | if (tableName === 'mergeURtoDF') { 372 | this.moveTables.mergeURtoDF = (function() { 373 | var UBtoDF, URtoUL, o, results; 374 | results = []; 375 | for (URtoUL = o = 0; o <= 335; URtoUL = ++o) { 376 | results.push((function() { 377 | var p, results1; 378 | results1 = []; 379 | for (UBtoDF = p = 0; p <= 335; UBtoDF = ++p) { 380 | results1.push(mergeURtoDF(URtoUL, UBtoDF)); 381 | } 382 | return results1; 383 | })()); 384 | } 385 | return results; 386 | })(); 387 | } else { 388 | ref2 = moveTableParams[tableName], scope = ref2[0], size = ref2[1]; 389 | this.moveTables[tableName] = computeMoveTable(scope, tableName, size); 390 | } 391 | } 392 | return this; 393 | }; 394 | 395 | allMoves1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]; 396 | 397 | nextMoves1 = (function() { 398 | var face, lastFace, m, next, o, p, power, results; 399 | results = []; 400 | for (lastFace = m = 0; m <= 5; lastFace = ++m) { 401 | next = []; 402 | for (face = o = 0; o <= 5; face = ++o) { 403 | if (face !== lastFace && face !== lastFace - 3) { 404 | for (power = p = 0; p <= 2; power = ++p) { 405 | next.push(face * 3 + power); 406 | } 407 | } 408 | } 409 | results.push(next); 410 | } 411 | return results; 412 | })(); 413 | 414 | allMoves2 = [0, 1, 2, 4, 7, 9, 10, 11, 13, 16]; 415 | 416 | nextMoves2 = (function() { 417 | var face, lastFace, len, m, next, o, p, power, powers, results; 418 | results = []; 419 | for (lastFace = m = 0; m <= 5; lastFace = ++m) { 420 | next = []; 421 | for (face = o = 0; o <= 5; face = ++o) { 422 | if (!(face !== lastFace && face !== lastFace - 3)) { 423 | continue; 424 | } 425 | powers = face === 0 || face === 3 ? [0, 1, 2] : [1]; 426 | for (p = 0, len = powers.length; p < len; p++) { 427 | power = powers[p]; 428 | next.push(face * 3 + power); 429 | } 430 | } 431 | results.push(next); 432 | } 433 | return results; 434 | })(); 435 | 436 | pruning = function(table, index, value) { 437 | var pos, shift, slot; 438 | pos = index % 8; 439 | slot = index >> 3; 440 | shift = pos << 2; 441 | if (value != null) { 442 | table[slot] &= ~(0xF << shift); 443 | table[slot] |= value << shift; 444 | return value; 445 | } else { 446 | return (table[slot] & (0xF << shift)) >>> shift; 447 | } 448 | }; 449 | 450 | computePruningTable = function(phase, size, currentCoords, nextIndex) { 451 | var current, depth, done, index, len, m, move, moves, next, o, ref2, table, x; 452 | table = (function() { 453 | var m, ref2, results; 454 | results = []; 455 | for (x = m = 0, ref2 = Math.ceil(size / 8) - 1; 0 <= ref2 ? m <= ref2 : m >= ref2; x = 0 <= ref2 ? ++m : --m) { 456 | results.push(0xFFFFFFFF); 457 | } 458 | return results; 459 | })(); 460 | if (phase === 1) { 461 | moves = allMoves1; 462 | } else { 463 | moves = allMoves2; 464 | } 465 | depth = 0; 466 | pruning(table, 0, depth); 467 | done = 1; 468 | while (done !== size) { 469 | for (index = m = 0, ref2 = size - 1; 0 <= ref2 ? m <= ref2 : m >= ref2; index = 0 <= ref2 ? ++m : --m) { 470 | if (!(pruning(table, index) === depth)) { 471 | continue; 472 | } 473 | current = currentCoords(index); 474 | for (o = 0, len = moves.length; o < len; o++) { 475 | move = moves[o]; 476 | next = nextIndex(current, move); 477 | if (pruning(table, next) === 0xF) { 478 | pruning(table, next, depth + 1); 479 | done++; 480 | } 481 | } 482 | } 483 | depth++; 484 | } 485 | return table; 486 | }; 487 | 488 | Cube.pruningTables = { 489 | sliceTwist: null, 490 | sliceFlip: null, 491 | sliceURFtoDLFParity: null, 492 | sliceURtoDFParity: null 493 | }; 494 | 495 | pruningTableParams = { 496 | sliceTwist: [ 497 | 1, N_SLICE1 * N_TWIST, function(index) { 498 | return [index % N_SLICE1, index / N_SLICE1 | 0]; 499 | }, function(current, move) { 500 | var newSlice, newTwist, slice, twist; 501 | slice = current[0], twist = current[1]; 502 | newSlice = Cube.moveTables.FRtoBR[slice * 24][move] / 24 | 0; 503 | newTwist = Cube.moveTables.twist[twist][move]; 504 | return newTwist * N_SLICE1 + newSlice; 505 | } 506 | ], 507 | sliceFlip: [ 508 | 1, N_SLICE1 * N_FLIP, function(index) { 509 | return [index % N_SLICE1, index / N_SLICE1 | 0]; 510 | }, function(current, move) { 511 | var flip, newFlip, newSlice, slice; 512 | slice = current[0], flip = current[1]; 513 | newSlice = Cube.moveTables.FRtoBR[slice * 24][move] / 24 | 0; 514 | newFlip = Cube.moveTables.flip[flip][move]; 515 | return newFlip * N_SLICE1 + newSlice; 516 | } 517 | ], 518 | sliceURFtoDLFParity: [ 519 | 2, N_SLICE2 * N_URFtoDLF * N_PARITY, function(index) { 520 | return [index % 2, (index / 2 | 0) % N_SLICE2, (index / 2 | 0) / N_SLICE2 | 0]; 521 | }, function(current, move) { 522 | var URFtoDLF, newParity, newSlice, newURFtoDLF, parity, slice; 523 | parity = current[0], slice = current[1], URFtoDLF = current[2]; 524 | newParity = Cube.moveTables.parity[parity][move]; 525 | newSlice = Cube.moveTables.FRtoBR[slice][move]; 526 | newURFtoDLF = Cube.moveTables.URFtoDLF[URFtoDLF][move]; 527 | return (newURFtoDLF * N_SLICE2 + newSlice) * 2 + newParity; 528 | } 529 | ], 530 | sliceURtoDFParity: [ 531 | 2, N_SLICE2 * N_URtoDF * N_PARITY, function(index) { 532 | return [index % 2, (index / 2 | 0) % N_SLICE2, (index / 2 | 0) / N_SLICE2 | 0]; 533 | }, function(current, move) { 534 | var URtoDF, newParity, newSlice, newURtoDF, parity, slice; 535 | parity = current[0], slice = current[1], URtoDF = current[2]; 536 | newParity = Cube.moveTables.parity[parity][move]; 537 | newSlice = Cube.moveTables.FRtoBR[slice][move]; 538 | newURtoDF = Cube.moveTables.URtoDF[URtoDF][move]; 539 | return (newURtoDF * N_SLICE2 + newSlice) * 2 + newParity; 540 | } 541 | ] 542 | }; 543 | 544 | Cube.computePruningTables = function() { 545 | var len, m, name, params, tableName, tables; 546 | tables = 1 <= arguments.length ? slice1.call(arguments, 0) : []; 547 | if (tables.length === 0) { 548 | tables = (function() { 549 | var results; 550 | results = []; 551 | for (name in pruningTableParams) { 552 | results.push(name); 553 | } 554 | return results; 555 | })(); 556 | } 557 | for (m = 0, len = tables.length; m < len; m++) { 558 | tableName = tables[m]; 559 | if (this.pruningTables[tableName] !== null) { 560 | continue; 561 | } 562 | params = pruningTableParams[tableName]; 563 | this.pruningTables[tableName] = computePruningTable.apply(null, params); 564 | } 565 | return this; 566 | }; 567 | 568 | Cube.initSolver = function() { 569 | Cube.computeMoveTables(); 570 | return Cube.computePruningTables(); 571 | }; 572 | 573 | Cube.prototype.solve = function(maxDepth) { 574 | var State, freeStates, moveNames, phase1, phase1search, phase2, phase2search, solution, state, x; 575 | if (maxDepth == null) { 576 | maxDepth = 22; 577 | } 578 | moveNames = (function() { 579 | var face, faceName, m, o, power, powerName, result; 580 | faceName = ['U', 'R', 'F', 'D', 'L', 'B']; 581 | powerName = ['', '2', "'"]; 582 | result = []; 583 | for (face = m = 0; m <= 5; face = ++m) { 584 | for (power = o = 0; o <= 2; power = ++o) { 585 | result.push(faceName[face] + powerName[power]); 586 | } 587 | } 588 | return result; 589 | })(); 590 | State = (function() { 591 | function State(cube) { 592 | this.parent = null; 593 | this.lastMove = null; 594 | this.depth = 0; 595 | if (cube) { 596 | this.init(cube); 597 | } 598 | } 599 | 600 | State.prototype.init = function(cube) { 601 | this.flip = cube.flip(); 602 | this.twist = cube.twist(); 603 | this.slice = cube.FRtoBR() / N_SLICE2 | 0; 604 | this.parity = cube.cornerParity(); 605 | this.URFtoDLF = cube.URFtoDLF(); 606 | this.FRtoBR = cube.FRtoBR(); 607 | this.URtoUL = cube.URtoUL(); 608 | this.UBtoDF = cube.UBtoDF(); 609 | return this; 610 | }; 611 | 612 | State.prototype.solution = function() { 613 | if (this.parent) { 614 | return this.parent.solution() + moveNames[this.lastMove] + ' '; 615 | } else { 616 | return ''; 617 | } 618 | }; 619 | 620 | State.prototype.move = function(table, index, move) { 621 | return Cube.moveTables[table][index][move]; 622 | }; 623 | 624 | State.prototype.pruning = function(table, index) { 625 | return pruning(Cube.pruningTables[table], index); 626 | }; 627 | 628 | State.prototype.moves1 = function() { 629 | if (this.lastMove !== null) { 630 | return nextMoves1[this.lastMove / 3 | 0]; 631 | } else { 632 | return allMoves1; 633 | } 634 | }; 635 | 636 | State.prototype.minDist1 = function() { 637 | var d1, d2; 638 | d1 = this.pruning('sliceFlip', N_SLICE1 * this.flip + this.slice); 639 | d2 = this.pruning('sliceTwist', N_SLICE1 * this.twist + this.slice); 640 | return max(d1, d2); 641 | }; 642 | 643 | State.prototype.next1 = function(move) { 644 | var next; 645 | next = freeStates.pop(); 646 | next.parent = this; 647 | next.lastMove = move; 648 | next.depth = this.depth + 1; 649 | next.flip = this.move('flip', this.flip, move); 650 | next.twist = this.move('twist', this.twist, move); 651 | next.slice = this.move('FRtoBR', this.slice * 24, move) / 24 | 0; 652 | return next; 653 | }; 654 | 655 | State.prototype.moves2 = function() { 656 | if (this.lastMove !== null) { 657 | return nextMoves2[this.lastMove / 3 | 0]; 658 | } else { 659 | return allMoves2; 660 | } 661 | }; 662 | 663 | State.prototype.minDist2 = function() { 664 | var d1, d2, index1, index2; 665 | index1 = (N_SLICE2 * this.URtoDF + this.FRtoBR) * N_PARITY + this.parity; 666 | d1 = this.pruning('sliceURtoDFParity', index1); 667 | index2 = (N_SLICE2 * this.URFtoDLF + this.FRtoBR) * N_PARITY + this.parity; 668 | d2 = this.pruning('sliceURFtoDLFParity', index2); 669 | return max(d1, d2); 670 | }; 671 | 672 | State.prototype.init2 = function(top) { 673 | if (top == null) { 674 | top = true; 675 | } 676 | if (this.parent === null) { 677 | return; 678 | } 679 | this.parent.init2(false); 680 | this.URFtoDLF = this.move('URFtoDLF', this.parent.URFtoDLF, this.lastMove); 681 | this.FRtoBR = this.move('FRtoBR', this.parent.FRtoBR, this.lastMove); 682 | this.parity = this.move('parity', this.parent.parity, this.lastMove); 683 | this.URtoUL = this.move('URtoUL', this.parent.URtoUL, this.lastMove); 684 | this.UBtoDF = this.move('UBtoDF', this.parent.UBtoDF, this.lastMove); 685 | if (top) { 686 | return this.URtoDF = this.move('mergeURtoDF', this.URtoUL, this.UBtoDF); 687 | } 688 | }; 689 | 690 | State.prototype.next2 = function(move) { 691 | var next; 692 | next = freeStates.pop(); 693 | next.parent = this; 694 | next.lastMove = move; 695 | next.depth = this.depth + 1; 696 | next.URFtoDLF = this.move('URFtoDLF', this.URFtoDLF, move); 697 | next.FRtoBR = this.move('FRtoBR', this.FRtoBR, move); 698 | next.parity = this.move('parity', this.parity, move); 699 | next.URtoDF = this.move('URtoDF', this.URtoDF, move); 700 | return next; 701 | }; 702 | 703 | return State; 704 | 705 | })(); 706 | solution = null; 707 | phase1search = function(state) { 708 | var depth, m, ref2, results; 709 | depth = 0; 710 | results = []; 711 | for (depth = m = 1, ref2 = maxDepth; 1 <= ref2 ? m <= ref2 : m >= ref2; depth = 1 <= ref2 ? ++m : --m) { 712 | phase1(state, depth); 713 | if (solution !== null) { 714 | break; 715 | } 716 | results.push(depth++); 717 | } 718 | return results; 719 | }; 720 | phase1 = function(state, depth) { 721 | var len, m, move, next, ref2, ref3, results; 722 | if (depth === 0) { 723 | if (state.minDist1() === 0) { 724 | if (state.lastMove === null || (ref2 = state.lastMove, indexOf.call(allMoves2, ref2) < 0)) { 725 | return phase2search(state); 726 | } 727 | } 728 | } else if (depth > 0) { 729 | if (state.minDist1() <= depth) { 730 | ref3 = state.moves1(); 731 | results = []; 732 | for (m = 0, len = ref3.length; m < len; m++) { 733 | move = ref3[m]; 734 | next = state.next1(move); 735 | phase1(next, depth - 1); 736 | freeStates.push(next); 737 | if (solution !== null) { 738 | break; 739 | } else { 740 | results.push(void 0); 741 | } 742 | } 743 | return results; 744 | } 745 | } 746 | }; 747 | phase2search = function(state) { 748 | var depth, m, ref2, results; 749 | state.init2(); 750 | results = []; 751 | for (depth = m = 1, ref2 = maxDepth - state.depth; 1 <= ref2 ? m <= ref2 : m >= ref2; depth = 1 <= ref2 ? ++m : --m) { 752 | phase2(state, depth); 753 | if (solution !== null) { 754 | break; 755 | } 756 | results.push(depth++); 757 | } 758 | return results; 759 | }; 760 | phase2 = function(state, depth) { 761 | var len, m, move, next, ref2, results; 762 | if (depth === 0) { 763 | if (state.minDist2() === 0) { 764 | return solution = state.solution(); 765 | } 766 | } else if (depth > 0) { 767 | if (state.minDist2() <= depth) { 768 | ref2 = state.moves2(); 769 | results = []; 770 | for (m = 0, len = ref2.length; m < len; m++) { 771 | move = ref2[m]; 772 | next = state.next2(move); 773 | phase2(next, depth - 1); 774 | freeStates.push(next); 775 | if (solution !== null) { 776 | break; 777 | } else { 778 | results.push(void 0); 779 | } 780 | } 781 | return results; 782 | } 783 | } 784 | }; 785 | freeStates = (function() { 786 | var m, ref2, results; 787 | results = []; 788 | for (x = m = 0, ref2 = maxDepth + 1; 0 <= ref2 ? m <= ref2 : m >= ref2; x = 0 <= ref2 ? ++m : --m) { 789 | results.push(new State); 790 | } 791 | return results; 792 | })(); 793 | state = freeStates.pop().init(this); 794 | phase1search(state); 795 | freeStates.push(state); 796 | if (solution.length > 0) { 797 | solution = solution.substring(0, solution.length - 1); 798 | } 799 | return solution; 800 | }; 801 | 802 | Cube.scramble = function() { 803 | return Cube.inverse(Cube.random().solve()); 804 | }; 805 | 806 | }).call(this); -------------------------------------------------------------------------------- /js/alg_jison.js: -------------------------------------------------------------------------------- 1 | /* 2 | # License 3 | 4 | The MIT License (MIT) 5 | 6 | Copyright (c) 2014 Lucas Garron 7 | 8 | Permission is hereby granted, free of charge, to any person obtaining a copy 9 | of this software and associated documentation files (the "Software"), to deal 10 | in the Software without restriction, including without limitation the rights 11 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | copies of the Software, and to permit persons to whom the Software is 13 | furnished to do so, subject to the following conditions: 14 | 15 | The above copyright notice and this permission notice shall be included in 16 | all copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | THE SOFTWARE. 25 | */ 26 | 27 | /* parser generated by jison 0.4.13 */ 28 | /* 29 | Returns a Parser object of the following structure: 30 | 31 | Parser: { 32 | yy: {} 33 | } 34 | 35 | Parser.prototype: { 36 | yy: {}, 37 | trace: function(), 38 | symbols_: {associative list: name ==> number}, 39 | terminals_: {associative list: number ==> name}, 40 | productions_: [...], 41 | performAction: function anonymous(yytext, yyleng, yylineno, yy, yystate, $$, _$), 42 | table: [...], 43 | defaultActions: {...}, 44 | parseError: function(str, hash), 45 | parse: function(input), 46 | 47 | lexer: { 48 | EOF: 1, 49 | parseError: function(str, hash), 50 | setInput: function(input), 51 | input: function(), 52 | unput: function(str), 53 | more: function(), 54 | less: function(n), 55 | pastInput: function(), 56 | upcomingInput: function(), 57 | showPosition: function(), 58 | test_match: function(regex_match_array, rule_index), 59 | next: function(), 60 | lex: function(), 61 | begin: function(condition), 62 | popState: function(), 63 | _currentRules: function(), 64 | topState: function(), 65 | pushState: function(condition), 66 | 67 | options: { 68 | ranges: boolean (optional: true ==> token location info will include a .range[] member) 69 | flex: boolean (optional: true ==> flex-like lexing behaviour where the rules are tested exhaustively to find the longest match) 70 | backtrack_lexer: boolean (optional: true ==> lexer regexes are tested in order and for each matching regex the action code is invoked; the lexer terminates the scan when a token is returned by the action code) 71 | }, 72 | 73 | performAction: function(yy, yy_, $avoiding_name_collisions, YY_START), 74 | rules: [...], 75 | conditions: {associative list: name ==> set}, 76 | } 77 | } 78 | 79 | 80 | token location info (@$, _$, etc.): { 81 | first_line: n, 82 | last_line: n, 83 | first_column: n, 84 | last_column: n, 85 | range: [start_number, end_number] (where the numbers are indexes into the input string, regular zero-based) 86 | } 87 | 88 | 89 | the parseError function receives a 'hash' object with these members for lexer and parser errors: { 90 | text: (matched text) 91 | token: (the produced terminal token, if any) 92 | line: (yylineno) 93 | } 94 | while parser (grammar) errors will also provide these members, i.e. parser errors deliver a superset of attributes: { 95 | loc: (yylloc) 96 | expected: (string describing the set of expected tokens) 97 | recoverable: (boolean: TRUE when the parser has a error recovery rule available for this particular error) 98 | } 99 | */ 100 | var alg_jison = (function(){ 101 | var parser = {trace: function trace() { }, 102 | yy: {}, 103 | symbols_: {"error":2,"expressions":3,"TOP_LEVEL_ALG":4,"EOF":5,"OPTIONAL_WHITESPACE":6,"LAYER":7,"NUMBER":8,"REPETITION":9,"AMOUNT":10,"PRIME":11,"COMMENT":12,"COMMENT_SHORT":13,"COMMENT_LONG":14,"BASE_WIDE":15,"BASE_W":16,"BASE_LOWERCASE":17,"BASE":18,"BASE_UPPERCASE":19,"BASE_ROTATION":20,"BASE_SLICE":21,"BLOCK":22,"DASH":23,"TIMESTAMP":24,"AT":25,"FLOAT":26,"SECONDS":27,"WHITESPACE":28,"REPEATABLE":29,"OPEN_BRACKET":30,"NESTED_ALG":31,"COMMA":32,"CLOSE_BRACKET":33,"COLON":34,"OPEN_PARENTHESIS":35,"CLOSE_PARENTHESIS":36,"REPEATED":37,"PAUSE":38,"NEWLINE":39,"$accept":0,"$end":1}, 104 | terminals_: {2:"error",5:"EOF",8:"NUMBER",11:"PRIME",13:"COMMENT_SHORT",14:"COMMENT_LONG",16:"BASE_W",17:"BASE_LOWERCASE",19:"BASE_UPPERCASE",20:"BASE_ROTATION",21:"BASE_SLICE",23:"DASH",25:"AT",26:"FLOAT",27:"SECONDS",28:"WHITESPACE",30:"OPEN_BRACKET",32:"COMMA",33:"CLOSE_BRACKET",34:"COLON",35:"OPEN_PARENTHESIS",36:"CLOSE_PARENTHESIS",38:"PAUSE",39:"NEWLINE"}, 105 | productions_: [0,[3,2],[3,2],[7,1],[9,1],[10,1],[10,2],[10,1],[12,1],[12,1],[15,1],[15,1],[18,1],[18,1],[18,1],[18,1],[22,1],[22,2],[22,2],[22,4],[24,3],[6,2],[6,0],[29,1],[29,5],[29,5],[29,3],[37,1],[37,2],[37,1],[37,1],[37,1],[31,3],[31,2],[4,1],[4,3],[4,2]], 106 | performAction: function anonymous(yytext, yyleng, yylineno, yy, yystate /* action[1] */, $$ /* vstack */, _$ /* lstack */) { 107 | /* this == yyval */ 108 | 109 | var $0 = $$.length - 1; 110 | switch (yystate) { 111 | case 1: return $$[$0-1]; 112 | break; 113 | case 2: return []; 114 | break; 115 | case 3:this.$ = parseInt($$[$0]); 116 | break; 117 | case 4:this.$ = parseInt($$[$0]); 118 | break; 119 | case 6:this.$ = -$$[$0-1]; 120 | break; 121 | case 7:this.$ = -1; 122 | break; 123 | case 8:this.$ = {type: "comment_short", comment: $$[$0]}; 124 | break; 125 | case 9:this.$ = {type: "comment_long", comment: $$[$0]}; 126 | break; 127 | case 16:this.$ = {type: "move", base: $$[$0]}; 128 | break; 129 | case 17:this.$ = {type: "move", base: $$[$0], layer: $$[$0-1]}; 130 | break; 131 | case 18:this.$ = {type: "move", base: $$[$0], endLayer: $$[$0-1]}; 132 | break; 133 | case 19:this.$ = {type: "move", base: $$[$0], startLayer: $$[$0-3], endLayer: $$[$0-1]}; 134 | break; 135 | case 20:this.$ = {type: "timestamp", time: parseFloat($$[$0-1])}; 136 | break; 137 | case 24:this.$ = {"type": "commutator", "A": $$[$0-3], "B": $$[$0-1]}; 138 | break; 139 | case 25:this.$ = {"type": "conjugate", "A": $$[$0-3], "B": $$[$0-1]}; 140 | break; 141 | case 26:this.$ = {"type": "group", "A": $$[$0-1]}; 142 | break; 143 | case 27:$$[$0].amount = 1; this.$ = $$[$0]; 144 | break; 145 | case 28:$$[$0-1].amount = $$[$0]; this.$ = $$[$0-1]; 146 | break; 147 | case 29:this.$ = {type: "pause"}; 148 | break; 149 | case 30:this.$ = {type: "newline"}; 150 | break; 151 | case 32:this.$ = [$$[$0-1]]; $$[$0-1].location = _$[$0-1]; 152 | break; 153 | case 33:this.$ = $$[$0-1].concat($$[$0]); 154 | break; 155 | case 35:this.$ = [$$[$0-1]]; $$[$0-1].location = _$[$0-1]; 156 | break; 157 | case 36:this.$ = $$[$0-1].concat($$[$0]); 158 | break; 159 | } 160 | }, 161 | table: [{3:1,4:2,5:[2,22],6:3,8:[2,22],13:[2,22],14:[2,22],16:[2,22],17:[2,22],19:[2,22],20:[2,22],21:[2,22],25:[2,22],28:[1,5],30:[2,22],31:4,35:[2,22],38:[2,22],39:[2,22]},{1:[3]},{4:7,5:[1,6],6:8,8:[2,22],13:[2,22],14:[2,22],16:[2,22],17:[2,22],19:[2,22],20:[2,22],21:[2,22],25:[2,22],28:[1,5],30:[2,22],31:4,35:[2,22],38:[2,22],39:[2,22]},{5:[1,9],7:23,8:[1,28],12:16,13:[1,20],14:[1,21],15:24,16:[1,29],17:[1,30],18:22,19:[1,25],20:[1,26],21:[1,27],22:17,24:10,25:[1,12],29:13,30:[1,18],35:[1,19],37:11,38:[1,14],39:[1,15]},{5:[2,34],6:32,8:[2,34],13:[2,34],14:[2,34],16:[2,34],17:[2,34],19:[2,34],20:[2,34],21:[2,34],25:[2,34],28:[1,5],30:[2,34],31:31,35:[2,34],38:[2,34],39:[2,34]},{5:[2,22],6:33,8:[2,22],13:[2,22],14:[2,22],16:[2,22],17:[2,22],19:[2,22],20:[2,22],21:[2,22],25:[2,22],28:[1,5],30:[2,22],32:[2,22],33:[2,22],34:[2,22],35:[2,22],36:[2,22],38:[2,22],39:[2,22]},{1:[2,1]},{4:7,5:[2,36],6:8,8:[2,36],13:[2,36],14:[2,36],16:[2,36],17:[2,36],19:[2,36],20:[2,36],21:[2,36],25:[2,36],28:[1,5],30:[2,36],31:4,35:[2,36],38:[2,36],39:[2,36]},{7:23,8:[1,28],12:16,13:[1,20],14:[1,21],15:24,16:[1,29],17:[1,30],18:22,19:[1,25],20:[1,26],21:[1,27],22:17,24:10,25:[1,12],29:13,30:[1,18],35:[1,19],37:11,38:[1,14],39:[1,15]},{1:[2,2]},{5:[2,22],6:34,8:[2,22],13:[2,22],14:[2,22],16:[2,22],17:[2,22],19:[2,22],20:[2,22],21:[2,22],25:[2,22],28:[1,5],30:[2,22],35:[2,22],38:[2,22],39:[2,22]},{5:[2,22],6:35,8:[2,22],13:[2,22],14:[2,22],16:[2,22],17:[2,22],19:[2,22],20:[2,22],21:[2,22],25:[2,22],28:[1,5],30:[2,22],32:[2,22],33:[2,22],34:[2,22],35:[2,22],36:[2,22],38:[2,22],39:[2,22]},{26:[1,36]},{5:[2,27],8:[1,40],9:38,10:37,11:[1,39],13:[2,27],14:[2,27],16:[2,27],17:[2,27],19:[2,27],20:[2,27],21:[2,27],25:[2,27],28:[2,27],30:[2,27],32:[2,27],33:[2,27],34:[2,27],35:[2,27],36:[2,27],38:[2,27],39:[2,27]},{5:[2,29],8:[2,29],13:[2,29],14:[2,29],16:[2,29],17:[2,29],19:[2,29],20:[2,29],21:[2,29],25:[2,29],28:[2,29],30:[2,29],32:[2,29],33:[2,29],34:[2,29],35:[2,29],36:[2,29],38:[2,29],39:[2,29]},{5:[2,30],8:[2,30],13:[2,30],14:[2,30],16:[2,30],17:[2,30],19:[2,30],20:[2,30],21:[2,30],25:[2,30],28:[2,30],30:[2,30],32:[2,30],33:[2,30],34:[2,30],35:[2,30],36:[2,30],38:[2,30],39:[2,30]},{5:[2,31],8:[2,31],13:[2,31],14:[2,31],16:[2,31],17:[2,31],19:[2,31],20:[2,31],21:[2,31],25:[2,31],28:[2,31],30:[2,31],32:[2,31],33:[2,31],34:[2,31],35:[2,31],36:[2,31],38:[2,31],39:[2,31]},{5:[2,23],8:[2,23],11:[2,23],13:[2,23],14:[2,23],16:[2,23],17:[2,23],19:[2,23],20:[2,23],21:[2,23],25:[2,23],28:[2,23],30:[2,23],32:[2,23],33:[2,23],34:[2,23],35:[2,23],36:[2,23],38:[2,23],39:[2,23]},{6:32,8:[2,22],13:[2,22],14:[2,22],16:[2,22],17:[2,22],19:[2,22],20:[2,22],21:[2,22],28:[1,5],30:[2,22],31:41,35:[2,22],38:[2,22],39:[2,22]},{6:32,8:[2,22],13:[2,22],14:[2,22],16:[2,22],17:[2,22],19:[2,22],20:[2,22],21:[2,22],28:[1,5],30:[2,22],31:42,35:[2,22],38:[2,22],39:[2,22]},{5:[2,8],8:[2,8],13:[2,8],14:[2,8],16:[2,8],17:[2,8],19:[2,8],20:[2,8],21:[2,8],25:[2,8],28:[2,8],30:[2,8],32:[2,8],33:[2,8],34:[2,8],35:[2,8],36:[2,8],38:[2,8],39:[2,8]},{5:[2,9],8:[2,9],13:[2,9],14:[2,9],16:[2,9],17:[2,9],19:[2,9],20:[2,9],21:[2,9],25:[2,9],28:[2,9],30:[2,9],32:[2,9],33:[2,9],34:[2,9],35:[2,9],36:[2,9],38:[2,9],39:[2,9]},{5:[2,16],8:[2,16],11:[2,16],13:[2,16],14:[2,16],16:[2,16],17:[2,16],19:[2,16],20:[2,16],21:[2,16],25:[2,16],28:[2,16],30:[2,16],32:[2,16],33:[2,16],34:[2,16],35:[2,16],36:[2,16],38:[2,16],39:[2,16]},{15:44,16:[1,29],17:[1,30],19:[1,43],23:[1,45]},{5:[2,12],8:[2,12],11:[2,12],13:[2,12],14:[2,12],16:[2,12],17:[2,12],19:[2,12],20:[2,12],21:[2,12],25:[2,12],28:[2,12],30:[2,12],32:[2,12],33:[2,12],34:[2,12],35:[2,12],36:[2,12],38:[2,12],39:[2,12]},{5:[2,13],8:[2,13],11:[2,13],13:[2,13],14:[2,13],16:[2,13],17:[2,13],19:[2,13],20:[2,13],21:[2,13],25:[2,13],28:[2,13],30:[2,13],32:[2,13],33:[2,13],34:[2,13],35:[2,13],36:[2,13],38:[2,13],39:[2,13]},{5:[2,14],8:[2,14],11:[2,14],13:[2,14],14:[2,14],16:[2,14],17:[2,14],19:[2,14],20:[2,14],21:[2,14],25:[2,14],28:[2,14],30:[2,14],32:[2,14],33:[2,14],34:[2,14],35:[2,14],36:[2,14],38:[2,14],39:[2,14]},{5:[2,15],8:[2,15],11:[2,15],13:[2,15],14:[2,15],16:[2,15],17:[2,15],19:[2,15],20:[2,15],21:[2,15],25:[2,15],28:[2,15],30:[2,15],32:[2,15],33:[2,15],34:[2,15],35:[2,15],36:[2,15],38:[2,15],39:[2,15]},{16:[2,3],17:[2,3],19:[2,3],23:[2,3]},{5:[2,10],8:[2,10],11:[2,10],13:[2,10],14:[2,10],16:[2,10],17:[2,10],19:[2,10],20:[2,10],21:[2,10],25:[2,10],28:[2,10],30:[2,10],32:[2,10],33:[2,10],34:[2,10],35:[2,10],36:[2,10],38:[2,10],39:[2,10]},{5:[2,11],8:[2,11],11:[2,11],13:[2,11],14:[2,11],16:[2,11],17:[2,11],19:[2,11],20:[2,11],21:[2,11],25:[2,11],28:[2,11],30:[2,11],32:[2,11],33:[2,11],34:[2,11],35:[2,11],36:[2,11],38:[2,11],39:[2,11]},{5:[2,33],6:32,8:[2,33],13:[2,33],14:[2,33],16:[2,33],17:[2,33],19:[2,33],20:[2,33],21:[2,33],25:[2,33],28:[1,5],30:[2,33],31:31,32:[2,33],33:[2,33],34:[2,33],35:[2,33],36:[2,33],38:[2,33],39:[2,33]},{7:23,8:[1,28],12:16,13:[1,20],14:[1,21],15:24,16:[1,29],17:[1,30],18:22,19:[1,25],20:[1,26],21:[1,27],22:17,29:13,30:[1,18],35:[1,19],37:11,38:[1,14],39:[1,15]},{5:[2,21],8:[2,21],13:[2,21],14:[2,21],16:[2,21],17:[2,21],19:[2,21],20:[2,21],21:[2,21],25:[2,21],28:[2,21],30:[2,21],32:[2,21],33:[2,21],34:[2,21],35:[2,21],36:[2,21],38:[2,21],39:[2,21]},{5:[2,35],8:[2,35],13:[2,35],14:[2,35],16:[2,35],17:[2,35],19:[2,35],20:[2,35],21:[2,35],25:[2,35],28:[2,35],30:[2,35],35:[2,35],38:[2,35],39:[2,35]},{5:[2,32],8:[2,32],13:[2,32],14:[2,32],16:[2,32],17:[2,32],19:[2,32],20:[2,32],21:[2,32],25:[2,32],28:[2,32],30:[2,32],32:[2,32],33:[2,32],34:[2,32],35:[2,32],36:[2,32],38:[2,32],39:[2,32]},{27:[1,46]},{5:[2,28],8:[2,28],13:[2,28],14:[2,28],16:[2,28],17:[2,28],19:[2,28],20:[2,28],21:[2,28],25:[2,28],28:[2,28],30:[2,28],32:[2,28],33:[2,28],34:[2,28],35:[2,28],36:[2,28],38:[2,28],39:[2,28]},{5:[2,5],8:[2,5],11:[1,47],13:[2,5],14:[2,5],16:[2,5],17:[2,5],19:[2,5],20:[2,5],21:[2,5],25:[2,5],28:[2,5],30:[2,5],32:[2,5],33:[2,5],34:[2,5],35:[2,5],36:[2,5],38:[2,5],39:[2,5]},{5:[2,7],8:[2,7],13:[2,7],14:[2,7],16:[2,7],17:[2,7],19:[2,7],20:[2,7],21:[2,7],25:[2,7],28:[2,7],30:[2,7],32:[2,7],33:[2,7],34:[2,7],35:[2,7],36:[2,7],38:[2,7],39:[2,7]},{5:[2,4],8:[2,4],11:[2,4],13:[2,4],14:[2,4],16:[2,4],17:[2,4],19:[2,4],20:[2,4],21:[2,4],25:[2,4],28:[2,4],30:[2,4],32:[2,4],33:[2,4],34:[2,4],35:[2,4],36:[2,4],38:[2,4],39:[2,4]},{6:32,8:[2,22],13:[2,22],14:[2,22],16:[2,22],17:[2,22],19:[2,22],20:[2,22],21:[2,22],28:[1,5],30:[2,22],31:31,32:[1,48],34:[1,49],35:[2,22],38:[2,22],39:[2,22]},{6:32,8:[2,22],13:[2,22],14:[2,22],16:[2,22],17:[2,22],19:[2,22],20:[2,22],21:[2,22],28:[1,5],30:[2,22],31:31,35:[2,22],36:[1,50],38:[2,22],39:[2,22]},{5:[2,17],8:[2,17],11:[2,17],13:[2,17],14:[2,17],16:[2,17],17:[2,17],19:[2,17],20:[2,17],21:[2,17],25:[2,17],28:[2,17],30:[2,17],32:[2,17],33:[2,17],34:[2,17],35:[2,17],36:[2,17],38:[2,17],39:[2,17]},{5:[2,18],8:[2,18],11:[2,18],13:[2,18],14:[2,18],16:[2,18],17:[2,18],19:[2,18],20:[2,18],21:[2,18],25:[2,18],28:[2,18],30:[2,18],32:[2,18],33:[2,18],34:[2,18],35:[2,18],36:[2,18],38:[2,18],39:[2,18]},{7:51,8:[1,28]},{5:[2,20],8:[2,20],13:[2,20],14:[2,20],16:[2,20],17:[2,20],19:[2,20],20:[2,20],21:[2,20],25:[2,20],28:[2,20],30:[2,20],35:[2,20],38:[2,20],39:[2,20]},{5:[2,6],8:[2,6],13:[2,6],14:[2,6],16:[2,6],17:[2,6],19:[2,6],20:[2,6],21:[2,6],25:[2,6],28:[2,6],30:[2,6],32:[2,6],33:[2,6],34:[2,6],35:[2,6],36:[2,6],38:[2,6],39:[2,6]},{6:32,8:[2,22],13:[2,22],14:[2,22],16:[2,22],17:[2,22],19:[2,22],20:[2,22],21:[2,22],28:[1,5],30:[2,22],31:52,35:[2,22],38:[2,22],39:[2,22]},{6:32,8:[2,22],13:[2,22],14:[2,22],16:[2,22],17:[2,22],19:[2,22],20:[2,22],21:[2,22],28:[1,5],30:[2,22],31:53,35:[2,22],38:[2,22],39:[2,22]},{5:[2,26],8:[2,26],11:[2,26],13:[2,26],14:[2,26],16:[2,26],17:[2,26],19:[2,26],20:[2,26],21:[2,26],25:[2,26],28:[2,26],30:[2,26],32:[2,26],33:[2,26],34:[2,26],35:[2,26],36:[2,26],38:[2,26],39:[2,26]},{15:54,16:[1,29],17:[1,30]},{6:32,8:[2,22],13:[2,22],14:[2,22],16:[2,22],17:[2,22],19:[2,22],20:[2,22],21:[2,22],28:[1,5],30:[2,22],31:31,33:[1,55],35:[2,22],38:[2,22],39:[2,22]},{6:32,8:[2,22],13:[2,22],14:[2,22],16:[2,22],17:[2,22],19:[2,22],20:[2,22],21:[2,22],28:[1,5],30:[2,22],31:31,33:[1,56],35:[2,22],38:[2,22],39:[2,22]},{5:[2,19],8:[2,19],11:[2,19],13:[2,19],14:[2,19],16:[2,19],17:[2,19],19:[2,19],20:[2,19],21:[2,19],25:[2,19],28:[2,19],30:[2,19],32:[2,19],33:[2,19],34:[2,19],35:[2,19],36:[2,19],38:[2,19],39:[2,19]},{5:[2,24],8:[2,24],11:[2,24],13:[2,24],14:[2,24],16:[2,24],17:[2,24],19:[2,24],20:[2,24],21:[2,24],25:[2,24],28:[2,24],30:[2,24],32:[2,24],33:[2,24],34:[2,24],35:[2,24],36:[2,24],38:[2,24],39:[2,24]},{5:[2,25],8:[2,25],11:[2,25],13:[2,25],14:[2,25],16:[2,25],17:[2,25],19:[2,25],20:[2,25],21:[2,25],25:[2,25],28:[2,25],30:[2,25],32:[2,25],33:[2,25],34:[2,25],35:[2,25],36:[2,25],38:[2,25],39:[2,25]}], 162 | defaultActions: {6:[2,1],9:[2,2]}, 163 | parseError: function parseError(str, hash) { 164 | if (hash.recoverable) { 165 | this.trace(str); 166 | } else { 167 | throw new Error(str); 168 | } 169 | }, 170 | parse: function parse(input) { 171 | var self = this, stack = [0], vstack = [null], lstack = [], table = this.table, yytext = '', yylineno = 0, yyleng = 0, recovering = 0, TERROR = 2, EOF = 1; 172 | var args = lstack.slice.call(arguments, 1); 173 | this.lexer.setInput(input); 174 | this.lexer.yy = this.yy; 175 | this.yy.lexer = this.lexer; 176 | this.yy.parser = this; 177 | if (typeof this.lexer.yylloc == 'undefined') { 178 | this.lexer.yylloc = {}; 179 | } 180 | var yyloc = this.lexer.yylloc; 181 | lstack.push(yyloc); 182 | var ranges = this.lexer.options && this.lexer.options.ranges; 183 | if (typeof this.yy.parseError === 'function') { 184 | this.parseError = this.yy.parseError; 185 | } else { 186 | this.parseError = Object.getPrototypeOf(this).parseError; 187 | } 188 | function popStack(n) { 189 | stack.length = stack.length - 2 * n; 190 | vstack.length = vstack.length - n; 191 | lstack.length = lstack.length - n; 192 | } 193 | function lex() { 194 | var token; 195 | token = self.lexer.lex() || EOF; 196 | if (typeof token !== 'number') { 197 | token = self.symbols_[token] || token; 198 | } 199 | return token; 200 | } 201 | var symbol, preErrorSymbol, state, action, a, r, yyval = {}, p, len, newState, expected; 202 | while (true) { 203 | state = stack[stack.length - 1]; 204 | if (this.defaultActions[state]) { 205 | action = this.defaultActions[state]; 206 | } else { 207 | if (symbol === null || typeof symbol == 'undefined') { 208 | symbol = lex(); 209 | } 210 | action = table[state] && table[state][symbol]; 211 | } 212 | if (typeof action === 'undefined' || !action.length || !action[0]) { 213 | var errStr = ''; 214 | expected = []; 215 | for (p in table[state]) { 216 | if (this.terminals_[p] && p > TERROR) { 217 | expected.push('\'' + this.terminals_[p] + '\''); 218 | } 219 | } 220 | if (this.lexer.showPosition) { 221 | errStr = 'Parse error on line ' + (yylineno + 1) + ':\n' + this.lexer.showPosition() + '\nExpecting ' + expected.join(', ') + ', got \'' + (this.terminals_[symbol] || symbol) + '\''; 222 | } else { 223 | errStr = 'Parse error on line ' + (yylineno + 1) + ': Unexpected ' + (symbol == EOF ? 'end of input' : '\'' + (this.terminals_[symbol] || symbol) + '\''); 224 | } 225 | this.parseError(errStr, { 226 | text: this.lexer.match, 227 | token: this.terminals_[symbol] || symbol, 228 | line: this.lexer.yylineno, 229 | loc: yyloc, 230 | expected: expected 231 | }); 232 | } 233 | if (action[0] instanceof Array && action.length > 1) { 234 | throw new Error('Parse Error: multiple actions possible at state: ' + state + ', token: ' + symbol); 235 | } 236 | switch (action[0]) { 237 | case 1: 238 | stack.push(symbol); 239 | vstack.push(this.lexer.yytext); 240 | lstack.push(this.lexer.yylloc); 241 | stack.push(action[1]); 242 | symbol = null; 243 | if (!preErrorSymbol) { 244 | yyleng = this.lexer.yyleng; 245 | yytext = this.lexer.yytext; 246 | yylineno = this.lexer.yylineno; 247 | yyloc = this.lexer.yylloc; 248 | if (recovering > 0) { 249 | recovering--; 250 | } 251 | } else { 252 | symbol = preErrorSymbol; 253 | preErrorSymbol = null; 254 | } 255 | break; 256 | case 2: 257 | len = this.productions_[action[1]][1]; 258 | yyval.$ = vstack[vstack.length - len]; 259 | yyval._$ = { 260 | first_line: lstack[lstack.length - (len || 1)].first_line, 261 | last_line: lstack[lstack.length - 1].last_line, 262 | first_column: lstack[lstack.length - (len || 1)].first_column, 263 | last_column: lstack[lstack.length - 1].last_column 264 | }; 265 | if (ranges) { 266 | yyval._$.range = [ 267 | lstack[lstack.length - (len || 1)].range[0], 268 | lstack[lstack.length - 1].range[1] 269 | ]; 270 | } 271 | r = this.performAction.apply(yyval, [ 272 | yytext, 273 | yyleng, 274 | yylineno, 275 | this.yy, 276 | action[1], 277 | vstack, 278 | lstack 279 | ].concat(args)); 280 | if (typeof r !== 'undefined') { 281 | return r; 282 | } 283 | if (len) { 284 | stack = stack.slice(0, -1 * len * 2); 285 | vstack = vstack.slice(0, -1 * len); 286 | lstack = lstack.slice(0, -1 * len); 287 | } 288 | stack.push(this.productions_[action[1]][0]); 289 | vstack.push(yyval.$); 290 | lstack.push(yyval._$); 291 | newState = table[stack[stack.length - 2]][stack[stack.length - 1]]; 292 | stack.push(newState); 293 | break; 294 | case 3: 295 | return true; 296 | } 297 | } 298 | return true; 299 | }}; 300 | /* generated by jison-lex 0.2.1 */ 301 | var lexer = (function(){ 302 | var lexer = { 303 | 304 | EOF:1, 305 | 306 | parseError:function parseError(str, hash) { 307 | if (this.yy.parser) { 308 | this.yy.parser.parseError(str, hash); 309 | } else { 310 | throw new Error(str); 311 | } 312 | }, 313 | 314 | // resets the lexer, sets new input 315 | setInput:function (input) { 316 | this._input = input; 317 | this._more = this._backtrack = this.done = false; 318 | this.yylineno = this.yyleng = 0; 319 | this.yytext = this.matched = this.match = ''; 320 | this.conditionStack = ['INITIAL']; 321 | this.yylloc = { 322 | first_line: 1, 323 | first_column: 0, 324 | last_line: 1, 325 | last_column: 0 326 | }; 327 | if (this.options.ranges) { 328 | this.yylloc.range = [0,0]; 329 | } 330 | this.offset = 0; 331 | return this; 332 | }, 333 | 334 | // consumes and returns one char from the input 335 | input:function () { 336 | var ch = this._input[0]; 337 | this.yytext += ch; 338 | this.yyleng++; 339 | this.offset++; 340 | this.match += ch; 341 | this.matched += ch; 342 | var lines = ch.match(/(?:\r\n?|\n).*/g); 343 | if (lines) { 344 | this.yylineno++; 345 | this.yylloc.last_line++; 346 | } else { 347 | this.yylloc.last_column++; 348 | } 349 | if (this.options.ranges) { 350 | this.yylloc.range[1]++; 351 | } 352 | 353 | this._input = this._input.slice(1); 354 | return ch; 355 | }, 356 | 357 | // unshifts one char (or a string) into the input 358 | unput:function (ch) { 359 | var len = ch.length; 360 | var lines = ch.split(/(?:\r\n?|\n)/g); 361 | 362 | this._input = ch + this._input; 363 | this.yytext = this.yytext.substr(0, this.yytext.length - len - 1); 364 | //this.yyleng -= len; 365 | this.offset -= len; 366 | var oldLines = this.match.split(/(?:\r\n?|\n)/g); 367 | this.match = this.match.substr(0, this.match.length - 1); 368 | this.matched = this.matched.substr(0, this.matched.length - 1); 369 | 370 | if (lines.length - 1) { 371 | this.yylineno -= lines.length - 1; 372 | } 373 | var r = this.yylloc.range; 374 | 375 | this.yylloc = { 376 | first_line: this.yylloc.first_line, 377 | last_line: this.yylineno + 1, 378 | first_column: this.yylloc.first_column, 379 | last_column: lines ? 380 | (lines.length === oldLines.length ? this.yylloc.first_column : 0) 381 | + oldLines[oldLines.length - lines.length].length - lines[0].length : 382 | this.yylloc.first_column - len 383 | }; 384 | 385 | if (this.options.ranges) { 386 | this.yylloc.range = [r[0], r[0] + this.yyleng - len]; 387 | } 388 | this.yyleng = this.yytext.length; 389 | return this; 390 | }, 391 | 392 | // When called from action, caches matched text and appends it on next action 393 | more:function () { 394 | this._more = true; 395 | return this; 396 | }, 397 | 398 | // When called from action, signals the lexer that this rule fails to match the input, so the next matching rule (regex) should be tested instead. 399 | reject:function () { 400 | if (this.options.backtrack_lexer) { 401 | this._backtrack = true; 402 | } else { 403 | return this.parseError('Lexical error on line ' + (this.yylineno + 1) + '. You can only invoke reject() in the lexer when the lexer is of the backtracking persuasion (options.backtrack_lexer = true).\n' + this.showPosition(), { 404 | text: "", 405 | token: null, 406 | line: this.yylineno 407 | }); 408 | 409 | } 410 | return this; 411 | }, 412 | 413 | // retain first n characters of the match 414 | less:function (n) { 415 | this.unput(this.match.slice(n)); 416 | }, 417 | 418 | // displays already matched input, i.e. for error messages 419 | pastInput:function () { 420 | var past = this.matched.substr(0, this.matched.length - this.match.length); 421 | return (past.length > 20 ? '...':'') + past.substr(-20).replace(/\n/g, ""); 422 | }, 423 | 424 | // displays upcoming input, i.e. for error messages 425 | upcomingInput:function () { 426 | var next = this.match; 427 | if (next.length < 20) { 428 | next += this._input.substr(0, 20-next.length); 429 | } 430 | return (next.substr(0,20) + (next.length > 20 ? '...' : '')).replace(/\n/g, ""); 431 | }, 432 | 433 | // displays the character position where the lexing error occurred, i.e. for error messages 434 | showPosition:function () { 435 | var pre = this.pastInput(); 436 | var c = new Array(pre.length + 1).join("-"); 437 | return pre + this.upcomingInput() + "\n" + c + "^"; 438 | }, 439 | 440 | // test the lexed token: return FALSE when not a match, otherwise return token 441 | test_match:function (match, indexed_rule) { 442 | var token, 443 | lines, 444 | backup; 445 | 446 | if (this.options.backtrack_lexer) { 447 | // save context 448 | backup = { 449 | yylineno: this.yylineno, 450 | yylloc: { 451 | first_line: this.yylloc.first_line, 452 | last_line: this.last_line, 453 | first_column: this.yylloc.first_column, 454 | last_column: this.yylloc.last_column 455 | }, 456 | yytext: this.yytext, 457 | match: this.match, 458 | matches: this.matches, 459 | matched: this.matched, 460 | yyleng: this.yyleng, 461 | offset: this.offset, 462 | _more: this._more, 463 | _input: this._input, 464 | yy: this.yy, 465 | conditionStack: this.conditionStack.slice(0), 466 | done: this.done 467 | }; 468 | if (this.options.ranges) { 469 | backup.yylloc.range = this.yylloc.range.slice(0); 470 | } 471 | } 472 | 473 | lines = match[0].match(/(?:\r\n?|\n).*/g); 474 | if (lines) { 475 | this.yylineno += lines.length; 476 | } 477 | this.yylloc = { 478 | first_line: this.yylloc.last_line, 479 | last_line: this.yylineno + 1, 480 | first_column: this.yylloc.last_column, 481 | last_column: lines ? 482 | lines[lines.length - 1].length - lines[lines.length - 1].match(/\r?\n?/)[0].length : 483 | this.yylloc.last_column + match[0].length 484 | }; 485 | this.yytext += match[0]; 486 | this.match += match[0]; 487 | this.matches = match; 488 | this.yyleng = this.yytext.length; 489 | if (this.options.ranges) { 490 | this.yylloc.range = [this.offset, this.offset += this.yyleng]; 491 | } 492 | this._more = false; 493 | this._backtrack = false; 494 | this._input = this._input.slice(match[0].length); 495 | this.matched += match[0]; 496 | token = this.performAction.call(this, this.yy, this, indexed_rule, this.conditionStack[this.conditionStack.length - 1]); 497 | if (this.done && this._input) { 498 | this.done = false; 499 | } 500 | if (token) { 501 | return token; 502 | } else if (this._backtrack) { 503 | // recover context 504 | for (var k in backup) { 505 | this[k] = backup[k]; 506 | } 507 | return false; // rule action called reject() implying the next rule should be tested instead. 508 | } 509 | return false; 510 | }, 511 | 512 | // return next match in input 513 | next:function () { 514 | if (this.done) { 515 | return this.EOF; 516 | } 517 | if (!this._input) { 518 | this.done = true; 519 | } 520 | 521 | var token, 522 | match, 523 | tempMatch, 524 | index; 525 | if (!this._more) { 526 | this.yytext = ''; 527 | this.match = ''; 528 | } 529 | var rules = this._currentRules(); 530 | for (var i = 0; i < rules.length; i++) { 531 | tempMatch = this._input.match(this.rules[rules[i]]); 532 | if (tempMatch && (!match || tempMatch[0].length > match[0].length)) { 533 | match = tempMatch; 534 | index = i; 535 | if (this.options.backtrack_lexer) { 536 | token = this.test_match(tempMatch, rules[i]); 537 | if (token !== false) { 538 | return token; 539 | } else if (this._backtrack) { 540 | match = false; 541 | continue; // rule action called reject() implying a rule MISmatch. 542 | } else { 543 | // else: this is a lexer rule which consumes input without producing a token (e.g. whitespace) 544 | return false; 545 | } 546 | } else if (!this.options.flex) { 547 | break; 548 | } 549 | } 550 | } 551 | if (match) { 552 | token = this.test_match(match, rules[index]); 553 | if (token !== false) { 554 | return token; 555 | } 556 | // else: this is a lexer rule which consumes input without producing a token (e.g. whitespace) 557 | return false; 558 | } 559 | if (this._input === "") { 560 | return this.EOF; 561 | } else { 562 | return this.parseError('Lexical error on line ' + (this.yylineno + 1) + '. Unrecognized text.\n' + this.showPosition(), { 563 | text: "", 564 | token: null, 565 | line: this.yylineno 566 | }); 567 | } 568 | }, 569 | 570 | // return next match that has a token 571 | lex:function lex() { 572 | var r = this.next(); 573 | if (r) { 574 | return r; 575 | } else { 576 | return this.lex(); 577 | } 578 | }, 579 | 580 | // activates a new lexer condition state (pushes the new lexer condition state onto the condition stack) 581 | begin:function begin(condition) { 582 | this.conditionStack.push(condition); 583 | }, 584 | 585 | // pop the previously active lexer condition state off the condition stack 586 | popState:function popState() { 587 | var n = this.conditionStack.length - 1; 588 | if (n > 0) { 589 | return this.conditionStack.pop(); 590 | } else { 591 | return this.conditionStack[0]; 592 | } 593 | }, 594 | 595 | // produce the lexer rule set which is active for the currently active lexer condition state 596 | _currentRules:function _currentRules() { 597 | if (this.conditionStack.length && this.conditionStack[this.conditionStack.length - 1]) { 598 | return this.conditions[this.conditionStack[this.conditionStack.length - 1]].rules; 599 | } else { 600 | return this.conditions["INITIAL"].rules; 601 | } 602 | }, 603 | 604 | // return the currently active lexer condition state; when an index argument is provided it produces the N-th previous condition state, if available 605 | topState:function topState(n) { 606 | n = this.conditionStack.length - 1 - Math.abs(n || 0); 607 | if (n >= 0) { 608 | return this.conditionStack[n]; 609 | } else { 610 | return "INITIAL"; 611 | } 612 | }, 613 | 614 | // alias for begin(condition) 615 | pushState:function pushState(condition) { 616 | this.begin(condition); 617 | }, 618 | 619 | // return the number of states currently on the stack 620 | stateStackSize:function stateStackSize() { 621 | return this.conditionStack.length; 622 | }, 623 | options: {}, 624 | performAction: function anonymous(yy,yy_,$avoiding_name_collisions,YY_START) { 625 | 626 | var YYSTATE=YY_START; 627 | switch($avoiding_name_collisions) { 628 | case 0: this.begin("timestamp"); return 25 629 | break; 630 | case 1:return 26 631 | break; 632 | case 2: this.popState(); return 27 633 | break; 634 | case 3:return "WHITESPACE" 635 | break; 636 | case 4:return "NUMBER" 637 | break; 638 | case 5:return "DASH" 639 | break; 640 | case 6:return "BASE_W" 641 | break; 642 | case 7:return "BASE_UPPERCASE" 643 | break; 644 | case 8:return "BASE_LOWERCASE" 645 | break; 646 | case 9:return "BASE_ROTATION" 647 | break; 648 | case 10:return "BASE_SLICE" 649 | break; 650 | case 11:return "PRIME" 651 | break; 652 | case 12:return "PAUSE" 653 | break; 654 | case 13:return "COMMENT_SHORT" 655 | break; 656 | case 14:return "COMMENT_LONG" 657 | break; 658 | case 15:return "NEWLINE" 659 | break; 660 | case 16:return "OPEN_BRACKET" 661 | break; 662 | case 17:return "CLOSE_BRACKET" 663 | break; 664 | case 18:return "OPEN_PARENTHESIS" 665 | break; 666 | case 19:return "CLOSE_PARENTHESIS" 667 | break; 668 | case 20:return "COMMA" 669 | break; 670 | case 21:return "COLON" 671 | break; 672 | case 22:return "EOF" 673 | break; 674 | case 23:return "INVALID" 675 | break; 676 | } 677 | }, 678 | rules: [/^(?:@)/,/^(?:[0-9]+(\.[0-9]+)?)/,/^(?:s\b)/,/^(?:[^\S\r\n]+)/,/^(?:[0-9]+)/,/^(?:-)/,/^(?:(Rw|Fw|Uw|Bw|Lw|Dw))/,/^(?:(R|F|U|B|L|D))/,/^(?:(r|f|u|b|l|d))/,/^(?:(x|y|z))/,/^(?:(M|E|S|m|e|s))/,/^(?:')/,/^(?:\.)/,/^(?:\/\/[^\n\r]*)/,/^(?:\/\*[^]*?\*\/)/,/^(?:[\n\r])/,/^(?:\[)/,/^(?:\])/,/^(?:\()/,/^(?:\))/,/^(?:,)/,/^(?::)/,/^(?:$)/,/^(?:.)/], 679 | conditions: {"timestamp":{"rules":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23],"inclusive":true},"INITIAL":{"rules":[0,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23],"inclusive":true}} 680 | }; 681 | return lexer; 682 | })(); 683 | parser.lexer = lexer; 684 | function Parser () { 685 | this.yy = {}; 686 | } 687 | Parser.prototype = parser;parser.Parser = Parser; 688 | return new Parser; 689 | })(); 690 | 691 | 692 | if (typeof require !== 'undefined' && typeof exports !== 'undefined') { 693 | exports.parser = alg_jison; 694 | exports.Parser = alg_jison.Parser; 695 | exports.parse = function () { return alg_jison.parse.apply(alg_jison, arguments); }; 696 | exports.main = function commonjsMain(args) { 697 | if (!args[1]) { 698 | console.log('Usage: '+args[0]+' FILE'); 699 | process.exit(1); 700 | } 701 | var source = require('fs').readFileSync(require('path').normalize(args[1]), "utf8"); 702 | return exports.parser.parse(source); 703 | }; 704 | if (typeof module !== 'undefined' && require.main === module) { 705 | exports.main(process.argv.slice(1)); 706 | } 707 | } --------------------------------------------------------------------------------