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

I'm Sure This Can Be Made More Efficient

Started by Chub1337, 20 February 2012 - 03:09 PM
Chub1337 #1
Posted 20 February 2012 - 04:09 PM
So, I made myself a nice little 6 digits redstoneclock ranging from 00:00:00 to 23:59:59, but I'm not really happy with the way I made the funtions "f_left, f_back and f_right".
I'm sure there is a way to decrease the amount of lines needed just for those three, but the question is, how?

Code:
Spoiler

--Vars
colon = false
left = 15351
back = 15351
right = 15351

--Intro
term.clear()
term.setCursorPos(1,1)
print("")
print("")
print("		---------------------------------")
print("		RedPower Clock, made by Chub1337!")
print("		---------------------------------")
print("")
print("")
print("")
print("")
print("")
print("")
print("")
print("")
print("")
print("")
print("")
print("")
write("Press <enter> to continue.")
io.read()
term.clear()
term.setCursorPos(1,1)

--Setup
write("Do you wish to enter a starting value? (Y/N) ")
anwser = io.read()
if anwser == "Y" or anwser == "y" then
	write("Amount of tens of hours (0-2): ")
	u2 = io.read()
	u2 = tonumber(u2)
	if u2 > 2 then u2 = 2 end
	write("Amount of units of hours (0-9): ")
	u1 = io.read()
	u1 = tonumber(u1)
	if u1 > 9 then u1 = 9 end
	if u1 > 3 and u2 == 2 then u1 = 3 end
	write("Amount of tens of minutes (0-5): ")
	m2 = io.read()
	m2 = tonumber(m2)
	if m2 > 5 then m2 = 5 end
	write("Amount of units of minutes (0-9): ")
	m1 = io.read()
	m1 = tonumber(m1)
	if m1 > 9 then m1 = 9 end
	write("Amount of tens of seconds (0-5): ")
	s2 = io.read()
	s2 = tonumber(s2)
	if s2 > 5 then s2 = 5 end
	write("Amount of units of seconds (0-9): ")
	s1 = io.read()
	s1 = tonumber(s1)
	if s1 > 9 then s1 = 9 end
	print("Starting values have been entered, the clock will start at "..u2..u1..":"..m2..m1..":"..s2..s1..".")
	else
	s1 = 0
	s2 = 0
	m1 = 0
	m2 = 0
	u1 = 0
	u2 = 0
	print("No starting values have been entered, the clock will start at 00:00:00")
end

--Functions
function f_colon()
	if colon == true then
	colon = false
	left = colors.combine(left,16384)
	rs.setBundledOutput("left",left)
	else
	colon = true
	left = colors.subtract(left,16384)
	rs.setBundledOutput("left",left)
	end
end

-- =========================== left ==============================

