Advertisement
CaptainSpaceCat

CardShuffler

Jul 6th, 2015
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.33 KB | None | 0 0
  1. local deck = {
  2.     [1] = {"A", "diamonds"},
  3.     [2] = {"2", "diamonds"},
  4.     [3] = {"3", "diamonds"},
  5.     [4] = {"4", "diamonds"},
  6.     [5] = {"5", "diamonds"},
  7.     [6] = {"6", "diamonds"},
  8.     [7] = {"7", "diamonds"},
  9.     [8] = {"8", "diamonds"},
  10.     [9] = {"9", "diamonds"},
  11.     [10] = {"10", "diamonds"},
  12.     [11] = {"J", "diamonds"},
  13.     [12] = {"Q", "diamonds"},
  14.     [13] = {"K", "diamonds"},
  15.     [14] = {"A", "hearts"},
  16.     [15] = {"2", "hearts"},
  17.     [16] = {"3", "hearts"},
  18.     [17] = {"4", "hearts"},
  19.     [18] = {"5", "hearts"},
  20.     [19] = {"6", "hearts"},
  21.     [20] = {"7", "hearts"},
  22.     [21] = {"8", "hearts"},
  23.     [22] = {"9", "hearts"},
  24.     [23] = {"10", "hearts"},
  25.     [24] = {"J", "hearts"},
  26.     [25] = {"Q", "hearts"},
  27.     [26] = {"K", "hearts"},
  28.     [27] = {"A", "clubs"},
  29.     [28] = {"2", "clubs"},
  30.     [29] = {"3", "clubs"},
  31.     [30] = {"4", "clubs"},
  32.     [31] = {"5", "clubs"},
  33.     [32] = {"6", "clubs"},
  34.     [33] = {"7", "clubs"},
  35.     [34] = {"8", "clubs"},
  36.     [35] = {"9", "clubs"},
  37.     [36] = {"10", "clubs"},
  38.     [37] = {"J", "clubs"},
  39.     [38] = {"Q", "clubs"},
  40.     [39] = {"K", "clubs"},
  41.     [40] = {"A", "spades"},
  42.     [41] = {"2", "spades"},
  43.     [42] = {"3", "spades"},
  44.     [43] = {"4", "spades"},
  45.     [44] = {"5", "spades"},
  46.     [45] = {"6", "spades"},
  47.     [46] = {"7", "spades"},
  48.     [47] = {"8", "spades"},
  49.     [48] = {"9", "spades"},
  50.     [49] = {"10", "spades"},
  51.     [50] = {"J", "spades"},
  52.     [51] = {"Q", "spades"},
  53.     [52] = {"K", "spades"}
  54. }
  55.  
  56. local cardBack = {{2^14, 1, 2^14}, {1, 2^14, 1}, {2^14, 1, 2^14}}
  57.  
  58. local function draw(string, xPos, yPos, txtcol, bakcol)
  59.     local string = string or ""
  60.     local txtcol = txtcol or colors.white
  61.     local bakcol = bakcol or colors.black
  62.     term.setCursorPos(xPos, yPos)
  63.     term.setBackgroundColor(bakcol)
  64.     term.setTextColor(txtcol)
  65.     term.write(string)
  66. end
  67.  
  68. local card = {
  69.     ["draw"] = function(cardInfo, xPos, yPos)
  70.         paintutils.drawFilledBox(xPos, yPos, xPos + 2, yPos + 2, colors.white)
  71.         draw(cardInfo[1], xPos, yPos, colors.black, colors.white)
  72.         draw(cardInfo[1], xPos + (3 - #cardInfo[1]), yPos + 2, colors.black, colors.white)
  73.         if cardInfo[2] == "diamonds" then
  74.             draw("D", xPos + 2, yPos, colors.white, colors.red)
  75.             draw("D", xPos, yPos + 2, colors.white, colors.red)
  76.         elseif cardInfo[2] == "hearts" then
  77.             draw("H", xPos + 2, yPos, colors.white, colors.red)
  78.             draw("H", xPos, yPos + 2, colors.white, colors.red)
  79.         elseif cardInfo[2] == "clubs" then
  80.             draw("C", xPos + 2, yPos, colors.white, colors.black)
  81.             draw("C", xPos, yPos + 2, colors.white, colors.black)
  82.         elseif cardInfo[2] == "spades" then
  83.             draw("S", xPos + 2, yPos, colors.white, colors.black)
  84.             draw("S", xPos, yPos + 2, colors.white, colors.black)
  85.         end
  86.     end,
  87.     ["back"] = function(xPos, yPos)
  88.         paintutils.drawImage(cardBack, xPos, yPos)
  89.     end
  90. }
  91.  
  92. local function shuffleDeck()
  93.     local tempdeck = deck
  94.     local finaldeck = {}
  95.     while #tempdeck > 0 do
  96.         finaldeck[#finaldeck + 1] = table.remove(tempdeck, math.random(1, #tempdeck))
  97.     end
  98.     return finaldeck
  99. end
  100.  
  101. local shuffled = shuffleDeck()
  102. term.setBackgroundColor(colors.green)
  103. term.clear()
  104. card.back(11, 7)
  105. local place = 1
  106. repeat
  107.     os.pullEvent("mouse_click")
  108.     card.draw(shuffled[place], 16, 7)
  109.     place = place + 1
  110.     if place == 3 then card.back(21, 7) end
  111.     if place == 52 then paintutils.drawFilledBox(11, 7, 13, 9, colors.green) end
  112. until place > 52
  113. os.pullEvent("mouse_click")
  114. paintutils.drawFilledBox(16, 7, 18, 9, colors.green)
  115. term.setCursorPos(1, 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement