Advertisement
FeynmanTech

Box-Off

Mar 31st, 2014
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.45 KB | None | 0 0
  1.  
  2. --# Main
  3. -- Box-Off
  4.  
  5. function setup()
  6.     supportedOrientations(ANY)
  7.     displayMode(FULLSCREEN)
  8.     codeSetup()
  9.     function orientationChanged(orient)
  10.         if (WIDTH - 20) / t.sizex < (HEIGHT - 20) / t.sizey then
  11.             t.size = math.floor((WIDTH - 20) / t.sizex)
  12.             else
  13.             t.size = math.floor((HEIGHT - 20) / t.sizey)
  14.         end
  15.     end
  16. end
  17.  
  18. function draw()
  19.     drawBoard()
  20. end
  21.  
  22.  
  23. --# Setup
  24. function codeSetup()
  25. --#####GLOBALS#####
  26. VER = "7.5.5"
  27. DESC = [[
  28. Rules:
  29. Drag to select tiles
  30. To remove a pair of tiles, they must be of the same color,
  31. and there must be no other tiles in between them.
  32. The goal of the game is to remove all the tiles.
  33. ]]
  34.  
  35. --#####MISC#####
  36. function math.round(num)
  37.     if num - math.floor(num) < 0.5 then
  38.         return math.floor(num)
  39.     else
  40.         return math.ceil(num)
  41.     end
  42. end
  43.  
  44. --#####TILES#####
  45. --Tile Info Table
  46. t = {}
  47.  
  48. --Size
  49. t.sizex = 6
  50. t.sizey = 4
  51.  
  52. --Colors
  53. t.colors = {
  54.     color(255, 0, 0),
  55.     color(255, 255, 255),
  56.     color(0, 0, 0)
  57. }
  58.  
  59. t.types = #t.colors
  60.  
  61. --Tile Table
  62. t.tiles = {}
  63.    
  64. valid = false --Solvable
  65.  
  66. -- Board Setup
  67. function loadBoard()
  68.     local valid = true --Is it valid?
  69.     local tiles = {} -- Tile colors table: used to count number of each type of tile
  70.     for y = 1, t.sizey do
  71.         t.tiles[y] = {} -- Horizontal arrays
  72.         for x = 1, t.sizex do
  73.             local n = math.random(t.types) -- Random color of tile
  74.             t.tiles[y][x] = n -- Set tile
  75.             tiles[n] = tiles[n] and tiles[n] + 1 or 1 -- Add 1 to number of tiles of type n
  76.         end
  77.     end
  78.        
  79.     -- Get tile box size
  80.     if (WIDTH - 20) / t.sizex < (HEIGHT - 20) / t.sizey then
  81.         t.size = math.floor((WIDTH - 20) / t.sizex)
  82.     else
  83.         t.size = math.floor((HEIGHT - 20) / t.sizey)
  84.     end
  85.  
  86.     -- Validity check
  87.     for k = 1, t.types do
  88.         if math.floor(tiles[k] / 2) * 2 ~= tiles[k] then -- If number of tiles of type k == odd
  89.             valid = false -- Invalid board!
  90.         end
  91.     end
  92.     if valid == false then return loadBoard() end -- If not a valid board then recurse!
  93.     return true
  94. end
  95. valid = loadBoard()
  96.  
  97. strokeWidth(0.5) -- For Retina display, set to 1 if regular display
  98. displayMode(FULLSCREEN) -- Set display mode
  99.  
  100. fontSize(35) -- Font size
  101.  
  102. font("HelveticaNeue-UltraLight") -- Should be fairly thin, small: set to HelveticaNeue-Light if not retina
  103.  
  104. --#####UI#####
  105. selecting = false
  106. active = true
  107. screenx, screeny = 100, 100
  108. endx, endy = WIDTH - 100, HEIGHT - 100
  109. t.currentx, t.firstx, t.currenty, t.firsty = 0, 0, 0, 0
  110. end
  111. --# Graphics
  112. pixMode("STANDARD")
  113.  
  114. --resetButtonTextWidth, resetButtonTextHeight = textSize("Reset")
  115.    
  116. function drawBoard()
  117.    
  118.     background(255, 255, 255, 255)
  119.     stroke(0, 0, 0, 255)
  120.    
  121.     strokeWidth(0.5)
  122.    
  123.     local currentx, firstx, currenty, firsty = t.currentx, t.firstx, t.currenty, t.firsty
  124.     if currentx < firstx then
  125.         currentx, firstx = firstx, currentx
  126.     end
  127.     if currenty < firsty then
  128.         currenty, firsty = firsty, currenty
  129.     end
  130.    
  131.     for x = 0, t.sizex - 1 do
  132.         for y = 0, t.sizey - 1 do
  133.             fill(255)
  134.            
  135.             if selecting then
  136.                
  137.                 if x == t.currentx and y == t.currenty then
  138.                     fill(127, 127, 127, 255)
  139.                 elseif x >= firstx and x <= currentx and y >= firsty and y <= currenty then
  140.                     fill(139, 224, 132, 255)
  141.                 end
  142.             end
  143.            
  144.             rect(
  145.                 x * t.size + 10, y * t.size + 10,
  146.                 t.size - 1, t.size - 1
  147.             )
  148.            
  149.             if not(t.tiles[y + 1][x + 1] == 0) then
  150.            
  151.                 fill(
  152.                     t.colors[t.tiles[y + 1][x + 1]]
  153.                 )
  154.            
  155.                 ellipse(
  156.                     x * t.size + t.size / 2 + 10, y * t.size + t.size / 2 + 10,
  157.                     t.size - 10, t.size - 10
  158.                 )
  159.             end
  160.         end
  161.     end
  162.    
  163.     noStroke()
  164.     fill(127, 127, 127, 73)
  165.    
  166.     rect(WIDTH - 100, HEIGHT - 50, 100, 50)
  167.     fill(255, 0, 0, 73)
  168.     rect(WIDTH - 150, HEIGHT - 50, 50, 50)
  169.     fill(0, 255, 0, 73)
  170.     rect(WIDTH - 200, HEIGHT - 50, 50, 50)
  171.     fill(127, 127, 127, 73)
  172.     rect(WIDTH - 250, HEIGHT - 50, 50, 50)
  173.     fill(0, 0, 0, 255)
  174.     --fontSize(35)
  175.     text("Reset", WIDTH - 50, HEIGHT - 25)
  176.  
  177.     text("-1", WIDTH - 125, HEIGHT - 25)
  178.     text("+1", WIDTH - 175, HEIGHT - 25)
  179.     text("P", WIDTH - 225, HEIGHT - 25)
  180.    
  181.     if not active then
  182.         pauseScreen()
  183.     end
  184.    
  185. end
  186.  
  187. --# Mouse
  188. rsx, rsy = 118, 41
  189.    
  190. touched = function(touch)
  191.     if active then
  192.         if touch.state == BEGAN and --[
  193.         math.floor(touch.x / t.size) <= t.sizex - 1 and -- IF
  194.         math.floor(touch.y / t.size) <= t.sizey - 1
  195.         and not(touch.x > WIDTH - 200 and touch.y > HEIGHT - 50) then --]
  196.            
  197.             t.firstx = math.floor(touch.x / t.size)
  198.             t.firsty = math.floor(touch.y / t.size)
  199.             t.currentx = math.floor(touch.x / t.size)
  200.             t.currenty = math.floor(touch.y / t.size)
  201.             selecting = true
  202.             elseif touch.state == ENDED and selecting and not(touch.x > WIDTH - 200 and touch.y > HEIGHT - 50) then
  203.             t.secondx = math.floor(touch.x / t.size)
  204.             t.secondy = math.floor(touch.y / t.size)
  205.             validityTest(t.firstx + 1, t.firsty + 1, t.currentx + 1, t.currenty + 1)
  206.             selecting = false
  207.             --print(touch.x, touch.y)
  208.            
  209.             elseif touch.x > WIDTH - 100 and touch.y > HEIGHT - 50 and touch.state == ENDED and not(selecting) then
  210.             valid = loadBoard()
  211.            
  212.             elseif testBounds(touch.x, touch.y, WIDTH - 200, HEIGHT - 50, WIDTH - 150, HEIGHT) and not(selecting) and touch.state == ENDED then
  213.            
  214.             t.sizex = t.sizex + 2
  215.             t.sizey = t.sizey + 2
  216.             valid = loadBoard()
  217.            
  218.             elseif testBounds(touch.x, touch.y, WIDTH - 150, HEIGHT - 50, WIDTH - 100, HEIGHT) and not(selecting) and touch.state == ENDED and t.sizex > 2 and t.sizey > 2 then
  219.            
  220.             t.sizex = t.sizex - 2
  221.             t.sizey = t.sizey - 2
  222.             valid = loadBoard()
  223.            
  224.             elseif testBounds(touch.x, touch.y, WIDTH - 250, HEIGHT - 50, WIDTH - 200, HEIGHT) and not(selecting) and touch.state == ENDED then
  225.             active = false
  226.            
  227.             else
  228.             t.currentx = math.floor(touch.x / t.size)
  229.             t.currenty = math.floor(touch.y / t.size)
  230.         end
  231.     else
  232.         if testBounds(touch.x, touch.y, WIDTH / 2 - rsx / 2 - 10, 135 - rsy / 2 - 10, WIDTH / 2 + rsx / 2 + 10, 135 + rsy / 2 + 10) then
  233.             active = true
  234.         end
  235.     end
  236. end
  237.  
  238. --# Tests
  239. local debug = false
  240.  
  241. local _print = print
  242.  
  243. local print = function(...)
  244.     if debug == true then
  245.         _print(...)
  246.     end
  247. end
  248.  
  249. function testBounds(c1, c2, x1, y1, x2, y2)
  250.     return c1 >= x1 and c1 <= x2 and c2 >= y1 and c2 <= y2
  251. end
  252.    
  253. function validityTest(x1, y1, x2, y2)
  254.     local a, b, c, d = x1, y1, x2, y2
  255.     if x1 == x2 and y1 == y2 then
  256.         print("Identical coords")
  257.         return false
  258.     end
  259.        
  260.     if x1 >= x2 then
  261.         x1, x2 = x2, x1
  262.     end
  263.     if y1 >= y2 then
  264.         y1, y2 = y2, y1
  265.     end
  266.    
  267.     local col1 = t.tiles[b][a]
  268.     local col2 = t.tiles[d][c]
  269.    
  270.     if col1 ~= col2 then
  271.         print("Color mismatch!")
  272.         return false
  273.     end
  274.    
  275.     for x = x1, x2 do
  276.         for y = y1, y2 do
  277.             if t.tiles[y][x] ~= 0 and not((x == a and y == b) or (x == c and y == d)) then
  278.                 print("Tile found at " .. x .. ", " .. y .. " (color: " .. t.tiles[y][x] .. ")")
  279.                 return false
  280.             end
  281.         end
  282.     end
  283.    
  284.     t.tiles[b][a] = 0
  285.     t.tiles[d][c] = 0
  286.    
  287.     print(a, b, c, d)
  288.    
  289. end
  290.  
  291. --# PauseScreen
  292. function pauseScreen()
  293.     fill(0, 0, 0, 194)
  294.    
  295.     rect(screenx, screeny, endx - 100, endy - 100)
  296.    
  297.     fill(255, 255, 255, 255)
  298.    
  299.     textAlign(CENTER)
  300.     text("BOX-OFF Version " .. VER .. "\n\nPAUSED", WIDTH / 2, endy - 70)
  301.     textAlign(LEFT)
  302.    
  303.     text(DESC, WIDTH / 2, HEIGHT / 2)
  304.     text("Resume", WIDTH / 2, screeny + 35)
  305. end
  306.  
  307. --# TODO
  308. --[[
  309.  
  310. 7.5.2:
  311.     Move orientationChanged to Graphics or Setup:UI
  312.     Add non-Retina support
  313.     Fully commented code
  314.  
  315. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement