Skip to content

Commit 9ede8df

Browse files
committed
use is_clockwise, is_counter_clockwise, Formula, use properties
1 parent 8108cf1 commit 9ede8df

File tree

8 files changed

+67
-45
lines changed

8 files changed

+67
-45
lines changed

pycuber/cube.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ def _single_layer(self, step):
445445
"E": ("UD", "LFRB"),
446446
}[step.face]
447447
if len(movement) == 2: slice_, movement = movement
448-
if step.is_inverse: movement = movement[::-1]
448+
if step.is_counter_clockwise: movement = movement[::-1]
449449
if step.face not in "MSE":
450450
to_move = {c.copy() for c in self.at_face(step.face)}
451451
else:
@@ -491,7 +491,7 @@ def _other_rotations(self, step):
491491
}[step.face]
492492
for s in movement:
493493
step_ = Step(s)
494-
if step.is_inverse: step_ = step_.inverse()
494+
if step.is_counter_clockwise: step_ = step_.inverse()
495495
elif step.is_180: step_ = step_ * 2
496496
_single_layer(self, step_)
497497
return self

pycuber/formula.py

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@ class Step(object):
4040
U'
4141
4242
You can check if it's clockwise (ex: U), counter-clockwise (ex: U'), or 180 degrees (ex: U2)
43-
>>> r.is_standard
43+
>>> r.is_clockwise
4444
True
45-
>>> r.is_inverse
45+
>>> r.is_counter_clockwise
4646
False
4747
>>> r.is_180
4848
False
4949
50-
>>> u_prime.is_standard
50+
>>> u_prime.is_clockwise
5151
False
52-
>>> u_prime.is_inverse
52+
>>> u_prime.is_counter_clockwise
5353
True
5454
>>> u_prime.is_180
5555
False
@@ -98,19 +98,21 @@ def __init__(self, name):
9898
try:
9999
if len(name) >= 2 and name[1] == "w":
100100
name = name[0].lower() + name[2:]
101+
if "i" in name:
102+
name = name.replace("i", "'")
101103
if name[1:] == "2'":
102104
name = name[0] + "2"
103105
if name[0] in "LUFDRBMSElufdrbxyz":
104106
if name[1:] in ["", "'", "2"]:
105-
self.name = name
106-
self.face = name[0]
107-
self.is_inverse = (name[1:] == "'")
108-
self.is_standard = (name[1:] == "")
109-
self.is_180 = (name[1:] == "2")
107+
self.__name = lambda: name
108+
self.__face = lambda: name[0]
109+
self.__is_counter_clockwise = lambda: name[1:] == "'"
110+
self.__is_clockwise = lambda: name[1:] == ""
111+
self.__is_180 = lambda: name[1:] == "2"
110112
return
111113
raise IndexError
112114
except IndexError:
113-
raise ValueError("Invalid action name.")
115+
raise ValueError("Invalid action name {0}".format(name))
114116

115117
def __repr__(self):
116118
"""
@@ -189,8 +191,8 @@ def __add__(self, another):
189191
if type(another) == str:
190192
another = Step(another)
191193
if self.face == another.face:
192-
status = ((self.is_standard + self.is_180*2 + self.is_inverse*3) + \
193-
(another.is_standard + another.is_180*2 + another.is_inverse*3)) % 4
194+
status = ((self.is_clockwise + self.is_180*2 + self.is_counter_clockwise*3) + \
195+
(another.is_clockwise + another.is_180*2 + another.is_counter_clockwise*3)) % 4
194196
try:
195197
return Step(self.face + [None, "", "2", "'"][status])
196198
except TypeError: return None
@@ -234,7 +236,7 @@ def set_face(self, new_face):
234236
L2
235237
"""
236238
if new_face in list("LUFDRBlufdrbMSExyz"):
237-
return Step(new_face + "'" * self.is_inverse + "2" * self.is_180)
239+
return Step(new_face + "'" * self.is_counter_clockwise + "2" * self.is_180)
238240
else:
239241
raise ValueError("Invalid name")
240242

