8-puzzle game

Creating Games? Developing something for fun?

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

Brittgriscom
Posts: 95
Joined: Wed Mar 30, 2011 10:15 am

Re: 8-puzzle game

Post by Brittgriscom » Mon May 16, 2011 12:25 am

I think you would need the whole script to make it work. Here is the first part of it:

Code: Select all

global gStartTime
global gBestHumanTime
global gYourTime
global gMiddleButton
global gSuperSwaps
global gBestBotTime
global gBotTime
global gCurrentState
global gHumanCurrentState
global gSpaceButton
global gHumanSpaceButton
global gWidthPadding
global gHeightPadding
global gHumanWidthPadding
global gHumanHeightPadding
global gGroupOffset
#settings for both ipad and iphone
local sTilesX = 3, sTilesY = 3, sWinningState = "1 2 3 4 5 6 7 8 9"
local sInitTime, sEndTime, sHoldTime =300

local sOffset, sWidth, sHeight, sWidthPadding, sHeightPadding, sHumanWidthPadding, sHumanHeightPadding
GroupOffset


on preOpenCard
   
   if the environment is "mobile" then
   iphoneStopPlayingOnChannel "Background"
   iphoneStopPlayingOnChannel "BackgroundOnce"
end if

   put 9 into gSpaceButton
   put 9 into gHumanSpaceButton
   
   set movespeed to 65535
   
   if (the machine is "iPad") or \
         (the width of this stack is 1024) then
      repeat with i = 1 to the number of controls in this card
         
         put 223 into gWidthPadding
         put 262 into gHeightPadding
         put 583 into gHumanWidthPadding
         put 262 into gHumanHeightPadding
         put 360 into gGroupOffset
         
         put 5 into sOffset
         put 100 into sWidth
         put 100 into sHeight
         
      end repeat
   end if
   
   if (the machine is "iPhone") or \
         (the machine is "iPod Touch") or \
         (the width of this stack is 480) or \
         (the machine is "iPhone Simulator") then
      
      put 94 into gWidthPadding
      put 115 into gHeightPadding
      put 274 into gHumanWidthPadding
      put 115 into gHumanHeightPadding
      put 180 into gGroupOffset
      
      put 5 into sOffset
      put 50 into sWidth
      put 50 into sHeight
      
      
   end if
   
   --   repeat with i = 1 to the number of buttons in this card
   --      if the name of button i contains "tile" then
   
   --         if the name of button i contains "human" then
   --            set the cPlayer of button i to "human"
   --         else
   --            set the cPlayer of button i to "bot"
   --         end if
   
   --      end if
   --   end repeat
   
   resetButtons
end preOpenCard

on mouseDown   
   
   --   set the moveSpeed to 65535
   
   --   if the cToggle of this card is "numbers" then
   --      if the name of the target contains "tile" then
   put the millisecs into sInitTime
   --end if
   --end if
   
   --   if the cToggle of this card is "pictures" then
   --      if the name of the target contains "shard" then
   --         put the millisecs into sInitTime
   --      end if
   --   end if
end mouseDown
   
on mouseUp
   --   local tCurrentState, tButtonToSwap
   local tButtonToSwap
   
   --   set the moveSpeed to 65535
   
   # minimum hold time in millisecs
   put the millisecs into sEndTime
   if the name of the target contains "human" then
      
      if the name of the target contains "human shard" then
         put character 12 of the short name of the target into tButtonToSwap
      end if
      
      if the name of the target contains "human tile" then
         put character 11 of the short name of the target into tButtonToSwap
      end if
      
      --   put location of button ("human tile" & tButtonToSwap) into tButtonLocation
      --   move button ("human tile" & tButtonToSwap) from location of button ("human tile" & tButtonToSwap) to \
            --         location of button ("human tile" & gSpaceButton) without messages
      --   move button ("human tile" & gSpaceButton) from location of button ("human tile" & gSpaceButton) to \
            --         tButtonLocation without messages
      
      --   repeat with i = 1 to 9
      --      set the loc of image ("shard" & i & ".png") to the loc of button ("tile" & i)
      --      set the loc of image ("human shard" & i & ".png") to the loc of button ("human tile" & i)
      --   end repeat
      
      --   if (sEndTime - sInitTime) >= sHoldTime then
      --      set the cHeld of the target to "yes"
      --      #superswap
      --      if gSuperSwaps > 0 then
      --         swapButtons gSpaceButton, tButtonToSwap
      --         subtract 1 from gSuperSwaps
      --         put gSuperSwaps into line 2 of fld "SuperSwaps"
      --      end if
      --   else
      --      set the cHeld of the target to "no"
      # swap buttons regularly
      if (isSwapable(gSpaceButton, tButtonToSwap) is true) then
         swapButtons gSpaceButton, tButtonToSwap
      else 
         if (isDoubleSwapable(gSpaceButton, tButtonToSwap) is true) then
            doubleSwapButtons gMiddleButton, gSpaceButton, tButtonToSwap
         end if
      end if
      --   end if
      
      
      put boardToBoardState (gHumanWidthPadding, gHumanHeightPadding) into tHumanCurrentState
      if  the cFinishable of this card is "yes" then
         if manhattanDistance (tHumanCurrentState) is 0 then
            finishHumanPuzzle
         end if
      end if
      
   end if
   
end mouseUp
   
   --   if the cToggle of this card is "numbers" then
   --      if the name of the target contains "tile" then
   
   --         # minimum hold time in millisecs
   --         put the millisecs into sEndTime
   
   --         --         if the name of the target contains "human" then
   --         put character 11 of the short name of the target into tButtonToSwap
   --         end if
   
   --         if the name of the target contains "human" is false then
   --            put character 5 of the short name of the target into tButtonToSwap
   --         end if
   
   --      put the name of the target into tButtonToSwap
   # check whether the hold was long enough for a superswap
   --      if (sEndTime - sInitTime) >= sHoldTime then
   --         set the cHeld of the target to "yes"
   --         #superswap
   --         if gSuperSwaps > 0 then
   --            swapButtons gSpaceButton, tButtonToSwap
   --            subtract 1 from gSuperSwaps
   --            put gSuperSwaps into line 2 of fld "SuperSwaps"
   --         end if
   --      else
   --         set the cHeld of the target to "no"
   --         # swap buttons regularly
   --         if (isSwapable(gSpaceButton, tButtonToSwap) is true) then
   --            swapButtons gSpaceButton, tButtonToSwap
   --         else 
   --            if (isDoubleSwapable(gSpaceButton, tButtonToSwap) is true) then
   --               doubleSwapButtons gMiddleButton, gSpaceButton, tButtonToSwap
   --            end if
   --         end if
   
   --         put boardToBoardState (gHumanWidthPadding, gHumanHeightPadding) into tCurrentState
   --         if  the cFinishable of this card is "yes" then
   --            if manhattanDistance (tCurrentState) is 0 then
   --               finishHumanPuzzle
   --            end if
   --         end if
   
   --      end if
   --   end if
   --end if
   
   
   --if the cToggle of this card is "pictures" then
   --   if the name of the target contains "shard" then
   
   --      # minimum hold time in millisecs
   --      put the millisecs into sEndTime
   
   --      --         if the name of the target contains "human" then
   --      --            put character 11 of the short name of the target into tButtonToSwap
   --      --         end if
   
   --      --         if the name of the target contains "human" is false then
   --      --            put character 5 of the short name of the target into tButtonToSwap
   --      --         end if
   
   --      put character 12 of the short name of the target into tButtonToSwap
   --      --      put the name of the target into tButtonToSwap
   
   --      # check whether the hold was long enough for a superswap
   --      if (sEndTime - sInitTime) >= sHoldTime then
   --         set the cHeld of the target to "yes"
   --         #superswap
   --         if gSuperSwaps > 0 then
   --            swapButtons gSpaceButton, tButtonToSwap
   --            subtract 1 from gSuperSwaps
   --            put gSuperSwaps into line 2 of fld "SuperSwaps"
   --         end if
   --      else
   --         set the cHeld of the target to "no"
   --         # swap buttons regularly
   --         if (isSwapable(gSpaceButton, tButtonToSwap) is true) then
   --            swapButtons gSpaceButton, tButtonToSwap
   --         else 
   --            if (isDoubleSwapable(gSpaceButton, tButtonToSwap) is true) then
   --               doubleSwapButtons gMiddleButton, gSpaceButton, tButtonToSwap
   --            end if
   --         end if
   
   --      end if
   --   end if
   --end if
   
   --put boardToBoardState (sHumanWidthPadding, sHumanHeightPadding) into gHumanCurrentState
   --if  the cFinishable of this card is "yes" then
   --   if manhattanDistance (gHumanCurrentState) is 0 then
   --      finishHumanPuzzle
   --   end if
   --end if


