initial commit
This commit is contained in:
192
mazeSolver
Normal file
192
mazeSolver
Normal file
@@ -0,0 +1,192 @@
|
||||
--[[
|
||||
Allows a turtle to keep track of it's
|
||||
location using a coordinate system.
|
||||
|
||||
Use the commands goForward(), goRight(),
|
||||
goLeft(), goUp(), and goDown() to
|
||||
control the turtle while adding to
|
||||
the coordinates.
|
||||
|
||||
goHome() will send the turtle back to
|
||||
it's origin and will point it north:
|
||||
the direction it was originally facing.
|
||||
|
||||
A turtle always faces north when it is placed down
|
||||
]]--
|
||||
|
||||
x = 0
|
||||
y = 0
|
||||
z = 0
|
||||
|
||||
saveX = 0
|
||||
saveY = 0
|
||||
saveZ = 0
|
||||
|
||||
ZX = 0
|
||||
ZY = 0
|
||||
ZZ = 0
|
||||
|
||||
saveDirection = NORTH
|
||||
|
||||
NORTH = 1
|
||||
EAST = 2
|
||||
SOUTH = 3
|
||||
WEST = 4
|
||||
|
||||
UP = 5
|
||||
DOWN = 6
|
||||
|
||||
RIGHT = 7
|
||||
LEFT = 8
|
||||
|
||||
direction = NORTH
|
||||
|
||||
--[[sends a turtle forward 1 block
|
||||
and adds to it's X coordinate]]--
|
||||
function goForward()
|
||||
if turtle.forward() then
|
||||
addToCoords(direction)
|
||||
return true
|
||||
else return false
|
||||
end
|
||||
end
|
||||
|
||||
--[[sends a turtle up 1 block
|
||||
and adds to it's Z coordinate]]--
|
||||
function goUp()
|
||||
if turtle.up() then
|
||||
addToCoords(UP)
|
||||
return true
|
||||
else return false
|
||||
end
|
||||
end
|
||||
|
||||
--[[sends a turtle down 1 block
|
||||
and subtracts from it's Z coordinate]]--
|
||||
function goDown()
|
||||
if turtle.down() then
|
||||
addToCoords(DOWN)
|
||||
return true
|
||||
else return false
|
||||
end
|
||||
end
|
||||
|
||||
--[[turns a turtle right and changes
|
||||
it's direction accordingly]]--
|
||||
function goRight()
|
||||
turtle.turnRight()
|
||||
changeDirection(RIGHT)
|
||||
end
|
||||
|
||||
--[[turns a turtle left and changes
|
||||
it's direction accordingly]]--
|
||||
function goLeft()
|
||||
turtle.turnLeft()
|
||||
changeDirection(LEFT)
|
||||
end
|
||||
|
||||
--[[moves a turtle forward a specified number of blocks]]--
|
||||
function move(distance)
|
||||
for i=1,distance do
|
||||
goForward()
|
||||
end
|
||||
end
|
||||
|
||||
--[[moves a turtle up a specified number of blocks]]--
|
||||
function moveUp(distance)
|
||||
for i=1,distance do
|
||||
goUp()
|
||||
end
|
||||
end
|
||||
|
||||
--[[moves a turtle down a specified number of blocks]]--
|
||||
function moveDown(distance)
|
||||
for i=1,distance do
|
||||
goDown()
|
||||
end
|
||||
end
|
||||
|
||||
--[[takes the direction of the turtle and adds 1
|
||||
to the appropriate coordinate based on that direction]]--
|
||||
function addToCoords(direction)
|
||||
if direction == NORTH then
|
||||
x = x+1
|
||||
elseif direction == EAST then
|
||||
y = y+1
|
||||
elseif direction == SOUTH then
|
||||
x = x-1
|
||||
elseif direction == WEST then
|
||||
y = y-1
|
||||
elseif direction == UP then
|
||||
z = z+1
|
||||
else
|
||||
z = z-1
|
||||
end
|
||||
end
|
||||
|
||||
--[[takes a direction turned (right or left) and changes the
|
||||
direction of the turtle to north, south, east, or west]]--
|
||||
function changeDirection(turned)
|
||||
if turned == RIGHT then
|
||||
if direction == NORTH then
|
||||
direction = EAST
|
||||
elseif direction == EAST then
|
||||
direction = SOUTH
|
||||
elseif direction == SOUTH then
|
||||
direction = WEST
|
||||
else
|
||||
direction = NORTH
|
||||
end
|
||||
else
|
||||
if direction == NORTH then
|
||||
direction = WEST
|
||||
elseif direction == WEST then
|
||||
direction = SOUTH
|
||||
elseif direction == SOUTH then
|
||||
direction = EAST
|
||||
else
|
||||
direction = NORTH
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--[[takes a direction (north, east, south, or west) and turns
|
||||
the turtle in that direction]]--
|
||||
function setDirection(newDirect)
|
||||
while true do
|
||||
if newDirect == direction then
|
||||
break
|
||||
end
|
||||
goRight()
|
||||
end
|
||||
end
|
||||
|
||||
--[[This will solve a three dimensional maze]]--
|
||||
|
||||
WENT_DOWN = 20
|
||||
WENT_UP = 21
|
||||
LAST_MOVE = 0
|
||||
|
||||
|
||||
|
||||
function solve2D()
|
||||
if not goForward() then
|
||||
goLeft()
|
||||
end
|
||||
end
|
||||
|
||||
function solve3D()
|
||||
if not goToZ() then
|
||||
solve2D()
|
||||
end
|
||||
end
|
||||
|
||||
function goToZ(way)
|
||||
if way = UP
|
||||
goUp()
|
||||
LAST_MOVE = WENT_UP
|
||||
else
|
||||
goDown()
|
||||
LAST_MOVE = WENT_DOWN
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user