-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcube.py
184 lines (152 loc) · 5.74 KB
/
cube.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from sty import bg, ef, rs
import time
import sys
cube_face = [[1,2,2],[1,2,3],[4,2,3]]
moves = {
"R": 2,
"L": 0,
"F": 1,
"B": 3,
"U": 4,
"D": 5
}
def create_cube(): # creates a standard rubiks cube as an array
cube = [[] for x in range(0,3)]
colors = [bg(214), bg.green, bg.red, bg.blue, bg.white, bg(226)]
#colors = [1, 2, 3, 4, 5, 6]
for x in range(0,4):
piece = colors[x] + " " + bg(0)
for y in range(0, 3):
cube[0].append([piece, piece, piece])
for x in range(0,2):
piece = colors[x+4] + " " + bg(0)
for y in range(0, 3):
cube[x+1].append([piece, piece, piece])
return cube
def create_test_cube(): # creates a numbered cube used to debug
cube = [[] for x in range(0,3)]
for y in range(0, 12):
n=3*y
cube[0].append([str(n) + (" "*(len(str(n)) == 1)), str(n+1) + (" "*(len(str(n+1)) == 1)), str(n+2) + (" "*(len(str(n+1)) == 1))])
for y in range(0,3):
n=3*y
cube[1].append([str(n)+" ",str(n+1)+" ",str(n+2)+" "])
for y in range(0,3):
n=3*y
cube[2].append([str(n)+" ",str(n+1)+" ",str(n+2)+" "])
return cube
def print_face(face): # deprecated prints a face given the array
print(face[0])
print(face[1])
print(face[2])
def print_cube(cube): # prints the cube as a flattened 2d shape
for y in range(0, 3):
print(" ", end='')
for x in range(0,3):
print(cube[1][x][2-y], " ", end='')
print("")
print("")
for y in range(0, 3):
for x in range(0, 12):
print(cube[0][x][2-y]," ", end='')
print("")
print("")
for y in range(0, 3):
print(" ", end='')
for x in range(0,3):
print(cube[2][x][2-y], " ", end='')
print("")
print("")
def print_dt(cube): #debug prints the cube as it appears as an array
for x in range(0,3):
for y in range(0,18):
print(cube[y][x], " ", end='')
print("")
def f_turn(cube, side): # defines how to change cube face
c = cube
#3n+1,2,3
if((side>=0) and (side<=3)):
c[0][(3*side)+0][0], c[0][(3*side)+0][2], c[0][(3*side)+2][2], c[0][(3*side)+2][0] = c[0][(3*side)+2][0], c[0][(3*side)+0][0], c[0][(3*side)+0][2], c[0][(3*side)+2][2]
c[0][(3*side)+0][1], c[0][(3*side)+1][2], c[0][(3*side)+2][1], c[0][(3*side)+1][0] = c[0][(3*side)+1][0], c[0][(3*side)+0][1], c[0][(3*side)+1][2], c[0][(3*side)+2][1]
elif(side == 4):
c[1][0][0], c[1][0][2], c[1][2][2], c[1][2][0] = c[1][2][0], c[1][0][0], c[1][0][2], c[1][2][2]
c[1][0][1], c[1][1][2], c[1][2][1], c[1][1][0] = c[1][1][0], c[1][0][1], c[1][1][2], c[1][2][1]
else:
c[2][0][0], c[2][0][2], c[2][2][2], c[2][2][0] = c[2][2][0], c[2][0][0], c[2][0][2], c[2][2][2]
c[2][0][1], c[2][1][2], c[2][2][1], c[2][1][0] = c[2][1][0], c[2][0][1], c[2][1][2], c[2][2][1]
return c
def a_turn(cube, side): # fix the adjacent squares
c = cube
read = []
if(side == 0):
for x in range(0,3):
c[0][-9][2-x], c[2][0][(2-x)], c[0][-1][0+x], c[1][0][(2-x)] = c[1][0][(2-x)], c[0][-9][2-x], c[2][0][(2-x)], c[0][-1][0+x]
elif(side == 2):
for x in range(0, 3):
c[0][-3][2-x], c[2][2][((2-x) * -1) + 2], c[0][5][0+x], c[1][2][(2-x) * (-1) + 2] = c[1][2][(2-x) * (-1) + 2], c[0][-3][2-x], c[2][2][(2-x) * (-1) + 2], c[0][5][0+x]
elif(side == 1):
for x in range(0,3):
c[0][(side*3)-9][(2-x) * (((side==3)*-2)+1) + (side-1)], c[2][(2-x) * ((((side-1)==2)*-2)+1) + (side-1)][(-1*side)+3], c[0][(side*3)-1][0+x], c[1][0+x][side-1] = c[1][0+x][side-1], c[0][(side*3)-9][(2-x) * ((((side-1)==2)*-2)+1) + (side-1)], c[2][(2-x) * ((((side-1)==2)*-2)+1) + (side-1)][(-1*side)+3], c[0][(side*3)-1][0+x]
elif(side == 3):
for x in range(0,3):
c[0][0][2-x], c[2][x][0], c[0][8][x], c[1][2-x][2] = c[1][2-x][2], c[0][0][2-x], c[2][x][0], c[0][8][x]
# not completely sure how this works but it does
elif(side == 4):
for x in range(0,12):
read.append(c[0][x][2])
for x in range(-3, 9):
c[0][x-3][2] = read[x]
else:
for x in range(0,12):
read.append(c[0][x][0])
for x in range(-3, 9):
c[0][x+3][0] = read[x]
return c
def F(cube, side): # rotate face clockwise
c = cube
c = f_turn(c, side)
c = a_turn(c, side)
return cube
def scramble(m):
cube = create_cube()
x = m
l = x.split(" ")
#print(l)
#print_cube(cube)
for move in l:
if("2" in move):
move = move[0]
cube = F(cube, moves[move])
cube = F(cube, moves[move])
elif("'" in move):
move = move[0]
cube = F(cube, moves[move])
cube = F(cube, moves[move])
cube = F(cube, moves[move])
else:
cube = F(cube, moves[move])
#print_cube(cube)
return cube
if __name__ == "__main__":
args = str(sys.argv[1])
if(args == "--test-all"):
cube = scramble("R2 U' L' R2 D2 B F2 U F' R' F D' U F2 U F2 L U2 R' F")
print_cube(cube)
elif(args == "--bench"):
start_time = time.time()
for x in range(0, 10000):
scramble("F' R2 D2 U' B2")
print("--- %s seconds ---" % (time.time() - start_time))
elif(args == "--test-turns"):
cube = create_cube()
while(True):
print_cube(cube)
x = input()
cube = F(cube,moves[x])
else:
cube = create_cube()
print(cube[1][0])
# print_cube(cube)
# cube = F(cube, 5)
# print_cube(cube)
#print_dt(cube)