on stopMessages
      repeat for each line aLine in the pendingMessages
            if aLine contains "arration" or \ 
            aLine contains "putLine" or \
            aLine contains "diamond" or \
            aLine contains "Timer" or \
            aLine contains "mouse"  or \
            aLine contains "showMovie" or \
            aLine contains "Dialogue" then
          cancel item 1 of aLine
      end if
   end repeat
end stopMessages

on resetButtons
   stopMessages
   
   set the backgroundColor of me to 85, 172, 116
   
   repeat with i = 1 to the number of buttons in this card
      set the style of button i to "shadow"
      set the opaque of button i to false
      set the backgroundColor of button i to red
      set the textColor of button i to purple
   end repeat
   
   repeat with i = 1 to the number of fields in this card
      set the textColor of field i to purple
      set the opaque of field i to false
      
      if the name of fld i contains "Time" then
         set the shadow of field i to true
         set the borderwidth of field i to 2
      end if
      
   end repeat
   
   set the cShuffleLabel of this card to "shuffle"
   --   set the cStopped of this card to "no"
   set the cFinishable of this card to "no"
   --   set the cToggle of this card to "numbers"
   
   put 0 into line 2 of field "Human Time"
   put 0 into line 2 of field "Bot Time"
   set the label of button "Shuffle" to "Shuffle"
   
   if gBestHumanTime = 0 then
      put empty into line 2 of field "Best Human Time"
   end if
   
   if gBestBotTime = 0 then
      put empty into line 2 of field "Best Bot Time"
   end if
   
   repeat with i = 1 to 9
      set the loc of image ("shard" & i & ".png") to the loc of button ("tile" & i)
      set the loc of image ("human shard" & i & ".png") to the loc of button ("human tile" & i)
   end repeat
   
   --   put 10 into gSuperSwaps
   --   put "Superswaps left:" & cr & gSuperSwaps into fld "SuperSwaps"
   
   --   repeat with i = 1 to the number of images in this card
   --      if the name of image i contains "shard" then
   --         --         hide image i
   --      end if
   --   end repeat
   
   repeat with i = 1 to 8
      set the backgroundColor of btn ("tile" & i) to red
      set the backgroundColor of btn ("human tile" & i) to red
      show button i
   end repeat
   
   
   hide button "tile9"
   hide btn "human tile9"
   
   --   hide image "shard9.png"
   --   hide image "human shard9.png"
   
   --   set the width of btn "numbers" to 100
   --   set the height of btn "numbers" to 100
   --   hide button "numbers"
   
   --   show image "pictures"
   
   repeat with i = 1 to 9
      put  (item 1 of the loc of btn ("tile" & i)) + gGroupOffset into tHumanTileXCoord
      put item 2 of the loc of btn ("tile" & i)  into tHumanTileYCoord
      set the loc of btn ("human tile" & i) to tHumanTileXCoord, tHumanTileYCoord
      set the loc of image ("human shard" & i & ".png") to tHumanTileXCoord, tHumanTileYCoord
   end repeat
   
   if (the machine is "iPad") or \
         (the width of this stack is 1024) then
      
      set the width of image "pictures" to 80
      set the height of image "pictures" to 80
      
      repeat with i = 1 to the number of controls in this card
         
         --         set the cStartRect of control i to the rect of control i
         --         set the cStartTextSize of control i to the textSize of control i
         --         set the cStartTextHeight of control i to the textHeight of control i
         --         set the cStartbackgroundColor of control i to the backgroundColor of control i
         
         set the rect of control i to the cStartRect of control i
         set the textSize of control i to the cStartTextSize of control i
         set the textHeight of control i to the cStartTextHeight of control i
         set the backgroundColor of control i to the cStartbackgroundColor of control i
         
         --      set the width of btn ("human tile" & i) to 100 
         --      set the height of btn ("human tile" & i) to 100 
         --         show control i
      end repeat
      
      --      set the width of image "pictures" to 80
      --      set the height of image "pictures" to 80
      --      set the width of btn "numbers" to 80
      --      set the height of btn "numbers" to 80
      
      
      --      set the textHeight of btn "Stop" to 40
      
      --      set the rect of btn "tile1" to 200, 200, 300, 300
      --      set the rect of btn "tile2" to 305, 200, 405, 300
      --      set the rect of btn "tile3" to 410, 200, 510, 300
      
      --      set the rect of btn "tile4" to 200, 305, 300, 405
      --      set the rect of btn "tile5" to 305, 305, 405, 405
      --      set the rect of btn "tile6" to 410, 305, 510, 405
      
      --      set the rect of btn "tile7" to 200, 410, 300, 510
      --      set the rect of btn "tile8" to 305, 410, 405, 510
      --      set the rect of btn "tile9" to 410, 410, 510, 510
   end if
   
   if (the machine is "iPhone") or \
         (the machine is "iPod Touch") or \
         (the width of this stack is 480) or \
         (the machine is "iPhone Simulator") then
      
      set the width of image "pictures" to 40
      set the height of image "pictures" to 40
      set the width of btn "numbers" to 40
      set the height of btn "numbers" to 40
      
      repeat with i = 1 to 9
         set the width of btn ("human tile" & i)  to 50
         set the height of btn ("human tile" & i) to 50
         set the width of img ("shard" & i & ".png")  to 50
         set the height of img ("shard" & i & ".png") to 50
         set the width of img ("human shard" & i & ".png")  to 50
         set the height of img ("human shard" & i & ".png") to 50
      end repeat
      
      repeat with i = 1 to the number of controls in this card
         
         --         set the cIphoneStartRect of control i to the rect of control i
         --         set the cIphoneStartTextSize of control i to the textSize of control i
         --         set the cIphoneStartTextHeight of control i to the textHeight of control i
         
         set the rect of control i to the cIphoneStartRect of control i
         set the textSize of control i to the cIphoneStartTextSize of control i
         set the textHeight of control i to the cIphoneStartTextHeight of control i
         
         --         set the width of btn ("human tile" & i) to 50
         --         set the height of btn ("human tile" & i) to 50
      end repeat
      
      repeat with n = 1 to the number of fields in this card
         set the textSize of fld n to 14
      end repeat
      
      repeat with n = 1 to the number of buttons in this card
         set the textSize of button n to 30
      end repeat
   end if
   
   
   
   --      repeat with i = 1 to the number of controls in this card
   --         if the short name of control i contains "Return" is false then
   --            put (item 1 of the cStartRect of control i * (480/1024)) into tIphoneRect1
   --            put (item 2 of the cStartRect of control i * (320/768)) into tIphoneRect2
   --            put (item 3 of the cStartRect of control i * (480/1024)) into tIphoneRect3
   --            put (item 4 of the cStartRect of control i * (320/768)) into tIphoneRect4
   
   --            --            set the cIphoneStartRect of control i to tIphoneRect1, tIphoneRect2, tIphoneRect3, tIphoneRect4
   --            --            set the cIphoneStartTextSize of control i to ((the cStarttextSize of control i) * .4)
   --            --            set the cIphoneStartTextHeight of control i to ((the cStarttextHeight of control i) * .4)
   
   --            set the rect of control i to the cIphoneStartRect of control i
   --            set the textSize of control i to the cIphoneStartTextSize of control i
   --            set the textHeight of control i to the cIphoneStartTextHeight of control i
   --         end if
   --         --         show control i
   --      end repeat
   --   end if
   
   
   --   set the rect of btn "tile1" to 55, 100, 105, 150
   --   set the rect of btn "tile2" to 110, 100, 160, 150
   --   set the rect of btn "tile3" to 165, 100, 215, 150
   
   --   set the rect of btn "tile4" to 55, 155, 105, 205
   --   set the rect of btn "tile5" to 110, 155, 160, 205
   --   set the rect of btn "tile6" to 165, 155, 215, 205
   
   --   set the rect of btn "tile7" to 55, 210, 105, 260
   --   set the rect of btn "tile8" to 110, 210, 160, 260
   --   set the rect of btn "tile9" to 165, 210, 215, 260
   
   --   set the rect of btn "Shuffle" to 170, 215, 220, 265
   --   set the rect of fld "Bot Time" to 170, 215, 220, 265
   --   set the rect of fld "Human Time" to 170, 215, 220, 265
   --   set the rect of fld "Tripp" to 170, 215, 220, 265
   --   set the rect of fld "You" to 170, 215, 220, 265
   
   --   set the rect of btn "Stop" to 170, 215, 220, 265
   --   set the rect of fld "Best Tripp Time" to 170, 215, 220, 265
   --   set the rect of fld "Best Human Time" to 170, 215, 220, 265
   
