ルービックキューブのスクリプト

Numpyを使ったルービックキューブスクリプト

 

import numpy as np

class CubeBase:
def __init__(self, plane):
self.__plane = self._plane_init(plane)
self.__connector = np.array([[1, 2, 3, 4],
[2, 0, 5, 3],
[0, 1, 4, 5],
[4, 5, 0, 1],
[5, 3, 2, 0],
[3, 4, 1, 2]])

def _plane_init(self, plane):
return plane

def _rot_size(self, axis):
t = np.array(self.__plane[self.__connector[axis][0], :, 0])
l = np.array(self.__plane[self.__connector[axis][1], 0])
r = np.array(self.__plane[self.__connector[axis][2], :, 2])
b = np.array(self.__plane[self.__connector[axis][3], 2])

#print(t, l, r, b)

return t, l, r, b

def rot_cw(self, axis):
t, l, r, b = self._rot_size(axis)

self.__plane[axis] = np.rot90(self.__plane[axis], 3)
self.__plane[self.__connector[axis][0], :, 0] = np.rot90([l]).reshape(3)
self.__plane[self.__connector[axis][1], 0] = np.rot90([b], 2).reshape(3)
self.__plane[self.__connector[axis][2], :, 2] = np.rot90([t], 2).reshape(3)
self.__plane[self.__connector[axis][3], 2] = np.rot90([r]).reshape(3)

print(self.__plane)

def rot_ccw(self, axis):
t, l, r, b = self._rot_size(axis)

self.__plane[axis] = np.rot90(self.__plane[axis], 1)
self.__plane[self.__connector[axis][0], :, 0] = np.rot90([r]).reshape(3)
self.__plane[self.__connector[axis][1], 0] = np.rot90([t], 2).reshape(3)
self.__plane[self.__connector[axis][2], :, 2] = np.rot90([b], 2).reshape(3)
self.__plane[self.__connector[axis][3], 2] = np.rot90([l]).reshape(3)

print(self.__plane)

def center_up(self, axis):
pass

def center_dw(self, axis):
pass

def center_lf(self, axis):
pass

def center_rg(self, axis):
pass

if __name__ == "__main__":
plane = np.array([[[11, 12, 13], [14, 15, 16], [17, 18, 19]],
[[21, 22, 23], [24, 25, 26], [27, 28, 29]],
[[31, 32, 33], [34, 35, 36], [37, 38, 39]],
[[41, 42, 43], [44, 45, 46], [47, 48, 49]],
[[51, 52, 53], [54, 55, 56], [57, 58, 59]],
[[61, 62, 63], [64, 65, 66], [67, 68, 69]]])
cb = CubeBase(plane)
cb.rot_ccw(0)