Here is the CalmoSoft Fifteen Puzzle Game.
The program uses mouse.
Code:
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 movetiles(empty)
for row = 1, 4 do
for col = 1, 4 do
tile = (row-1)*4 + col
if (button[tile] == "---") then
love.graphics.clear()
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[tile] = button[empty]
button[empty] = "---"
tile = empty
end
end
end
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
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()
love.graphics.clear()
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
movetiles(btn)
end
if (x>=80 and x<=120 and y>=40 and y<=80) then
btn = 2
movetiles(btn)
end
if (x>=120 and x<=160 and y>=40 and y<=80) then
btn = 3
movetiles(btn)
end
if (x>=160 and x<=2000 and y>=40 and y<=80) then
btn = 4
movetiles(btn)
end
if (x>=40 and x<=80 and y>=80 and y<=120) then
btn = 5
movetiles(btn)
end
if (x>=80 and x<=120 and y>=80 and y<=120) then
btn = 6
movetiles(btn)
end
if (x>=120 and x<=160 and y>=80 and y<=120) then
btn = 7
movetiles(btn)
end
if (x>=160 and x<=200 and y>=80 and y<=120) then
btn = 8
movetiles(btn)
end
if (x>=40 and x<=80 and y>=120 and y<=160) then
btn = 9
movetiles(btn)
end
if (x>=80 and x<=120 and y>=120 and y<=160) then
btn = 10
movetiles(btn)
end
if (x>=120 and x<=160 and y>=120 and y<=160) then
btn = 11
movetiles(btn)
end
if (x>=160 and x<=200 and y>=120 and y<=160) then
btn = 12
movetiles(btn)
end
if (x>=40 and x<=80 and y>=160 and y<=200) then
btn = 13
movetiles(btn)
end
if (x>=80 and x<=120 and y>=160 and y<=200) then
btn = 14
movetiles(btn)
end
if (x>=120 and x<=160 and y>=160 and y<=200) then
btn = 15
movetiles(btn)
end
if (x>=160 and x<=200 and y>=160 and y<=200) then
btn = 16
movetiles(btn)
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
Greetings,
Gal Zsolt
(~ CalmoSoft ~)