Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --# Main
- -- Box-Off
- function setup()
- supportedOrientations(ANY)
- displayMode(FULLSCREEN)
- codeSetup()
- function orientationChanged(orient)
- if (WIDTH - 20) / t.sizex < (HEIGHT - 20) / t.sizey then
- t.size = math.floor((WIDTH - 20) / t.sizex)
- else
- t.size = math.floor((HEIGHT - 20) / t.sizey)
- end
- end
- end
- function draw()
- drawBoard()
- end
- --# Setup
- function codeSetup()
- --#####GLOBALS#####
- VER = "7.5.5"
- DESC = [[
- Rules:
- Drag to select tiles
- To remove a pair of tiles, they must be of the same color,
- and there must be no other tiles in between them.
- The goal of the game is to remove all the tiles.
- ]]
- --#####MISC#####
- function math.round(num)
- if num - math.floor(num) < 0.5 then
- return math.floor(num)
- else
- return math.ceil(num)
- end
- end
- --#####TILES#####
- --Tile Info Table
- t = {}
- --Size
- t.sizex = 6
- t.sizey = 4
- --Colors
- t.colors = {
- color(255, 0, 0),
- color(255, 255, 255),
- color(0, 0, 0)
- }
- t.types = #t.colors
- --Tile Table
- t.tiles = {}
- valid = false --Solvable
- -- Board Setup
- function loadBoard()
- local valid = true --Is it valid?
- local tiles = {} -- Tile colors table: used to count number of each type of tile
- for y = 1, t.sizey do
- t.tiles[y] = {} -- Horizontal arrays
- for x = 1, t.sizex do
- local n = math.random(t.types) -- Random color of tile
- t.tiles[y][x] = n -- Set tile
- tiles[n] = tiles[n] and tiles[n] + 1 or 1 -- Add 1 to number of tiles of type n
- end
- end
- -- Get tile box size
- if (WIDTH - 20) / t.sizex < (HEIGHT - 20) / t.sizey then
- t.size = math.floor((WIDTH - 20) / t.sizex)
- else
- t.size = math.floor((HEIGHT - 20) / t.sizey)
- end
- -- Validity check
- for k = 1, t.types do
- if math.floor(tiles[k] / 2) * 2 ~= tiles[k] then -- If number of tiles of type k == odd
- valid = false -- Invalid board!
- end
- end
- if valid == false then return loadBoard() end -- If not a valid board then recurse!
- return true
- end
- valid = loadBoard()
- strokeWidth(0.5) -- For Retina display, set to 1 if regular display
- displayMode(FULLSCREEN) -- Set display mode
- fontSize(35) -- Font size
- font("HelveticaNeue-UltraLight") -- Should be fairly thin, small: set to HelveticaNeue-Light if not retina
- --#####UI#####
- selecting = false
- active = true
- screenx, screeny = 100, 100
- endx, endy = WIDTH - 100, HEIGHT - 100
- t.currentx, t.firstx, t.currenty, t.firsty = 0, 0, 0, 0
- end
- --# Graphics
- pixMode("STANDARD")
- --resetButtonTextWidth, resetButtonTextHeight = textSize("Reset")
- function drawBoard()
- background(255, 255, 255, 255)
- stroke(0, 0, 0, 255)
- strokeWidth(0.5)
- local currentx, firstx, currenty, firsty = t.currentx, t.firstx, t.currenty, t.firsty
- if currentx < firstx then
- currentx, firstx = firstx, currentx
- end
- if currenty < firsty then
- currenty, firsty = firsty, currenty
- end
- for x = 0, t.sizex - 1 do
- for y = 0, t.sizey - 1 do
- fill(255)
- if selecting then
- if x == t.currentx and y == t.currenty then
- fill(127, 127, 127, 255)
- elseif x >= firstx and x <= currentx and y >= firsty and y <= currenty then
- fill(139, 224, 132, 255)
- end
- end
- rect(
- x * t.size + 10, y * t.size + 10,
- t.size - 1, t.size - 1
- )
- if not(t.tiles[y + 1][x + 1] == 0) then
- fill(
- t.colors[t.tiles[y + 1][x + 1]]
- )
- ellipse(
- x * t.size + t.size / 2 + 10, y * t.size + t.size / 2 + 10,
- t.size - 10, t.size - 10
- )
- end
- end
- end
- noStroke()
- fill(127, 127, 127, 73)
- rect(WIDTH - 100, HEIGHT - 50, 100, 50)
- fill(255, 0, 0, 73)
- rect(WIDTH - 150, HEIGHT - 50, 50, 50)
- fill(0, 255, 0, 73)
- rect(WIDTH - 200, HEIGHT - 50, 50, 50)
- fill(127, 127, 127, 73)
- rect(WIDTH - 250, HEIGHT - 50, 50, 50)
- fill(0, 0, 0, 255)
- --fontSize(35)
- text("Reset", WIDTH - 50, HEIGHT - 25)
- text("-1", WIDTH - 125, HEIGHT - 25)
- text("+1", WIDTH - 175, HEIGHT - 25)
- text("P", WIDTH - 225, HEIGHT - 25)
- if not active then
- pauseScreen()
- end
- end
- --# Mouse
- rsx, rsy = 118, 41
- touched = function(touch)
- if active then
- if touch.state == BEGAN and --[
- math.floor(touch.x / t.size) <= t.sizex - 1 and -- IF
- math.floor(touch.y / t.size) <= t.sizey - 1
- and not(touch.x > WIDTH - 200 and touch.y > HEIGHT - 50) then --]
- t.firstx = math.floor(touch.x / t.size)
- t.firsty = math.floor(touch.y / t.size)
- t.currentx = math.floor(touch.x / t.size)
- t.currenty = math.floor(touch.y / t.size)
- selecting = true
- elseif touch.state == ENDED and selecting and not(touch.x > WIDTH - 200 and touch.y > HEIGHT - 50) then
- t.secondx = math.floor(touch.x / t.size)
- t.secondy = math.floor(touch.y / t.size)
- validityTest(t.firstx + 1, t.firsty + 1, t.currentx + 1, t.currenty + 1)
- selecting = false
- --print(touch.x, touch.y)
- elseif touch.x > WIDTH - 100 and touch.y > HEIGHT - 50 and touch.state == ENDED and not(selecting) then
- valid = loadBoard()
- elseif testBounds(touch.x, touch.y, WIDTH - 200, HEIGHT - 50, WIDTH - 150, HEIGHT) and not(selecting) and touch.state == ENDED then
- t.sizex = t.sizex + 2
- t.sizey = t.sizey + 2
- valid = loadBoard()
- 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
- t.sizex = t.sizex - 2
- t.sizey = t.sizey - 2
- valid = loadBoard()
- elseif testBounds(touch.x, touch.y, WIDTH - 250, HEIGHT - 50, WIDTH - 200, HEIGHT) and not(selecting) and touch.state == ENDED then
- active = false
- else
- t.currentx = math.floor(touch.x / t.size)
- t.currenty = math.floor(touch.y / t.size)
- end
- else
- 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
- active = true
- end
- end
- end
- --# Tests
- local debug = false
- local _print = print
- local print = function(...)
- if debug == true then
- _print(...)
- end
- end
- function testBounds(c1, c2, x1, y1, x2, y2)
- return c1 >= x1 and c1 <= x2 and c2 >= y1 and c2 <= y2
- end
- function validityTest(x1, y1, x2, y2)
- local a, b, c, d = x1, y1, x2, y2
- if x1 == x2 and y1 == y2 then
- print("Identical coords")
- return false
- end
- if x1 >= x2 then
- x1, x2 = x2, x1
- end
- if y1 >= y2 then
- y1, y2 = y2, y1
- end
- local col1 = t.tiles[b][a]
- local col2 = t.tiles[d][c]
- if col1 ~= col2 then
- print("Color mismatch!")
- return false
- end
- for x = x1, x2 do
- for y = y1, y2 do
- if t.tiles[y][x] ~= 0 and not((x == a and y == b) or (x == c and y == d)) then
- print("Tile found at " .. x .. ", " .. y .. " (color: " .. t.tiles[y][x] .. ")")
- return false
- end
- end
- end
- t.tiles[b][a] = 0
- t.tiles[d][c] = 0
- print(a, b, c, d)
- end
- --# PauseScreen
- function pauseScreen()
- fill(0, 0, 0, 194)
- rect(screenx, screeny, endx - 100, endy - 100)
- fill(255, 255, 255, 255)
- textAlign(CENTER)
- text("BOX-OFF Version " .. VER .. "\n\nPAUSED", WIDTH / 2, endy - 70)
- textAlign(LEFT)
- text(DESC, WIDTH / 2, HEIGHT / 2)
- text("Resume", WIDTH / 2, screeny + 35)
- end
- --# TODO
- --[[
- 7.5.2:
- Move orientationChanged to Graphics or Setup:UI
- Add non-Retina support
- Fully commented code
- ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement