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

Ignore some results in a subtable.

Started by Cranium, 17 August 2012 - 03:26 PM
Cranium #1
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:

--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
Cranium #2
Posted 18 August 2012 - 12:40 AM
I was wondering, if I could use the same patterns as string.match() to make this work? Things like this:
Spoiler
  • x: (where x is not one of the magic characters ^$()%.[]*+-?) represents the character x itself.
  • .: (a dot) represents all characters.
  • %a: represents all letters.
  • %c: represents all control characters.
  • %d: represents all digits.
  • %l: represents all lowercase letters.
  • %p: represents all punctuation characters.
  • %s: represents all space characters.
  • %u: represents all uppercase letters.
  • %w: represents all alphanumeric characters.
  • %x: represents all hexadecimal digits.
  • %z: represents the character with representation 0.
  • %x: (where x is any non-alphanumeric character) represents the character x. This is the standard way to escape the magic characters. Any punctuation character (even the non magic) can be preceded by a '%' when used to represent itself in a pattern.
  • [set]: represents the class which is the union of all characters in set. A range of characters can be specified by separating the end characters of the range with a '-'. All classes %x described above can also be used as components in set. All other characters in set represent themselves. For example, [%w_] (or [_%w]) represents all alphanumeric characters plus the underscore, [0-7] represents the octal digits, and [0-7%l%-] represents the octal digits plus the lowercase letters plus the '-' character.
Would those be useable in what I want?
Ponder #3
Posted 18 August 2012 - 01:10 AM
That is exactly what you should go for. Have the hands defined by strings and not tables (or just concatenate the tables the moment you check them) and also have some pattern defined as your victory conditions.
Cranium #4
Posted 18 August 2012 - 01:39 AM
So for example, I would want a table to look like this:

{3,%d,3,%d,3,%d}
The 3's represent all clubs, and the others are able to be any card number, right?