36 posts
Posted 14 May 2013 - 09:22 PM
Hello all! Before I start, thank you!
Anyways
I am codeing a turtle to solve a maze, its VERY basic at the moment, but my problem is follows:
My turtle will check the directions, but will not move in any direction at all.
Code:
function checkroute()
Foward = turtle.detect()
turtle.turnLeft()
Left = turtle.detect()
turtle.turnRight()
turtle.turnRight()
Right = turtle.detect()
turtle.turnLeft()
end
function whichway()
if Left == true and Right == true and Foward == true then
decision = math.random(1, 3)
if decision == 1 then
turtle.turnLeft()
turtle.foward()
end
if decision == 2 then
turtle.foward()
end
if decision == 3 then
turtle.turnRight()
turtle.foward()
end
end
if Left == false and Right == true and Foward == true then
decision = math.random(1, 2)
if decision == 1 then
turtle.turnRight()
turtle.foward()
end
if decision == 2 then
turtle.foward()
end
end
end
term.clear()
term.setCursorPos(1,1)
print("MazeOS")
checkroute()
whichway()
1548 posts
Location
That dark shadow under your bed...
Posted 15 May 2013 - 02:08 AM
You should be using
Foward = not turtle.detect()
because you can only move in that direction if there is no block to be detected. You are also making an if statement for every possible combination of detections, that is not very effective. Here is a basic code based on your technique, give it a try
local function left()
turtle.turnLeft()
return turtle.forward()
end
local function right()
turtle.turnRight()
return turtle.forward()
end
function path()
--CHECK OPTIONS
local tOptions={}
if not turtle.detect() then
table.insert(tOptions, turtle.forward)
end
turtle.turnLeft()
if not turtle.detect() then
table.insert(tOptions, left)
end
turtle.turnRight() turtle.turnRight()
if not turtle.detect() then
table.insert(tOptions, right)
end
-- MOVE
if #tOptions==0 then
error("dead end :(/>") --you can also just turtle.turnRight() turtle.turnRight()
else
tOptions[math.random(1, #tOptions)]()
end
end
36 posts
Posted 15 May 2013 - 06:51 PM
Thank you!
36 posts
Posted 15 May 2013 - 08:10 PM
You should be using
Foward = not turtle.detect()
because you can only move in that direction if there is no block to be detected. You are also making an if statement for every possible combination of detections, that is not very effective. Here is a basic code based on your technique, give it a try
local function left()
turtle.turnLeft()
return turtle.forward()
end
local function right()
turtle.turnRight()
return turtle.forward()
end
function path()
--CHECK OPTIONS
local tOptions={}
if not turtle.detect() then
table.insert(tOptions, turtle.forward)
end
turtle.turnLeft()
if not turtle.detect() then
table.insert(tOptions, left)
end
turtle.turnRight() turtle.turnRight()
if not turtle.detect() then
table.insert(tOptions, right)
end
-- MOVE
if #tOptions==0 then
error("dead end :(/>/>") --you can also just turtle.turnRight() turtle.turnRight()
else
tOptions[math.random(1, #tOptions)]()
end
end
Im sorry to bug you again, but
tOptions[math.random(1, #tOptions)]()
Will not make the turtle move, I understand what it is doing, but the turtle will not respond… Have also tried putting it in other parts of the code
Thank you!
-Ice
135 posts
Posted 15 May 2013 - 08:19 PM
Will not make the turtle move, I understand what it is doing, but the turtle will not respond… Have also tried putting it in other parts of the code
Thank you!
-Ice
I haven't read the entire code, but does your turtle have fuel? Maybe that is the problem it doesn't move :P/>, it's a small problem which I read here all the time :P/>
36 posts
Posted 15 May 2013 - 08:38 PM
Im using an older version of computercraft (Tekkit Classic) which may be the problem, it will check the directions, but will not move in any direction
135 posts
Posted 15 May 2013 - 08:40 PM
Im using an older version of computercraft (Tekkit Classic) which may be the problem, it will check the directions, but will not move in any direction
Try making it print out everything it does, after every action, thats how I solve most of my problems. When it doesn't print a message that it should, you know where your error is.
1548 posts
Location
That dark shadow under your bed...
Posted 16 May 2013 - 01:50 AM
Will not make the turtle move, I understand what it is doing, but the turtle will not respond…
is the turtle at least turning as if it is going to move? add a print("right") and print("left") to those functions and replace line 13 with
table.insert(tOptions, function() print("forward") turtle.forward() end)
check if anything prints
36 posts
Posted 19 May 2013 - 08:02 PM
I got it working, thank you for all your help, the problem was the return before the turtle.forward()
(don't know why)
1548 posts
Location
That dark shadow under your bed...
Posted 19 May 2013 - 08:11 PM
saywhat :blink:/> that shouldn't cause any issues
anyways I'm glad