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

Slots help

Started by Zaccaboom97, 22 October 2013 - 06:07 AM
Zaccaboom97 #1
Posted 22 October 2013 - 08:07 AM
Title: Slots help

Hey, I have "borrowed" this code here:
http://www.computercraft.info/forums2/index.php?/topic/2177-slot-machine/

Here is what I am requesting.

I have a system in place where a possible user of my casino will come in and buy money for a floppy disk with in game items, so if they give a iron nugget (tekkit) then they get +1 on their card.
Then when they wish to play a game such as slots, they put their card in and press the button, causing the game to run.

Now, what I would like is help on how to make the computer firstly detect that a card is in the disk drive, then make it check if it is a positive number, and upon turning the redstone on, deducting the number on the card by one. If there is not a positive number in the card the game does not run.

Anybody willing to help?

Zacc
Zacklogan #2
Posted 22 October 2013 - 10:46 AM
Ok, so to tell if the disk is inserted. Heres a link to the wiki page you will need. http://www.computercraft.info/wiki/Os.pullEvent
But you'll need to pull the event disk.
Since I'm at school I can't post a code. If your not helped when I get home I'll edit the post.

Kind regards,
zacklogan
TheOddByte #3
Posted 22 October 2013 - 02:37 PM
Are you asking if someone is willing to create the code for you? If that's the case I can say that lots of people don't want to create a code for you..
I don't mean to sound harsh but this is a section where people help you out if you stumble upon a problem or something you want help with, This is not a section you can ask for someone to make the code for you, I'd suggest you would have put this in general if you are asking that( You can ask a moderator to move it )

So this is some things you need help with?
Events
Redstone
Fs API

EventsEvents

This is an example how you can use the events system

local event, param1 = os.pullEvent() -- This is waiting for an event to be pulled
	if event == "disk" then
		print("A disk has been inserted!")
	else
		print(event .. ":" .. param1)
	end

You can also add something like "key" to only pull that event

local event, param1 = os.pullEvent("key")
	print("A key has been pressed")

RedstoneRedstone

Turning redstone on and off

rs.setOutput("left", true)

So this is quite simple, You choose the side you want to change the redstone output off(left, right, bottom, top, front, back) and you set it to true to turn it on and false to turn it off.

Fs APIFs API

I'm showing how you can use th fs API since that would be needed if you save the 'cash' in a file and then want to check it
So first of all I'm guessing you want it saved onto the disk drive right? And it needs to be a number ofcourse.
The first time you create your disk drive file

local cash = 0
local file = fs.open("disk/cashfile_data","w")
file.writeLine(cash)
file.close()
So now the file is created in the disk drive named as 'cashfile_data'
You can check that it's there by doing 'cd disk' and then 'ls' if you want to check what's in the file call 'edit cashfile_data'

Anyway.. Now you kinda want to check how much cash is on it

local file = fs.open("disk/cashfile_data","r")
local cash = file.readLine() -- Reads the first line in the file
file.close()
if not tonumber(cash) then
	error("It was not a number in the file! D:",0) -- Erroring if the first line in the file was not a number
end

So how I would do that when doing +1 with the 'cash' is something like this
Example

while true do
local evt, side = os.pullEvent("disk")
	print("Disk inserted")
	if fs.exists("disk/cashfile_data") then
		print("cashfile_data found")
		local file = fs.open("disk/cashfile_data","r")
		local cash = file.readLine()
		file.close()
			if not tonumber(cash) then
				fs.delete("disk/cashfile_data")
				error("Cashfile corrupt, Deleting..", 0)
			end
		cash = cash + 1
		local file = fs.open("disk/cashfile_data","w")
		file.writeLine(cash)
		file.close()
	else
		local file = fs.open("disk/cashfile_data","w")
		file.writeLine("0")
		file.close()
	end
	disk.eject(side) -- Ejecting the disk when finished
end

And if you want to check if the user has enough 'cash' you can do something like this
Example

local game = "TestGame" --# The game that you want to run if the user has enough cash

while true do
local evt, side = os.pullEvent("disk")
	if fs.exists("disk/cashfile_data") then
		local file = fs.open("disk/cashfile_data","r")
		local cash = file.readLine()
			if not tonumber(cash) then
				disk.eject(side)
				error("cashfile_data was corrupt! D:",0)
			end

			if cash > 1 then
				print(cash .. "$ - 1$")
				cash = cash - 1
				local file = fs.open("disk/cashfile_data","w")
				file.writeLine(cash)
				file.close()
				sleep(.8)
				shell.run(game)
			else
				disk.eject(side)
				error("Insufficient funds! D:",0)

			end
	else
		disk.eject(side)
	end
end

Zaccaboom97 #4
Posted 22 October 2013 - 06:59 PM
*snip*
I keep recieving "Cash:13: attempt to compare string with number expected, got string"

Halp?
TheOddByte #5
Posted 23 October 2013 - 01:43 AM
Try changing the tonumber part to this

if not tonumber(cash) then
    error("Your error message here", 0)
else
    cash = tonumber(cash)
end
theoriginalbit #6
Posted 23 October 2013 - 01:46 AM
Try changing the tonumber part to this

if not tonumber(cash) then
	error("Your error message here", 0)
else
	cash = tonumber(cash)
end
Or as a "prettier" solution that also doesn't require calling tonumber twice

cash = tonumber(cash)
if not cash then
  error("Your error message here", 0)
end
Zaccaboom97 #7
Posted 23 October 2013 - 10:59 AM
Tanks all! This worked beautifully and I'm now working on the integration of this into my system.