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

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)

 

 

二次元のリスト追加

 

 

 

空のリストを生成して、そこにリストを追加する

 

  • 軸0に追加
import numpy as np

array_axis0_0 = np.arange(0, 4)
array_axis0_1 = np.arange(10, 14)
array_axis0_2 = np.arange(20, 24)

array_2d = np.empty((0, 4))
array_2d = np.append(array_2d, array_axis0_0.reshape(1, 4), axis=0)
array_2d = np.append(array_2d, array_axis0_1.reshape(1, 4), axis=0)
array_2d = np.append(array_2d, array_axis0_2.reshape(1, 4), axis=0)

print(array_2d)
# [[ 0. 1. 2. 3.]
# [10. 11. 12. 13.]
# [20. 21. 22. 23.]]

 

  • 軸1に追加
import numpy as np

array_axis0_0 = np.arange(0, 4)
array_axis0_1 = np.arange(10, 14)
array_axis0_2 = np.arange(20, 24)

array_2d = np.empty((4, 0))
array_2d = np.append(array_2d, array_axis0_0.reshape(4, 1), axis=1)
array_2d = np.append(array_2d, array_axis0_1.reshape(4, 1), axis=1)
array_2d = np.append(array_2d, array_axis0_2.reshape(4, 1), axis=1)

print(array_2d)
# [[ 0. 10. 20.]
# [ 1. 11. 21.]
# [ 2. 12. 22.]
# [ 3. 13. 23.]]