function f_left()
	if s1 == 0 then
		if s2 == 0 then
			left = colors.combine(119,15232)
		elseif s2 == 1 then
			left = colors.combine(119,2176)
		elseif s2 == 2 then
			left = colors.combine(119,7936)
		elseif s2 == 3 then
			left = colors.combine(119,7552)
		elseif s2 == 4 then
			left = colors.combine(119,11392)
		elseif s2 == 5 then
			left = colors.combine(119,13696)
		end
	elseif s1 == 1 then
		if s2 == 0 then
			left = colors.combine(17,15232)
		elseif s2 == 1 then
			left = colors.combine(17,2176)
		elseif s2 == 2 then
			left = colors.combine(17,7936)
		elseif s2 == 3 then
			left = colors.combine(17,7552)
		elseif s2 == 4 then
			left = colors.combine(17,11392)
		elseif s2 == 5 then
			left = colors.combine(17,13696)
		end
	elseif s1 == 2 then
		if s2 == 0 then
			left = colors.combine(62,15232)
		elseif s2 == 1 then
			left = colors.combine(62,2176)
		elseif s2 == 2 then
			left = colors.combine(62,7936)
		elseif s2 == 3 then
			left = colors.combine(62,7552)
		elseif s2 == 4 then
			left = colors.combine(62,11392)
		elseif s2 == 5 then
			left = colors.combine(62,13696)
		end	
	elseif s1 == 3 then
		if s2 == 0 then
			left = colors.combine(59,15232)
		elseif s2 == 1 then
			left = colors.combine(59,2176)
		elseif s2 == 2 then
			left = colors.combine(59,7936)
		elseif s2 == 3 then
			left = colors.combine(59,7552)
		elseif s2 == 4 then
			left = colors.combine(59,11392)
		elseif s2 == 5 then
			left = colors.combine(59,13696)
		end	
	elseif s1 == 4 then
		if s2 == 0 then
			left = colors.combine(89,15232)
		elseif s2 == 1 then
			left = colors.combine(89,2176)
		elseif s2 == 2 then
			left = colors.combine(89,7936)
		elseif s2 == 3 then
			left = colors.combine(89,7552)
		elseif s2 == 4 then
			left = colors.combine(89,11392)
		elseif s2 == 5 then
			left = colors.combine(89,13696)
		end	
	elseif s1 == 5 then
		if s2 == 0 then
			left = colors.combine(107,15232)
		elseif s2 == 1 then
			left = colors.combine(107,2176)
		elseif s2 == 2 then
			left = colors.combine(107,7936)
		elseif s2 == 3 then
			left = colors.combine(107,7552)
		elseif s2 == 4 then
			left = colors.combine(107,11392)
		elseif s2 == 5 then
			left = colors.combine(107,13696)
		end
	elseif s1 == 6 then
		if s2 == 0 then
			left = colors.combine(111,15232)
		elseif s2 == 1 then
			left = colors.combine(111,2176)
		elseif s2 == 2 then
			left = colors.combine(111,7936)
		elseif s2 == 3 then
			left = colors.combine(111,7552)
		elseif s2 == 4 then
			left = colors.combine(111,11392)
		elseif s2 == 5 then
			left = colors.combine(111,13696)
		end
	elseif s1 == 7 then
		if s2 == 0 then
			left = colors.combine(49,15232)
		elseif s2 == 1 then
			left = colors.combine(49,2176)
		elseif s2 == 2 then
			left = colors.combine(49,7936)
		elseif s2 == 3 then
			left = colors.combine(49,7552)
		elseif s2 == 4 then
			left = colors.combine(49,11392)
		elseif s2 == 5 then
			left = colors.combine(49,13696)
		end
	elseif s1 == 8 then
		if s2 == 0 then
			left = colors.combine(127,15232)
		elseif s2 == 1 then
			left = colors.combine(127,2176)
		elseif s2 == 2 then
			left = colors.combine(127,7936)
		elseif s2 == 3 then
			left = colors.combine(127,7552)
		elseif s2 == 4 then
			left = colors.combine(127,11392)
		elseif s2 == 5 then
			left = colors.combine(127,13696)
		end
	elseif s1 == 9 then
		if s2 == 0 then
			left = colors.combine(123,15232)
		elseif s2 == 1 then
			left = colors.combine(123,2176)
		elseif s2 == 2 then
			left = colors.combine(123,7936)
		elseif s2 == 3 then
			left = colors.combine(123,7552)
		elseif s2 == 4 then
			left = colors.combine(123,11392)
		elseif s2 == 5 then
			left = colors.combine(123,13696)
		end
	end
	rs.setBundledOutput("left",left)
end

-- =========================== back ==============================

