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

Random Message Program

Started by NeoGenMike, 28 March 2014 - 07:57 AM
NeoGenMike #1
Posted 28 March 2014 - 08:57 AM
Hey there guys! I've been fiddling with Computercraft for a while and every once in a while I like to go out and think of a program that will teach me something new about lua and computercraft. Im comming along slowly but I've hit a bit of a snag. I wanted to create a gameboard (Monopoly) and I wanted to make computercraft display the Dice results. I was able to make it generate the numbers, but what I was hoping for was maybe a pressure plate or a lever next to a computer that, when activated, would display a random message of 16 (The chance cards) On a large monitor overhead. Would anyone happen to know code to do this? I think once I get that I should be able to make the dice program no problem. Thanks for your help!
Bomb Bloke #2
Posted 28 March 2014 - 03:08 PM
Something like:

-- Define some card messages in a table:
local chanceCards = {"You got second place in a beauty contest! With two people in it!",
	"Get out of jail free!",
	"Pay some percent tax per property owned!",
	"Go to jail! Go directly to jail! Do not pass go, do not collect $200!"}
	
-- Wrap a monitor:
local mon = peripheral.wrap("right")  -- Or whatever "side" is relevant.

-- Main program loop:
while true do  -- Start a loop that repeats indefinitely.
	if rs.getInput("left") then  -- If receiving a redstone signal from a certain side, then...
		mon.clear()
		mon.setCursorPos(1,1)
		mon.write(chanceCards[math.random(#chanceCards)])  -- "#chanceCards" returns the number of entries in the table.
	end
	
	os.pullEvent("redstone")  -- Sit and wait until a redstone signal change is detected.
end

Though you may want to expand that to remove entries from the card table as they're dealt, then replace them once the table is empty. This should get you started though.

rs.getInput()
os.pullEvent()
NeoGenMike #3
Posted 28 March 2014 - 06:43 PM
That's amazing! Thank you so much! Works like a charm.