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

Dice!

Started by Cranium, 14 August 2012 - 12:58 AM
Cranium #1
Posted 14 August 2012 - 02:58 AM
To follow up the success of my Magic 8 Block/, I decided to do a dice program. Inspired by Kazimir's Program.
Enjoy!
Spoiler


function blank()
term.setCursorPos(7,3)
print("+--------------+  +--------------+")
term.setCursorPos(7,4)
print("|			  |  |			  |")
term.setCursorPos(7,5)
print("|			  |  |			  |")
term.setCursorPos(7,6)
print("|			  |  |			  |")
term.setCursorPos(7,7)
print("| Press ENTER  |  |   To  Roll   |")
term.setCursorPos(7,8)
print("|			  |  |			  |")
term.setCursorPos(7,9)
print("|			  |  |			  |")
term.setCursorPos(7,10)
print("|			  |  |			  |")
term.setCursorPos(7,11)
print("+--------------+  +--------------+")
end
function die1()
local x = math.random(1,6)
if x == 1 then
term.setCursorPos(7,3)
print("+--------------+")
term.setCursorPos(7,4)
print("|			  |")
term.setCursorPos(7,5)
print("|			  |")
term.setCursorPos(7,6)
print("|			  |")
term.setCursorPos(7,7)
print("|	  O	   |")
term.setCursorPos(7,8)
print("|			  |")
term.setCursorPos(7,9)
print("|			  |")
term.setCursorPos(7,10)
print("|			  |")
term.setCursorPos(7,11)
print("+--------------+")
elseif x == 2 then
term.setCursorPos(7,3)
print("+--------------+")
term.setCursorPos(7,4)
print("|			  |")
term.setCursorPos(7,5)
print("|		  O   |")
term.setCursorPos(7,6)
print("|			  |")
term.setCursorPos(7,7)
print("|			  |")
term.setCursorPos(7,8)
print("|			  |")
term.setCursorPos(7,9)
print("|  O		   |")
term.setCursorPos(7,10)
print("|			  |")
term.setCursorPos(7,11)
print("+--------------+")
elseif x == 3 then
term.setCursorPos(7,3)
print("+--------------+")
term.setCursorPos(7,4)
print("|			  |")
term.setCursorPos(7,5)
print("|		  O   |")
term.setCursorPos(7,6)
print("|			  |")
term.setCursorPos(7,7)
print("|	  O	   |")
term.setCursorPos(7,8)
print("|			  |")
term.setCursorPos(7,9)
print("|  O		   |")
term.setCursorPos(7,10)
print("|			  |")
term.setCursorPos(7,11)
print("+--------------+")
elseif x == 4 then
term.setCursorPos(7,3)
print("+--------------+")
term.setCursorPos(7,4)
print("|			  |")
term.setCursorPos(7,5)
print("|  O	   O   |")
term.setCursorPos(7,6)
print("|			  |")
term.setCursorPos(7,7)
print("|			  |")
term.setCursorPos(7,8)
print("|			  |")
term.setCursorPos(7,9)
print("|  O	   O   |")
term.setCursorPos(7,10)
print("|			  |")
term.setCursorPos(7,11)
print("+--------------+")
elseif x == 5 then
term.setCursorPos(7,3)
print("+--------------+")
term.setCursorPos(7,4)
print("|			  |")
term.setCursorPos(7,5)
print("|  O	   O   |")
term.setCursorPos(7,6)
print("|			  |")
term.setCursorPos(7,7)
print("|	  O	   |")
term.setCursorPos(7,8)
print("|			  |")
term.setCursorPos(7,9)
print("|  O	   O   |")
term.setCursorPos(7,10)
print("|			  |")
term.setCursorPos(7,11)
print("+--------------+")
elseif x == 6 then
term.setCursorPos(7,3)
print("+--------------+")
term.setCursorPos(7,4)
print("|			  |")
term.setCursorPos(7,5)
print("|  O	   O   |")
term.setCursorPos(7,6)
print("|			  |")
term.setCursorPos(7,7)
print("|  O	   O   |")
term.setCursorPos(7,8)
print("|			  |")
term.setCursorPos(7,9)
print("|  O	   O   |")
term.setCursorPos(7,10)
print("|			  |")
term.setCursorPos(7,11)
print("+--------------+")
end
end
function die2()
local x = math.random(1,6)
if x == 1 then
term.setCursorPos(25,3)
print("+--------------+")
term.setCursorPos(25,4)
print("|			  |")
term.setCursorPos(25,5)
print("|			  |")
term.setCursorPos(25,6)
print("|			  |")
term.setCursorPos(25,7)
print("|	  O	   |")
term.setCursorPos(25,8)
print("|			  |")
term.setCursorPos(25,9)
print("|			  |")
term.setCursorPos(25,10)
print("|			  |")
term.setCursorPos(25,11)
print("+--------------+")
elseif x == 2 then
term.setCursorPos(25,3)
print("+--------------+")
term.setCursorPos(25,4)
print("|			  |")
term.setCursorPos(25,5)
print("|		  O   |")
term.setCursorPos(25,6)
print("|			  |")
term.setCursorPos(25,7)
print("|			  |")
term.setCursorPos(25,8)
print("|			  |")
term.setCursorPos(25,9)
print("|  O		   |")
term.setCursorPos(25,10)
print("|			  |")
term.setCursorPos(25,11)
print("+--------------+")
elseif x == 3 then
term.setCursorPos(25,3)
print("+--------------+")
term.setCursorPos(25,4)
print("|			  |")
term.setCursorPos(25,5)
print("|		  O   |")
term.setCursorPos(25,6)
print("|			  |")
term.setCursorPos(25,7)
print("|	  O	   |")
term.setCursorPos(25,8)
print("|			  |")
term.setCursorPos(25,9)
print("|  O		   |")
term.setCursorPos(25,10)
print("|			  |")
term.setCursorPos(25,11)
print("+--------------+")
elseif x == 4 then
term.setCursorPos(25,3)
print("+--------------+")
term.setCursorPos(25,4)
print("|			  |")
term.setCursorPos(25,5)
print("|  O	   O   |")
term.setCursorPos(25,6)
print("|			  |")
term.setCursorPos(25,7)
print("|			  |")
term.setCursorPos(25,8)
print("|			  |")
term.setCursorPos(25,9)
print("|  O	   O   |")
term.setCursorPos(25,10)
print("|			  |")
term.setCursorPos(25,11)
print("+--------------+")
elseif x == 5 then
term.setCursorPos(25,3)
print("+--------------+")
term.setCursorPos(25,4)
print("|			  |")
term.setCursorPos(25,5)
print("|  O	   O   |")
term.setCursorPos(25,6)
print("|			  |")
term.setCursorPos(25,7)
print("|	  O	   |")
term.setCursorPos(25,8)
print("|			  |")
term.setCursorPos(25,9)
print("|  O	   O   |")
term.setCursorPos(25,10)
print("|			  |")
term.setCursorPos(25,11)
print("+--------------+")
elseif x == 6 then
term.setCursorPos(25,3)
print("+--------------+")
term.setCursorPos(25,4)
print("|			  |")
term.setCursorPos(25,5)
print("|  O	   O   |")
term.setCursorPos(25,6)
print("|			  |")
term.setCursorPos(25,7)
print("|  O	   O   |")
term.setCursorPos(25,8)
print("|			  |")
term.setCursorPos(25,9)
print("|  O	   O   |")
term.setCursorPos(25,10)
print("|			  |")
term.setCursorPos(25,11)
print("+--------------+")
end
end
function roll()
local r = 1
for r = 1,50 do
parallel.waitForAll(die1,die2,sleep(.1))
end
end
function title()
term.setCursorPos(1,14)
print("	  __			__		  ___  _		")
print("	 / /  __ ______/ /____ __  / _ (_)______ ")
print("	/ /__/ // / __/  '_/ // / / // / / __/ -_)")
print("   /____/_,_/__/_/__, / /____/_/__/__/ ")
print("					  /___/				   ")
end
--code here
while true do
term.clear()
title()
blank()
event, p1 = os.pullEvent("key")
if p1 == 28 then
roll()
sleep(3)
end
end