@@ -253,14 +255,34 @@ def inverse(self):
253255
>>> s.inverse()
254256
R2
255257
"""
256-
return Step(self.name[0] + ("" if self.is_inverse else "'" if self.is_standard else "2"))
258+
return Step(self.name[0] + ("" if self.is_counter_clockwise else "'" if self.is_clockwise else "2"))
257259

258260
def __hash__(self):
259261
"""
260262
Step object is hashable.
261263
"""
262264
return hash(self.name)
263265

266+
@property
267+
def name(self):
268+
return self.__name()
269+
270+
@property
271+
def is_counter_clockwise(self):
272+
return self.__is_counter_clockwise()
273+
274+
@property
275+
def is_clockwise(self):
276+
return self.__is_clockwise()
277+
278+
@property
279+
def is_180(self):
280+
return self.__is_180()
281+
282+
@property
283+
def face(self):
284+
return self.__face()
285+
264286

265287

266288
class Formula(list):
@@ -653,7 +675,7 @@ def _optimise_rotations(self):
653675
self.insert(0, _self[i])
654676
else:
655677
cr_pattern = pattern[_self[i].face]
656-
if _self[i].is_inverse:
678+
if _self[i].is_counter_clockwise:
657679
cr_pattern = cr_pattern[::-1]
658680
for j in range(len(self)):
659681
if self[j].face in cr_pattern:

pycuber/solver/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Usage:
55
>>> import pycuber as pc
66
>>> from pycuber.solver import CFOPSolver
7-
>>> rand_alg = pc.Algo().random()
7+
>>> rand_alg = pc.Formula().random()
88
>>> cube = pc.Cube()
99
>>> cube(rand_alg)
1010
>>> solver = CFOPSolver(cube)

pycuber/solver/cfop/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def feed(self, cube):
1212
def solve(self):
1313
if not self.cube.is_valid():
1414
raise ValueError("Invalid Cube.")
15-
result = pycuber.Algo()
15+
result = pycuber.Formula()
1616
sys.stdout.write("Solver starts....")
1717
sys.stdout.write("\rSolving Cross ......")
1818
solver = CrossSolver(self.cube)

pycuber/solver/cfop/cross.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def _rotate(edges, step):
3434
"B": "ULDR",
3535
}[step.face]
3636
movement = {
37-
movement[i]: movement[(i + step.is_standard + (-1 * step.is_inverse) + (2 * step.is_180)) % 4]
37+
movement[i]: movement[(i + step.is_clockwise + (-1 * step.is_counter_clockwise) + (2 * step.is_180)) % 4]
3838
for i in range(4)
3939
}
4040
for edge in edges:
@@ -147,7 +147,7 @@ def solve(self):
147147
"""
148148
Solve the cross.
149149
"""
150-
result = Algo(path_actions(a_star_search(
150+
result = Formula(path_actions(a_star_search(
151151
({f: self.cube[f] for f in "LUFDRB"},
152152
self.cube.select_type("edge") & self.cube.has_colour(self.cube["D"].colour)),
153153
self.cross_successors,

pycuber/solver/cfop/f2l.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def _rotate(pair, step):
109109
"B": "ULDR",
110110
}[step.face]
111111
movement = {
112-
movement[i]: movement[(i + step.is_standard + (-1 * step.is_inverse) + (2 * step.is_180)) % 4]
112+
movement[i]: movement[(i + step.is_clockwise + (-1 * step.is_counter_clockwise) + (2 * step.is_180)) % 4]
113113
for i in range(4)
114114
}
115115
for cubie in pair:
@@ -137,9 +137,9 @@ def combining_successors(state, last_action=()):
137137
Successors function for finding path of combining F2L pair.
138138
"""
139139
((corner, edge), (L, U, F, D, R, B)) = state
140-
U_turns = [Algo("U"), Algo("U'"), Algo("U2")] if len(last_action) != 1 else []
141-
R_turns = [Algo("R U R'"), Algo("R U' R'"), Algo("R U2 R'")] if "R" not in last_action else []
142-
F_turns = [Algo("F' U F"), Algo("F' U' F"), Algo("F' U2 F")] if "F" not in last_action else []
140+
U_turns = [Formula("U"), Formula("U'"), Formula("U2")] if len(last_action) != 1 else []
141+
R_turns = [Formula("R U R'"), Formula("R U' R'"), Formula("R U2 R'")] if "R" not in last_action else []
142+
F_turns = [Formula("F' U F"), Formula("F' U' F"), Formula("F' U2 F")] if "F" not in last_action else []
143143
for act in (U_turns + R_turns + F_turns):
144144
new = (corner, edge)
145145
for q in act:
@@ -164,7 +164,7 @@ def combining_search(self):
164164
return sum(path_actions(a_star_search(start,
165165
self.combining_successors,
166166
lambda x: len(x),
167-
self.combining_goal)), Algo())
167+
self.combining_goal)), Formula())
168168

