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

math problems

Started by pinksheep00, 18 March 2013 - 11:06 AM
pinksheep00 #1
Posted 18 March 2013 - 12:06 PM
Title: math problems

so my problem is how can i tell how many percent of work is done?
e.g.
you gave your turtle two numbers (length, width) and his job is to walk all around a square (like the excavate program except it doesn't mine blocks) can I use the two numbers given to the computer to tell how many percent of work is done and send that percent to another computer ( via rednet ). if so can anyone help me?

btw here's the code:

local wlk = { ... }
local length, width = tonumber(wlk[1]), tonumber(wlk[2])

if #wlk ~= 2 or type(length) ~= "number" or type(width) ~= "number" then --checking user'sInput
	print( "use the program properly!" )
	return
end

local function walkLength() --move forward for (length) times
	for i = 2, length do
		turtle.forward()
	end
end

face = 0
local function altTurn() --alternating turn (left, left),(right, right)
	if face < 2 then
		face = face+1
		turtle.turnLeft()
	elseif face == 2 then
		face = face+1
		turtle.turnRight()
	else
		face = 0
		turtle.turnRight()
	end
end

local cWidth = 0
turtle.turnRight()
while width ~= cWidth do
	walkLength()
	cWidth = cWidth+1
	if width ~= cWidth then -- rechecking
		altTurn()
		turtle.forward()
		altTurn()
	end
end
The_Awe35 #2
Posted 18 March 2013 - 03:44 PM
I'm not sure how to work it into your code, but it is fairly simple.


len = 10
wid = 10
totalArea = len*wid

--now find your location in the code
currentLen = 3 -- example
currentWid = 7 

spot = currentLen*currentWid
percent = spot/totalArea
percent = percent*100
print(percent) -- if all went correctly, percent should come out as 21 

Again, I'm not sure how to incorporate that into your code, but I don't think it would to terribly hard.
theoriginalbit #3
Posted 18 March 2013 - 03:58 PM
Something like this should work for percentage Code on Pastebin

As for sending it to another computer take a look at the Rednet API
SadKingBilly #4
Posted 18 March 2013 - 04:50 PM
The additional code for the transmitting computer:

rednet.open(side)

percentLength = (currentLength / len) * 100
percentWidth = (currentWidth / width) * 100

rednet.send(ID, percentLength.."%:"..percentWidth.."%")

That will store the percent completed length and the percent completed width, then send it to the computer specified by ID, as the string "50%:50%" (or whatever the current percentages are). The colon is then be used by the receiving computer to separate the percent strings. Here is the code for that computer:

rednet.open(side)
length = "0%"
width = "0%"
while length ~= "100%" and width ~= "100%" do
_, message = rednet.receive()
s, e = string.find(message, ":", 1, true)
length = string.sub(message, 1, s-1)
width = string.sub(message, s+1)
term.clear()
print("Length: "..length)
print("Width:  "..width)
end

That will force the computer to continually receive messages until the turtle has finished moving (that is, both percentages reach 100). When it receives a message, it uses the colon to split the string into the separate percentages, stores those values, clears the screen, and then prints them. You could also use the percentages to create a progress bar if you wanted.

EDIT: Sorry, just realized that you wanted to print a single percentage. Here's the proper code, then:

# Code for the transmitting computer.
rednet.open(side)
percent = ((currentWidth + currentLength) / (length + width)) * 100
rednet.send(ID, percent.."%")

# Code for the receiving computer.
rednet.open(side)
percent = "0%"
while percent ~= "100%" do
    _, message = rednet.receive()
    term.clear()
    print(percent.." completed.")
end