Edit: Pastebin Here!
Pharap #2
Posted 14 August 2012 - 03:04 AM
Nice :P/>/>
It's always fun to have little apps like that.

Next you should shorten the required code by adding functions.

Or, you could just use term.clearline instead of clearing the whole screen, might make things easier.
It's odd, it's a useful function but it never gets mentioned. even by the 'pros'.

If I weren't busy working on the WH program, I would start working on my Graphics API
(unless anyone would prefer the graphics thing, cause now I'm really stuck for which I should be working on lol)
Cranium #3
Posted 14 August 2012 - 03:06 AM
I get that too. I'm torn between working on fun little projects like this, and my main project for the MCS. (Master Control System)
Kazimir #4
Posted 14 August 2012 - 07:17 AM
Hmm.. Cool)
Yesterday I wrote a similar program, but was unable to optimize the algorithm.
Pharap #5
Posted 14 August 2012 - 08:47 AM
Hmm.. Cool)
Yesterday I wrote a similar program, but was unable to optimize the algorithm.

Functions are the way forward.
Kazimir #6
Posted 14 August 2012 - 09:21 AM
Hmm.. Cool)
Yesterday I wrote a similar program, but was unable to optimize the algorithm.

Functions are the way forward.
I like "GOTO" statement))

I mixed two programs, now Dice on the monitor 5x8
http://pastebin.com/WJfUEygw
BigSHinyToys #7
Posted 14 August 2012 - 10:23 AM
GOTO is bad programing
Any way here is my enter in the Most compact Dice code section and only 25 lines
I release this as Open Source and Public Domain
Spoiler

