Skip to content

Commit d7e090d

Browse files
committed
Refactor cube moves
1 parent 127f447 commit d7e090d

1 file changed

Lines changed: 35 additions & 23 deletions

File tree

magiccube/cube_move.py

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -95,34 +95,35 @@ def __init__(self, move_type: CubeMoveType, is_reversed: bool = False, wide: boo
9595
"""Number of repetitions of the move"""
9696

9797
@staticmethod
98-
def _create_move(result, special_move):
99-
if special_move is not None:
100-
is_reversed = result[-2] == "'"
101-
if special_move in ("X", "x"):
98+
def _create_move(layer_str, move_type_str, wide_str, special_move_str, is_reversed_str):
99+
100+
if special_move_str is not None:
101+
is_reversed = is_reversed_str == "'"
102+
if special_move_str in ("X", "x"):
102103
return CubeMove(CubeMoveType.X, is_reversed)
103-
if special_move in ("Y", "y"):
104+
if special_move_str in ("Y", "y"):
104105
return CubeMove(CubeMoveType.Y, is_reversed)
105-
if special_move in ("Z", "z"):
106+
if special_move_str in ("Z", "z"):
106107
return CubeMove(CubeMoveType.Z, is_reversed)
107-
if special_move == "M":
108+
if special_move_str == "M":
108109
return CubeMove(CubeMoveType.M, is_reversed)
109-
if special_move == "E":
110+
if special_move_str == "E":
110111
return CubeMove(CubeMoveType.E, is_reversed)
111-
if special_move == "S":
112+
if special_move_str == "S":
112113
return CubeMove(CubeMoveType.S, is_reversed)
113114

114-
raise CubeException(
115-
"Invalid special move") # pragma: no cover
116-
move_type = CubeMoveType.create(result[2])
117-
wide = result[3] == "w"
118-
is_reversed = result[-2] == "'"
115+
raise CubeException("Invalid special move") # pragma: no cover
116+
117+
move_type = CubeMoveType.create(move_type_str)
118+
wide = wide_str == "w"
119+
is_reversed = is_reversed_str == "'"
119120

120-
if result[0] == "" and not wide:
121+
if layer_str == "" and not wide:
121122
layer = 1
122-
elif result[0] == "" and wide:
123+
elif layer_str == "" and wide:
123124
layer = 2
124125
else:
125-
layer = int(result[0])
126+
layer = int(layer_str)
126127

127128
move = CubeMove(move_type, is_reversed, wide, layer)
128129
return move
@@ -135,12 +136,23 @@ def create(move_str: str):
135136
result_match = CubeMove._regex_pattern.match(move_str)
136137
if result_match is None:
137138
raise CubeException("invalid movement " + str(move_str))
138-
result = result_match.groups()
139-
special_move = result[4]
140-
move_count = result[-1]
141-
142-
move = CubeMove._create_move(result, special_move)
143-
move.count = int(move_count) if move_count else 1
139+
move_parameters = result_match.groups()
140+
141+
layer_str = move_parameters[0]
142+
move_type_str = move_parameters[2]
143+
wide_str = move_parameters[3]
144+
special_move_str = move_parameters[4]
145+
is_reversed_str = move_parameters[-2]
146+
move_count_str = move_parameters[-1]
147+
148+
move = CubeMove._create_move(
149+
layer_str,
150+
move_type_str,
151+
wide_str,
152+
special_move_str,
153+
is_reversed_str
154+
)
155+
move.count = int(move_count_str) if move_count_str else 1
144156
return move
145157

146158
def reverse(self):

0 commit comments

Comments
 (0)