This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
icecube45's profile picture

[Solved] Something wrong in my beginning maze code

Started by icecube45, 14 May 2013 - 07:22 PM
icecube45 #1
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()
KaoS #2
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
icecube45 #3
Posted 15 May 2013 - 06:51 PM
Thank you!
icecube45 #4
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
W00dyR #5
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/>
icecube45 #6
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
W00dyR #7
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.
KaoS #8
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
icecube45 #9
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)
KaoS #10
Posted 19 May 2013 - 08:11 PM
saywhat :blink:/> that shouldn't cause any issues

anyways I'm glad