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

Need help w/ the code I'm writing

Started by Blackspier, 08 February 2016 - 07:42 PM
Blackspier #1
Posted 08 February 2016 - 08:42 PM
Hello,
I'm new to this forum and programming for the most part.
The code I have linked below is my first attempt at creation of a turtle program intended to dig a room in specified dimensions by the user. It will eventually have other options added to it, but for now I am running into a problem where when the "width" of the room stored in the variable "W" is an odd number, when the "length" is advanced forward it digs down rather than dig up. I am not sure if I require an additional "if" statement or something else to correct this. Keep in mind that this only happens when width of the room is an odd number… width as an even number and length and width as either even or odd numbers work correctly.
Thanks in advance for the help.

local L = 0
local W = 0
local D = 0
local lenght = 0
local width = 0
local depth = 0

function clear()
	term.clear()
	term.setCursorPos(1,1)
end

function stopProgram()
	print("(Hold Ctrl+T to end program.)")
end

function askLWD()
	print("Enter numerical values that are greater than one for the following.")
	write("Lenght: ") L = tonumber(read())
	write("Width : ") W = tonumber(read())
	write("Depth : ") D = tonumber(read())
end

function digDown()
	while turtle.detectDown() do
		turtle.digDown()
	end
	turtle.down()
end

function digUp()
	while turtle.detectUp() do
		turtle.digUp()
		os.sleep(.5)
	end
	turtle.up()
end

function digRight()
	turtle.turnRight()
	while turtle.detect() do
		turtle.dig()
		os.sleep(.5)
	end
	turtle.forward()
	turtle.turnLeft()
end

function digLeft()
	turtle.turnLeft()
	while turtle.detect() do
		turtle.dig()
		os.sleep(.5)
	end
	turtle.forward()
	turtle.turnRight()
end

clear()
stopProgram()
print("Place the turtle 'IN THE BLOCK' that is top left corner of the room facing the direction that will be the lenght of the room.")
print("Press enter to continue...")
read()
clear()
stopProgram()
askLWD()
while type(L) ~= "number" or type(W) ~= "number" or type(D) ~= "number" do
	clear()
	stopProgram()
	print("Error... You must enter a valid number greater than zero for all 3 values.")
	askLWD()
end
print("The room will be "..L.." in lenght,"..W.." in width &"..D.."in depth.")
print("Press enter to begin creating room.")
stopProgram()
read()

for lenght = 1, L do
	for width = 1, W do
		for depth = 1, D do
			if width % 2 ~= 0 then
				if depth ~= D then
					digDown()
				else
				end
			else
				if depth ~= D then
					digUp()
				else
				end
			end
		end
		if lenght % 2 ~= 0 then
			if width ~= W then
				digRight()
			else
			end
		else
			if width ~= W then
				digLeft()
			else
			end
		end
	end
	if lenght ~= L then
		while turtle.detect() do
			turtle.dig()
			os.sleep(.5)
		end
		turtle.forward()
	else
	end
end
Bomb Bloke #2
Posted 09 February 2016 - 02:52 AM
for lenght = 1, L do
        if W % 2 ~= 0 and lenght ~= 1 then bump = 1 else bump = 0 end
        for width = 1, W do
                for depth = 1, D do
                        if (width + bump) % 2 ~= 0 then

"else" is an optional keyword. If you don't have anything for it to do, then there's no need to include it.