# Rubik's_cube_demo

Nov 27th, 2014
482
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. local commandBlock = peripheral.wrap('top')
2.
3. tCube = {['s1'] = {}, ['s2'] = {}, ['s3'] = {}, ['s4'] = {}, ['s5'] = {}, ['s6'] = {}}
4.
5. local tCell = {'~-1 ', '~ ', '~1 ', '~2 ', '~-2 '}
6.
7. local tCoord = {
8.   ['s1'] = {
9.     {3, 4, 1}, {3, 4, 2}, {3, 4, 3},
10.     {2, 4, 1}, {2, 4, 2}, {2, 4, 3},
11.     {1, 4, 1}, {1, 4, 2}, {1, 4, 3}},
12.   ['s2'] = {
13.     {1, 3, 4}, {2, 3, 4}, {3, 3, 4},
14.     {1, 2, 4}, {2, 2, 4}, {3, 2, 4},
15.     {1, 1, 4}, {2, 1, 4}, {3, 1, 4}},
16.   ['s3'] = {
17.     {4, 3, 3}, {4, 3, 2}, {4, 3, 1},
18.     {4, 2, 3}, {4, 2, 2}, {4, 2, 1},
19.     {4, 1, 3}, {4, 1, 2}, {4, 1, 1}},
20.   ['s4'] = {
21.     {3, 3, 5}, {2, 3, 5}, {1, 3, 5},
22.     {3, 2, 5}, {2, 2, 5}, {1, 2, 5},
23.     {3, 1, 5}, {2, 1, 5}, {1, 1, 5}},
24.   ['s5'] = {
25.     {5, 3, 1}, {5, 3, 2}, {5, 3, 3},
26.     {5, 2, 1}, {5, 2, 2}, {5, 2, 3},
27.     {5, 1, 1}, {5, 1, 2}, {5, 1, 3}},
28.   ['s6'] = {
29.     {1, 5, 1}, {1, 5, 2}, {1, 5, 3},
30.     {2, 5, 1}, {2, 5, 2}, {2, 5, 3},
31.     {3, 5, 1}, {3, 5, 2}, {3, 5, 3}}}
32.
33. local function reset()
34.   for i = 1, 9 do
35.     tCube['s1'][i] = 14
36.     tCube['s2'][i] = 0
37.     tCube['s3'][i] = 13
38.     tCube['s4'][i] = 4
39.     tCube['s5'][i] = 11
40.     tCube['s6'][i] = 1
41.   end
42. end
43.
44. local function drawAngles()
45.   for a = 4, 5 do
46.     for b = 4, 5 do
47.       for c = 1, 5 do
48.         commandBlock.setCommand('setblock '..tCell[a]..tCell[b]..tCell[c]..'glass')
49.         commandBlock.runCommand{}
50.         commandBlock.setCommand('setblock '..tCell[b]..tCell[c]..tCell[a]..'glass')
51.         commandBlock.runCommand{}
52.         commandBlock.setCommand('setblock '..tCell[c]..tCell[a]..tCell[b]..'glass')
53.         commandBlock.runCommand{}
54.       end
55.     end
56.   end
57. end
58.
59. local function drawCube()
60.   for pG = 1, 9 do
61.     for eS = 1, 6 do
62.       commandBlock.setCommand('setblock '..
63.         tCell[tCoord['s'..eS][pG][1]]..
64.         tCell[tCoord['s'..eS][pG][2]]..
65.         tCell[tCoord['s'..eS][pG][3]]..
66.         'wool '..
67.         tCube['s'..eS][pG])
68.       commandBlock.runCommand{}
69.     end
70.   end
71. end
72.
74.   if rule == '01' then
75.     tCube[side][1], tCube[side][2], tCube[side][3], tCube[side][4], tCube[side][6], tCube[side][7], tCube[side][8], tCube[side][9] =
76.     tCube[side][7], tCube[side][4], tCube[side][1], tCube[side][8], tCube[side][2], tCube[side][9], tCube[side][6], tCube[side][3]
77.   elseif rule == '02' then
78.     tCube[side][7], tCube[side][4], tCube[side][1], tCube[side][8], tCube[side][2], tCube[side][9], tCube[side][6], tCube[side][3] =
79.     tCube[side][1], tCube[side][2], tCube[side][3], tCube[side][4], tCube[side][6], tCube[side][7], tCube[side][8], tCube[side][9]
80.   end
81. end
82.
83. local function magic(sides, cells)
84.   tCube[sides[1]][cells[1]],
85.   tCube[sides[2]][cells[2]],
86.   tCube[sides[3]][cells[3]],
87.   tCube[sides[4]][cells[4]],
88.   tCube[sides[1]][cells[5]],
89.   tCube[sides[2]][cells[6]],
90.   tCube[sides[3]][cells[7]],
91.   tCube[sides[4]][cells[8]],
92.   tCube[sides[1]][cells[9]],
93.   tCube[sides[2]][cells[10]],
94.   tCube[sides[3]][cells[11]],
95.   tCube[sides[4]][cells[12]] =
96.   tCube[sides[5]][cells[13]],
97.   tCube[sides[6]][cells[14]],
98.   tCube[sides[7]][cells[15]],
99.   tCube[sides[8]][cells[16]],
100.   tCube[sides[5]][cells[17]],
101.   tCube[sides[6]][cells[18]],
102.   tCube[sides[7]][cells[19]],
103.   tCube[sides[8]][cells[20]],
104.   tCube[sides[5]][cells[21]],
105.   tCube[sides[6]][cells[22]],
106.   tCube[sides[7]][cells[23]],
107.   tCube[sides[8]][cells[24]]
108. end
109.
110. local function rotate(V)
111.   if V == '01' then
112.     magic({'s2', 's3', 's4', 's5', 's3', 's4', 's5', 's2'}, {1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,})
114.
115.   elseif V == '02' then
116.     magic({'s2', 's3', 's4', 's5', 's3', 's4', 's5', 's2'}, {4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6,})
117.
118.   elseif V == '03' then
119.     magic({'s2', 's3', 's4', 's5', 's3', 's4', 's5', 's2'}, {7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9,})
121.
122.   elseif V == '04' then
123.     magic({'s1', 's5', 's6', 's3', 's5', 's6', 's3', 's1'}, {1, 1, 1, 9, 4, 4, 4, 6, 7, 7, 7, 3, 1, 1, 9, 1, 4, 4, 6, 4, 7, 7, 3, 7})
125.
126.   elseif V == '05' then
127.     magic({'s1', 's5', 's6', 's3', 's5', 's6', 's3', 's1'}, {2, 2, 2, 8, 5, 5, 5, 5, 8, 8, 8, 2, 2, 2, 8, 2, 5, 5, 5, 5, 8, 8, 2, 8})
128.
129.   elseif V == '06' then
130.     magic({'s1', 's5', 's6', 's3', 's5', 's6', 's3', 's1'}, {3, 3, 3, 7, 6, 6, 6, 4, 9, 9, 9, 1, 3, 3, 7, 3, 6, 6, 4, 6, 9, 9, 1, 9})
132.
133.   elseif V == '07' then
134.     magic({'s1', 's4', 's6', 's2', 's4', 's6', 's2', 's1'}, {3, 1, 7, 9, 2, 4, 8, 6, 1, 7, 9, 3, 1, 7, 9, 3, 4, 8, 6, 2, 7, 9, 3, 1,})
136.
137.   elseif V == '08' then
138.     magic({'s1', 's4', 's6', 's2', 's4', 's6', 's2', 's1'}, {6, 2, 4, 8, 5, 5, 5, 5, 4, 8, 6, 2, 2, 4, 8, 6, 5, 5, 5, 5, 8, 6, 2, 4,})
139.
140.   elseif V == '09' then
141.     magic({'s1', 's4', 's6', 's2', 's4', 's6', 's2', 's1'}, {9, 3, 1, 7, 8, 6, 2, 4, 7, 9, 3, 1, 3, 1, 7, 9, 6, 2, 4, 8, 9, 3, 1, 7,})
143.
144.   elseif V == '11' then
145.     magic({'s3', 's5', 's2', 's4', 's2', 's4', 's5', 's3'}, {1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,})
147.
148.   elseif V == '12' then
149.     magic({'s3', 's5', 's2', 's4', 's2', 's4', 's5', 's3'}, {4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6,})
150.
151.   elseif V == '13' then
152.     magic({'s3', 's5', 's2', 's4', 's2', 's4', 's5', 's3'}, {7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9,})
154.
155.   elseif V == '14' then
156.     magic({'s5', 's1', 's3', 's6', 's1', 's3', 's6', 's5'}, {1, 1, 9, 1, 4, 4, 6, 4, 7, 7, 3, 7, 1, 9, 1, 1, 4, 6, 4, 4, 7, 3, 7, 7})
158.
159.   elseif V == '15' then
160.     magic({'s5', 's1', 's3', 's6', 's1', 's3', 's6', 's5'}, {2, 2, 8, 2, 5, 5, 5, 5, 8, 8, 2, 8, 2, 8, 2, 2, 5, 5, 5, 5, 8, 2, 8, 8})
161.
162.   elseif V == '16' then
163.     magic({'s5', 's1', 's3', 's6', 's1', 's3', 's6', 's5'}, {3, 3, 7, 3, 6, 6, 4, 6, 9, 9, 1, 9, 3, 7, 3, 3, 6, 4, 6, 6, 9, 1, 9, 9})
165.
166.   elseif V == '17' then
167.     magic({'s4', 's1', 's2', 's6', 's1', 's2', 's6', 's4'}, {7, 1, 3, 9, 4, 2, 6, 8, 1, 3, 9, 7, 1, 3, 9, 7, 2, 6, 8, 4, 3, 9, 7, 1,})
169.
170.   elseif V == '18' then
171.     magic({'s4', 's1', 's2', 's6', 's1', 's2', 's6', 's4'}, {8, 4, 2, 6, 5, 5, 5, 5, 2, 6, 8, 4, 4, 2, 6, 8, 5, 5, 5, 5, 6, 8, 4, 2,})
172.
173.   elseif V == '19' then
174.     magic({'s4', 's1', 's2', 's6', 's1', 's2', 's6', 's4'}, {9, 7, 1, 3, 6, 8, 4, 2, 3, 9, 7, 1, 7, 1, 3, 9, 8, 4, 2, 6, 9, 7, 1, 3,})
176.   end
177. end
178.
179. reset()
180. drawAngles()
181. drawCube()
182.
183. while true do
184.   sleep(5)
185.   g = math.random(0, 1)
186.   s = math.random(1, 9)
187.   rotate(g..s)
188.   drawCube()
189. end
190.
191. --[[default
192.               3
193.           [9][8][7]
194.           [6][5][4]
195.           [3][2][1]
196.     4         1         2
197. [7][4][1] [1][2][3] [3][6][9]
198. [8][5][2] [4][5][6] [2][5][8]
199. [9][6][3] [7][8][9] [1][4][7]
200.               5
201.           [1][2][3]
202.           [4][5][6]
203.           [7][8][9]
204.               6
205.           [1][2][3]
206.           [4][5][6]
207.           [7][8][9]
208. ]]