end resetButtons
      
      --         set the rectangle of btn "tile1" to 120, 25, 195, 100
      --      set the rectangle of btn "tile2" to 200, 25, 275, 100
      --      set the rectangle of btn "tile3" to 280, 25, 355, 100
      --      set the rectangle of btn "tile4" to 120, 105, 195, 180
      --      set the rectangle of btn "tile5" to 200, 105, 275, 180
      --      set the rectangle of btn "tile6" to 280, 105, 355, 180
      --      set the rectangle of btn "tile7" to 120, 185, 195, 260
      --      set the rectangle of btn "tile8" to 200, 185, 275, 260
      --      set the rectangle of btn "tile9" to 280, 185, 355, 260
      
      --      set the rectangle of img "shard1.png" to 120, 25, 195, 100
      --      set the rectangle of img "shard2.png" to 200, 25, 275, 100
      --      set the rectangle of img "shard3.png" to 280, 25, 355, 100
      --      set the rectangle of img "shard4.png" to 120, 105, 195, 180
      --      set the rectangle of img "shard5.png" to 200, 105, 275, 180
      --      set the rectangle of img "shard6.png" to 280, 105, 355, 180
      --      set the rectangle of img "shard7.png" to 120, 185, 195, 260
      --      set the rectangle of img "shard8.png" to 200, 185, 275, 260
      --      set the rectangle of img "shard9.png" to 280, 185, 355, 260

      
      --   if (the machine is "iPhone") or \
            --         (the machine is "iPod Touch") or \
            --         (the width of this stack is 480) or \
            --         (the machine is "iPhone Simulator") then
      

      
      --   defineCard
      
      
      --   if (the machine is "iPad") or \
            --         (the width of this stack is 1024) then
      
      --      set the rectangle of btn "tile1" to 272, 125, 422, 275
      --      set the rectangle of btn "tile2" to 437, 125, 587, 275
      --      set the rectangle of btn "tile3" to 602, 125, 752, 275
      
      --      set the rectangle of btn "tile4" to 272, 290, 422, 440
      --      set the rectangle of btn "tile5" to 437, 290, 587, 440
      --      set the rectangle of btn "tile6" to 602, 290, 752, 440
      
      --      set the rectangle of btn "tile7" to 272, 455, 422, 605
      --      set the rectangle of btn "tile8" to 437, 455, 587, 605
      --      set the rectangle of btn "tile9" to 602, 455, 752, 605
      
      
      --      set the rectangle of img "shard1.png" to 272, 125, 422, 275
      --      set the rectangle of img "shard2.png" to 437, 125, 587, 275
      --      set the rectangle of img "shard3.png" to 602, 125, 752, 275
      
      --      set the rectangle of img "shard4.png" to 272, 290, 422, 440
      --      set the rectangle of img "shard5.png" to 437, 290, 587, 440
      --      set the rectangle of img "shard6.png" to 602, 290, 752, 440
      
      --      set the rectangle of img "shard7.png" to 272, 455, 422, 605
      --      set the rectangle of img "shard8.png" to 437, 455, 587, 605
      --      set the rectangle of img "shard9.png" to 602, 455, 752, 605
      
      
      --      set the rectangle of btn "Tripp" to 524, 620, 752, 695
      --      set the rectangle of btn "You" to 274, 620, 508, 695
      --      set the rectangle of fld "Timer" to 850, 0, 1024, 80
      --      set the rectangle of fld "Best Time" to 0, 0, 212, 80
      --      set the rectangle of fld "Reset" to 860, 630, 960, 705
      --      set the rectangle of fld "SuperSwaps" to 0, 320, 208, 395
      --      set the rectangle of btn "numbers" to 860, 320, 935, 395
      --      set the rectangle of img "pictures" to 860, 320, 935, 395
      
      --      --      set the textSize of fld "Timer" to 18
      --      --      set the textSize of fld "Best Time" to 18
      --      --      set the textSize of fld "SuperSwaps" to 18
      --      --      set the textSize of fld "RESET" to 18
      
      --      repeat with i = 1 to the number of buttons on this card
      --         --         if button i contains "tile" then
      --         set the textSize of btn i to 70
      --         set the textHeight of btn i to 100
      --         --      end if
      --      end repeat
      
      --      --      set the textSize of button "Solve" to 10
      
      --      repeat with n = 1 to the number of fields on this card
      --         set the textSize of fld n to 22
      --      end repeat
      
      --   end if
      
      --      if (the machine is "iPhone") or \
            --            (the machine is "iPod Touch") or \
            --            (the width of this stack is 480) or \
            --            (the machine is "iPhone Simulator") then
      
      --      put 75 into sWidth
      --      put 75 into sHeight
      --      put 5 into sOffset
      
      --      repeat with i = 1 to the number of controls in this card
      --         put round (item 1 of the cStartRect of control i * (480/1024)) into tIphoneRect1
      --         put round (item 2 of the cStartRect of control i * (320/768)) into tIphoneRect2
      --         put round (item 3 of the cStartRect of control i * (480/1024)) into tIphoneRect3
      --         put round (item 4 of the cStartRect of control i * (320/768)) into tIphoneRect4
      
      --         --         set the cIphoneStartRect of control i to tIphoneRect1, tIphoneRect2, tIphoneRect3, tIphoneRect4
      --         set the rect of control i to the cIphoneStartRect of control i
      
      --         --         set the cIphoneStartTextSize of control i to round((the cStartTextSize of control i) * .45)
      --         set the textSize of control i to the cIphoneStartTextSize of control i
      
      --         --         set the cIphoneStartTextHeight of control i to round((the cStartTextHeight of control i) * .45)
      --         set the textHeight of control i to the cIphoneStartTextHeight of control i
      
      --      set the rect of img "Return.png" to the cIphoneStartRect of img "Return.png"
      
      --         show control i
      
      --      set the rectangle of btn "tile1" to 120, 25, 195, 100
      --      set the rectangle of btn "tile2" to 200, 25, 275, 100
      --      set the rectangle of btn "tile3" to 280, 25, 355, 100
      --      set the rectangle of btn "tile4" to 120, 105, 195, 180
      --      set the rectangle of btn "tile5" to 200, 105, 275, 180
      --      set the rectangle of btn "tile6" to 280, 105, 355, 180
      --      set the rectangle of btn "tile7" to 120, 185, 195, 260
      --      set the rectangle of btn "tile8" to 200, 185, 275, 260
      --      set the rectangle of btn "tile9" to 280, 185, 355, 260
      
      --      set the rectangle of img "shard1.png" to 120, 25, 195, 100
      --      set the rectangle of img "shard2.png" to 200, 25, 275, 100
      --      set the rectangle of img "shard3.png" to 280, 25, 355, 100
      --      set the rectangle of img "shard4.png" to 120, 105, 195, 180
      --      set the rectangle of img "shard5.png" to 200, 105, 275, 180
      --      set the rectangle of img "shard6.png" to 280, 105, 355, 180
      --      set the rectangle of img "shard7.png" to 120, 185, 195, 260
      --      set the rectangle of img "shard8.png" to 200, 185, 275, 260
      --      set the rectangle of img "shard9.png" to 280, 185, 355, 260
      
      --      --      set the rectangle of btn "Shuffle" to 120, 265, 355, 295
      
      --      --            repeat with i = 1 to the number of buttons on this card
      --      --         --         if button i contains "tile" then
      --      --         set the textSize of btn i to 70
      --      --         set the textHeight of btn i to 100
      --      --         --      end if
      --      --      end repeat
      
      --      repeat with n = 1 to the number of fields on this card
      --         set the textSize of fld n to 10
      --      end repeat
      
      --   end if
      --   --   set the moveSpeed to 9535
      --   move button "b1" to 347, 200 //in 10 milliseconds
      --   move button "b2" to 512, 200 //in 10 milliseconds
      --   move button "b3" to 677, 200 //in 10 milliseconds
      --   move button "b4" to 347, 365// in 10 milliseconds
      --   move button "b5" to 512, 365// in 10 milliseconds
      --   move button "b6" to 677, 365 //in 10 milliseconds
      --   move button "b7" to 347, 530// in 10 milliseconds
      --   move button "b8" to 512, 530 //in 10 milliseconds
      --   move button "b9" to 677, 530 //in 10 milliseconds
      
      

      
      
      
      
      
      --   repeat with i = 1 to the number of buttons in this card
      --      set the backgroundColor of button i to "blue"
      --      set the textColor of button i to red
      --      if the name of the target contains "tile" then
      --         set the cStartLoc of button i to the loc of button ("tile" & i)
      --      end if
      --   --   end repeat
      
      
      --   if (the machine is "iPhone") or \
            --         (the machine is "iPod Touch") or \
            --         (the width of this stack is 480) or \
            --         (the machine is "iPhone Simulator") then
      
      --      put 75 into sWidth
      --      put 75 into sHeight
      --      put 5 into sOffset
      
      --      set the textSize of fld "Timer" to 14
      
      --      repeat with i = 1 to the number of buttons on this card
      --         --         if i contains "tile" then
      --         set the textSize of btn i to 30
      --         set the textHeight of btn i to 40
      --         --      end if
      --      end repeat
      
      --      set the textSize of fld "Timer" to 18
      
      --      repeat with n = 1 to the number of fields on this card
      --         set the textSize of fld n to 25
      --      end repeat
      
      --      set the rectangle of btn "tile1" to 120, 40, 195, 115
      --      set the rectangle of btn "tile2" to 200, 40, 275, 115
      --      set the rectangle of btn "tile3" to 280, 40, 355, 115
      --      set the rectangle of btn "tile4" to 120, 120, 195, 195
      --      set the rectangle of btn "tile5" to 200, 120, 275, 195
      --      set the rectangle of btn "tile6" to 280, 120, 355, 195
      --      set the rectangle of btn "tile7" to 120, 200, 195, 275
      --      set the rectangle of btn "tile8" to 200, 200, 275, 275
      --      set the rectangle of btn "tile9" to 280, 200, 355, 275
      
      --      set the rectangle of img "shard1.png" to 120, 40, 195, 115
      --      set the rectangle of img "shard2.png" to 200, 40, 275, 115
      --      set the rectangle of img "shard3.png" to 280, 40, 355, 115
      --      set the rectangle of img "shard4.png" to 120, 120, 195, 195
      --      set the rectangle of img "shard5.png" to 200, 120, 275, 195
      --      set the rectangle of img "shard6.png" to 280, 120, 355, 195
      --      set the rectangle of img "shard7.png" to 120, 200, 195, 275
      --      set the rectangle of img "shard8.png" to 200, 200, 275, 275
      --      set the rectangle of img "shard9.png" to 280, 200, 355, 275
      
      
      --      set the rectangle of btn "Shuffle" to 120, 280, 355, 310
      --      set the rectangle of fld "Timer" to 380, 0, 480, 100
      --      set the rectangle of fld "Best Time" to 0, 0, 100, 100
      --      set the rectangle of fld "Reset" to 370, 260, 470, 310
      --      set the rectangle of fld "SuperSwaps" to 0, 120, 100, 220
      --      set the rectangle of btn "numbers" to 400, 130, 450, 180
      --      set the rectangle of img "pictures" to 400, 130, 450, 180
      --   end if
      


