Advertisement
astral17

barley-break

Feb 27th, 2016
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.08 KB | None | 0 0
  1. ------------SETTINGS---------------
  2. local COLORTYPE="gray" -- brown,gray
  3. local SWAPSCOUNT=50000
  4. local FIELDSIZEX=36--10
  5. local FIELDSIZEY=20--7
  6. local IMAGEMODE=true -- false - block numbers, true block part of image
  7. local IMAGEPATH="/disk/disk/test4.pic"
  8. local SHOWNUMBER=true
  9. local tilew=4--5
  10. local tileh=2--3
  11. -----------------------------------
  12. local image
  13. if IMAGEMODE then
  14.   image=require("image")
  15.   img=image.load(IMAGEPATH)
  16.   --img=image.transform(img,1,1)--tilew*(FIELDSIZEX-1),tileh*(FIELDSIZEY-1))
  17.   img=image.crop(img,"fromRight",img.width-tilew*(FIELDSIZEX))
  18.   img=image.crop(img,"fromBottom",img.height-tileh*(FIELDSIZEY))
  19. --  image.draw(1,5,img)
  20. end
  21. --require("component").gpu.setForeground(0xffffff)
  22. --os.exit()
  23. local component = require("component")
  24. local event = require("event")
  25. local gpu = component.gpu
  26. sizex = FIELDSIZEX
  27. sizey = FIELDSIZEY
  28. emptyx = sizex
  29. emptyy = sizey
  30. local steps = 0
  31. dif = SWAPSCOUNT
  32. local tiles,suctiles = {},{}
  33. local tilecol = {}
  34. if COLORTYPE == "brown" then
  35.   tilecol = {0x483500,0x000000,0xAA7700}
  36. else
  37.   tilecol = {0x666666,0xFFFFFF,0x999999}
  38. end
  39. for i=1,sizey do
  40.   tiles[i] = {}
  41.   suctiles[i] = {}
  42.   for j=1,sizex do
  43.     if not (i == sizey and j == sizex) then
  44.       tiles[i][j] = (i-1)*sizex+j
  45.       suctiles[i][j] = tiles[i][j]
  46.     else
  47.       tiles[i][j] = 0
  48.       suctiles[i][j] = 0
  49.     end
  50.   end
  51. end
  52. local function drawtile(x,y,num)
  53.   if (IMAGEMODE)and(num ~= 0) then
  54.   end
  55.   if num ~= 0 then
  56.     gpu.setBackground(tilecol[1])
  57.     gpu.setForeground(tilecol[2])
  58.   else
  59.     gpu.setBackground(tilecol[3])
  60.   end
  61.   if ((IMAGEMODE)and(num==0))or(not IMAGEMODE) then
  62.     gpu.fill(x,y,tilew,tileh," ")
  63.   end
  64.   if num ~= 0 then
  65.     gpu.set(x+1,y+1,tostring(num))
  66.   end
  67.   gpu.setBackground(0x000000)
  68.   gpu.setForeground(0xFFFFFF)
  69. end
  70.  
  71. function moveTile(x1,y1,x2,y2)
  72.   gpu.copy((x1-1)*tilew+1,(y1-1)*tileh+5,tilew,tileh,tilew*(x2-x1),tileh*(y2-y1))
  73. end
  74.  
  75. function drawfield(x,y,bool)
  76.   gpu.setBackground(0x666666)
  77.   gpu.fill(x,y-2,35,2," ")
  78.   gpu.set(x,y-2,"Пятнашки, чтобы выйти нажмите за")
  79.   gpu.set(x,y-1,"количество ходов:"..tostring(steps))
  80.   gpu.set(x+28,y-1,"поле")
  81.   if bool then
  82.     if IMAGEMODE then--ЖЕСТОКОЕ МЕСТО, ГДЕ ПОЛЕ СОРТИРУЕТСЯ, ПО КРАЙНЕЙ МЕРЕ ЛУЧШЕ ЧЕМ ПОСТОЯННО ЗАГРУЖАТЬ И РЕЗАТЬ КАРТИНКУ 3 ЧАСА УГРОБИЛ ИЗ-ЗА ТУПОЙ ОШИБКИ
  83.       image.draw(x,y,img)
  84.       local tmp=0
  85.       if (sizey~=emptyy)or(sizex~=emptyx) then
  86.         suctiles[sizey][sizex]=suctiles[emptyy][emptyx]
  87.         suctiles[emptyy][emptyx]=0
  88.         moveTile(emptyx,emptyy,sizex,sizey)
  89.       end
  90.       for i=1,sizey do
  91.         for j=1,sizex do
  92.           if tiles[i][j]==0 then
  93.           elseif tiles[i][j]~=suctiles[i][j] then
  94.             i2,j2=0,0
  95.             for i1=1,sizey do
  96.               for j1=1,sizex do
  97.                 if suctiles[i1][j1]==tiles[i][j] then
  98.                   i2,j2=i1,j1
  99.                   break
  100.                 end
  101.               end
  102.               if i2~=0 then
  103.                 break
  104.               end
  105.             end
  106.             suctiles[emptyy][emptyx]=suctiles[i][j]
  107.             suctiles[i][j]=suctiles[i2][j2]
  108.             suctiles[i2][j2]=suctiles[emptyy][emptyx]
  109.             suctiles[emptyy][emptyx]=0
  110.             moveTile(j,i,emptyx,emptyy)
  111.             moveTile(j2,i2,j,i)
  112.             moveTile(emptyx,emptyy,j2,i2)
  113.           end
  114.         end
  115.       end
  116.       drawtile((emptyx-1)*tilew+x,(emptyy-1)*tileh+y,0)
  117.     end
  118.   end
  119.   if (not IMAGEMODE)or((IMAGEMODE)and(SHOWNUMBER)) then
  120.     for i=1,sizey do
  121.       for j=1,sizex do
  122.         drawtile((j-1)*tilew+x,(i-1)*tileh+y,tiles[i][j])
  123.       end
  124.     end
  125.   end
  126. end
  127. function usecell(x,y)
  128.   local bool,emxb,emyb=false,emptyx,emptyy
  129.   if (emptyx == x-1 and emptyy == y) then
  130.     tiles[y][x],tiles[emptyy][emptyx] = 0,tiles[y][x]
  131.     emptyx = x
  132.     bool=true
  133.   elseif (emptyx == x+1 and emptyy == y) then
  134.     tiles[y][x],tiles[emptyy][emptyx] = 0,tiles[y][x]
  135.     emptyx = x
  136.     bool=true
  137.   elseif (emptyy == y-1 and emptyx == x) then
  138.     tiles[y][x],tiles[emptyy][emptyx] = 0,tiles[y][x]
  139.     emptyy = y
  140.     bool=true
  141.   elseif (emptyy == y+1 and emptyx == x) then
  142.     tiles[y][x],tiles[emptyy][emptyx] = 0,tiles[y][x]
  143.     emptyy = y
  144.     bool=true
  145.   end
  146.   if bool then
  147. --    gpu.copy((x-1)*tilew+1,(y-1)*tileh+5,tilew,tileh,-(x-1)*tilew+(emxb-1)*tilew,-(y-1)*tileh+(emyb-1)*tileh)
  148.     moveTile(x,y,emxb,emyb)
  149.     drawtile((x-1)*tilew+1,(y-1)*tileh+5,tiles[y][x])
  150. --    drawtile((emxb-1)*tilew+5,(emyb-1)*tileh+5,tiles[emyb][emxb])
  151.   end
  152.   return bool
  153. --  emptyx,emptyy = emptyy,emptyx
  154. end
  155. function testfield()
  156.   t = true
  157.   for i=1,sizey do
  158.     for j=1,sizex do
  159.       if tiles[i][j] ~= ((i-1)*sizex+j)%(sizex*sizey) then--suctiles[i][j] then
  160. --        print(tiles[i][j],(i-1)*sizex+j,i,j)
  161.         t = false
  162.       end
  163.     end
  164.   end
  165.   return t
  166. end
  167. --os.sleep(1)
  168. -------------------------------- genfield ----------------------------------------
  169. local r2 = 0
  170. for i=1,dif do
  171.   r = math.random(1,4)
  172.   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
  173.     r = math.random(1,4)
  174.   end
  175.   r2 = r
  176.   x = emptyx
  177.   y = emptyy
  178.   if r == 1 then
  179.     x = emptyx-1
  180.   elseif r == 2 then
  181.     y = emptyy-1
  182.   elseif r == 3 then
  183.     x = emptyx+1
  184.   elseif r == 4 then
  185.     y = emptyy+1
  186.   end
  187.   tiles[y][x],tiles[emptyy][emptyx] = 0,tiles[y][x]
  188.   emptyx = x
  189.   emptyy = y
  190. end
  191. ---------------------------------- play ------------------------------------------
  192. st = true
  193. local dir=-1
  194. drawfield(1,5,true)
  195. while st do
  196.   st = false
  197.   drawfield(1,5)
  198.   _,_,x,y = event.pull("touch")
  199.   if (x>=1 and y>=5 and x<=tilew*sizex and y<5+tileh*sizey) then
  200.     if usecell(math.floor((x-1)/tilew)+1,math.floor((y-5)/tileh)+1) then
  201.       steps = steps + 1
  202.     end
  203.     st = true
  204.     if testfield() then
  205.       drawfield(1,5)
  206.       print("You win!")
  207.       st = false
  208.     end
  209.   end
  210. end
  211. gpu.setBackground(0x000000)
  212. gpu.setForeground(0xffffff)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement