# cubepuzzle.kbs
# 2010-05-24 j.m.reneau
fastgraphics
graphsize 500,500
cellw = graphheight / 14
dim colors(6)
colors = {red, yellow, blue, green, white, orange}
dim cell(6,9)
dim facex(6)
dim facey(6)
facex = {graphwidth/2 - cellw * 1.5, graphwidth/2 - cellw * 1.5, graphwidth/2 - cellw * 1.5, graphwidth/2 - cellw * 1.5, graphwidth/2 - cellw * 4.5, graphwidth/2 + cellw * 1.5}
facey = {cellw*4, cellw * 7, cellw * 10, cellw, cellw*4, cellw*4}
# the axisface and axissquare arrays are used to define the relationship of
# cells in each of the 9 degrees of freedom of the puzzle
dim axisface(9,12)
dim axissquare(9,12)
axisface = {3,3,3,0,0,0,1,1,1,2,2,2, 3,3,3,0,0,0,1,1,1,2,2,2, 3,3,3,0,0,0,1,1,1,2,2,2, 4,4,4,1,1,1,5,5,5,3,3,3, 4,4,4,1,1,1,5,5,5,3,3,3, 4,4,4,1,1,1,5,5,5,3,3,3, 4,4,4,0,0,0,5,5,5,2,2,2, 4,4,4,0,0,0,5,5,5,2,2,2, 4,4,4,0,0,0,5,5,5,2,2,2}
axissquare = {0,3,6,0,3,6,0,3,6,0,3,6, 1,4,7,1,4,7,1,4,7,1,4,7, 2,5,8,2,5,8,2,5,8,2,5,8, 0,3,6,6,7,8,8,5,2,2,1,0, 1,4,7,3,4,5,7,4,1,5,4,3, 2,5,8,0,1,2,6,3,0,8,7,6, 0,1,2,0,1,2,0,1,2,8,7,6, 3,4,5,3,4,5,3,4,5,5,4,3, 6,7,8,6,7,8,6,7,8,2,1,0}
gosub initpuzzle
gosub drawpuzzle
print "cubepuzzle.kbs"
print "This puzzle is like the Rubick's Cube but it is flattened out."
print "Movement is made by clicking and dragging a square in the desired direction."
print
input "Do you wish me to scramble it? (y)", scramble$
if upper(left(scramble$,1)) = "Y" then
while rand > .05
axis = int(rand * 9)
gosub rotateaxis
gosub drawpuzzle
end while
end if
while true
gosub getclick
# print "you clicked on " + face + " " + square + " in direction " + direction
if (face = 3 or face = 0 or face = 1 or face = 2) and direction = 0 then
# axis 0-2 forward
axis = square % 3
gosub rotateaxis
end if
if (face = 3 or face = 0 or face = 1 or face = 2) and direction = 1 then
# axis 0-2 backward - center rotate down
axis = square % 3
gosub rotatereverseaxis
end if
if (face = 3 and direction = 3) then
axis = square\3 + 3
gosub rotateaxis
endif
if (face = 3 and direction = 2) then
axis = square\3 + 3
gosub rotatereverseaxis
endif
if (face = 4 and direction = 1) then
axis = square%3 + 3
gosub rotateaxis
endif
if (face = 4 and direction = 0) then
axis = square%3 + 3
gosub rotatereverseaxis
endif
if (face = 1 and direction = 2) then
axis = 5 - square\3
gosub rotateaxis
endif
if (face = 1 and direction = 3) then
axis = 5 - square\3
gosub rotatereverseaxis
endif
if (face = 5 and direction = 1) then
axis = 5 - square%3
gosub rotateaxis
endif
if (face = 5 and direction = 0) then
axis = 5 - square%3
gosub rotatereverseaxis
endif
if (face = 4 or face = 0 or face = 5) and direction = 2 then
# axis 6-8 forward
axis = 6 + square \ 3
gosub rotateaxis
end if
if (face = 4 or face = 0 or face = 5) and direction = 3 then
# axis 6-8 backward
axis = 6 + square \ 3
gosub rotatereverseaxis
end if
if (face = 2 and direction = 3) then
axis = 8 - square\3
gosub rotateaxis
endif
if (face = 2 and direction = 2) then
axis = 8 - square\3
gosub rotatereverseaxis
endif
gosub drawpuzzle
end while
end
rotatereverseaxis:
# three forwards makes a backward
gosub rotateaxis
gosub rotateaxis
gosub rotateaxis
return
rotateaxis:
# rotate faces along axis - pass axis with the number 0-8
for j = 0 to 2
z = cell[axisface[axis,j],axissquare[axis,j]]
cell[axisface[axis,j],axissquare[axis,j]] = cell[axisface[axis,3+j],axissquare[axis,3+j]]
cell[axisface[axis,3+j],axissquare[axis,3+j]] = cell[axisface[axis,6+j],axissquare[axis,6+j]]
cell[axisface[axis,6+j],axissquare[axis,6+j]] = cell[axisface[axis,9+j],axissquare[axis,9+j]]
cell[axisface[axis,9+j],axissquare[axis,9+j]] = z
next j
if axis = 0 then
spin = 4
gosub rotateccw
end if
if axis = 2 then
spin = 5
gosub rotatecw
end if
if axis = 3 then
spin = 2
gosub rotatecw
end if
if axis = 5 then
spin = 0
gosub rotateccw
end if
if axis = 6 then
spin = 3
gosub rotatecw
end if
if axis = 8 then
spin = 1
gosub rotateccw
end if
return
rotateccw:
#spin face counterclockwise - same as 3 cw
gosub rotatecw
gosub rotatecw
gosub rotatecw
return
rotatecw:
# spin face clockwise
z = cell[spin,0]
cell[spin,0] = cell[spin,6]
cell[spin,6] = cell[spin,8]
cell[spin,8] = cell[spin,2]
cell[spin,2] = z
z = cell[spin,1]
cell[spin,1] = cell[spin,3]
cell[spin,3] = cell[spin,7]
cell[spin,7] = cell[spin,5]
cell[spin,5] = z
return
getclick:
# return face 0-5 if the square
# square on face 0-8
# and direction 0-up, 1-down, 2-right, 3-left
direction = -1
while direction = -1
while mouseb = 0
pause .01
end while
x = mousex
y = mousey
for i = 0 to 5
if x >= facex[i] and x < facex[i] + cellw * 3 and y >= facey[i] and y < facey[i] + cellw * 3 then
face = i
square = int((y-facey[i])/cellw) * 3 + int((x-facex[i])/cellw)
while mouseb <> 0
pause .01
end while
releasex = mousex
releasey = mousey
direction = -1
if releasey < y and abs(releasex - x) < 10 then direction = 0
if releasey > y and abs(releasex - x) < 10 then direction = 1
if releasex < x and abs(releasey - y) < 10 then direction = 2
if releasex > x and abs(releasey - y) < 10 then direction = 3
end if
next i
end while
return
drawpuzzle:
clg
color darkgrey
rect 0, 0, graphwidth, graphheight
color black
rect facex[3], facey[3], cellw * 3, cellw * 12
rect facex[4], facey[4], cellw * 9, cellw * 3
# face
for i = 0 to 5
for j = 0 to 8
color colors[cell[i,j]]
rect facex[i]+(j%3)*cellw+2, facey[i]+(j\3)*cellw+2, cellw-4, cellw-4
next j
next i
refresh
return
initpuzzle:
for i = 0 to 5
for j = 0 to 8
cell[i,j] = i
next j
next i
return