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

Ascii numbers - countdown print function?

Started by Goof, 25 January 2014 - 07:07 PM
Goof #1
Posted 25 January 2014 - 08:07 PM
Hello


Let me start this by saying that i honestly don't know exactly how to explain this but i will try.


I am making a ASCII number art printer (i dont know what its called but it print numbers in ASCII art)

so:

I'm making it to display the number on the screen, but im in trouble when it gets to numbers ABOVE 9. ( because then it needs two different ascii art numbers to combine and print correctly )

And thats where my problem is.


ascii table:
Spoiler

local ascii={
	["1"]={
		[[   ]];
		[[###]];
		[[###]];
		[[###]];
		[[###]];
		[[###]];
		[[###]];
		[[###]];
		[[###]];
		[[###]];
		[[###]];
	};
	["2"]={
		[[		 ]];
		[[#########]];
		[[#########]];
		[[	   ##]];
		[[	   ##]];
		[[#########]];
		[[#########]];
		[[##	   ]];
		[[##	   ]];
		[[#########]];
		[[#########]];
	};
	["3"]={
		[[		 ]];
		[[#########]];
		[[#########]];
		[[	   ##]];
		[[	   ##]];
		[[   ######]];
		[[   ######]];
		[[	   ##]];
		[[	   ##]];
		[[#########]];
		[[#########]];
	};
	["4"]={
		[[		 ]];
		[[##	 ##]];
		[[##	 ##]];
		[[##	 ##]];
		[[##	 ##]];
		[[#########]];
		[[#########]];
		[[	   ##]];
		[[	   ##]];
		[[	   ##]];
		[[	   ##]];
	};
	["5"]={
		[[		 ]];
		[[#########]];
		[[#########]];
		[[##	   ]];
		[[##	   ]];
		[[#########]];
		[[#########]];
		[[	   ##]];
		[[	   ##]];
		[[#########]];
		[[#########]];
	};
	["6"]={
		[[		 ]];
		[[#########]];
		[[#########]];
		[[##	   ]];
		[[##	   ]];
		[[#########]];
		[[#########]];
		[[##	 ##]];
		[[##	 ##]];
		[[#########]];
		[[#########]];
	};
	["7"]={
		[[		 ]];
		[[#########]];
		[[#########]];
		[[	   ##]];
		[[	   ##]];
		[[	   ##]];
		[[	   ##]];
		[[	   ##]];
		[[	   ##]];
		[[	   ##]];
		[[	   ##]];
	};
	["8"]={
		[[		 ]];
		[[#########]];
		[[#########]];
		[[##	 ##]];
		[[##	 ##]];
		[[#########]];
		[[#########]];
		[[##	 ##]];
		[[##	 ##]];
		[[#########]];
		[[#########]];
	};
	["9"]={
		[[		 ]];
		[[#########]];
		[[#########]];
		[[##	 ##]];
		[[##	 ##]];
		[[#########]];
		[[#########]];
		[[	   ##]];
		[[	   ##]];
		[[	   ##]];
		[[	   ##]];
	};
	["0"]={
		[[		 ]];
		[[#########]];
		[[#########]];
		[[##	 ##]];
		[[##	 ##]];
		[[##	 ##]];
		[[##	 ##]];
		[[##	 ##]];
		[[##	 ##]];
		[[#########]];
		[[#########]];
	};
}
Then I have a function which works perfectly, IF i only type in letters below 10

function:

function printAsciiNumber(num, x)
	if not num then error("NUMBER NOT SPECIFIED!",2) end
	if not x then x=10 end
	for k,v in pairs(ascii[num]) do
		term.setCursorPos(x,k+43)
		term.setTextColor(colors.blue)
		write(v)
	end
end


if i for example enter 10 in the "num" variable, then it won't do anything, except error out.

Can anyone help me unpacking a doublesymboled number ( idk if thats what higher than 9-numbers are called that )

I might be using the string.gsub or string.gmatch here, to unpack it to a table and then use a for k,v in pairs loop?


well i have no idea, how to…


Thanks in Advance
Edited on 26 January 2014 - 03:12 PM
CometWolf #2
Posted 25 January 2014 - 08:14 PM
This would obviously error as there is no value indexed at 10 in your table. If you want to split the numbers into singular ones, you'll have to use string.gmatch, with a digit pattern(%d).

function printAsciiNumber(num, x)
  if not num then error("NUMBER NOT SPECIFIED!",2) end
  if not x then x=10 end
  for number in string.gmatch(string.format(num),"%d") do
    for k,v in pairs(ascii[number]) do
	  term.setCursorPos(x,k+43)
	  term.setTextColor(colors.blue)
	  write(v)
    end
  end
end
This will print derp though, cause of the cursor position. Im sure you can work that out on your own.
Goof #3
Posted 25 January 2014 - 08:16 PM
Yay! I almost knew it was something with the string.gmatch :D/>

Thank you so much!
:D/>


I'll return later, if anything else wents wrong :D/>
OReezy #4
Posted 25 January 2014 - 08:18 PM
Edit: Never mind my bad
Edited on 25 January 2014 - 07:19 PM
Goof #5
Posted 26 January 2014 - 11:43 AM
Well… I came up with the following "solution" ;)/>

function printAsciiNumber(num, x)
	if not num then error("NUMBER NOT SPECIFIED!",2) end
	if not x then x=10 end
	local count = 1
	for number in string.gmatch(string.format(num),"%d") do
		for k,v in pairs(ascii[number]) do
			term.setTextColor(colors.blue)
			if count == 1 then
				term.setCursorPos(x,k+43)
				write(v)
			elseif count == 2 then
				term.setCursorPos(x*2,k+43)
				write(v)
			elseif count == 3 then
				term.setCursorPos(x*3,k+43)
				write(v)
			elseif count == 4 then
				term.setCursorPos(x*4,k+43)
				write(v)
			end
		end
		count = count + 1
	end
end

I think, that might be the shortest and simplest solution? :huh:/>


Well. Thank you for your help :D/>
CometWolf #6
Posted 26 January 2014 - 01:19 PM

function printAsciiNumber(num, x)
	if not num then error("NUMBER NOT SPECIFIED!",2) end
    num = string.format(num)
    if num:match"%D" then
	  error"ASCII print only accepts numbers!"
    end
	if not x then x=10 end
    for i=1,#num do
		for k,v in pairs(ascii[num:sub(i,i)]) do
			term.setTextColor(colors.blue)
		    term.setCursorPos(x*i,k+43)
		    write(v)
		end
	end
end
:P/>
Goof #7
Posted 26 January 2014 - 02:33 PM
Hehe :D/>

I just found something very bad.

If i for an example call printAsciiNumber with:

printAsciiNumber("00:15:00",10,43)


and here is my new printAsciiNumber func:

function printAsciiNumber(num, x, y, bg)-- t = type
	term.setBackgroundColor(bg)
	if not num then error("num Variable NOT SPECIFIED!",2) end
	if not x then x=10 end
	local count = 1
	for number in string.gmatch(string.format(num),"%d") do
		for k,v in pairs(ascii[number]) do
			term.setTextColor(colors.blue)
			if count == 1 then
				term.setCursorPos(x,y+k-1)
				write(v)
			elseif count == 2 then
				term.setCursorPos(x+(#("		  ")),y+k-1)
				write(v)
			elseif count == 3 then
				term.setCursorPos(x+(#("					")),y+k-1)
				write(v)
			elseif count == 4 then
				term.setCursorPos(x+(#("							  ")),y+k-1)
				write(v)
			end
		end
		count = count + 1
	end
end

I know i didn't use yours yet, but i think im going to soon, when i've solved this problem:

As the code before ( when i call the function ) and i put a ":" then it doesnt print it,,,… In the Ascii table i already placed that in the table, but the problem is in the string.match("%D")
it only goes through numbers inserted..

I want to use this function on a countdown also( like this: )

Spoiler



I hope you know how to solve that ehhm stupid problem.



Thanks in Advance
Edited on 26 January 2014 - 01:33 PM
CometWolf #8
Posted 26 January 2014 - 04:20 PM
the pattern "%D" means anything but digits, so basically if the string contained anything other than digits, it would error.
give this a try.

function printAsciiNumber(num, x)
	if not num then error("NUMBER NOT SPECIFIED!",2) end
	num = string.format(num)
	if not num:match"[0-9:]" then
	  error"Non-existant ASCII print character!"
	end
	if not x then x=10 end
	for i=1,#num do
		for k,v in pairs(ascii[num:sub(i,i)]) do
			term.setTextColor(colors.blue)
			term.setCursorPos(x*i,k+43)
			write(v)
		end
	end
end
Also, if you wanna learn about patterns
http://lua-users.org/wiki/PatternsTutorial
Goof #9
Posted 26 January 2014 - 04:26 PM
Ehhm… Weird looking?
Oh EDIT:

Wait…I were using a wrong X position xD

Derpy me :D/>


Thanks :D/>
Edited on 26 January 2014 - 03:27 PM