Skip to content

Commit

Permalink
Reverse history fix (#16)
Browse files Browse the repository at this point in the history
* Fix move.reverse() by taking care of self.count

* Add test to point out the problem
  • Loading branch information
JulianKarlBauer committed Jul 15, 2024
1 parent e579049 commit b901ee2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ dist/
.coverage
venv/
junit/
.venv*
9 changes: 5 additions & 4 deletions magiccube/cube_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ class CubeMove():
regex_pattern = re.compile(
"^(?:([0-9]*)(([LRDUBF])([w]?)|([XYZMES]))([']?)([0-9]?))$")

def __init__(self, move_type: CubeMoveType, is_reversed: bool = False, wide: bool = False, layer: int = 1):
def __init__(self, move_type: CubeMoveType, is_reversed: bool = False, wide: bool = False, layer: int = 1, count: int = 1):
self.type = move_type
self.is_reversed = is_reversed
self.wide = wide
self.layer = layer
self.count = 1
self.count = count

@staticmethod
def _create_move(result, special_move):
Expand Down Expand Up @@ -132,7 +132,7 @@ def create(move_str: str):

def reverse(self):
"""return the reverse move"""
return CubeMove(self.type, not self.is_reversed, self.wide, self.layer)
return CubeMove(self.type, not self.is_reversed, self.wide, self.layer, count=self.count)

def __str__(self):
if (self.wide and self.layer == 2)\
Expand All @@ -142,7 +142,8 @@ def __str__(self):
layer = self.layer # pragma: no cover
wide = "w" if self.wide else ""
reversed_move = "'" if self.is_reversed else ""
return f"{layer}{self.type.name}{wide}{reversed_move}"
count = "" if int(self.count) == 1 else int(self.count)
return f"{layer}{self.type.name}{wide}{reversed_move}{count}"

def __repr__(self):
return str(self) # pragma: no cover
Expand Down
7 changes: 7 additions & 0 deletions test/test_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ def test_reverse():
c.rotate(c.reverse_history())
assert c.is_done()

def test_reverse_multiplicative_moves():
c = Cube(3)
moves = "R' L2"
c.rotate(moves)
assert c.reverse_history(to_str=True) == "L'2 R"
c.rotate(c.reverse_history())
assert c.is_done()

def test_scramble_3x3():
c = Cube(3)
Expand Down

0 comments on commit b901ee2

Please sign in to comment.