# traffic.kbs - traffic jam
# the object is to get the red car into it's parking space
# 2010-05-07 j.m.reneau
# puzzles are from http://www.dr-mikes-math-games-for-kids.com/traffic-jam-puzzle-books.html where there are humders of more traffic jam puzzles
fastgraphics
graphsize 500,500
gosub setup
gosub drawcity
clickcount = 0
while pieces[0,0] <> goalx or pieces[0,1] <> goaly
gosub getclick
# set piece, dx, dy
piece = -1 # click not valid
for n = 0 to npieces - 1
# is click on front of a horizontal mover
if x = pieces[n,0] and y = pieces[n,1] and pieces[n,3] = 0 then
piece = n
dx = -1
dy = 0
endif
# click on back of horizontal mover
if x = (pieces[n,0] + pieces[n,2] - 1) and y = pieces[n,1] and pieces[n,3] = 0 then
piece = n
dx = 1
dy = 0
endif
# is click on top of a vertical mover
if x = pieces[n,0] and y = pieces[n,1] and pieces[n,3] = 1 then
piece = n
dx = 0
dy = -1
endif
# click on bottom of vertical mover
if x = pieces[n,0] and y = (pieces[n,1] + pieces[n,2] - 1) and pieces[n,3] = 1 then
piece = n
dx = 0
dy = 1
end if
next n
#
if piece <> -1 then
# we have a piece - can we move to the next square
x = x + dx
y = y + dy
gosub isoccupied
if not occupied then
# we can move piece in the direction dx, dy
clickcount = clickcount + 1
pieces[piece,0] = pieces[piece,0] + dx
pieces[piece,1] = pieces[piece,1] + dy
gosub drawcity
end if
end if
end while
print "You solved the puzzle in " + clickcount + " moves."
if clickcount = best then
print "Wow, Not an extra move. Perfect!!"
else
print "Can you do it again using " + (clickcount - best) + " fewer moves?"
end if
end
isoccupied:
# return occupied = true if x,y has somebody parked on it or if it is off the board
occupied = false
if x < 0 or x >= width then occupied = true
if y < 0 or y >= height then occupied = true
for n = 0 to npieces - 1
if pieces[n,3] = 0 then
# horizontal
if y = pieces[n,1] and x >= pieces[n,0] and x < pieces[n,0] + pieces[n,2] then occupied = true
else
# vertical
if x = pieces[n,0] and y >= pieces[n,1] and y < pieces[n,1] + pieces[n,2] then occupied = true
end if
next n
return
getclick:
# get mouse click and return x,y location on city grid
clickclear
while clickb = 0
pause .1
end while
x = (clickx / (cellwidth + margin)) % width
y = (clicky / (cellheight + margin)) % height
return
drawcity:
# draw the city
cellwidth = int((graphwidth - (margin * (width+1))) / width)
cellheight = int((graphheight - (margin * (height+1))) / height)
r = int((graphwidth - (2 * margin * (width+1))) / width / 2) # radius of piece
# background
color darkgrey
rect 0, 0, graphwidth, graphheight
# street cells
color grey
for x = 0 to width - 1
for y = 0 to height - 1
rect x * cellwidth + (x + 1) * margin, y * cellwidth + (y + 1) * margin, cellwidth, cellheight
next y
next x
# goal cell
color darkred
for x = 0 to pieces[0,2] - 1
if pieces[0,3] = 0 then
rect (goalx + x) * cellwidth + (goalx + x + 1) * margin, goaly * cellwidth + (goaly + 1) * margin, cellwidth, cellheight
else
rect goalx * cellwidth + (goalx + 1) * margin, (goaly + x) * cellwidth + (goaly + x + 1) * margin, cellwidth, cellheight
end if
next x
#
for n = 0 to npieces - 1
color pieces[n,4]
if pieces[n,3] = 0 then
# horizontal
x = pieces[n,0] * cellwidth + (pieces[n,0] + 1) * margin + cellwidth / 2
x2 = (pieces[n,0] + pieces[n,2] - 1) * cellwidth + (pieces[n,0] + pieces[n,2]) * margin + cellwidth / 2
y = pieces[n,1] * cellheight + (pieces[n,1] + 1) * margin + cellheight / 2
circle x,y,r
circle x2,y,r
rect x, y-r, x2 - x, 2 * r + 1
color white
stamp x,y,r*.8,-pi/2,{-1,0,0,-1,1,0}
stamp x2,y,r*.8,pi/2,{-1,0,0,-1,1,0}
else
# vertical
x = pieces[n,0] * cellwidth + (pieces[n,0] + 1) * margin + cellwidth / 2
y = pieces[n,1] * cellheight + (pieces[n,1] + 1) * margin + cellheight / 2
y2 = (pieces[n,1] + pieces[n,2] - 1) * cellheight + (pieces[n,1] + pieces[n,2]) * margin + cellheight / 2
circle x,y,r
circle x,y2,r
rect x-r, y, 2 * r + 1, y2 - y
color white
stamp x,y,r*.8,0,{-1,0,0,-1,1,0}
stamp x,y2,r*.8,pi,{-1,0,0,-1,1,0}
end if
next n
refresh
return
setup:
setup:
print "traffic.kbs"
print "Get the red car out of the traffic jam and into it's parking space."
print "You may move cars by clicking on their ends and they will move in the direction of the arrows (if they are not blocked in)."
do
input "Which puzzle 1 (easy) - 5 (hard) ", sel
until sel >=1 and sel <= 5
# setup the puzzle defining variables
# margin - space between cells
# width - number of cells wide
# height - number of cells tall
# goalx, goaly - top left corner of parking spot
# best - best possible number of moves
# npieces - number of pieces in puzzle
# pieces[r,0] = x position
# pieces[r,1] = y position
# pieces[r,2] = length
# pieces[r,3] = direction 0 = horizontal, 1 = vertical
# pieces[r,4] = color
margin = 5
if sel = 1 then
height = 6
width = height
goalx = 2
goaly = 0
best = 5
npieces = 3
dim pieces(npieces,5)
pieces[0,0] = 2
pieces[0,1] = 1
pieces[0,2] = 3
pieces[0,3] = 1
pieces[0,4] = red
pieces[1,0] = 0
pieces[1,1] = 0
pieces[1,2] = 3
pieces[1,3] = 0
pieces[1,4] = blue
pieces[2,0] = 3
pieces[2,1] = 0
pieces[2,2] = 3
pieces[2,3] = 1
pieces[2,4] = green
end if
if sel = 2 then
height = 6
width = height
goalx = 3
goaly = 0
best = 6
npieces = 7
dim pieces(npieces,5)
pieces = {3, 1, 2, 1, red, 1, 0, 2, 1, yellow, 1, 2, 2, 1, blue, 2, 0, 3, 0, green, 1, 4, 3, 0, yellow, 5, 0, 3, 1, blue, 5, 3, 3, 1, purple}
end if
if sel = 3 then
margin = 5
height = 6
width = 6
goalx = 5
goaly = 0
best = 17
npieces = 6
dim pieces(npieces,5)
pieces = {5, 1, 3, 1, red, 3, 0, 3, 0, blue, 1, 2, 3, 0, blue, 3, 3, 2, 0, green, 4, 1, 2, 1, yellow, 2, 0, 2, 1, yellow}
end if
if sel = 4 then
margin = 5
height = 6
width = 6
goalx = 2
goaly = 0
best = 28
npieces = 8
dim pieces(npieces,5)
pieces = {2, 3, 2, 1, red, 1, 0, 3, 0, blue, 2, 1, 2, 0, blue, 2, 2, 3, 0, green, 3, 3, 2, 0, yellow, 4, 4, 2, 0, yellow, 4, 0, 2, 1, purple, 5, 0, 2, 1, purple}
end if
if sel = 5 then
margin = 5
height = 6
width = 6
goalx = 4
goaly = 0
best = 41
npieces = 8
dim pieces(npieces,5)
pieces = {4, 1, 2, 1, red, 3, 0, 3, 0, blue, 0, 1, 2, 0, blue, 0, 2, 3, 0, green, 4, 3, 2, 0, yellow, 4, 5, 2, 0, yellow, 2, 0, 2, 1, purple, 1, 3, 3, 1, purple}
end if
return