Hello everyone,
I’m trying to make a pocket cube solver in Julia.
I would like some advises, remarks and ultimately some help to write a optimized recursive function to replace my “for loop”.
First some explanations of my method:
-
I fix one of the 8 corners and move the others around it.
-
I represent the cube with an a 1x21 Array{Int8,2} (3 faces for each 7 corners left).
Int8[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21]
- I use the permute!(v,p) function from the combinatorics.jl packages to rotate the cube.I define 6 permutations, one for every quarter turn possible (top,right,front+anticlockwise).
T =Int8[7 ,8 ,9 ,1 ,2 ,3 ,10,11,12,4 ,5 ,6 ,13,14,15,16,17,18,19,20,21]
Ti=Int8[4 ,5 ,6 ,10,11,12,1 ,2 ,3 ,7 ,8 ,9 ,13,14,15,16,17,18,19,20,21]
F =Int8[1 ,2 ,3 ,4 ,5 ,6 ,15,13,14,9 ,7 ,8 ,16,17,18,12,10,11,19,20,21]
Fi=Int8[1 ,2 ,3 ,4 ,5 ,6 ,11,12,10,17,18,16,8 ,9 ,7 ,13,14,15,19,20,21]
R =Int8[1 ,2 ,3 ,12,10,11,7 ,8 ,9 ,16,17,18,13,14,15,21,19,20,6 ,4 ,5 ]
Ri=Int8[1 ,2 ,3 ,20,21,19,7 ,8 ,9 ,5 ,6 ,4 ,13,14,15,10,11,12,17,18,16]
- I stock the cube states in a
Int32[]
array and one cube state is define by:
state=Int32((cube[1]-1)*21^5+(cube[4]-1)*21^4+(cube[7]-1)*21^3+(cube[10]-1)*21^2+(cube[13]-1)*21+(cube[16]-1)
So I look at one face of 6 corners and I made a integer in a 21 radix/base.
- I use string to remember the moves. “TFRtfr” mean rotate the cube (top clockwise then front clockwise then right clockwise then top anticlockwise then front anticlockwise then right anticlockwise).
Here is my code:
using Combinatorics
global const T =Int8[7 ,8 ,9 ,1 ,2 ,3 ,10,11,12,4 ,5 ,6 ,13,14,15,16,17,18,19,20,21]
global const Ti=Int8[4 ,5 ,6 ,10,11,12,1 ,2 ,3 ,7 ,8 ,9 ,13,14,15,16,17,18,19,20,21]
global const F =Int8[1 ,2 ,3 ,4 ,5 ,6 ,15,13,14,9 ,7 ,8 ,16,17,18,12,10,11,19,20,21]
global const Fi=Int8[1 ,2 ,3 ,4 ,5 ,6 ,11,12,10,17,18,16,8 ,9 ,7 ,13,14,15,19,20,21]
global const R =Int8[1 ,2 ,3 ,12,10,11,7 ,8 ,9 ,16,17,18,13,14,15,21,19,20,6 ,4 ,5 ]
global const Ri=Int8[1 ,2 ,3 ,20,21,19,7 ,8 ,9 ,5 ,6 ,4 ,13,14,15,10,11,12,17,18,16]
global cube = Int8[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21]
global stateslist=Int32[]
global rotlist=String[]
global rot=""
global numb=Int32(1)
global inversrot=String["t","f","r","T","F","R"]
function rotcube(r)
global cube,rot,T,F,R,Ti,Fi,Ri
if r==0 && !endswith(rot,"t") && !endswith(rot,"TT")
permute!(cube,T)
rot*="T"
return 0
elseif r==1 && !endswith(rot,"f") && !endswith(rot,"FF")
permute!(cube,F)
rot*="F"
return 0
elseif r==2 && !endswith(rot,"r") && !endswith(rot,"RR")
permute!(cube,R)
rot*="R"
return 0
elseif r==3 && !endswith(rot,"T") && !endswith(rot,"t")
permute!(cube,Ti)
rot*="t"
return 0
elseif r==4 && !endswith(rot,"F") && !endswith(rot,"f")
permute!(cube,Fi)
rot*="f"
return 0
elseif r==5 && !endswith(rot,"R") && !endswith(rot,"r")
permute!(cube,Ri)
rot*="r"
return 0
else
return 1
end
end
function jumpcube(way)
global cube,T,F,R,Ti,Fi,Ri
for i in way
if i=='T'
permute!(cube,T)
elseif i=='F'
permute!(cube,F)
elseif i=='R'
permute!(cube,R)
elseif i=='t'
permute!(cube,Ti)
elseif i=='f'
permute!(cube,Fi)
elseif i=='r'
permute!(cube,Ri)
end
end
end
function upstates()
global cube,rot,stateslist,rotlist
state=Int32((cube[1]-1)*21^5+(cube[4]-1)*21^4+(cube[7]-1)*21^3+(cube[10]-1)*21^2+(cube[13]-1)*21+cube[16]-1)
index=findin(stateslist,state)
if index==[]
push!(stateslist,state)
push!(rotlist,rot)
elseif length(rotlist[index[1]])>=length(rot) && findin([rot],split(rotlist[index[1]],'/'))==[]
rotlist[index[1]]*="/"*rot
end
end
while sizeof(rot)<5 #the number of quarter turn, 14 is the maximum to solve any position of the cube.
for i in 0:5
flag=rotcube(i)
upstates()
if flag == 0
jumpcube(inversrot[i+1])
rot=chop(rot)
end
end
cube = Int8[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21]
jumpcube(split(rotlist[numb],'/')[1])
rot=split(rotlist[numb],'/')[1]
numb+=1
end
for o in rotlist
print(o*"-|-")
end
println("---------------------------")
Please help me to improve this code, target the problems and bad use of coding.
I wanna replace the for loop with a recursive function but I need some advises.
Thanks you very much
rapasite