 


Task

Implement the Fifteen Puzzle Game.



The   15-puzzle   is also known as:

   Fifteen Puzzle
   Gem Puzzle
   Boss Puzzle
   Game of Fifteen
   Mystic Square
   14-15 Puzzle
   and some others.


Related Tasks

   15 Puzzle Solver
   16 Puzzle Game


 
#Please note that all this code can be performed in livecode with just few mouse clicks
#This is just a pure script exampe
on OpenStack
   show me  #Usually not necessary
   #tile creation
   set the width of the templateButton to 50
   set the height of the templateButton to 50
   repeat with i=1 to 16
      create button 
      set the label of  button i to i
      if i =1 then
         set the top of button 1 to 0
         set the left of button 1 to 0
      end if
      if i > 1 and 1 <=4  then
         set the left of button i to the right of button (i-1)
         set the top of button i to the top of button 1
      end if
      if i >= 5 and i <= 8 then
         set the top of button i to the bottom of button 1
         if i = 5 then 
            set the left of button i to the left of button 1
         else
            set the left of button i to the right of button (i - 1)
         end if
      end if   
      if i >= 9 and i <= 12 then
         set the top of button i to the bottom of button 5
         if i = 9 then 
            set the left of button i to the left of button 1
         else 
            set the left of button i to the right of button (i - 1)
         end if
      end if   
      if i >= 13 and i <= 16 then
         set the top of button i to the bottom of button 9
         if i = 13 then 
            set the left of button i to the left of button 1
         else 
            set the left of button i to the right of button (i - 1)
         end if
      end if   
      #this is usally the script directly wirtten in the objects, it's really weird this way
      put "on MouseUp" &CR& "if checkDistance(the label of me) then" & CR &"put the loc of me into temp" into ts
      put CR& "set the loc of me to the loc of button 16" after ts
      put CR& "set the loc of button 16 to temp" & Cr & "end if " &CR &"End MouseUp" after ts
      set the script of button i to ts
   end repeat
   #graphic adjustements
   set the visible of button 16 to false   
   set the width of this stack to the right of button 16
   set the height of this stack to the bottom of button 16
end openStack
 
function checkDistance i
   if (((the top of button i - the bottom of button 16) = 0 OR (the top of button 16 - the bottom of button i) = 0) AND the left of button i = the left of button 16) OR (((the left of button i - the right of button 16) = 0 OR (the right of button i - the left of button 16) = 0) AND the top of button i = the top of button 16) then
      return true
   else 
      return false
   end if
end checkDistance
 