on startHumanTimer
   put 0 into line 2 of fld "Human Time"
   put the seconds - gStartTime into gYourTime
   put gYourTime into line 2 of fld "Human Time"
   send "startHumanTimer" to me in 1 second
end startHumanTimer

on startBotTimer
   put 0 into line 2 of fld "Bot Time"
   put the seconds - gStartTime into gBotTime
   put gBotTime into line 2 of fld "Bot Time"
   send "startBotTimer" to me in 1 second
end startBotTimer

on finishBotPuzzle
      repeat for each line aLine in the pendingMessages
      if aLine contains "startBotTimer"  then
          cancel item 1 of aLine
      end if
   end repeat
   
   wait 1 milliseconds with messages
   if gBestBotTime = 0 then
      put gBotTime into gBestBotTime
      put gBestBotTime into line 2 of fld "Best Bot Time"
   end if
   
   if gBotTime < gBestBotTime is true then
      put gBotTime into gBestBotTime
      put gBestBotTime into line 2 of fld "Best Bot Time"
      --         answer "Best Tripp Time!"
      --      else
      --         answer "Done!"
   end if
         set the cTrippFinished of this card to "yes"
   --      set the cFinishable of me to "no"
   set the cBestBotTime of me to gBestBotTime
end finishBotPuzzle

on finishHumanPuzzle
      repeat for each line aLine in the pendingMessages
      if aLine contains "startHumanTimer"  then
          cancel item 1 of aLine
      end if
   end repeat
   
   if gBestHumanTime = 0 then
      put gYourTime into gBestHumanTime
      put gBestHumanTime into line 2 of fld "Best Human Time"
   end if
   if gYourTime < gBestHumanTime is true then
      put gYourTime into gBestHumanTime
      put gBestTime into line 2 of fld "Best Human Time"
      answer "Best Human Time!"
   else if the cTrippFinished of this card is "no" then
      answer "You beat Tripp!"
   else
      answer "Tripp won."
   end if
   set the cHumanFinished of this card to "yes"
   set the cFinishable of me to "no"
   set the cBestHumanTime of me to gBestHumanTime
end finishHumanPuzzle

Brittgriscom
Posts: 95
Joined: Wed Mar 30, 2011 10:15 am

Re: 8-puzzle game

Post by Brittgriscom » Mon May 16, 2011 12:27 am

Here is the second part of it:

Code: Select all

command shuffleBoard pTimes
   --   stopMessages
   local tSwaps, tButtonToSwap, tLastTile
   
   --   set the moveSpeed to 65535
   
   repeat while tSwaps < pTimes
      # randomly chose a tile
      --      put random (sSpaceButton) - 1 into tButtonToSwap
      put random (gSpaceButton - 1) into tButtonToSwap
      # if the tile is not the last one that was moved and
      # the tile can be moved into the empty space then move
      # it and increment the number of successful moves
      if (tLastTile is not tButtonToSwap) and \
            (isSwapable (gSpaceButton, tButtonToSwap) is true) then
         swapButtons gSpaceButton, tButtonToSwap
         put tButtonToSwap into tLastTile
         set the cFinishable of this card to "yes"
         add 1 to tSwaps
      end if
      
      repeat with i = 1 to 9
         put  (item 1 of the loc of btn ("tile" & i)) + gGroupOffset into tHumanTileXCoord
         put item 2 of the loc of btn ("tile" & i)  into tHumanTileYCoord
         set the loc of btn ("human tile" & i) to tHumanTileXCoord, tHumanTileYCoord
         set the loc of image ("human shard" & i & ".png") to tHumanTileXCoord, tHumanTileYCoord
      end repeat
      
   end repeat