169169
def combining_setup(self):
170170
"""
@@ -173,25 +173,25 @@ def combining_setup(self):
173173
(slot_type, (corner_slot, edge_slot), (corner, edge)) = self.get_slot()
174174
cycle = ["FR", "RB", "BL", "LF"]
175175
if slot_type == "SLOTFREE":
176-
return ("FR", Algo(Step("y") * cycle.index(self.pair) or []))
176+
return ("FR", Formula(Step("y") * cycle.index(self.pair) or []))
177177
elif slot_type == "CSLOTFREE":
178178
return (cycle[-(cycle.index(edge_slot) - cycle.index(self.pair))],
179-
Algo(Step("y") * cycle.index(edge_slot) or []))
179+
Formula(Step("y") * cycle.index(edge_slot) or []))
180180
elif slot_type in ("ESLOTFREE", "WRONGSLOT"):
181181
return (cycle[-(cycle.index(corner_slot) - cycle.index(self.pair))],
182-
Algo(Step("y") * cycle.index(corner_slot) or []))
182+
Formula(Step("y") * cycle.index(corner_slot) or []))
183183
elif slot_type == "DIFFSLOT":
184184
if corner_slot != self.pair: corner_slot, edge_slot = edge_slot, corner_slot
185-
result = Algo(Step("y") * cycle.index(edge_slot) or [])
186-
result += Algo("R U R'")
187-
result += Algo(Step("y'") * cycle.index(edge_slot) or [])
188-
result += Algo(Step("y") * cycle.index(corner_slot) or [])
185+
result = Formula(Step("y") * cycle.index(edge_slot) or [])
186+
result += Formula("R U R'")
187+
result += Formula(Step("y'") * cycle.index(edge_slot) or [])
188+
result += Formula(Step("y") * cycle.index(corner_slot) or [])
189189
if result[-1].face == "y" and result[-2].face == "y":
190190
result[-2] += result[-1]
191191
del result[-1]
192192
return (cycle[-(cycle.index(corner_slot) - cycle.index(self.pair))], result)
193193
else:
194-
return (cycle[-cycle.index(self.pair)], Algo())
194+
return (cycle[-cycle.index(self.pair)], Formula())
195195

196196
def combine(self):
197197
"""
@@ -209,14 +209,14 @@ def solve(self):
209209
"""
210210
cycle = ["FR", "RB", "BL", "LF"]
211211
combine = self.combine()
212-
put = Algo(Step("y") * cycle.index(self.pair) or [])
212+
put = Formula(Step("y") * cycle.index(self.pair) or [])
213213
self.cube(put)
214214
self.pair = "FR"
215215
estimated = self.estimated_position()
216-
for U_act in [Algo(), Algo("U"), Algo("U2"), Algo("U'")]:
216+
for U_act in [Formula(), Formula("U"), Formula("U2"), Formula("U'")]:
217217
self.cube(U_act)
218-
for put_act in [Algo("R U R'"), Algo("R U' R'"), Algo("R U2 R'"),
219-
Algo("F' U F"), Algo("F' U' F"), Algo("F' U2 F")]:
218+
for put_act in [Formula("R U R'"), Formula("R U' R'"), Formula("R U2 R'"),
219+
Formula("F' U F"), Formula("F' U' F"), Formula("F' U2 F")]:
220220
self.cube(put_act)
221221
if self.get_pair() == estimated:
222222
return combine + put + U_act + put_act

pycuber/solver/cfop/oll.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
reader = csv.reader(f, delimiter=",")
1010
algo_dict = {}
1111
for line in reader:
12-
algo_dict[line[1]] = Algo(line[2])
12+
algo_dict[line[1]] = Formula(line[2])
1313
for i in range(1, 4):
14-
algo_dict[line[1][-3*i:] + line[1][:-3*i]] = Algo(line[2]).insert(0, Step("U") * i)
15-
algo_dict["000000000000"] = Algo()
14+
algo_dict[line[1][-3*i:] + line[1][:-3*i]] = Formula(line[2]).insert(0, Step("U") * i)
15+
algo_dict["000000000000"] = Formula()
1616

1717
class OLLSolver(object):
1818
"""
@@ -44,7 +44,7 @@ def recognise(self):
4444

4545
def solve(self):
4646
"""
47-
Solve the OLL. Returns an Algo.
47+
Solve the OLL. Returns an Formula.
4848
"""
4949
if not isinstance(self.cube, Cube):
5050
raise ValueError("Use Solver.feed(cube) to feed the cube to solver.")

pycuber/solver/cfop/pll.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
algo_dict = {}
1111
for name, rec_id, algo in reader:
1212
for i in range(4):
13-
algo_dict[rec_id[-3*i:] + rec_id[:-3*i]] = Algo(algo).insert(0, Step("U") * i)
13+
algo_dict[rec_id[-3*i:] + rec_id[:-3*i]] = Formula(algo).insert(0, Step("U") * i)
1414
rec_id = "LLLFFFRRRBBB"
1515
algo = []
1616
for i in range(4):
17-
algo_dict[rec_id[-3*i:] + rec_id[:-3*i]] = Algo(algo).insert(0, Step("U") * i)
17+
algo_dict[rec_id[-3*i:] + rec_id[:-3*i]] = Formula(algo).insert(0, Step("U") * i)
1818

1919
class PLLSolver(object):
2020
"""
@@ -52,7 +52,7 @@ def solve(self):
5252
rec_id = self.recognise()
5353
if rec_id in algo_dict:
5454
self.cube(algo_dict[rec_id])
55-
return Algo((Step("y") * i) or []) + algo_dict[rec_id]
55+
return Formula((Step("y") * i) or []) + algo_dict[rec_id]
5656
self.cube(Step("y"))
5757
raise ValueError("Invalid cube.")
5858

0 commit comments

Comments
 (0)