function f_back()
	if m1 == 0 then
		if m2 == 0 then
			back = colors.combine(119,15232)
		elseif m2 == 1 then
			back = colors.combine(119,2176)
		elseif m2 == 2 then
			back = colors.combine(119,7936)
		elseif m2 == 3 then
			back = colors.combine(119,7552)
		elseif m2 == 4 then
			back = colors.combine(119,11392)
		elseif m2 == 5 then
			back = colors.combine(119,13696)
		end
	elseif m1 == 1 then
		if m2 == 0 then
			back = colors.combine(17,15232)
		elseif m2 == 1 then
			back = colors.combine(17,2176)
		elseif m2 == 2 then
			back = colors.combine(17,7936)
		elseif m2 == 3 then
			back = colors.combine(17,7552)
		elseif m2 == 4 then
			back = colors.combine(17,11392)
		elseif m2 == 5 then
			back = colors.combine(17,13696)
		end
	elseif m1 == 2 then
		if m2 == 0 then
			back = colors.combine(62,15232)
		elseif m2 == 1 then
			back = colors.combine(62,2176)
		elseif m2 == 2 then
			back = colors.combine(62,7936)
		elseif m2 == 3 then
			back = colors.combine(62,7552)
		elseif m2 == 4 then
			back = colors.combine(62,11392)
		elseif m2 == 5 then
			back = colors.combine(62,13696)
		end	
	elseif m1 == 3 then
		if m2 == 0 then
			back = colors.combine(59,15232)
		elseif m2 == 1 then
			back = colors.combine(59,2176)
		elseif m2 == 2 then
			back = colors.combine(59,7936)
		elseif m2 == 3 then
			back = colors.combine(59,7552)
		elseif m2 == 4 then
			back = colors.combine(59,11392)
		elseif m2 == 5 then
			back = colors.combine(59,13696)
		end	
	elseif m1 == 4 then
		if m2 == 0 then
			back = colors.combine(89,15232)
		elseif m2 == 1 then
			back = colors.combine(89,2176)
		elseif m2 == 2 then
			back = colors.combine(89,7936)
		elseif m2 == 3 then
			back = colors.combine(89,7552)
		elseif m2 == 4 then
			back = colors.combine(89,11392)
		elseif m2 == 5 then
			back = colors.combine(89,13696)
		end	
	elseif m1 == 5 then
		if m2 == 0 then
			back = colors.combine(107,15232)
		elseif m2 == 1 then
			back = colors.combine(107,2176)
		elseif m2 == 2 then
			back = colors.combine(107,7936)
		elseif m2 == 3 then
			back = colors.combine(107,7552)
		elseif m2 == 4 then
			back = colors.combine(107,11392)
		elseif m2 == 5 then
			back = colors.combine(107,13696)
		end
	elseif m1 == 6 then
		if m2 == 0 then
			back = colors.combine(111,15232)
		elseif m2 == 1 then
			back = colors.combine(111,2176)
		elseif m2 == 2 then
			back = colors.combine(111,7936)
		elseif m2 == 3 then
			back = colors.combine(111,7552)
		elseif m2 == 4 then
			back = colors.combine(111,11392)
		elseif m2 == 5 then
			back = colors.combine(111,13696)
		end
	elseif m1 == 7 then
		if m2 == 0 then
			back = colors.combine(49,15232)
		elseif m2 == 1 then
			back = colors.combine(49,2176)
		elseif m2 == 2 then
			back = colors.combine(49,7936)
		elseif m2 == 3 then
			back = colors.combine(49,7552)
		elseif m2 == 4 then
			back = colors.combine(49,11392)
		elseif m2 == 5 then
			back = colors.combine(49,13696)
		end
	elseif m1 == 8 then
		if m2 == 0 then
			back = colors.combine(127,15232)
		elseif m2 == 1 then
			back = colors.combine(127,2176)
		elseif m2 == 2 then
			back = colors.combine(127,7936)
		elseif m2 == 3 then
			back = colors.combine(127,7552)
		elseif m2 == 4 then
			back = colors.combine(127,11392)
		elseif m2 == 5 then
			back = colors.combine(127,13696)
		end
	elseif m1 == 9 then
		if m2 == 0 then
			back = colors.combine(123,15232)
		elseif m2 == 1 then
			back = colors.combine(123,2176)
		elseif m2 == 2 then
			back = colors.combine(123,7936)
		elseif m2 == 3 then
			back = colors.combine(123,7552)
		elseif m2 == 4 then
			back = colors.combine(123,11392)
		elseif m2 == 5 then
			back = colors.combine(123,13696)
		end
	end
	rs.setBundledOutput("back",back)
end

-- =========================== right ==============================