end shuffleBoard

function isSwapable pButton1, pButton2
   local tRelativeX, tRelativeY
   if the cPlayer of the target is "bot" then
      # calculate the horizontal offset between two button locations
      put item 1 of location of button ("tile" & pButton1) - \
            item 1 of location of button ("tile" & pButton2) into tRelativeX
      # calculate the vertical offset between two button locations
      put item -1 of location of button ("tile" & pButton1) - \
            item -1 of location of button ("tile" & pButton2) into tRelativeY
   end if
   
   if the cPlayer of the target is "human" then
      # calculate the horizontal offset between two button locations
      put item 1 of location of button ("human tile" & pButton1) - \
            item 1 of location of button ("human tile" & pButton2) into tRelativeX
      # calculate the vertical offset between two button locations
      put item -1 of location of button ("human tile" & pButton1) - \
            item -1 of location of button ("human tile" & pButton2) into tRelativeY
   end if
   
   # if there is only a horizontal or a vertical offset and the offset is of a specific size
   # then the tiles can be swapped
   if (tRelativeX is 0 and abs (tRelativeY) is sWidth + sOffset) or \
         (tRelativeY is 0 and abs (tRelativeX) is sHeight + sOffset) then
      return true
   else
      return false
   end if
   
end isSwapable

function isDoubleSwapable pButton1, pButton2
   local tRelativeX, tRelativeY
   
   # calculate the horizontal offset between two button locations
   put item 1 of location of button ("human tile" & pButton1) - \
         item 1 of location of button ("human tile" & pButton2) into tRelativeX
   # calculate the vertical offset between two button locations
   put item -1 of location of button ("human tile" & pButton1) - \
         item -1 of location of button ("human tile" & pButton2) into tRelativeY
   # if there is only a horizontal or a vertical offset and the offset is of a specific size
   # then the tiles can be swapped
   if (tRelativeX is 0 and abs (tRelativeY) is 2*sHeight + 2*sOffset) then
      
      repeat with i = 1 to 8
         if (item 1 of the location of button ("human tile" & i) = item 1 of the location of button ("human tile" & pButton1)) and \
         (the name of button ("human tile" & i) contains pButton1 is false) and \
         (the name of button ("human tile" & i) contains pButton2 is false) then
            put character 11 of the short name of button ("human tile" & i) into gMiddleButton
         end if
      end repeat
      return true
      
   else if (tRelativeY is 0 and abs (tRelativeX) is 2*sWidth + 2*sOffset) then
      
      repeat with i = 1 to 8
         if (item 2 of the location of button ("human tile" & i) = item 2 of the location of button ("human tile" & pButton1)) and \
               (the name of button ("human tile" & i) <> the name of button ("human tile" & pButton1)) and \
               (the name of button ("human tile" & i) <> the name of button ("human tile" & pButton2)) then
            put character 11 of the short name of button ("human tile" & i) into gMiddleButton
         end if
      end repeat
      return true
   else
      return false
   end if
end isDoubleSwapable

command swapButtons pButton1, pButton2
   --   if the cToggle of this card is "numbers" then
   
   if the cPlayer of the target is "bot" then
      put location of button ("tile" & pButton1) into tButtonLocation
      move button ("tile" & pButton1) from location of button ("tile" & pButton1) to \
            location of button ("tile" & pButton2)// without messages
      move button ("tile" & pButton2) from location of button ("tile" & pButton2) to \
            tButtonLocation// without messages
      
      repeat with i = 1 to 9
         set the loc of image ("shard" & i & ".png") to the loc of button ("tile" & i)
         --         set the loc of image ("human shard" & i & ".png") to the loc of button ("human tile" & i)
      end repeat
      
      put boardToBoardState (gWidthPadding, gHeightPadding) into tCurrentState
      --      if  the cFinishable of this card is "yes" then
      if manhattanDistance (tCurrentState) is 0 then
         finishBotPuzzle
      end if
   end if
   --end if
   
   --   repeat with i = 1 to 9
   --      set the cPlayer of image ("human shard" & i & ".png") to "human"
   --   end repeat
   
   
   if the cPlayer of the target is "human" then
      put location of button ("human tile" & pButton1) into tButtonLocation
      move button ("human tile" & pButton1) from location of button ("human tile" & pButton1) to \
            location of button ("human tile" & pButton2) //without messages
      move button ("human tile" & pButton2) from location of button ("human tile" & pButton2) to \
            tButtonLocation //without messages
      
      repeat with i = 1 to 9
         --         set the loc of image ("shard" & i & ".png") to the loc of button ("tile" & i)
         set the loc of image ("human shard" & i & ".png") to the loc of button ("human tile" & i)
      end repeat
      
      put boardToBoardState (gHumanWidthPadding, gHumanHeightPadding) into tHumanCurrentState
      if  the cFinishable of this card is "yes" then
         if manhattanDistance (tHumanCurrentState) is 0 then
            finishHumanPuzzle
         end if
      end if
   end if
   
   --end if
   
   
   
   
   --   if the cToggle of this card is "pictures" then
   --      put location of image ("shard" & pButton1 & ".png") into tButtonLocation
   --      --      set the moveSpeed to 65535
   --      move image ("shard" & pButton1 & ".png") from location of image ("shard" & pButton1 & ".png")to \
         --            location of image ("shard" & pButton2 & ".png") without messages
   --      move image ("shard" & pButton2 & ".png") from location of image ("shard" & pButton2 & ".png") to \
         --            tButtonLocation without messages
   
   --      repeat with i = 1 to 9
   --         set the loc of button ("tile" & i) to the loc of image ("shard" & i & ".png")
   --      end repeat
   
   --   end if
   
   --   if the cPlayer of the target is "bot" then
   --      put boardToBoardState (sWidthPadding, sHeightPadding) into gCurrentState
   --      if  the cFinishable of this card is "yes" then
   --         if manhattanDistance (gCurrentState) is 0 then
   --            finishBotPuzzle
   --         end if
   --      end if
   --   end if
   
   --   if the cPlayer of the target is "human" then
   --      put boardToBoardState (sHumanWidthPadding, sHumanHeightPadding) into gHumanCurrentState
   --      if  the cFinishable of this card is "yes" then
   --         if manhattanDistance (gHumanCurrentState) is 0 then
   --            finishHumanPuzzle
   --         end if
   --      end if
   --   end if
   
   --end if
   
   
end swapButtons

command doubleSwapButtons pButton1, pButton2, pButton3
   --   if the cToggle of this card is "numbers" then
   lock screen
   
   put location of button ("human tile" & pButton1) into tButtonLocation
   move button ("human tile" & pButton1) from location of button ("human tile" & pButton1) to \
         location of button ("human tile" & pButton2) without messages
   move button ("human tile" & pButton2) from location of button ("human tile" & pButton2) to \
         tButtonLocation without messages
   
   put location of button ("human tile" & pButton3) into tButtonLocation2
   --      set the moveSpeed to 65535
   move button ("human tile" & pButton3) from location of button ("human tile" & pButton3) to \
         location of button ("human tile" & pButton2) without messages
   move button ("human tile" & pButton2) from location of button ("human tile" & pButton2) to \
         tButtonLocation2 without messages
   
   repeat with i = 1 to 9
      set the loc of image ("human shard" & i & ".png") to the loc of button ("human tile" & i)
   end repeat
   
   unlock screen
   --   end if
   
   --   if the cToggle of this card is "pictures" then
   --      lock screen
   
   --      put location of image ("shard" & pButton1 & ".png") into tButtonLocation
   --      set the moveSpeed to 65535
   --      move image ("shard" & pButton1 & ".png") from location of image ("shard" & pButton1 & ".png")to \
         --            location of image ("shard" & pButton2 & ".png") without messages
   --      move image ("shard" & pButton2 & ".png") from location of image ("shard" & pButton2 & ".png") to \
         --            tButtonLocation without messages
   
   --      put location of image ("shard" & pButton3 & ".png") into tButtonLocation2
   --      move image ("shard" & pButton3 & ".png") from location of image ("shard" & pButton3 & ".png") to \
         --            location of image ("shard" & pButton2 & ".png") without messages
   --      move image ("shard" & pButton2 & ".png") from location of image ("shard" & pButton2 & ".png") to \
         --            tButtonLocation2 without messages
   
   --      repeat with i = 1 to 9
   --         set the loc of button ("tile" & i) to the loc of image ("shard" & i & ".png")
   --      end repeat
   
   --      unlock screen
   --   end if
