Skip to content

Commit

Permalink
Implemented an unhand() function that converts "hand" notation (M, …
Browse files Browse the repository at this point in the history
…x, Rw etc) to "cube" notation (BRDFLU).

Needs cleanup.
  • Loading branch information
larspetrus committed Feb 21, 2017
1 parent dd967c6 commit 64308a9
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 0 deletions.
2 changes: 2 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ gulp.task('test', ['compile-test', 'compile-src'], function(){
test_html += '<script src="'+test_file+'"></script>\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'))
Expand Down
22 changes: 22 additions & 0 deletions src/Alg.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
12 changes: 12 additions & 0 deletions src/CompositeMove.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions src/Move.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
18 changes: 18 additions & 0 deletions test/Alg_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions test/CompositeMove_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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")
8 changes: 8 additions & 0 deletions test/Move_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 64308a9

Please sign in to comment.