adrianliaw / PyCuber

Rubik's Cube solver in Python
MIT License
195 stars 46 forks source link

Array representation of cube? #19

Open PAK90 opened 7 years ago

PAK90 commented 7 years ago

I have need of an array representation of the cube, either as a flat array or an array of six arrays for each face. I haven't found anything existing to do this, so I've come up with my own hacky method, but this feels like something that should be part of the Cube class. Here's what I did:

def cubeAsArray(cube):
    faces = ["L", "R", "U", "D", "B", "F"]
    cubeArray = []
    for face in faces:
        face = cube.get_face(face)  # get face, iterate over all squares.
        for x in [0,1,2]:
            for y in [0,1,2]:
                cubeArray.append(str(face[x][y]))
    return cubeArray
adrianliaw commented 7 years ago

Hi @PAK90, although this kind of usage might be useful, I wouldn't consider this "should be" a part of the Cube class, because the design of Cube class is to use "cubie" as component of the cube instead of six faces. Though if you'd like to contribute to this project, you can add this function into pycuber/helpers.py and submit a pull request!

jerpint commented 6 years ago

Hi, here is a function I use to convert a cube to an ndarray, this is especially useful for ML applications

def cube2np(mycube):
    # transform cube object to np array
    # works around the weird data type used
    global faces
    global colors
    cube_np = np.zeros((6,3,3))
    for i,face in enumerate(faces):
        face_tmp = mycube.get_face(face)
        for j in range(3):
            for k in range(len(face_tmp[j])):
                caca = face_tmp[j][k]
                cube_np[i,j,k] = colors.index(str(caca))
    return cube_np