end doubleSwapButtons

function boardToBoardState pWidthPadding, pHeightPadding
   --   local tHorizontal, tVertical, tButtonNumber, tPositionArray, tLine, tResult
   # cycle through the vertical board buttons
   repeat with tVertical = 0 to sTilesY - 1
      # cycle through the horizontal board buttons
      repeat with tHorizontal = 0 to sTilesX - 1
         # cycle through the button numbers
         repeat with tButtonNumber = 1 to sTilesX * sTilesY
            
            # put the number of the button label into an array
            if the cPlayer of the target is "human" then
               if location of button ("human tile" & tButtonNumber) is \
                     pWidthPadding + tHorizontal * (sWidth + sOffset), pHeightPadding + tVertical * (sHeight + sOffset) then
                  put tHorizontal + tVertical * sTilesX into tPositionArray [tButtonNumber]
               end if
            end if
            
            if the cPlayer of the target is "bot" then
               if location of button ("tile" & tButtonNumber) is \
                     pWidthPadding + tHorizontal * (sWidth + sOffset), pHeightPadding + tVertical * (sHeight + sOffset) then
                  put tHorizontal + tVertical * sTilesX into tPositionArray [tButtonNumber]
               end if
            end if
            
         end repeat
      end repeat
   end repeat
   # order the array by its keys
   get the keys of tPositionArray
   sort lines of it numeric by tPositionArray[each]
   # convert the list of keys into a string
   repeat for each line tLine in it
      if tResult is not empty then
         put " " after tResult
      end if
      put tLine after tResult
   end repeat
   return tResult
end boardToBoardState

--function humanBoardToBoardState pWidthPadding, pHeightPadding
--   --   local tHorizontal, tVertical, tButtonNumber, tPositionArray, tLine, tResult
--   # cycle through the vertical board buttons
--   repeat with tVertical = 0 to sTilesY - 1
--      # cycle through the horizontal board buttons
--      repeat with tHorizontal = 0 to sTilesX - 1
--         # cycle through the button numbers
--         repeat with tButtonNumber = 1 to sTilesX * sTilesY

--            # put the number of the button label into an array
--            if location of button ("human tile" & tButtonNumber) is \
      --                  pWidthPadding + tHorizontal * (sWidth + sOffset), pHeightPadding + tVertical * (sHeight + sOffset) then
--               put tHorizontal + tVertical * sTilesX into tPositionArray [tButtonNumber]
--            end if

--         end repeat
--      end repeat
--   end repeat
--   # order the array by its keys
--   get the keys of tPositionArray
--   sort lines of it numeric by tPositionArray[each]
--   # convert the list of keys into a string
--   repeat for each line tLine in it
--      if tResult is not empty then
--         put " " after tResult
--      end if
--      put tLine after tResult
--   end repeat
--   return tResult
--end humanBoardToBoardState

function manhattanDistance pBoardState
   local tButton, tButton1H, tButton1V, tButton2H, tButton2V, tResult
   # test how far each tile is away from its winning location
   # we are not testing the location of the space
   
   if the cPlayer of the target is "bot" then
      --   repeat with tButton = 0 to sSpaceButton - 1
      repeat with tButton = 1 to gSpaceButton - 1 of group "Bot"
         # get the x and y positions of the two buttons to be swapped,
         # with respect to the horizontal and vertical game grid
         put (wordOffset (tButton, sWinningState) - 1) mod sTilesX into tButton1H
         put (wordOffset (tButton, sWinningState) - 1) div sTilesY into tButton1V
         put (wordOffset (tButton, pBoardState) - 1) mod sTilesX into tButton2H
         put (wordOffset (tButton, pBoardState) - 1) div sTilesY into tButton2V
         add abs (tButton1H - tButton2H) + abs (tButton1V - tButton2V) to tBotResult
      end repeat
      return tBotResult
   end if
   
   if the cPlayer of the target is "human" then
      --   repeat with tButton = 0 to sSpaceButton - 1
      repeat with tButton = 1 to gSpaceButton - 1 of group "Human"
         # get the x and y positions of the two buttons to be swapped,
         # with respect to the horizontal and vertical game grid
         put (wordOffset (tButton, sWinningState) - 1) mod sTilesX into tButton1H
         put (wordOffset (tButton, sWinningState) - 1) div sTilesY into tButton1V
         put (wordOffset (tButton, pBoardState) - 1) mod sTilesX into tButton2H
         put (wordOffset (tButton, pBoardState) - 1) div sTilesY into tButton2V
         add abs (tButton1H - tButton2H) + abs (tButton1V - tButton2V) to tHumanResult
      end repeat
         return tHumanResult
   end if

end manhattanDistance

function fDistance pParent, pChild
   put item 1 of the loc of btn ("b" & pParent) into tParentX
   put item 1 of the loc of button ("b" & pChild) into tChildX
   
   put item 2 of the loc of btn ("b" & pParent)  into tParentY
   put item 2 of the loc of button ("b" & pChild)  into tChildY
   
   put abs ((tParentX - tChildX))/60 into tXDist
   put abs ((tParentY - tChildY))/60 into tYDist
   
   if tXDist = 1 and tYDist = 0 or \
         tXDist = 0 and tYDist = 1 then
      
      return 10
   end if
   
   if tXDist = 1 and tYDist = 1 then
      return 14
   end if
end fDistance

command solvePuzzle pWidthPadding
, pHeightPadding 
    
   local tStatesToExplore, tExploredStates, tMove
    
   # create the first state to explore and calculate its estimated cost
    
   put "," & boardToBoardState (pWidthPadding
, pHeightPadding) into tStatesToExplore
    
   if manhattanDistance (item 2 of tStatesToExplore) is 0 then
        
      --         answer "Puzzle is Already Solved!" with "OK"
    
   else
        
      put costGH (tStatesToExplore) & "," & tStatesToExplore into tStatesToExplore
        
      repeat until tStatesToExplore is empty
  
              
         if manhattanDistance (item 3 of line 1 of tStatesToExplore) is 0 then
            
            repeat for each word tMove in item 2 of line 1 tStatesToExplore
  
               # trying to be able to stop it
               --               wait 0 seconds with messages      
               
               swapButtons gSpaceButton, tMove
 
               --               wait 1600 milliseconds with messages        
            end repeat
   
            exit repeat                               
         else
                
            exploreStates tStatesToExplore, tExploredStates
            
         end if
        
      end repeat
    
   end if
end solvePuzzle

command exploreStates @pStatesToExplore, @pExploredStates
    
   local tNewStates, tResult
    
   # get the next possible moves for pStateToExplore 
    
   put getNewStatePaths (line 1 of pStatesToExplore) into tNewStates
    
   # move the state path that is being explored to pExploredStates
    
   if pExploredStates is not empty then
        
      put cr after pExploredStates
    
   end if
    
   put line 1 of pStatesToExplore after pExploredStates
    
   # remove the state path that is being explored from pStatesToExplore
    
   put line 2 to -1 of pStatesToExplore into pStatesToExplore 
    
   # prune the existing explored and unexplored search space
    
   pruneStateSpace tNewStates, pStatesToExplore, pExploredStates
    
   # sort pStatesToExplore so the cheapest path is located at the beginning
    
   sort lines of pStatesToExplore numeric by item 1 of each 

