Now I have the next code and it works properly.
Thank you very much for your help.
Code: Select all
function love.load()
love.graphics.setNewFont(30)
button = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, "---", "Scramble", "Reset"}
end
function reset()
love.graphics.clear()
button = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, "---", "Scramble", "Reset"}
for row = 1, 4 do
for col = 1, 4 do
item = (row-1)*4+col
love.graphics.print(tostring(button[item]), col*40, row*40)
love.graphics.rectangle('line', col*40, row*40, 40, 40)
end
end
end
function scramble()
for n = 1, 100 do
love.graphics.clear()
for m = 1, 16 do
if button[m] == "---" then
empty = m
end
end
tile = love.math.random(1, 16)
love.timer.sleep(0.01)
up = (empty == (tile - 4))
down = (empty == (tile + 4))
left = ((empty == tile - 1) and (tile % 4 ~= 1))
right = ((empty == tile + 1) and (tile % 4 ~= 0))
move = (up or down or left or right)
if (move == true) then
button[empty] = button[tile]
button[tile] = "---"
empty = tile
end
end
for row = 1, 4 do
for col = 1, 4 do
item = (row-1)*4+col
love.graphics.print(tostring(button[item]), col*40, row*40)
love.graphics.rectangle('line', col*40, row*40, 40, 40)
end
end
love.graphics.print(button[17], 40, 200)
love.graphics.rectangle('line', 40, 200, 160, 40)
love.graphics.print(button[18], 40, 240)
love.graphics.rectangle('line', 40, 240, 160, 40)
--reset()
end
function love.draw()
for row = 1, 4 do
for col = 1, 4 do
item = (row-1)*4+col
love.graphics.print(tostring(button[item]), col*40, row*40)
love.graphics.rectangle('line', col*40, row*40, 40, 40)
end
end
love.graphics.print(button[17], 40, 200)
love.graphics.rectangle('line', 40, 200, 160, 40)
love.graphics.print(button[18], 40, 240)
love.graphics.rectangle('line', 40, 240, 160, 40)
end
function love.mousepressed(x, y, button, istouch)
if button == 1 then
if (x>=40 and x<=80 and y>=40 and y<=80) then
btn = 1
reset()
end
if (x>=80 and x<=120 and y>=40 and y<=80) then
btn = 2
end
if (x>=120 and x<=160 and y>=40 and y<=80) then
btn = 3
end
if (x>=160 and x<=2000 and y>=40 and y<=80) then
btn = 4
end
if (x>=40 and x<=80 and y>=80 and y<=120) then
btn = 5
end
if (x>=80 and x<=120 and y>=80 and y<=120) then
btn = 6
end
if (x>=120 and x<=160 and y>=80 and y<=120) then
btn = 7
end
if (x>=160 and x<=200 and y>=80 and y<=120) then
btn = 8
end
if (x>=40 and x<=80 and y>=120 and y<=160) then
btn = 9
end
if (x>=80 and x<=120 and y>=120 and y<=160) then
btn = 10
end
if (x>=120 and x<=160 and y>=120 and y<=160) then
btn = 11
end
if (x>=160 and x<=200 and y>=120 and y<=160) then
btn = 12
end
if (x>=40 and x<=80 and y>=160 and y<=200) then
btn = 13
end
if (x>=80 and x<=120 and y>=160 and y<=200) then
btn = 14
end
if (x>=120 and x<=160 and y>=160 and y<=200) then
btn = 15
end
if (x>=160 and x<=200 and y>=160 and y<=200) then
btn = 16
end
if (x>=40 and x<=200 and y>=200 and y<=240) then
btn = 17
scramble()
end
if (x>=40 and x<=200 and y>=240 and y<=280) then
btn = 18
reset()
end
end
end