function f_right()
	if u1 == 0 then
		if u2 == 0 then
			right = colors.combine(119,15232)
		elseif u2 == 1 then
			right = colors.combine(119,2176)
		elseif u2 == 2 then
			right = colors.combine(119,7936)
		elseif u2 == 3 then
			right = colors.combine(119,7552)
		elseif u2 == 4 then
			right = colors.combine(119,11392)
		elseif u2 == 5 then
			right = colors.combine(119,13696)
		end
	elseif u1 == 1 then
		if u2 == 0 then
			right = colors.combine(17,15232)
		elseif u2 == 1 then
			right = colors.combine(17,2176)
		elseif u2 == 2 then
			right = colors.combine(17,7936)
		elseif u2 == 3 then
			right = colors.combine(17,7552)
		elseif u2 == 4 then
			right = colors.combine(17,11392)
		elseif u2 == 5 then
			right = colors.combine(17,13696)
		end
	elseif u1 == 2 then
		if u2 == 0 then
			right = colors.combine(62,15232)
		elseif u2 == 1 then
			right = colors.combine(62,2176)
		elseif u2 == 2 then
			right = colors.combine(62,7936)
		elseif u2 == 3 then
			right = colors.combine(62,7552)
		elseif u2 == 4 then
			right = colors.combine(62,11392)
		elseif u2 == 5 then
			right = colors.combine(62,13696)
		end	
	elseif u1 == 3 then
		if u2 == 0 then
			right = colors.combine(59,15232)
		elseif u2 == 1 then
			right = colors.combine(59,2176)
		elseif u2 == 2 then
			right = colors.combine(59,7936)
		elseif u2 == 3 then
			right = colors.combine(59,7552)
		elseif u2 == 4 then
			right = colors.combine(59,11392)
		elseif u2 == 5 then
			right = colors.combine(59,13696)
		end	
	elseif u1 == 4 then
		if u2 == 0 then
			right = colors.combine(89,15232)
		elseif u2 == 1 then
			right = colors.combine(89,2176)
		elseif u2 == 2 then
			right = colors.combine(89,7936)
		elseif u2 == 3 then
			right = colors.combine(89,7552)
		elseif u2 == 4 then
			right = colors.combine(89,11392)
		elseif u2 == 5 then
			right = colors.combine(89,13696)
		end	
	elseif u1 == 5 then
		if u2 == 0 then
			right = colors.combine(107,15232)
		elseif u2 == 1 then
			right = colors.combine(107,2176)
		elseif u2 == 2 then
			right = colors.combine(107,7936)
		elseif u2 == 3 then
			right = colors.combine(107,7552)
		elseif u2 == 4 then
			right = colors.combine(107,11392)
		elseif u2 == 5 then
			right = colors.combine(107,13696)
		end
	elseif u1 == 6 then
		if u2 == 0 then
			right = colors.combine(111,15232)
		elseif u2 == 1 then
			right = colors.combine(111,2176)
		elseif u2 == 2 then
			right = colors.combine(111,7936)
		elseif u2 == 3 then
			right = colors.combine(111,7552)
		elseif u2 == 4 then
			right = colors.combine(111,11392)
		elseif u2 == 5 then
			right = colors.combine(111,13696)
		end
	elseif u1 == 7 then
		if u2 == 0 then
			right = colors.combine(49,15232)
		elseif u2 == 1 then
			right = colors.combine(49,2176)
		elseif u2 == 2 then
			right = colors.combine(49,7936)
		elseif u2 == 3 then
			right = colors.combine(49,7552)
		elseif u2 == 4 then
			right = colors.combine(49,11392)
		elseif u2 == 5 then
			right = colors.combine(49,13696)
		end
	elseif u1 == 8 then
		if u2 == 0 then
			right = colors.combine(127,15232)
		elseif u2 == 1 then
			right = colors.combine(127,2176)
		elseif u2 == 2 then
			right = colors.combine(127,7936)
		elseif u2 == 3 then
			right = colors.combine(127,7552)
		elseif u2 == 4 then
			right = colors.combine(127,11392)
		elseif u2 == 5 then
			right = colors.combine(127,13696)
		end
	elseif u1 == 9 then
		if u2 == 0 then
			right = colors.combine(123,15232)
		elseif u2 == 1 then
			right = colors.combine(123,2176)
		elseif u2 == 2 then
			right = colors.combine(123,7936)
		elseif u2 == 3 then
			right = colors.combine(123,7552)
		elseif u2 == 4 then
			right = colors.combine(123,11392)
		elseif u2 == 5 then
			right = colors.combine(123,13696)
		end
	end
	rs.setBundledOutput("right",right)
end

--Clock
while true do
	s1 = s1+1
	if s1 > 9 then
	s1 = 0
	s2 = s2+1
	end
	if s2 > 5 then
	s2 = 0
	m1 = m1+1
	end
	if m1 > 9 then
	m1 = 0
	m2 = m2+1
	end
	if m2 > 5 then
	m2 = 0
	u1 = u1+1
	end
	if u1 > 9 then
	u1 = 0
	u2 = u2+1
	end
	if u2 > 2 then
	u2 = 0
	end
	if u1 > 3 and u2 == 2 then
	u1 = 0
	u2 = 0
	end
	f_left()
	f_back()
	f_right()
	f_colon()
	sleep(1)
	print(u2..u1..":"..m2..m1..":"..s2..s1)
end

So.. Anyone knows how to? ;)/>/>

Chub1337

P.S. If you need more info on the code, I'd happily give it to you :)/>/>
Advert #2
Posted 20 February 2012 - 04:50 PM
I'd suggest using tables:

Here's an example, using one of your elseif chains:

        elseif u1 == 5 then
                if u2 == 0 then
                        right = colors.combine(107,15232)
                elseif u2 == 1 then
                        right = colors.combine(107,2176)
                elseif u2 == 2 then
                        right = colors.combine(107,7936)
                elseif u2 == 3 then
                        right = colors.combine(107,7552)
                elseif u2 == 4 then
                        right = colors.combine(107,11392)
                elseif u2 == 5 then
                        right = colors.combine(107,13696)
                end
end


local t107 = {[0] = 15232, [1] = 2176, [2] = 7936, [3] = 7552, [4] = 11392, [5] = 13696} -- I don't know what else to name your table ;)/>/>
 elseif u1 == 5 then
   right = colors.combine(107, t107[u2]
 end

There are probably other ways of doing this, but it'd require more rewriting.
Chub1337 #3
Posted 20 February 2012 - 05:27 PM
Hmm, that might work I'll try that :)/>/>
I'll edit this post when I've put this in my program :)/>/>

Anyone else who has some bright ideas? ;)/>/>

Chub1337