end exploreStates
   
   
function getNewStatePaths pPath
    
   local tPossibleMove, tNewPath, tResult
    
   # we are testing for all possible tile moves 
    
   # from the most recent state of the current path 
    
   --   repeat with tPossibleMove = 0 to sSpaceButton - 1
    
   repeat with tPossibleMove = 1 to 8  
      put deriveNextState (pPath, tPossibleMove, gSpaceButton) into tNewPath
       
       if tNewPath is not empty then
            
         if tResult is not empty then
               
             put cr after tResult
            
         end if
            
         put tNewPath after tResult
        
      end if
    
   end repeat
    
   return tResult
end getNewStatePaths
   
function deriveNextState pCurrentPath, pSwap1, pSwap2
    
   local tHorizontal, tVertical, tButton1H, tButton2H, \
        
   tButton1V, tButton2V, tPossibleNewState, tResult
    
   # get the x and y positions of the two buttons to be swapped
    
   # with respect to the horizontal and vertical game grid of the first
    
   # state of the current path
    
   # we are using item 3 here as item 1 is the cost of the path and
    
   # item 2 contains the moves needed to solve the puzzle
    
   put (wordOffset (pSwap1, item 3 of pCurrentPath) - 1) mod sTilesX into tButton1H
    
   put (wordOffset (pSwap1, item 3 of pCurrentPath) - 1) div sTilesY into tButton1V
    
   put (wordOffset (pSwap2, item 3 of pCurrentPath) - 1) mod sTilesX into tButton2H
    
   put (wordOffset (pSwap2, item 3 of pCurrentPath) - 1) div sTilesY into tButton2V
    
   # determine if the two buttons can be swapped,
    
   # if so create a new state that represents the swap
    
   if ((tButton1H - tButton2H is 0) and (abs (tButton1V - tButton2V) is 1)) or \
        
   ((tButton1V - tButton2V is 0) and (abs (tButton1H - tButton2H) is 1)) then
       
       put item 3 pCurrentPath into tPossibleNewState
        
      replace pSwap1 with "X" in tPossibleNewState
        
      replace pSwap2 with pSwap1 in tPossibleNewState
        
      replace "X" with pSwap2 in tPossibleNewState
        
      # check here if we have a loop in the path and reject it if so
       
       if itemOffset (tPossibleNewState, item 3 to -1 of pCurrentPath) is 0 then
            
         # add the new state to the front of other states
            
         put tPossibleNewState & "," & item 3 to -1 of pCurrentPath into tResult
           
          # add the move that got us to this new state to the list of moves we made so far
           
          if item 2 of pCurrentPath is empty then
                
            put pSwap1 & "," & tResult into tResult
           
          else
                
            put item 2 of pCurrentPath && pSwap1 & "," & tResult into tResult
            
         end if
            
         put costGH (tResult) & "," & tResult into tResult
        
      end if
    
   end if
    
   return tResult

end deriveNextState
   
function costGH pPath
    
   local tMove, tResult
 
   # calculate g for pPath
    
   
   repeat for each word tMove in item 1 of pPath
  
      add 1 to tResult
    
      # trying to be able to stop it
      wait 0 seconds with messages
   end repeat
    
   # calculate h for pPath and add it to g
    
   add manhattanDistance (item 2 of pPath) to tResult
    
   return tResult
end costGH
   function costGH pPath
    
   local tMove, tResult
 
   # calculate g for pPath
    
   
   repeat for each word tMove in item 1 of pPath
  
      add 1 to tResult
    
      # trying to be able to stop it
      wait 0 seconds with messages
   end repeat
    
   # calculate h for pPath and add it to g
    
   add manhattanDistance (item 2 of pPath) to tResult
    
   return tResult
end costGH
   
   
command pruneStateSpace pNewPaths, @pStatesToExplore, @pExploredStates
    
   local tPath, tStatesToExploreDuplicate, tExploredStatesDuplicate, tAddFlag
   
    repeat for each line tPath in pNewPaths
        
      put false into tAddFlag
        
      # find out if the new state to explore exists 
        
      # as a head in one of the already explored states
        
      put stateMatch (tPath, pExploredStates) into tExploredStatesDuplicate
        
      # find out if the new state to explore exists
        
      # as a head in one of the states that are still to be explored
        
      put stateMatch (tPath, pStatesToExplore) into tStatesToExploreDuplicate
        
      if tExploredStatesDuplicate is false and tStatesToExploreDuplicate is false 
      then
            
         # if we found no match, then set a flag to add the new path
            
         # to the paths that are to be explored
            
         put true into tAddFlag
        
      else if tExploredStatesDuplicate is not false and item 1 of tPath < \
           
       item 1 of line tExploredStatesDuplicate of pExploredStates then 
            
         # if we found a match with the states we already explored and the new path
            
         # is cheaper then remove the matched state from the explored states
           
          # and set the flag to add the new path to the paths that are to be explored
            
         delete line tExploredStatesDuplicate of pExploredStates
            
         put true into tAddFlag
        
      else if tStatesToExploreDuplicate is not false and item 1 of tPath < \
           
       item 1 of line tStatesToExploreDuplicate of pStatesToExplore then 
           
          # if we found a match with the states we have not yet explored and the new
            
         # path is cheaper then remove the matched state from the unexplored states
            
         # and set the flag to add the new path to the paths that are to be explored
            
         delete line tStatesToExploreDuplicate of pStatesToExplore 
            
         put true into tAddFlag
        end if
        
         # if tAddFlag is set then add the new path to the paths that are to be explored
        
         # any new paths that exist for which tAddFlag is not set to true are skipped
        
         if tAddFlag is true then
           
             if pStatesToExplore is not empty then
                
               put cr after pStatesToExplore
            
            end if
            
            put tPath after pStatesToExplore
        
         end if
    
      end repeat
end pruneStateSpace
      
function stateMatch pPath, pPathList
    
   local tPath, tLineCount
    
   repeat for each line tPath in pPathList
       
       add 1 to tLineCount
        
      if item 3 of tPath is item 3 of pPath 
      then
            
         return tLineCount
        
      end if
    
   end repeat
    
   return false

end stateMatch
Here is the script for the shuffle button:

Code: Select all

global gStartTime
global gSuperSwaps
global gWidthPadding
global gHeightPadding

on mouseUp
   # if the label is shuffle
   if the cShuffleLabel of this card is "shuffle" then
      hide btn "Shuffle"
      stopMessages
      shuffleBoard 10
      
      set the cShuffleLabel of this card to "reset"
      set the label of me to "Reset"
      show btn "Shuffle"
      
      put the seconds into gStartTime
      put 10 into gSuperSwaps
      put gSuperSwaps into line 2 of fld "SuperSwaps"
      set  the cFinishable of this card to "yes"
      --      set the cStopped of this card to "no"
      
      put 0 into line 2 of field "Human Time"
      put 0 into line 2 of field "Bot Time"
      wait 1 seconds with messages
      startHumanTimer
      startBotTimer
      set the cTrippFinished of this card to "no"
      set the cHumanFinished of this card to "no"
      solvePuzzle gWidthPadding, gHeightPadding
      set the label of me to "Shuffle"
      set the cShuffleLabel of this card to "shuffle"
   end if
   
   # if the label is Reset
   if the cShuffleLabel of this card is "reset" then
      --      set the cStopped of this card to "yes"
      
      resetButtons
      set the label of me to "Shuffle"
      
   end if
end mouseUp
Last edited by Brittgriscom on Mon May 16, 2011 12:40 am, edited 1 time in total.

Brittgriscom
Posts: 95
Joined: Wed Mar 30, 2011 10:15 am