local tCombos = {{" "," "," "," ","0"," "," "," "," "},
{" "," ","0"," "," "," ","0"," "," "},
{" "," ","0"," ","0"," ","0"," "," "},
{"0"," ","0"," "," "," ","0"," ","0"},
{"0"," ","0"," ","0"," ","0"," ","0"},
{"0"," ","0","0"," ","0","0"," ","0"}}
local nOffX,nOffY = 0,1
local function display(nInput)
    for i = 1,3 do
	    for t = 1,3 do
		    term.setCursorPos(t+nOffX,i+nOffY)
		    print(tCombos[nInput][((i-1)*3)+t])
	    end
    end
    term.setCursorPos(1+nOffX,1+nOffY)
end
term.clear()
term.setCursorPos(1,1)
print("press Enter for Dice: ")
while true do
    local e,e1 = os.pullEvent()
    if e == "key" and e1 == 28 then -- enter key
	    display(math.random(1,6))
    end
end
Kazimir #8
Posted 14 August 2012 - 10:30 AM
GOTO is bad programing
Any way here is my enter in the Most compact Dice code section and only 25 lines
I release this as Open Source and Public Domain
Spoiler

local tCombos = {{" "," "," "," ","0"," "," "," "," "},
{" "," ","0"," "," "," ","0"," "," "},
{" "," ","0"," ","0"," ","0"," "," "},
{"0"," ","0"," "," "," ","0"," ","0"},
{"0"," ","0"," ","0"," ","0"," ","0"},
{"0"," ","0","0"," ","0","0"," ","0"}}
local nOffX,nOffY = 0,1
local function display(nInput)
	for i = 1,3 do
		for t = 1,3 do
			term.setCursorPos(t+nOffX,i+nOffY)
			print(tCombos[nInput][((i-1)*3)+t])
		end
	end
	term.setCursorPos(1+nOffX,1+nOffY)
end
term.clear()
term.setCursorPos(1,1)
print("press Enter for Dice: ")
while true do
	local e,e1 = os.pullEvent()
	if e == "key" and e1 == 28 then -- enter key
		display(math.random(1,6))
	end
end
wow! nice code :P/>/>
Pharap #9
Posted 14 August 2012 - 10:37 AM
Hmm.. Cool)
Yesterday I wrote a similar program, but was unable to optimize the algorithm.

Functions are the way forward.
I like "GOTO" statement))

I mixed two programs, now Dice on the monitor 5x8
http://pastebin.com/WJfUEygw

I didn't think lua had gotos. I wish it did D:

Nice program. I can see room for optimisation though.
Pharap #10
Posted 14 August 2012 - 10:47 AM
GOTO is bad programing
Any way here is my enter in the Most compact Dice code section and only 25 lines
I release this as Open Source and Public Domain

They teach everyone GOTO is bad, but it's not really all that bad when used for a good reason.
GOTOs aren't bad, people who use them stupidly are bad. Like not adding a break or return in a while true do end in lua, exact same effect that everyone uses to complain about gotos:
begining:
sleep(1)
goto begining

And anyway, Linus is fine with Gotos, and if he's fine with them, they can't be all bad.

Also, I was not aware we were having a compact dice contest.
BigSHinyToys #11
Posted 14 August 2012 - 10:48 AM
I didn't think lua had gotos. I wish it did D:
Nice program. I can see room for optimisation though.
lua 5.2 has GOTO lua 5.1 doesn't CC is lua 5.1
so don't keep me in suspense What optimizations ?
Pharap #12
Posted 14 August 2012 - 11:14 AM
I didn't think lua had gotos. I wish it did D:
Nice program. I can see room for optimisation though.
lua 5.2 has GOTO lua 5.1 doesn't CC is lua 5.1
so don't keep me in suspense What optimizations ?
Want gotos, must annoy structured programming elitists XD
*pauses, gathers self*
Not on your code, on his code.
Though I must admit, yours doesn't have an exit function.
BigSHinyToys #13
Posted 14 August 2012 - 11:35 AM
Want gotos, must annoy structured programming elitists XD
*pauses, gathers self*
Not on your code, on his code.
Though I must admit, yours doesn't have an exit function.
I could add a exit in one line But was trying to keep it 25 lines But ok 26 will have to do.
Spoiler

local tCombos = {{" "," "," "," ","0"," "," "," "," "},
{" "," ","0"," "," "," ","0"," "," "},
{" "," ","0"," ","0"," ","0"," "," "},
{"0"," ","0"," "," "," ","0"," ","0"},
{"0"," ","0"," ","0"," ","0"," ","0"},
{"0"," ","0","0"," ","0","0"," ","0"}}
local nOffX,nOffY = 0,1
local function display(nInput)
    for i = 1,3 do
	    for t = 1,3 do
		    term.setCursorPos(t+nOffX,i+nOffY)
		    print(tCombos[nInput][((i-1)*3)+t])
	    end
    end
    term.setCursorPos(1+nOffX,1+nOffY)
end
term.clear()
term.setCursorPos(1,1)
print("Enter for Dice roll. Backspace for Exit")
while true do
    local e,e1 = os.pullEvent()
    if e == "key" and e1 == 14 then break end
    if e == "key" and e1 == 28 then -- enter key
	    display(math.random(1,6))
    end
end
Pharap #14
Posted 14 August 2012 - 01:22 PM
Want gotos, must annoy structured programming elitists XD
*pauses, gathers self*
Not on your code, on his code.
Though I must admit, yours doesn't have an exit function.
I could add a exit in one line But was trying to keep it 25 lines But ok 26 will have to do.
Spoiler

local tCombos = {{" "," "," "," ","0"," "," "," "," "},
{" "," ","0"," "," "," ","0"," "," "},
{" "," ","0"," ","0"," ","0"," "," "},
{"0"," ","0"," "," "," ","0"," ","0"},
{"0"," ","0"," ","0"," ","0"," ","0"},
{"0"," ","0","0"," ","0","0"," ","0"}}
local nOffX,nOffY = 0,1
local function display(nInput)
	for i = 1,3 do
		for t = 1,3 do
			term.setCursorPos(t+nOffX,i+nOffY)
			print(tCombos[nInput][((i-1)*3)+t])
		end
	end
	term.setCursorPos(1+nOffX,1+nOffY)
end
term.clear()
term.setCursorPos(1,1)
print("Enter for Dice roll. Backspace for Exit")
while true do
	local e,e1 = os.pullEvent()
	if e == "key" and e1 == 14 then break end
	if e == "key" and e1 == 28 then -- enter key
		display(math.random(1,6))
	end
end

Well technically speaking if you moved the display(math.random(1,6)) up to replace the enter key comment, then move the end up to the end of that line, it would make it 24 lines, but it might cut off a bit on the CC editor.
I believe it's technically possible for lua programs to be only one line long (like C++ and C#) judging by how it determines the start and end points of its code. But that's just being silly.
Cranium #15
Posted 14 August 2012 - 02:24 PM
Wow, I had no idea that there were so many people wanting to make neat little apps like this…