How to resolve the algorithm 15 puzzle game step by step in the LiveCode programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm 15 puzzle game step by step in the LiveCode programming language

Table of Contents

Problem Statement

Implement the Fifteen Puzzle Game.

The   15-puzzle   is also known as:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm 15 puzzle game step by step in the LiveCode programming language

Source code in the livecode programming language

#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

  

You may also check:How to resolve the algorithm Comments step by step in the gecho programming language
You may also check:How to resolve the algorithm Colorful numbers step by step in the Ruby programming language
You may also check:How to resolve the algorithm Enforced immutability step by step in the Pascal programming language
You may also check:How to resolve the algorithm Five weekends step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Range consolidation step by step in the Python programming language