Re: 8-puzzle game

Post by Brittgriscom » Mon May 16, 2011 12:35 am

Better yet, here is the stack, zipped.
Attachments
SwapBot.livecode.zip
(167.35 KiB) Downloaded 212 times

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3281
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: 8-puzzle game

Post by bn » Mon May 16, 2011 11:30 am

Hi Britt,
did you test the stack?
For me it opens too small, when I enlarge it nothing happens. If I click swap it goes into an endless loop.

Since this is a complex stack a working example or some pointers how to use it would be nice.

Kind regards

Bernd

Brittgriscom
Posts: 95
Joined: Wed Mar 30, 2011 10:15 am

Re: 8-puzzle game

Post by Brittgriscom » Mon May 16, 2011 11:42 am

Sorry. I thought I had made it bigger.

This should be full-size. Press shuffle to start it.
Attachments
SwapBot.livecode.zip
(167.43 KiB) Downloaded 219 times

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3281
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: 8-puzzle game

Post by bn » Mon May 16, 2011 9:57 pm

Hi Britt,

I give up.

I see you adapted the 8-puzzle very nicely to your needs. So Livecode language and logic are no problem for you.

I would have to unwind the whole logic which now, as far as I see is used by both the human and the bot to solve the puzzle.

I would split the puzzle up into two puzzles, for example put both the human and the bot puzzle into a group on its own. Transfer the script to the groups. Adapt the human and the bot scripts to the fact that they are in groups now:

You would have to change the global variables to script local variables of the respective groups, this effectively isolates them from each other. In your Shuffle button you would start both puzzles and then you should be able to slow down the bot puzzle without slowing down the whole app. The only thing you might want to share is the initial position of the tiles after shuffling. And then some I have not fully appreciated maybe.

The actual setup of the puzzles is fast, the individual moves are fast, I just don't find a way to slow down the bot without slowing everything down. Maybe I overlooked something but that is what I found. From my impression it should be fast enough for the iOS devices (although there is a lot of computing going on there is little animation.) I think it would be even better if you find a way to trigger individual moves of the bot with a send in time handler. That way you would have almost full control over the execution without "wait with messages" which is ok, but a little messy in terms of the programming. Wait with messages is good to make a tight repeat loop responsive but not really made to intertwine two or more actions. As I see you use "send in time" and "pending messages" already in your timers so the concept is familiar to you.

Kind regards

Bernd

Brittgriscom
Posts: 95
Joined: Wed Mar 30, 2011 10:15 am

Re: 8-puzzle game

Post by Brittgriscom » Tue May 17, 2011 12:41 am

In the "solvePuzzle" handler, I've changed

Code: Select all

swapButtons gSpaceButton, tMove
to

Code: Select all

send "swapButtons gSpaceButton, tMove
" to me in 1600 milliseconds
but then all the bot's buttons end up in the same place. Can you think of any reason why this might be so?

Edit: When I go though it with breakpoints, I see that we never go to the 'swapButtons' handler. It looks like the message never gets sent because the puzzle is solved within 1600 milliseconds.

Brittgriscom
Posts: 95
Joined: Wed Mar 30, 2011 10:15 am

Re: 8-puzzle game

Post by Brittgriscom » Tue May 17, 2011 1:52 am

For now, I've put the delay in here:

Code: Select all

function costGH pPath
    
   local tMove, tResult
 
   # calculate g for pPath
    
   
   repeat for each word tMove in item 1 of pPath
  
      add 1 to tResult
    
      # trying to be able to stop it
      wait 3 milliseconds with messages
   end repeat
    
   # calculate h for pPath and add it to g
    
   add manhattanDistance (item 2 of pPath) to tResult
    
   return tResult
end costGH
That means that the bot will think longer before doing any swapping, and then will swap at its normal super-fast speed. This doesn't cause any problems with the responsiveness of the app. It's strange that the app's responsiveness would be slower when the bot is moving, but not when it is thinking. Perhaps I could fix use this to my advantage by setting the buttons to their new locations, rather than moving them?

Edit: nope. I tried that, and it was still unresponsive. It is so mysterious why a 'wait...with messages' would cause unresponsiveness in the 'solvePuzzle' handler, but not in the 'costGH' function!

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3281
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: 8-puzzle game

Post by bn » Tue May 17, 2011 10:11 pm

Hi Britt,

I changed the stack a bit. It works as far as responsiveness is concerned as far as I have tested.

I changed the "shuffle" button to branch out to the solvePuzzle handler without staying in the handler as it was before.(with a send in time to solvePuzzle) That makes for some of the improvements.

Then I changed the solvePuzzle handler so when it is finished and has all the moves of the bot assembled it branches out to a "displayBotButtons" handler again with a send in time. The "displayBotButtons" handler calls itself in definable intervalls to move the puzzle pieces of the bot. Here you can adjust the time, currently 1000 milliseconds.

What I did not finish doing and you will have to add is to stop the timer of the human and reset the whole thing. I think it is easier for you than for me since you know the logic more closely than I do.

Please have a try, it should work and should also be useable on an iPad. The only real lag there is is when the bot assembles all his moves right at the beginning.

Kind regards

Bernd
Attachments
SwapBot_bn_0.0.2.livecode.zip
(167.84 KiB) Downloaded 221 times

Brittgriscom
Posts: 95
Joined: Wed Mar 30, 2011 10:15 am

Re: 8-puzzle game

Post by Brittgriscom » Wed May 18, 2011 1:45 am

That works great Bernd. Thanks!

Any idea what the outlines around the buttons are? They show up only in the sim and devices.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3281
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: 8-puzzle game

Post by bn » Wed May 18, 2011 10:02 am

Hi Britt,

on iOS the buttons are not native iOS buttons but 'motif' buttons. You can change that by assigning an image as an icon to a button. You would have to make an image of how you want your button to look and then set the icon of the button to the id of that image. Presently not advised is the use of graphic effects to change the appearance of buttons since they have to be rendered by Livecode and that is slow.

For the standard iOS buttons I made a stack that produces images for buttons à la iOS.

http://www.berndniggemann.on-rev.com/bt ... vecode.zip



Kind regards

Bernd

Brittgriscom
Posts: 95
Joined: Wed Mar 30, 2011 10:15 am

Re: 8-puzzle game

Post by Brittgriscom » Wed May 18, 2011 10:19 am

Wow. That looks amazing. I'm going to have to spend some time with it to figure out how to put it into my app.

Brittgriscom
Posts: 95
Joined: Wed Mar 30, 2011 10:15 am

Re: 8-puzzle game

Post by Brittgriscom » Wed May 18, 2011 10:24 am

Is there a way to set the height of your buttons?

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3281
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: 8-puzzle game

Post by bn » Wed May 18, 2011 12:05 pm

Hi Britt,

the height is not settable since the standard height is fixed on iOS. It is more like an example how to set the icon of a button to an image. I guess you would like to have non-standard buttons for most of your buttons.

BUT if you don't tell anybody: you can go up to 40 pixel:

go to the application browser or the properties inspector, look for a scrollbar called "adjustHeight", set its visible to true and the disabled to false. Rearrange it in the group. Set it to the desired height. If your are bold change the max of the scrollbar to e.g. 60 and you can go up to 60 pixel height. There is also a label field that is hidden. You might want to make that visible too.

If you want me to do a version that accomodates setting the height I could send you one; that is a lot easier than changing your script :). Just don't complain about interface clutter :)

put

Code: Select all

 put base64decode ("bmlnZ2VtYW5uQHVuaS13aC5kZQ==")
into your message box and hit return

I took out the option to set the height because 32 is the standard height and I wanted to reduce clutter.

Kind regards

Bernd

Brittgriscom
Posts: 95
Joined: Wed Mar 30, 2011 10:15 am

Re: 8-puzzle game

Post by Brittgriscom » Wed May 18, 2011 8:12 pm

My buttons are 100x100 pixels. Will your app work with that size?

Post Reply

Return to “Creating Games”