Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ KlotskiPuzzle.lua
- Klotski-style sliding puzzle for MBMA.
- --]]
- solution = {{9, 0, 0,10},
- {9,11,11,10},
- {8,11,11, 6},
- {8, 7, 7, 3},
- {1, 5, 4, 2}}
- --initialise board (double index)
- board = {{9,11,11,10},
- {9,11,11,10},
- {8, 7, 7, 6},
- {8, 5, 4, 3},
- {1, 0, 0, 2}}
- -- initialise pieces
- -- { 1 2 3 4 5 }
- -- tbl[x] = {CharObject, letter, height, width, TLposition}
- tblPieces = {}
- tblPieces[1] = {getObject("Characters[KlotskiA]"), "A", 1, 1, 51}
- tblPieces[2] = {getObject("Characters[KlotskiB]"), "B", 1, 1, 54}
- tblPieces[3] = {getObject("Characters[KlotskiC]"), "C", 1, 1, 44}
- tblPieces[4] = {getObject("Characters[KlotskiD]"), "D", 1, 1, 43}
- tblPieces[5] = {getObject("Characters[KlotskiE]"), "E", 1, 1, 42}
- tblPieces[6] = {getObject("Characters[KlotskiF]"), "F", 1, 1, 34}
- tblPieces[7] = {getObject("Characters[KlotskiG]"), "G", 1, 2, 32}
- tblPieces[8] = {getObject("Characters[KlotskiH]"), "H", 2, 1, 31}
- tblPieces[9] = {getObject("Characters[KlotskiI]"), "I", 2, 1, 11}
- tblPieces[10] = {getObject("Characters[KlotskiJ]"), "J", 2, 1, 14}
- tblPieces[11] = {getObject("Characters[KlotskiK]"), "K", 2, 2, 12}
- solved = getObject("Conditions[KlotskiPuzzleSolved]")
- MoreSteps = getObject("Conditions[AnotherStep]")
- function ResetPuzzle()
- ResetOutfits()
- board = {{9,11,11,10},
- {9,11,11,10},
- {8, 7, 7, 6},
- {8, 5, 4, 3},
- {1, 0, 0, 2}}
- tblPieces[1][5] = 51
- tblPieces[2][5] = 54
- tblPieces[3][5] = 44
- tblPieces[4][5] = 43
- tblPieces[5][5] = 42
- tblPieces[6][5] = 34
- tblPieces[7][5] = 32
- tblPieces[8][5] = 31
- tblPieces[9][5] = 11
- tblPieces[10][5] = 14
- tblPieces[11][5] = 12
- end
- function CheckSolution()
- tmpsolved = false
- runsolved = true
- for r = 1,5 do
- for c = 1,4 do
- tmpsolved = board[r][c] == solution[r][c]
- runsolved = runsolved and tmpsolved
- end
- end
- if runsolved then
- solved:setValue(VConditionValue, true)
- end
- end
- function ResetOutfits()
- for x = 1,11 do
- tblPieces[x][1]:setValue(VCharacterCurrentOutfit,
- getObject("Characters[Klotski"..tblPieces[x][2].."].CharacterOutfits[NotSelected]"))
- end
- end
- function row(index)
- return math.floor(index/10)
- end
- function col(index)
- return index % 10
- end
- function blocksize(blockno)
- bsizec = tblPieces[blockno][4]
- bsizer = tblPieces[blockno][3]
- return bsizer,bsizec
- end
- --OnBlockClick - select block, deselect other, selected block = current character (all in VS)
- function OnSpaceClick(S)
- curN,curpos = GetBlockInfo()
- xtrans,ytrans = GetMoveDir(curpos,S,curN)
- movable = DoMoves(xtrans,ytrans,curN,curpos)
- MoreSteps:setValue(VConditionValue, movable)
- end
- function GetBlockInfo()
- CurBlock = game:getLink(VGameCurrentCharacter)
- curN = getObject("Characters["..CurBlock:getName().."].CharacterValues[BlockN]"):getInt(VValueInt)
- tlpos = tblPieces[curN][5]
- return curN,tlpos
- end
- function GetMoveDir(from,to,curN)
- bsizer,bsizec = blocksize(curN)
- xtrans = col(to)-col(from) --raw
- ytrans = row(to)-row(from) --raw
- if xtrans > 0 then
- xtrans = xtrans-(bsizec-1) --fixed for size
- end
- if ytrans > 0 then
- ytrans = ytrans-(bsizer-1) --fixed for size
- end
- return xtrans, ytrans
- end
- function DoMoves(xtm,ytm,curN,curpos)
- --check space to determin first move
- ready = false
- dox = 0
- doy = 0
- bsizer,bsizec = blocksize(curN)
- --check x
- if xtm > 0 then
- if bsizec == 1 and bsizer == 1 then
- ready = board[row(curpos)][col(curpos)+1] == 0
- elseif bsizec == 2 and bsizer == 1 then
- ready = board[row(curpos)][col(curpos)+2] == 0
- elseif bsizec == 1 and bsizer == 2 then
- ready = board[row(curpos)][col(curpos)+1] == 0 and
- board[row(curpos)+1][col(curpos)+1] == 0
- elseif bsizec == 2 and bsizer == 2 then
- ready = board[row(curpos)][col(curpos)+2] == 0 and
- board[row(curpos)+1][col(curpos)+2] == 0
- end
- elseif xtm < 0 then
- if (bsizec == 1 or bsizec == 2) and bsizer == 1 then
- ready = board[row(curpos)][col(curpos)-1] == 0
- elseif (bsizec == 1 or bsizec == 2) and bsizer == 2 then
- ready = board[row(curpos)][col(curpos)-1] == 0 and
- board[row(curpos)+1][col(curpos)-1] == 0
- end
- end
- if ready then dox = xtm/math.abs(xtm) end --do a single x move (with direction)
- --check y
- if not ready and ytm > 0 then
- if bsizec == 1 and bsizer == 1 then
- ready = board[row(curpos)+1][col(curpos)] == 0
- elseif bsizec == 2 and bsizer == 1 then
- ready = board[row(curpos)+1][col(curpos)] == 0 and
- board[row(curpos)+1][col(curpos)+1] == 0
- elseif bsizec == 1 and bsizer == 2 then
- ready = board[row(curpos)+2][col(curpos)] == 0
- elseif bsizec == 2 and bsizer == 2 then
- ready = board[row(curpos)+2][col(curpos)] == 0 and
- board[row(curpos)+2][col(curpos)+1] == 0
- end
- elseif not ready and ytm < 0 then
- if bsizec == 1 and (bsizer == 1 or bsizer == 2) then
- ready = board[row(curpos)-1][col(curpos)] == 0
- elseif bsizec == 2 and (bsizer == 1 or bsizer == 2) then
- ready = board[row(curpos)-1][col(curpos)] == 0 and
- board[row(curpos)-1][col(curpos)+1] == 0
- end
- end
- if ready and dox == 0 then doy = ytm/math.abs(ytm) end --do a single y move
- --single move
- if ready then
- newpos = curpos+(10*doy)+(dox)
- nowrunning = startAction(actionnumber(newpos))
- updateboard(curN,newpos)
- end
- return ready
- end
- function actionnumber(index)
- return getObject("Scenes[PUZ_KlotskiBox].SceneActions[MoveTo"..index.."]")
- end
- function updateboard(curN,newpos)
- --update piece TLposition
- tblPieces[curN][5] = newpos
- --update board layout
- ----'remove' moved block
- for r = 1,5 do
- for c = 1,4 do
- if board[r][c] == curN then
- board[r][c] = 0
- end
- end
- end
- ----'add' moved block
- if bsizec == 1 and bsizer == 1 then
- board[row(newpos)][col(newpos)] = curN
- elseif bsizec == 2 and bsizer == 1 then
- board[row(newpos)][col(newpos)] = curN
- board[row(newpos)][col(newpos)+1] = curN
- elseif bsizec == 1 and bsizer == 2 then
- board[row(newpos)][col(newpos)] = curN
- board[row(newpos)+1][col(newpos)] = curN
- elseif bsizec == 2 and bsizer == 2 then
- board[row(newpos)][col(newpos)] = curN
- board[row(newpos)+1][col(newpos)] = curN
- board[row(newpos)][col(newpos)+1] = curN
- board[row(newpos)+1][col(newpos)+1] = curN
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement