Advertisement
Kodos

Deck Dealer

Aug 15th, 2015
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.75 KB | None | 0 0
  1. suits = {"Clubs", "Diamonds", "Hearts", "Spades"}
  2. faces = {2,3,4,5,6,7,8,9,10,"Jack","Queen","King","Ace"}
  3. --a stack is a set of cards. a stack of length 1 acts as a card; the stack constructor only creates decks.
  4.  
  5. stack = setmetatable({
  6. --shuffles a stack
  7. __unm = function(z)
  8.   local ret = {}
  9.   for i = #z, 1, -1 do
  10.     ret[#ret + 1] = table.remove(z,math.random(i))
  11.   end
  12.   return setmetatable(ret, stack)
  13. end,
  14. --puts two stacks together
  15. __add = function(z, z2)
  16.   for i = 1, #z2 do
  17.     z[#z+1] = table.remove(z2)
  18.   end
  19.   return z
  20. end,
  21. --removes n cards from a stack and returns a stack of those cards
  22. __sub = function(z, n)
  23.   local ret = {}
  24.   for i = 1, n do
  25.     ret[i] = table.remove(z)
  26.   end
  27.   return setmetatable(ret, stack)
  28. end,
  29. --breaks a stack into n equally sized stacks and returns them all
  30. deal = function(z, n)
  31.   local ret = {}
  32.   for i = 1, #z/n do
  33.     ret[i] = table.remove(z)
  34.   end
  35.   if n > 1 then return setmetatable(ret, stack), stack.deal(z,n-1)
  36.   else return setmetatable(ret, stack)
  37.   end
  38. end,
  39. --returns a and b as strings, concatenated together. Simple enough, right?
  40. __concat = function(a, b)
  41.   if getmetatable(a) == stack then
  42.     return stack.stackstring(a) .. b
  43.   else
  44.     return a .. stack.stackstring(b)
  45.   end
  46. end,
  47. stackstring = function(st, ind)
  48.     ind = ind or 1
  49.     if not st[ind] then return "" end
  50.     return st[ind] and (faces[math.ceil(st[ind]/4)] .. " of " .. suits[st[ind]%4+1] .. "\n" .. stack.stackstring(st, ind+1)) or ""
  51. end}, {
  52. --creates a deck
  53. __call = function(z)
  54.   local ret = {}
  55.   for i = 1, 52 do ret[i] = i end
  56.   return -setmetatable(ret,z)
  57. end})
  58.  
  59. print(stack() .. "\n")
  60. a, b, c, d = stack.deal(stack(), 4)
  61. print(a .. "\n\n\n")
  62. print(b + c .. "\n\n\n")
  63. print(d - 4 .. "")
  64. print(-b .. "")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement