Jump to content




Program error.

turtle help

75 replies to this topic

#41 detsuo04

  • Members
  • 35 posts

Posted 14 January 2013 - 12:35 PM

OH SNAP i think its working. will report back if it returns :D


ok so sadly it dosnt fully work.

it goes down to bed rock mining the block below it each stem. it also looks at the walls around it each step but it ignores all and just goes to bedrock.
when it hits bedrock it fails to return. still looking over the code

#42 detsuo04

  • Members
  • 35 posts

Posted 14 January 2013 - 01:20 PM

ok now i have fiddle and cleaned the best i can. now its telling me that i need to end the value() function but it sais that even if i add 20 ends to it.
also sais something is wrong with my else statment in value() eof expected?
And it sais do expected on lines where there already is a do statment

rednet.open("right")
rn = rednet.send
fx = turtle.getItemCount(16)
a = fx - 1
sleep(1)
base = 13
rn (base , "Connection established")
--sleep(1)
rn (base , "getting fuel level")
--sleep(1)
turtle.getFuelLevel()
local f = turtle.getFuelLevel()
--sleep(1)
rn (base , "Current fuel level is "..f.."" )
--sleep(1)
if f < 100 then
  turtle.getItemCount(16)
  rn (base , "current coal stack is")
  turtle.select(16)
  turtle.refuel(a)
	else
	rn (base , "Fuel levels are acceptable")
   -- sleep(1)
  end
--sleep(1)
rn (base , "getting coal count")
turtle.getItemCount(16)
--sleep(1)
rn (base, "I have "..fx.." Coal")
--sleep(1)
if fx < 64 then
  rn (base, "DANGER! Coal is low")
  os.reboot()
  else
	rn (base, "Coal storage is full")
	--sleep(2)
	  end
--sleep(1)
rn (base , "comencing program")
--=======comence mining=======--
z = 0
--=======return code==========--
function home()
for n = 1, z +2 do
  turtle.up()
  end
  rn (base , "Mining Operation finished")
  os.shutdown()
end
--============value===========--
function value()
if turtle.detect() == false then
  return false
end
   else
	for i = 1 , 3
	if turtle.compareTo(i) == true then
	return false
	end
	 else
	  return true
	  end
end
--============walls===========--
function walls()
  j = 1
  for j = 1,4 do
	if value() == true then do
	turtle.dig()
	rn (base, "ORE!")
	end
	  else
	  turtle.turnRight()
	  j = j + 1
	  end
end
--=============home===========--
function home()
for i = 1 , z do
turtle.up()
rn (base , "Done, ready for pickup")
os.shutdown()
end
end
--===========Dig Code=========--
while true do
print("2")
f = turtle.getFuelLevel()
  if f < z + 10 then
  do
   rn (base , "Danger! fuel levels low.")
   turtle.select(16)
   turtle.refuel(all)
   f =  turtle.getFuelLevel()
   if f < z + 10 then
   do
   rn (base , "Insuficient Coal, Returning.")
   home()
   end
   end
   end
  
   else
	if turtle.detectDown() == true then
	do
	turtle.digDown()
	turtle.detectDown()
	print("3")
	 if turtle.detectDown() == true then
	 do
	 rn (base , "I hit bedrock, Returning")
	 home()
	 end
	 end
	 end
	  else
	  do
		turtle.down()
		rn (base , "Nothing on level "..z.."next")
		walls()
		z = z + 1
	end
   end
  end
end



#43 crazyguymgd

  • Members
  • 139 posts
  • LocationUSA

Posted 14 January 2013 - 01:24 PM

Here's your value function with proper indentation. As you can see, you're missing key ends (and you have unnecessary ends)
function value()
  if turtle.detect() == false then
	return false
  end
  else
	for i = 1 , 3
	  if turtle.compareTo(i) == true then
		return false
	  end
	  else
		return true
	  end
	end
And here's the correct way to use the if's, else's and ends
function value()
  if turtle.detect() == false then
	return false
  else
	for i = 1 , 3
	  if turtle.compareTo(i) == true then
		return false
	  else
		return true
	  end
	end
  end
end


#44 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 14 January 2013 - 01:29 PM

Again with all the '== true' and '== false' ... while its not a 'bug' or an 'issue' with the code, it is bad coding practise to do this ... a function that returns a boolean or a boolean variable do not need an equality check in an if statement... if you want to check if a boolean variable/return value is true then just type the variable/return value ... if you want to check if its false then add a not before the variable/return value ... saying turtle.detect() == true is like making your while loop this, while true == true do ... its pointless, true will always equal true, just like not false will always equal true, and false will always equal false, and not true will always equal false ... does this make sense?

#45 detsuo04

  • Members
  • 35 posts

Posted 14 January 2013 - 01:32 PM

OMG you are my hero it kept asking me for ends so i think at one point in a blind rage i just "Here take all the ends you can handle mua hahahaha !!! take them untill you explode!!!"

yes this section of code resolved.

ill continue pooring over the
if argument then do error need more dos thing

Quote

Again with all the '== true' and '== false' ...
Oh ok i see what that means now. using if argument then basicly means if argument is true then i understand


EDIT: ok so it still wants there to be a do after each then. if i put a do there it sais i need more ends. when i add the ends it wants more do's

function value()
  if not turtle.detect() then
		return false
		
  else
		for i = 1 , 3
		  if turtle.compareTo(i) then
				return false
			  
		  else
				return true
		  end
		end
  end
end


#46 crazyguymgd

  • Members
  • 139 posts
  • LocationUSA

Posted 14 January 2013 - 01:54 PM

sorry, I missed that do,
for i = 1, 3 do
  if turtle.compare..........
end


#47 detsuo04

  • Members
  • 35 posts

Posted 14 January 2013 - 01:55 PM

i should have noticed that. thank you

#48 crazyguymgd

  • Members
  • 139 posts
  • LocationUSA

Posted 14 January 2013 - 02:01 PM

yup yup

#49 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 14 January 2013 - 02:11 PM

View Postdetsuo04, on 14 January 2013 - 01:32 PM, said:

Quote

Again with all the '== true' and '== false' ...
Oh ok i see what that means now. using if argument then basicly means if argument is true then i understand
Yes thats it, but thats ONLY if the argument either is a boolean variable or is a function returning a boolean or a combo of both ( remember and/or in the tips )
if you have say some input variable and you want to check the user types hello you have to do this
someVariable = read()

if someVariable == "hello" then
this is because someVariable is not a boolean but is a string, so we need to test equality

Pro tip:
Spoiler


#50 crazyguymgd

  • Members
  • 139 posts
  • LocationUSA

Posted 14 January 2013 - 02:13 PM

I keep wanting to like your posts...
dam facebook

#51 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 14 January 2013 - 02:15 PM

View Postcrazyguymgd, on 14 January 2013 - 02:13 PM, said:

I keep wanting to like your posts...
dam facebook
haha yeh, I wish there was a like, and a rep +1 instead of just rep +1... coz sometimes I like what someone says and agree with it, but its not worth a rep up....

#52 detsuo04

  • Members
  • 35 posts

Posted 14 January 2013 - 02:56 PM

I was going backwards there for a bit and hit my loop 222222 problem but i fixed that. now im cycling properly however
im having a problem where the value function only compares slot 1 despite having for i = 1 , 3 do
function value()
  if not turtle.detect() then
  return false
	 else
	    for i = 1 , 3 do
	    print("comparing "..i.." now")
	    turtle.select(i)
		  if turtle.compare() then		 
			    return false
				  else
				  return true
				
				  end
		  end
	    end
end


#53 crazyguymgd

  • Members
  • 139 posts
  • LocationUSA

Posted 14 January 2013 - 03:01 PM

when you return, that means stop the function right there. So here's what I would do:
function value()
  if not turtle.detect() then
    return false
  else
    for i = 1 , 3 do
    print("comparing "..i.." now")
    turtle.select(i)
      if not turtle.compare() then
        return true
      end
    end
    return false
  end
end

As you can see, it will return true if it finds something at i, and returns false only if the for loop exits, and it hasn't returned true yet.
However, this might cause a problem if there is something at 1 and 2 because it'll return true at 1, then skip 2 because it returned.

#54 detsuo04

  • Members
  • 35 posts

Posted 14 January 2013 - 03:06 PM

Awsome. i had just noticed in my program that i was returning false to to the walls function which would accept false as the argument. it wouldnt wate for value() to check slot 2 or 3

Edit:

Ok i just danced a jig when lil hal grabed the wood planking of my floore and proclaimed triumphantly "ORE!"

edit 2:

though he still refuses to return up

#55 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 14 January 2013 - 03:07 PM

Here is what I would do:
function value()
  if not turtle.detect() then
	return false
  end

  for i = 1 , 3 do
	print("comparing "..i.." now")
	turtle.select(i)
	  if turtle.compare() then -- I'm assuming this was meant to be when it finds something
		return true
	  end
	end
  end

  return false
end
Its not any different logically, but it has a slightly better flow, then also if you decide to add anything else in later you wont forget to put return false in at the end of the function...

also just a question, shouldn't the commented line, be how I have it typed there

Edited by TheOriginalBIT, 14 January 2013 - 03:14 PM.


#56 crazyguymgd

  • Members
  • 139 posts
  • LocationUSA

Posted 14 January 2013 - 03:11 PM

View PostTheOriginalBIT, on 14 January 2013 - 03:07 PM, said:

Here is what I would do:
function value()
  if not turtle.detect() then
	return false
  end

  for i = 1 , 3 do
	print("comparing "..i.." now")
	turtle.select(i)
	  if turtle.compare() then -- I'm assuming this was meant to be when it finds something
		return true
	  end
	end
  end

  return false
end
Its not any different logically, but it has a slightly better flow, then also if you decide to add anything else in later you wont forget to put return false in at the end of the function...

also just a question, shouldn't

How is it not different logically? Originally, if the compare with the block at slot 1 was false, he would return false and never check slot 2 or 3. This checks slots 2 and 3 if 1 fails. Which is differentt

#57 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 14 January 2013 - 03:13 PM

View Postcrazyguymgd, on 14 January 2013 - 03:11 PM, said:

How is it not different logically? Originally, if the compare with the block at slot 1 was false, he would return false and never check slot 2 or 3. This checks slots 2 and 3 if 1 fails. Which is differentt

Oops that change to the code was a last minute edit, forgot that I said that ;) :P

#58 crazyguymgd

  • Members
  • 139 posts
  • LocationUSA

Posted 14 January 2013 - 03:18 PM

Ahh and yes it should be if turtle.compare() then.... without the not. thanks for the catch

#59 detsuo04

  • Members
  • 35 posts

Posted 14 January 2013 - 03:32 PM

well in slots 1 2 and 3 i have dirt gravel and stone respectivly. so it checks that its not thoes before it mines it.

i just tested my program and it worked 100%
he mines down checking all his sides as he goes looking for not stone dirt or gravel, when he finds it he mines that.
later i plan on adding a location ticker setup like i did with the z axis so he can venure away from the mine shaft to get an entire veign of ore.
here is the finished code.
rednet.open("right")
rn = rednet.send
fx = turtle.getItemCount(16)
a = fx - 1
sleep(1)
base = 13
rn (base , "Connection established")
--sleep(1)
rn (base , "getting fuel level")
--sleep(1)
turtle.getFuelLevel()
local f = turtle.getFuelLevel()
--sleep(1)
rn (base , "Current fuel level is "..f.."" )
--sleep(1)
if f < 100 then
  turtle.getItemCount(16)
  rn (base , "current coal stack is")
  turtle.select(16)
  turtle.refuel(a)
    else
    rn (base , "Fuel levels are acceptable")
   -- sleep(1)
  end
--sleep(1)
rn (base , "getting coal count")
turtle.getItemCount(16)
--sleep(1)
rn (base, "I have "..fx.." Coal")
--sleep(1)
if fx < 64 then
  rn (base, "DANGER! Coal is low")
  os.reboot() 
  else
    rn (base, "Coal storage is full")
    --sleep(2)
	  end
--sleep(1)
rn (base , "comencing program")
--=======comence mining=======--
z = 0
--=======return code==========--
function home()
n = 0
while n ~= z do
  turtle.up()
  n = n + 1
  end
  rn (base , "Mining Operation finished")
  rn (base , "ready for Pickup")
  os.shutdown()
end
--============value===========--
function value()
ts = turtle.select
tc = turtle.compare
  if not turtle.detect() then
    return false
  else
   ts(1)
    b = turtle.compare()
    ts(2)
    n = turtle.compare()
    ts(3)
    m = turtle.compare()
    if m or n or b then
	  return false
	  else
	  return true
	  end
   
	  end
    end
--============walls===========--
function walls()
  for j = 1,4 do
  print("value function")
    if value() then
    turtle.dig()
    rn (base, "ore collected at "..z.." ")
	  else
	  turtle.turnRight()
	  end
    end
end
--===========Dig Code=========--
z = 0
while true do
  print("2")
  f = turtle.getFuelLevel()
  if f < z + 10 then
    rn (base , "Danger! fuel levels low.")
    turtle.select(16)
    turtle.refuel()
    z = turtle.getFuelLevel()
    if f < z + 10 then
	  rn (base , "Insuficient Coal, Returning.")
	  home()
    end
  elseif turtle.detectDown() then
    turtle.digDown()
    turtle.detectDown()
    print("3")
    if turtle.detectDown() then
	  rn (base , "I hit bedrock, Returning")
	  print("Your home function")
	  home()
	  break  -- to break out of the while loop
    end
  else
	  turtle.down()
	  rn (base , "Nothing on "..z.." moving down")
	  print("your walls function")
	  walls()
	  z = z + 1
  end
end



#60 crazyguymgd

  • Members
  • 139 posts
  • LocationUSA

Posted 14 January 2013 - 03:35 PM

congrats!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users