From 64308a9f31a2ef685f2bcceabc6b0a7868aaae47 Mon Sep 17 00:00:00 2001 From: Lars Date: Mon, 20 Feb 2017 20:17:58 -0800 Subject: [PATCH] Implemented an `unhand()` function that converts "hand" notation (M, x, Rw etc) to "cube" notation (BRDFLU). Needs cleanup. --- gulpfile.js | 2 ++ src/Alg.coffee | 22 ++++++++++++++++++++++ src/CompositeMove.coffee | 12 ++++++++++++ src/Move.coffee | 20 ++++++++++++++++++++ test/Alg_spec.coffee | 18 ++++++++++++++++++ test/CompositeMove_spec.coffee | 10 ++++++++++ test/Move_spec.coffee | 8 ++++++++ 7 files changed, 92 insertions(+) diff --git a/gulpfile.js b/gulpfile.js index 68d74ef..be4ee57 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -80,6 +80,8 @@ gulp.task('test', ['compile-test', 'compile-src'], function(){ test_html += '\n'; }); + console.log("\nTests generated. `open rptest.html` can run them!\n") + return gulp.src('misc/rptest_template.html') .pipe(replace('@@TEST_FILES_GO_HERE@@', test_html)) .pipe(rename('rptest.html')) diff --git a/src/Alg.coffee b/src/Alg.coffee index 78153ee..01af155 100644 --- a/src/Alg.coffee +++ b/src/Alg.coffee @@ -93,6 +93,28 @@ class Alg else new Move(code, @world3d, @speed) + unhand: -> + drift = {U: 'U', D: 'D', L: 'L', R: 'R', F: 'F', B: 'B'} + colors = new Colors(drift, "", "") + + result = [] + for move in @moves + drifted_move = this.fwd_drift(move.as_brdflu(), drift) + result.push(drifted_move) + move.track_fwd_drift(drift) + result.join(' ').replace(/[ ]+/g, ' ').replace(/^ +| +$/g, '') + + fwd_drift: (code, side_drift) -> + return code unless code + + inverted_drift = {} + for own key, value of side_drift + inverted_drift[value] = key + inverted_drift[value.toLowerCase()] = key.toLowerCase() + + ((inverted_drift[char] || char) for char in code.split('')).join('') + + @side_drift: (moves) -> new Alg(moves, null, "")._side_drift() diff --git a/src/CompositeMove.coffee b/src/CompositeMove.coffee index 72d6dbf..58532cc 100644 --- a/src/CompositeMove.coffee +++ b/src/CompositeMove.coffee @@ -20,6 +20,9 @@ class CompositeMove track_drift: (side_drift) -> move.track_drift(side_drift) for move in @moves + track_fwd_drift: (side_drift) -> + move.track_fwd_drift(side_drift) for move in @moves + count: (count_rotations) -> return 1 if @official_text @@ -31,6 +34,15 @@ class CompositeMove to_s: -> "(#{(@moves.map (move) -> move.to_s()).join(' ')})" + as_brdflu: -> + result = (@moves.map (move) -> move.as_brdflu()).join(' ').split(' ').sort().join(' ') + + for side in ['B', 'R', 'D', 'F', 'L', 'U'] + result = result.replace("#{side} #{side}'", "") + result = result.replace("#{side}2 #{side}2", "") + result.replace(/[ ]+/g, ' ').replace(/^ +| +$/g, '') + + display_text: (algdisplay) -> if @official_text return Move.displayify(@official_text, algdisplay) diff --git a/src/Move.coffee b/src/Move.coffee index 9ec0d0c..dbf7751 100644 --- a/src/Move.coffee +++ b/src/Move.coffee @@ -40,6 +40,26 @@ class Move if location == cycle[i] side_drift[side] = cycle[(i+@turns+4)% 4] + track_fwd_drift: (side_drift) -> + for cycle in [@layer.cycle1, @layer.cycle2] when cycle[0].length == 1 # center cycle + for side, location of side_drift + for i in [0..3] + if location == cycle[i] + side_drift[side] = cycle[(i-@turns+4)% 4] + + as_brdflu: -> + return '' if @is_rotation + + standard_turn_codes = { 1: '', 2: '2', '-1': "'", '-2': '2'} + t1 = standard_turn_codes[@turns] + t2 = standard_turn_codes[-@turns] + + switch @layer + when Layer.M then "L#{t2} R#{t1}" + when Layer.E then "D#{t2} U#{t1}" + when Layer.S then "B#{t1} F#{t2}" + else this.to_s().replace('Z', '2') + show_do: -> this._do(@turns, true) diff --git a/test/Alg_spec.coffee b/test/Alg_spec.coffee index 55d6846..1bd1879 100644 --- a/test/Alg_spec.coffee +++ b/test/Alg_spec.coffee @@ -136,6 +136,24 @@ describe "Alg", -> expect(Alg.side_drift("F")).to.deep.equal(U: 'U', D: 'D', L: 'L', R: 'R', F: 'F', B: 'B') expect(Alg.side_drift("M")).to.deep.equal(U: 'B', D: 'F', L: 'L', R: 'R', F: 'U', B: 'D') + it "unhand", -> + expect(new Alg("F L2", null, "").unhand()).to.equal("F L2") + expect(new Alg("M", null, "").unhand()).to.equal("L' R") + expect(new Alg("Uw", null, "").unhand()).to.equal("D") + expect(new Alg("F Uw F", null, "").unhand()).to.equal("F D R") + expect(new Alg("F Uw M", null, "").unhand()).to.equal("F D F' B") + expect(new Alg("x2", null, "").unhand()).to.equal("") + + expect(new Alg("F x F y F z F", null, "").unhand()).to.equal("F D R R") + + # real algs from the wild + expect(new Alg("Rw U' L' D L U Rw' B'", null, "").unhand()).to.equal("L F' L' B L F L' B'") + expect(new Alg("x2 y' Lw' U x z' L U L2 U' z' U R U' R' U R' U' R U2 L U L' U2 L' U' L U' L' U L y L' U' L U R2 U' R' U' R U R U R U' R", null, "").unhand()).to.equal("B' L U B U2 B' D F D' F' D F' D' F D2 B D B' D2 B' D' B D' B' D B R' D' R D L2 D' L' D' L D L D L D' L") + + # weird cases + expect(new Alg("F y L2", null, "").unhand()).to.equal("F F2") + + move_should_be = (move, layer, turns, is_rotation = false) -> expect(move.layer, move.to_s()).to.equal(layer) expect(move.turns, move.to_s()).to.equal(turns) diff --git a/test/CompositeMove_spec.coffee b/test/CompositeMove_spec.coffee index 2ca421a..f99f68f 100644 --- a/test/CompositeMove_spec.coffee +++ b/test/CompositeMove_spec.coffee @@ -25,3 +25,13 @@ describe "CompositeMove", -> it "detects impossible move combinations", -> expect(-> new CompositeMove("L+F", {}, 200)).to.throw("Impossible Move combination 'L+F'") expect(-> new CompositeMove("U>+L", {}, 200)).to.not.throw(Error) + + it "#as_brdflu", -> + expect(new CompositeMove("R+M'").as_brdflu()).to.equal("L") # Rw + expect(new CompositeMove("R+M'+L'").as_brdflu()).to.equal("") # x + expect(new CompositeMove("U'+E+D").as_brdflu()).to.equal("") # y' + expect(new CompositeMove("U2+E2+D2").as_brdflu()).to.equal("") # y2 + expect(new CompositeMove("B'+S").as_brdflu()).to.equal("F'") # Bw' + expect(new CompositeMove("B2+S2").as_brdflu()).to.equal("F2") # Bw2 + expect(new CompositeMove("R2+M2+L2").as_brdflu()).to.equal("") # x + expect(new CompositeMove("U>+L").as_brdflu()).to.equal("L") diff --git a/test/Move_spec.coffee b/test/Move_spec.coffee index 96b14d0..b9f49d9 100644 --- a/test/Move_spec.coffee +++ b/test/Move_spec.coffee @@ -71,6 +71,14 @@ describe "Move", -> expect(new Move("U>>").count(false)).to.equal(0) expect(new Move("U>>").count(true)).to.equal(1) + it "#as_brdflu", -> + expect(new Move("U2").as_brdflu()).to.equal("U2") + expect(new Move("M").as_brdflu()).to.equal("L' R") + expect(new Move("M2").as_brdflu()).to.equal("L2 R2") + expect(new Move("M'").as_brdflu()).to.equal("L R'") + expect(new Move("E").as_brdflu()).to.equal("D' U") + expect(new Move("S").as_brdflu()).to.equal("B F'") + move_should_be = (move, layer, turns, is_rotation, turn_time) -> expect(move.layer, move.to_s()).to.equal(layer) expect(move.turns, move.to_s()).to.equal(turns)