Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ------------SETTINGS---------------
- local COLORTYPE="gray" -- brown,gray
- local SWAPSCOUNT=50000
- local FIELDSIZEX=36--10
- local FIELDSIZEY=20--7
- local IMAGEMODE=true -- false - block numbers, true block part of image
- local IMAGEPATH="/disk/disk/test4.pic"
- local SHOWNUMBER=true
- local tilew=4--5
- local tileh=2--3
- -----------------------------------
- local image
- if IMAGEMODE then
- image=require("image")
- img=image.load(IMAGEPATH)
- --img=image.transform(img,1,1)--tilew*(FIELDSIZEX-1),tileh*(FIELDSIZEY-1))
- img=image.crop(img,"fromRight",img.width-tilew*(FIELDSIZEX))
- img=image.crop(img,"fromBottom",img.height-tileh*(FIELDSIZEY))
- -- image.draw(1,5,img)
- end
- --require("component").gpu.setForeground(0xffffff)
- --os.exit()
- local component = require("component")
- local event = require("event")
- local gpu = component.gpu
- sizex = FIELDSIZEX
- sizey = FIELDSIZEY
- emptyx = sizex
- emptyy = sizey
- local steps = 0
- dif = SWAPSCOUNT
- local tiles,suctiles = {},{}
- local tilecol = {}
- if COLORTYPE == "brown" then
- tilecol = {0x483500,0x000000,0xAA7700}
- else
- tilecol = {0x666666,0xFFFFFF,0x999999}
- end
- for i=1,sizey do
- tiles[i] = {}
- suctiles[i] = {}
- for j=1,sizex do
- if not (i == sizey and j == sizex) then
- tiles[i][j] = (i-1)*sizex+j
- suctiles[i][j] = tiles[i][j]
- else
- tiles[i][j] = 0
- suctiles[i][j] = 0
- end
- end
- end
- local function drawtile(x,y,num)
- if (IMAGEMODE)and(num ~= 0) then
- end
- if num ~= 0 then
- gpu.setBackground(tilecol[1])
- gpu.setForeground(tilecol[2])
- else
- gpu.setBackground(tilecol[3])
- end
- if ((IMAGEMODE)and(num==0))or(not IMAGEMODE) then
- gpu.fill(x,y,tilew,tileh," ")
- end
- if num ~= 0 then
- gpu.set(x+1,y+1,tostring(num))
- end
- gpu.setBackground(0x000000)
- gpu.setForeground(0xFFFFFF)
- end
- function moveTile(x1,y1,x2,y2)
- gpu.copy((x1-1)*tilew+1,(y1-1)*tileh+5,tilew,tileh,tilew*(x2-x1),tileh*(y2-y1))
- end
- function drawfield(x,y,bool)
- gpu.setBackground(0x666666)
- gpu.fill(x,y-2,35,2," ")
- gpu.set(x,y-2,"Пятнашки, чтобы выйти нажмите за")
- gpu.set(x,y-1,"количество ходов:"..tostring(steps))
- gpu.set(x+28,y-1,"поле")
- if bool then
- if IMAGEMODE then--ЖЕСТОКОЕ МЕСТО, ГДЕ ПОЛЕ СОРТИРУЕТСЯ, ПО КРАЙНЕЙ МЕРЕ ЛУЧШЕ ЧЕМ ПОСТОЯННО ЗАГРУЖАТЬ И РЕЗАТЬ КАРТИНКУ 3 ЧАСА УГРОБИЛ ИЗ-ЗА ТУПОЙ ОШИБКИ
- image.draw(x,y,img)
- local tmp=0
- if (sizey~=emptyy)or(sizex~=emptyx) then
- suctiles[sizey][sizex]=suctiles[emptyy][emptyx]
- suctiles[emptyy][emptyx]=0
- moveTile(emptyx,emptyy,sizex,sizey)
- end
- for i=1,sizey do
- for j=1,sizex do
- if tiles[i][j]==0 then
- elseif tiles[i][j]~=suctiles[i][j] then
- i2,j2=0,0
- for i1=1,sizey do
- for j1=1,sizex do
- if suctiles[i1][j1]==tiles[i][j] then
- i2,j2=i1,j1
- break
- end
- end
- if i2~=0 then
- break
- end
- end
- suctiles[emptyy][emptyx]=suctiles[i][j]
- suctiles[i][j]=suctiles[i2][j2]
- suctiles[i2][j2]=suctiles[emptyy][emptyx]
- suctiles[emptyy][emptyx]=0
- moveTile(j,i,emptyx,emptyy)
- moveTile(j2,i2,j,i)
- moveTile(emptyx,emptyy,j2,i2)
- end
- end
- end
- drawtile((emptyx-1)*tilew+x,(emptyy-1)*tileh+y,0)
- end
- end
- if (not IMAGEMODE)or((IMAGEMODE)and(SHOWNUMBER)) then
- for i=1,sizey do
- for j=1,sizex do
- drawtile((j-1)*tilew+x,(i-1)*tileh+y,tiles[i][j])
- end
- end
- end
- end
- function usecell(x,y)
- local bool,emxb,emyb=false,emptyx,emptyy
- if (emptyx == x-1 and emptyy == y) then
- tiles[y][x],tiles[emptyy][emptyx] = 0,tiles[y][x]
- emptyx = x
- bool=true
- elseif (emptyx == x+1 and emptyy == y) then
- tiles[y][x],tiles[emptyy][emptyx] = 0,tiles[y][x]
- emptyx = x
- bool=true
- elseif (emptyy == y-1 and emptyx == x) then
- tiles[y][x],tiles[emptyy][emptyx] = 0,tiles[y][x]
- emptyy = y
- bool=true
- elseif (emptyy == y+1 and emptyx == x) then
- tiles[y][x],tiles[emptyy][emptyx] = 0,tiles[y][x]
- emptyy = y
- bool=true
- end
- if bool then
- -- gpu.copy((x-1)*tilew+1,(y-1)*tileh+5,tilew,tileh,-(x-1)*tilew+(emxb-1)*tilew,-(y-1)*tileh+(emyb-1)*tileh)
- moveTile(x,y,emxb,emyb)
- drawtile((x-1)*tilew+1,(y-1)*tileh+5,tiles[y][x])
- -- drawtile((emxb-1)*tilew+5,(emyb-1)*tileh+5,tiles[emyb][emxb])
- end
- return bool
- -- emptyx,emptyy = emptyy,emptyx
- end
- function testfield()
- t = true
- for i=1,sizey do
- for j=1,sizex do
- if tiles[i][j] ~= ((i-1)*sizex+j)%(sizex*sizey) then--suctiles[i][j] then
- -- print(tiles[i][j],(i-1)*sizex+j,i,j)
- t = false
- end
- end
- end
- return t
- end
- --os.sleep(1)
- -------------------------------- genfield ----------------------------------------
- local r2 = 0
- for i=1,dif do
- r = math.random(1,4)
- while ((r == 1 and emptyx == 1)or(r == 2 and emptyy == 1)or(r == 3 and emptyx == sizex)or(r == 4 and emptyy == sizey))or r2 == r do
- r = math.random(1,4)
- end
- r2 = r
- x = emptyx
- y = emptyy
- if r == 1 then
- x = emptyx-1
- elseif r == 2 then
- y = emptyy-1
- elseif r == 3 then
- x = emptyx+1
- elseif r == 4 then
- y = emptyy+1
- end
- tiles[y][x],tiles[emptyy][emptyx] = 0,tiles[y][x]
- emptyx = x
- emptyy = y
- end
- ---------------------------------- play ------------------------------------------
- st = true
- local dir=-1
- drawfield(1,5,true)
- while st do
- st = false
- drawfield(1,5)
- _,_,x,y = event.pull("touch")
- if (x>=1 and y>=5 and x<=tilew*sizex and y<5+tileh*sizey) then
- if usecell(math.floor((x-1)/tilew)+1,math.floor((y-5)/tileh)+1) then
- steps = steps + 1
- end
- st = true
- if testfield() then
- drawfield(1,5)
- print("You win!")
- st = false
- end
- end
- end
- gpu.setBackground(0x000000)
- gpu.setForeground(0xffffff)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement