Posted 17 August 2012 - 05:26 PM
I am working on a slot machine, and I have it set to compare the results of the spins, with a set table of wins. Now, I am comparing with 6 different variables, three of which have 4 possible values, and the other three have 13 possible values. If I were to work out each possible outcome, my code would be ridiculously long. Is there any (simple) way that I can have a variable compared without actually defining every single outcome I want? Here is my code with some notes:
EDIT: fixed the ASCII messing up the forum formatting.
EDIT 2: Just realized that I may not have explained what type of table I would like to set as a win.
--variables
local symbol = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"}
local wins = {{3,3,3,3,3,3}} --just the one for now, triple 3 of clubs
--I will add several different "win" tables here, but that would
--take forever... Possible simplification?
--functions
function title()
term.setCursorPos(1,15)
print(" __ __ ___ ___ __ _ ___ _____ __ ")
print(" / /' / / | |_)| | ( ('| | / / | | ( ('")
print(" __,/_/--|_| |_|_/ _)_)|_|____/ |_| _)_)")
end
function cardtop(x,y)
term.setCursorPos(x,y)
write([[ .------------. ]])
term.setCursorPos(x,y+1)
write([[| .----------. |]])
term.setCursorPos(x,y+2)
write([[| | | |]])
term.setCursorPos(x,y+3)
write([[| | .------. | |]])
term.setCursorPos(x,y+4)
end
function cardbase(x,y)
term.setCursorPos(x,y+8)
write([[| | '------' | |]])
term.setCursorPos(x,y+9)
write([[| | | |]])
term.setCursorPos(x,y+10)
write([[| '----------' |]])
term.setCursorPos(x,y+11)
write([[ '------------' ]])
end
function numtop(n,x,y)
if symbol[n] == "10" then
term.setCursorPos(x,y+4)
write([[| | |]]..symbol[n]..[[--. | | |]])
else
term.setCursorPos(x,y+4)
write([[| | |]]..symbol[n]..[[.--. | | |]])
end
end
function numbase(n,x,y)
if symbol[n] == "10" then
term.setCursorPos(x,y+7)
write([[| | | '--]]..symbol[n]..[[| | |]]) --' adding apostrophe here so it shows proper on the forum post
else
term.setCursorPos(x,y+7)
write([[| | | '--']]..symbol[n]..[[| | |]])
end
end
function spade(x,y)
term.setCursorPos(x,y+5)
write([[| | | :/: | | |]]) --/ not liking my ASCII formatting...
term.setCursorPos(x,y+6)
write([[| | | (__) | | |]])
end
function heart(x,y)
term.setCursorPos(x,y+5)
write([[| | | (/) | | |]]) --/ not liking my ASCII formatting...
term.setCursorPos(x,y+6)
write([[| | | :/: | | |]]) --/ not liking my ASCII formatting...
end
function club(x,y)
term.setCursorPos(x,y+5)
write([[| | | :(/>/>): | | |]])
term.setCursorPos(x,y+6)
write([[| | | ()() | | |]])
end
function diamond(x,y)
term.setCursorPos(x,y+5)
write([[| | | :/: | | |]]) --/ not liking my ASCII formatting...
term.setCursorPos(x,y+6)
write([[| | | :/: | | |]]) --/ not liking my ASCII formatting...
end
function slot1()
local c = math.random(1,4)
local n = math.random(1,13)
cardtop(2,1)
numtop(n,2,1)
if c == 1 then
spade(2,1)
elseif c == 2 then
heart(2,1)
elseif c == 3 then
club(2,1)
elseif c == 4 then
diamond(2,1)
end
numbase(n,2,1)
cardbase(2,1)
return c,n
end
function slot2()
local c = math.random(1,4)
local n = math.random(1,13)
cardtop(18,1)
numtop(n,18,1)
if c == 1 then
spade(18,1)
elseif c == 2 then
heart(18,1)
elseif c == 3 then
club(18,1)
elseif c == 4 then
diamond(18,1)
end
numbase(n,18,1)
cardbase(18,1)
return c,n
end
function slot3()
local c = math.random(1,4)
local n = math.random(1,13)
cardtop(34,1)
numtop(n,34,1)
if c == 1 then
spade(34,1)
elseif c == 2 then
heart(34,1)
elseif c == 3 then
club(34,1)
elseif c == 4 then
diamond(34,1)
end
numbase(n,34,1)
cardbase(34,1)
return c,n
end
function spin()
local x = 1
for x = 1,10 do
slot1()
slot2()
slot3()
sleep(.25)
local x = x + 1
end
end
function jackpot()
local x = 1
for x = 1,5 do
term.clear()
term.setCursorPos(1,5)
print(" __ ___ ___ __ __ ____ ___ ______")
print(" || // // || // || // | || |")
print(" || ||=|| (( ||<< ||_// (( )) || ")
print("|__|| || || __ || || _// || ")
sleep(.5)
x = x + 1
end
end
function loser()
term.clear()
term.setCursorPos(1,5)
print(" __ ___ __ ____ ____ ")
print(" || // (( || || ")
print(" || (( )) ||== ||_//")
print(" ||__| _// _)) ||___ || ")
sleep(3)
term.clear()
end
function testwin(results)
local value = 0
local testval = 0
for cnt = 1, table.maxn(wins) do
for var = 1, table.maxn(wins[cnt]) do
if results[var] == wins[cnt][var] then
testval = testval+1
end
end
if testval == 6 then value = cnt
else testval = 0 end
end
return value
end
--code
function test()
while true do
term.clear()
title()
slot1()
slot2()
slot3()
local event, p1 = os.pullEvent()
if event == "key" then
if p1 == 28 then
spin()
local c1, n1 = slot1()
local c2, n2 = slot2()
local c3, n3 = slot3()
local results = {c1,n1,c2,n2,c3,n3}
--local results = {3,3,3,3,3,3,3,3} --test
if testwin(results) == 0 then loser()
elseif testwin(results) == 1 then
jackpot()
end
end
end
end
end
--test
test() -- just using test so far...
I have asked a few select people, and their input tells me that this would require some fun and complicated coding… Please prove them wrong!EDIT: fixed the ASCII messing up the forum formatting.
EDIT 2: Just realized that I may not have explained what type of table I would like to set as a win.
{{3,3,3,3,3,3,3},{3,_,3,_,3,_}} --the _ indicates that those areas can be ANY value.
Edited on 17 August 2012 - 03:48 PM