0 Members and 1 Guest are viewing this topic.
local str = Moves .. " moves"
Why do you need a bat file ? Luna just works like that : luna[.exe] source.lua output.tns
@echo offset /p luafile= Type the lua filename (without extension):set /p tnsfile= Type the tns filename (without extension):luna.exe %luafile%.lua %tnsfile%.tns
Concatenates two strings (joins them together into a single one)Edit: So for the following code:Code: [Select]local str = Moves .. " moves"Moves is a variable that contains the number of times you've moved a piece" moves" is a string & remains the sameLet say Moves = 200, so the string is equivalent to:"200 moves"
Hopefully, all the hassles so far to create Lua scripts won't exist anymore with the 3.2 Lua SDK
Ha, I'll stilll use Luna+shellscripts+TiLP. Nothing beats that!
function on.charIn(ch) --called when a character key is pressed if ch == "r" then --when r is pressed board = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 } --this is how you create arrays! -- Randomize local swaps = 0 -- a local variable (wonder why) for i = 1, 16 do -- ah this is the For...To syntax in TI-84 and Casio basic and other stuff local j = math.random(i, 16) -- local variable, random number from i to 16 if i ~= j then -- i not equal to j board[i], board[j] = board[j], board[i] --swap number in position i and j? swaps = swaps + 1 --swaps number of turns end if board[i] == 16 then --reach the end empty = i end end -- If the number of swaps plus the number of empty-space movements is odd, -- then the puzzle is impossible, so do an impossible move to make it possible -- (exchange the space with a tile two squares away) if (swaps + (empty - 1) + math.floor((empty - 1) / 4)) % 2 == 1 then -- not 100% how this works, I know this is a way to fix an impossible puzzle :P local i = (empty + 7) % 16 + 1 board[empty] = board[i] empty = i board[empty] = 16 end moves = 0 platform.window:invalidate() --force redraw of the screen calls on.paint(gc) endendon.charIn("r")function on.paint(gc) local won = true for i, n in ipairs(board) do if n ~= 16 then local x = (platform.window:width() - 128) / 2 + 32*((i - 1) % 4) local y = (platform.window:height() - 128) / 2 + 32*math.floor((i - 1) / 4) gc:drawRect(x, y, 30, 30) gc:drawString(n, x + (30 - gc:getStringWidth(n)) / 2, y + 15, "middle") end won = (won and n == i) end local str = moves .. " moves" gc:drawString(str, (platform.window:width() - gc:getStringWidth(str)) / 2, 0, "top") if won then str = "You win! Press R to reset" gc:drawString(str, (platform.window:width() - gc:getStringWidth(str)) / 2, platform.window:height(), "bottom") endendfunction on.arrowKey(key) local dir if key == "left" and empty % 4 ~= 0 then dir = 1 elseif key == "right" and empty % 4 ~= 1 then dir = -1 elseif key == "up" and empty <= 12 then dir = 4 elseif key == "down" and empty > 4 then dir = -4 else return end board[empty] = board[empty + dir] empty = empty + dir board[empty] = 16 moves = moves + 1 platform.window:invalidate()end