Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- x, y = ...
- direction = 1
- xPos = 0
- yPos = 0
- function turnLeft(x)
- for i=1, x do
- turtle.turnLeft()
- if direction == 1 then
- direction = 4
- else
- direction = direction - 1
- end
- end
- end
- function turnRight(x)
- for i=1, x do
- turtle.turnRight()
- if direction == 4 then
- direction = 1
- else
- direction = direction + 1
- end
- end
- end
- function setDirection(x)
- delta = x - xPos
- if x < 0 then
- turnLeft(-x)
- else
- turnRight(x)
- end
- end
- function forward(x)
- for i=1, x do
- turtle.forward()
- if direction == 1 then
- yPos = yPos + 1
- elseif direction == 2 then
- xPos = xPos + 1
- elseif direction == 3 then
- yPos = yPos - 1
- else
- xPos = xPos - 1
- end
- end
- end
- function back(x)
- for i=1, x do
- turtle.back()
- if direction == 1 then
- yPos = yPos - 1
- elseif direction == 2 then
- xPos = xPos - 1
- elseif direction == 3 then
- yPos = yPos + 1
- else
- xPos = xPos + 1
- end
- end
- end
- function navigate(x, y)
- dx = x - xPos
- dy = y - yPos
- if dx < 0 then
- setDirection(4)
- forward(-dx)
- else
- setDirection(2)
- forward(dx)
- end
- if dy < 0 then
- setDirection(3)
- forward(-dy)
- else
- setDirection(1)
- forward(dy)
- end
- print xPos + ", " + yPos
- end
- navigate(x, y)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement