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

Random Numbers printing (1.3)

Started by Wing, 23 October 2012 - 08:44 PM
Wing #1
Posted 23 October 2012 - 10:44 PM
Unless you couldn't tell, this is a navigation program for a turtle. Although I ran into a problem! When the program is ran, it instantly terminates and prints out a random number generally below 100. So I came here looking for help, make any suggestion please!
(code is incomplete, Y vector isn't coded or collisions)

Dir = 0 --1 North, 2 East, 3 South, 4 West
--Ini Vector of turtle
sX = -884
sY = 4
sZ = -1554
--Current Vector of turtle
cX = -884
cY = 4
cZ = -1554
--Vector Adjustements
hX = 0
hY = 0
hZ = 0
--Vector Wanted
wX = -885
wY = 4
wZ = -1557
iniVector()
function Vectoradd(VaX,VaY,VaZ,VbX,VbY,VbZ,RvX,RvY,RvZ)
local VrX = VaX + VbX
local VrY = VaY + VbY
local VrZ = VaZ + VbZ
    RvX = VrX
    RvY = VrY
    RvZ = VrZ
end
function Vectorsous(VaX,VaY,VaZ,VbX,VbY,VbZ,RvX,RvY,RvZ)
local VrX = VaX - VbX
local VrY = VaY - VbY
local VrZ = VaZ - VbZ
    RvX = VrX
    RvY = VrY
    RvZ = VrZ
end
function DirAnalyze(wDir,cDir)
if (wDir == 1) and (cDir == 2) then
    left(1)
elseif (wDir == 1) and (cDir == 3) then
    left(2)
elseif (wDir == 1) and (cDir == 4) then
    right(1)
elseif (wDir == 2) and (cDir == 1) then
    right(1)
elseif (wDir == 2) and (cDir == 3) then
    left(1)
elseif (wDir == 2) and (cDir == 4) then
    left(2)
elseif (wDir == 3) and (cDir == 1) then
    right(2)
elseif (wDir == 3) and (cDir == 2) then
    right(1)
elseif (wDir == 3) and (cDir == 4) then
    left(1)
elseif (wDir == 4) and (cDir == 1) then
    left(1)
elseif (wDir == 4) and (cDir == 2) then
    right(2)
elseif (wDir == 4) and (cDir == 3) then
    right(1)
end
    wDir = Dir
end
function left(times)
    local done = 0
repeat
    turtle.turnLeft()
    done = done +1
until done == times
end
function right(times)
    local done = 0
repeat
    turtle.turnRight()
    done = done +1
until done == times
end
function forward()
    turtle.forward()
    VectorAdjust("forward",Dir,cX,cY,cZ)
end
function VectorAdjust(move,cDir,VaX,VaY,VaZ)
if (move == "forward") then
    if (cDir == 1) then
	    local VaZ = VaZ -1
    elseif (cDir == 2) then
	    local VaX = VaX +1
    elseif (cDir == 3) then
	    local VaZ = VaZ +1
    elseif (cDir == 4) then
	    local VaX = VaX -1
    end
elseif (move == "backward") then
    if (cDir == 1) then
	    local VaZ = VaZ +1
    elseif (cDir == 2) then
	    local VaX = VaX -1
    elseif (cDir == 3) then
	    local VaZ = VaZ -1
    elseif (cDir == 4) then
	    local VaX = VaX +1
    end
end
end
function iniVector()
Vectorsous(sX,sY,sZ,wX,wY,wZ,hX,hY,hZ)
handler()
end
function handler()
if (Dir == 1) and (hZ > 0) then
    forward()
elseif (Dir == 1) and (hZ < 0) then
    DirAnalyze(3,1)
elseif (Dir == 2) and (hX > 0) then
    forward()
elseif (Dir == 2) and (hX < 0) then
    DirAnalyze(4,2)
elseif (Dir == 3) and (hZ < 0) then
    forward()
elseif (Dir == 3) and (hZ > 0) then
    DirAnalyze(1,3)
elseif (Dir == 4) and (hX < 0) then
    forward()
elseif (Dir == 4) and (hX > 0) then
    DirAnalyze(2,4)
end
end
ChunLing #2
Posted 23 October 2012 - 11:14 PM
It's outputting an error and the line where it occurred. Tell us the error message.
remiX #3
Posted 24 October 2012 - 12:10 AM
You call iniVector() before it's even defined.
Wing #4
Posted 24 October 2012 - 01:03 AM
sIdEkIcK_ good point lol, It's not printing random numbers anymore, but simply not doing anything, boots and terminates….
Here are my changes:

Dir = 0 --1 North, 2 East, 3 South, 4 West
--Ini Vector of turtle
sX = -884
sY = 4
sZ = -1554
--Current Vector of turtle
cX = -884
cY = 4
cZ = -1554
--Vector Adjustements
hX = 0
hY = 0
hZ = 0
--Vector Wanted
wX = -885
wY = 4
wZ = -1557
function Vectoradd(VaX,VaY,VaZ,VbX,VbY,VbZ,RvX,RvY,RvZ)
local VrX = VaX + VbX
local VrY = VaY + VbY
local VrZ = VaZ + VbZ
    RvX = VrX
    RvY = VrY
    RvZ = VrZ
end
function Vectorsous(VaX,VaY,VaZ,VbX,VbY,VbZ,RvX,RvY,RvZ)
local VrX = VaX - VbX
local VrY = VaY - VbY
local VrZ = VaZ - VbZ
    RvX = VrX
    RvY = VrY
    RvZ = VrZ
end
function DirAnalyze(wDir,cDir)
if (wDir == 1) and (cDir == 2) then
    left(1)
elseif (wDir == 1) and (cDir == 3) then
    left(2)
elseif (wDir == 1) and (cDir == 4) then
    right(1)
elseif (wDir == 2) and (cDir == 1) then
    right(1)
elseif (wDir == 2) and (cDir == 3) then
    left(1)
elseif (wDir == 2) and (cDir == 4) then
    left(2)
elseif (wDir == 3) and (cDir == 1) then
    right(2)
elseif (wDir == 3) and (cDir == 2) then
    right(1)
elseif (wDir == 3) and (cDir == 4) then
    left(1)
elseif (wDir == 4) and (cDir == 1) then
    left(1)
elseif (wDir == 4) and (cDir == 2) then
    right(2)
elseif (wDir == 4) and (cDir == 3) then
    right(1)
end
    wDir = Dir
end
function left(times)
    local done = 0
repeat
    turtle.turnLeft()
    done = done +1
until done == times
end
function right(times)
    local done = 0
repeat
    turtle.turnRight()
    done = done +1
until done == times
end
function forward()
    turtle.forward()
    VectorAdjust("forward",Dir,cX,cY,cZ)
end
function VectorAdjust(move,cDir,VaX,VaY,VaZ)
if (move == "forward") then
    if (cDir == 1) then
	    local VaZ = VaZ -1
    elseif (cDir == 2) then
	    local VaX = VaX +1
    elseif (cDir == 3) then
	    local VaZ = VaZ +1
    elseif (cDir == 4) then
	    local VaX = VaX -1
    end
elseif (move == "backward") then
    if (cDir == 1) then
	    local VaZ = VaZ +1
    elseif (cDir == 2) then
	    local VaX = VaX -1
    elseif (cDir == 3) then
	    local VaZ = VaZ -1
    elseif (cDir == 4) then
	    local VaX = VaX +1
    end
end
end
function iniVector()
Vectorsous(sX,sY,sZ,wX,wY,wZ,hX,hY,hZ)
handler()
end
function handler()
if (Dir == 1) and (hZ > 0) then
    forward()
elseif (Dir == 1) and (hZ < 0) then
    DirAnalyze(3,1)
elseif (Dir == 2) and (hX > 0) then
    forward()
elseif (Dir == 2) and (hX < 0) then
    DirAnalyze(4,2)
elseif (Dir == 3) and (hZ < 0) then
    forward()
elseif (Dir == 3) and (hZ > 0) then
    DirAnalyze(1,3)
elseif (Dir == 4) and (hX < 0) then
    forward()
elseif (Dir == 4) and (hX > 0) then
    DirAnalyze(2,4)
end
end
if (hX == 0) and (hY == 0) and (hZ == 0) then
    iniVector()
else
    handler()
    end
ChunLing #5
Posted 24 October 2012 - 01:47 AM
You can call something before it gets defined if it is defined global or has a namespace reserved in the local scope.

But now it's more apparent what the problem is. You don't have a program loop. You call iniVector() which calls some other things, which do a bunch of math and that would eventually cause you to do something I guess with repeated calls to handler, but you only ever just get the one call and then you're done and the program is over.