Jump to content




[Solved] Something wrong in my beginning maze code


9 replies to this topic

#1 icecube45

  • Members
  • 38 posts

Posted 14 May 2013 - 08: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()


#2 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 15 May 2013 - 01: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


#3 icecube45

  • Members
  • 38 posts

Posted 15 May 2013 - 05:51 PM

Thank you!

#4 icecube45

  • Members
  • 38 posts

Posted 15 May 2013 - 07:10 PM

View PostKaoS, on 15 May 2013 - 01:08 AM, said:

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

#5 W00dyR

  • Members
  • 135 posts

Posted 15 May 2013 - 07:19 PM

View Posticecube45, on 15 May 2013 - 07:10 PM, said:

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

#6 icecube45

  • Members
  • 38 posts

Posted 15 May 2013 - 07: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

#7 W00dyR

  • Members
  • 135 posts

Posted 15 May 2013 - 07:40 PM

View Posticecube45, on 15 May 2013 - 07:38 PM, said:

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.

#8 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 16 May 2013 - 12:50 AM

View Posticecube45, on 15 May 2013 - 07:10 PM, said:

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

#9 icecube45

  • Members
  • 38 posts

Posted 19 May 2013 - 07: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)

#10 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 19 May 2013 - 07:11 PM

saywhat :blink: that shouldn't cause any issues

anyways I'm glad





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users