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

Turtle Labyrinth Problem

Started by darkwarecookie, 23 October 2018 - 05:49 PM
darkwarecookie #1
Posted 23 October 2018 - 07:49 PM
So I wanted to program a turtle to go through this labyrinth: https://drive.google.com/open?id=1Tn2FbKSmAMFO8uwVTZCG0ToA3T35ALWS but for some reason it doesn't work… I programmed the turtle to use RNG since I don't know how to program the turtle, to remember the track^^
Thanks for every help.
Code:
repeat
x = false
y = false
z = false
a1 = false
a2 = false
a3 = false
a4 = false
r = false
g = false
turtle.down()
if turtle.detect() then
x = true
else
x = false
end
if x then
print("Front=Positiv")
else
print("Front=Negativ")
end
turtle.turnLeft()
if turtle.detect() then
y = true
else
y = false
end
if y then
print("Links=Positiv")
else
print("Links=Negativ")
end
turtle.turnRight()
turtle.turnRight()
if turtle.detect() then
z = true
else
z = false
end
if z then
print("Rechts=Positiv")
else
print("Rechts=Negativ")
end
turtle.turnLeft()
repeat
repeat
local a = {1, 2, 3}
b = math.random(#a)
print(B)/>
if b == 1 and x then
g = true
end
if b == 2 and y then
turtle.turnLeft()
g = true
end
if b == 3 and z then
turtle.turnRight()
g = true
end
if x and y and z then
turtle.turnLeft()
end
until g == true
if turtle.detect() then
r = false
else
r = true
end
until r == true
turtle.forward()
if turtle.detect() then
a1 = true
end
turtle.turnLeft()
if turtle.detect() then
a2 = true
end
turtle.turnLeft()
if turtle.detect() then
a3 = true
end
turtle.turnLeft()
if turtle.detect() then
a4 = true
end
turtle.turnLeft()
until a1 and a2 and a3 and a4
print("Ziel erreicht")
Bomb Bloke #2
Posted 24 October 2018 - 01:04 AM
for some reason it doesn't work…

So what does it do, and how exactly does this differ from what you expect it to do?
darkwarecookie #3
Posted 24 October 2018 - 10:27 AM
for some reason it doesn't work…

So what does it do, and how exactly does this differ from what you expect it to do?

It should go through the labyrinth, but as you can see in the picture, it just moves the first three blocks :(/>
Bomb Bloke #4
Posted 24 October 2018 - 04:40 PM
Looking closer at the code, I don't see that it'd run at all: you've got three "until"s in there, but only two "repeat"s.

Putting that aside, there seems to be a bit of a mixup with your x/y/z variables. You set these to true if there's a block at the front/left/right of the turtle: but in your loop lower down, you're doing checks like this:

if b == 1 and x then

This is saying "if b rolled 1, and x is true ("there IS a block in front"), then…". This is the opposite of what you seem to be intending there: "b == 1 and not x" seems to be what you meant.

You would have a much easier time tracking what your code does if you indented it. You could also stand to simplify some things - for eg:

if turtle.detect() then
x = true
else
x = false
end

… could be written as just:

x = turtle.detect()

Whereas the opposite:

if turtle.detect() then
r = false
else
r = true
end

… can be expressed as:

r = not turtle.detect()

This randomisation:

local a = {1, 2, 3}
b = math.random(#a)

… doesn't need a table to measure:

b = math.random(3)

Also consider what happens if the turtle turns inside your loop and then sets "r" as false: your loop will repeat, but x/y/z will no longer relate to the direction the turtle's actually facing.
Edited on 24 October 2018 - 02:41 PM
darkwarecookie #5
Posted 24 October 2018 - 07:31 PM
<p>
Looking closer at the code, I don&amp;amp;#39;t see that it&amp;amp;#39;d run at all: you&amp;amp;#39;ve got three &amp;amp;quot;until&amp;amp;quot;s in there, but only two &amp;amp;quot;repeat&amp;amp;quot;s. Putting that aside, there seems to be a bit of a mixup with your x/y/z variables. You set these to true if there&amp;amp;#39;s a block at the front/left/right of the turtle: but in your loop lower down, you&amp;amp;#39;re doing checks like this:
if b == 1 and x then
This is saying &amp;amp;quot;if b rolled 1, and x is true (&amp;amp;quot;there IS a block in front&amp;amp;quot;), then…&amp;amp;quot;. This is the opposite of what you seem to be intending there: &amp;amp;quot;b == 1 and not x&amp;amp;quot; seems to be what you meant. You would have a much easier time tracking what your code does if you indented it. You could also stand to simplify some things - for eg:
if turtle.detect() then x = true else x = false end
… could be written as just:
x = turtle.detect()
Whereas the opposite:
if turtle.detect() then r = false else r = true end
… can be expressed as:
r = not turtle.detect()
This randomisation:
local a = {1, 2, 3} b = math.random(#a)
… doesn&amp;amp;#39;t need a table to measure:
b = math.random(3)
Also consider what happens if the turtle turns inside your loop and then sets &amp;amp;quot;r&amp;amp;quot; as false: your loop will repeat, but x/y/z will no longer relate to the direction the turtle&amp;amp;#39;s actually facing.
</p>
<p>So i rewrote the hole code, but I still have the same problem https://youtu.be/HrwzKfy7hLs, the turtle takes a new number until it get 3 to move to the right after it moved 7 times left. I don&amp;amp;#39;t know why :(/>/&amp;gt; Thanks for your help</p>
<p>p.s: Do you know a method, so that i don&amp;amp;#39;t need to set every string false invidually ? :o/>/&amp;gt;</p>
new code:
repeat
repeat
a1 = false
a2 = false
a3 = false
b1 = false
b2 = false
c1 = false
c2 = false
c3 = false
c4 = false
d1 = false
a1 = turtle.detect()
turtle.turnLeft()
a2 = turtle.detect()
turtle.turnLeft()
turtle.turnLeft()
a3 = turtle.detect()
turtle.turnLeft()
if a1 then
print("Front=Positiv") else
print("Front=Negativ")
end
if a2 then
print("Links=Positiv") else
print("Links=Negativ")
end
if a3 then
print("Rechts=Positiv") else
print("Rechts=Negativ")
end
b1 = math.random(3)
print(b1)
if b1 == 1 and not a1 then
b2 = true
end
if b1 == 2 and not a2 then
turtle.turnLeft()
b2 = true
end
if b1 == 3 and not a3 then
turtle.turnRight()
b2 = true
end
if a1 and a2 and a3 then
turtle.turnLeft()
end
until b2 == true
turtle.forward()
c1 = turtle.detect()
turtle.turnLeft()
c2 = turtle.detect()
turtle.turnLeft()
c3 = turtle.detect()
turtle.turnLeft()
c4 = turtle.detect()
if c1 and c2 and c3 and c4 then
d1 = true
end
until d1 == true
Edited on 24 October 2018 - 07:07 PM
Bomb Bloke #6
Posted 25 October 2018 - 10:20 AM
Turn left one more time after "c4 = turtle.detect()" and you should see somewhat improved results.
darkwarecookie #7
Posted 25 October 2018 - 11:24 AM
Turn left one more time after "c4 = turtle.detect()" and you should see somewhat improved results.
thanks :3