Advertisement
astral17

minesweeper

Nov 4th, 2016
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.46 KB | None | 0 0
  1. -- ДОБАВИТЬ ТАБЛУ РЕКОРДОВ ; ДОБАВИТЬ КНОПКИ СЛОЖНОСТИ И РЕСТАРТА
  2. local event=require("event")
  3. local component=require("component")
  4. local gpu=component.gpu
  5. -----------------SETTINGS-------------------------
  6. local size=30
  7. local MINES=100
  8. -----------------------------------------------------------------------------------------------------------------
  9. local SAVEW,SAVEH=gpu.getResolution()
  10. gpu.setResolution(30*2,30+2)
  11. timerID=-1
  12. timeC={year=2000,month=0,day=0,min=0,sec=0}
  13. timeS=os.time(timeC)
  14. time=timeS
  15. tir=false
  16. function onTick()
  17.   time=time+1
  18.   drawTimer(size*2-4,1)
  19.   if time-timeS == 59*61 then
  20.     stopTimer()
  21.   end
  22. end
  23.  
  24. function startTimer()
  25.   time=timeS
  26.   timerID=event.timer(1,onTick,math.huge)
  27. end
  28.  
  29. function stopTimer()
  30.   event.cancel(timerID)
  31. end
  32.  
  33. function drawTimer(x,y)
  34.   gpu.setBackground(0x333333)
  35.   gpu.set(x,y,os.date("%M:%S",time))
  36.   gpu.setBackground(0x999999)
  37. end
  38.  
  39. -----------------------------------------------------------------------------------------------------------------
  40. function centerText(x,y,w,text)
  41.   gpu.set(x+math.floor(w/2-string.len(text)/2),y,text)
  42. end
  43.  
  44. function messageBox(title,text,color)
  45.   local x,y=size-7,math.floor(size/2)-3
  46.   local len1,len2=string.len(title),string.len(text)
  47.   local len3=math.max(len1,len2)+2
  48.   gpu.setBackground(0xffffff)
  49.   gpu.fill(x,y,len3,2+3," ")
  50.   gpu.setForeground(color or 0xFF0000)
  51.   centerText(x,y+1,len3,title)
  52.   gpu.setForeground(0x000000)
  53.   centerText(x,y+3,len3,text)
  54.   gpu.setBackground(color or 0xFF0000)
  55.   gpu.setForeground(0xffffff)
  56.   gpu.fill(x,y+5,len3,3," ")
  57.   centerText(x,y+6,len3,"OK!")
  58. end
  59. -----------------------------------------------------------------------------------------------------------------
  60. local sym="☢"
  61. local cnum = {0x0000FF,0x00FF00,0xFF0000,0x000055,0x550000,0x005500,0xFF00FF,0x000000}
  62.  
  63. -----------------------------------------------------------------------------------------------------------------
  64. --[[size = 10
  65.   tmp = 1
  66.  
  67.   size = 30
  68.   tmp = 10
  69.  
  70.   size = 40
  71.   tmp = 15--]]
  72. --end
  73. if dif == "легко" then
  74.     mines = size+math.floor(size/7)*tmp
  75. elseif dif == "средне" then
  76.   mines = 2*size+math.floor(size/6)*tmp
  77. elseif dif == "сложно" then
  78.   mines = 3*size+math.floor(size/5)*tmp
  79. end--]]
  80. mines=MINES
  81. flags = mines
  82. openfield = 0
  83. --------------------------------------- генерирование поля -----------------------------------------------
  84. gpu.setBackground(0x333333)
  85. gpu.fill(1,1,size*2,2," ")
  86. local field = {}
  87. for i = 0,size+1 do
  88.   field[i] = {}
  89.   for j = 0,size+1 do
  90.     field[i][j] = {}
  91.     field[i][j][0] = 0  -- -1 - мина, 1-8 кол-во мин вокруг
  92.     field[i][j][1] = 0  -- 0 - закрыто, 1 - открыто, 2 - флажок
  93.   end
  94. end
  95. gpu.setBackground(0x666666)
  96. gpu.fill(1,3,size*2,size," ")
  97. for i = 1,mines do
  98.   x = math.random(1,size)
  99.   y = math.random(1,size)
  100.   while field[x][y][0] == -1 do
  101.     x = math.random(1,size)
  102.     y = math.random(1,size)
  103.   end
  104.   field[x][y][0] = -1
  105. --      gpu.set(x*2+1,y+2,"#")
  106. end
  107. local tmp = 0
  108. for x = 1,size do
  109.   for y = 1, size do
  110. --      print(field[x][y][0])
  111.     if field[x][y][0] ~= -1 then
  112.       tmp = 0
  113.       if field[x-1][y-1][0] == -1 then
  114.         tmp = tmp + 1
  115.       end
  116.       if field[x-1][y][0] == -1 then
  117.         tmp = tmp + 1
  118.       end
  119.       if field[x-1][y+1][0] == -1 then
  120.         tmp = tmp + 1
  121.       end
  122.       if field[x][y-1][0] == -1 then
  123.         tmp = tmp + 1
  124.       end
  125.       if field[x][y+1][0] == -1 then
  126.         tmp = tmp + 1
  127.       end
  128.       if field[x+1][y-1][0] == -1 then
  129.         tmp = tmp + 1
  130.       end
  131.       if field[x+1][y][0] == -1 then
  132.         tmp = tmp + 1
  133.       end
  134.       if field[x+1][y+1][0] == -1 then
  135.         tmp = tmp + 1
  136.       end
  137.       field[x][y][0] = tmp
  138.       tmp2 = tostring(tmp)
  139. --        gpu.set(x*2,2+y,tmp2)
  140. --        gpu.set(1+x*2,2+y,tmp2)
  141. --        os.sleep(0.1)
  142.     end
  143.   end
  144. end
  145. gpu.setBackground(0x999999)
  146. gpu.setForeground(0xFFFFFF)
  147. --------------------------------------- обработка событий ------------------------------------------------
  148. function usecell(x,y,type)
  149.   if gamestate==false then
  150.     return
  151.   end
  152.   if type == 0 then
  153.     if (field[x][y][0] == -1)and(field[x][y][1] ~= 2) then
  154.       for i = 1,size do
  155.         for j = 1,size do
  156.           if field[i][j][0]==-1 then
  157.             gpu.setBackground((field[i][j][1]==2 and 0xFFFF00)or 0x666666)
  158.             gpu.setForeground(0xFF0000)
  159.             gpu.set(i*2-1,2+j,sym)
  160.           end
  161.         end
  162.       end
  163. --[[      ecs.universalWindow("auto","auto",30,0xDDDDDD,true,
  164.         {"EmptyLine"},
  165.         {"CenterText",0x111111,"вы проиграли"},
  166.         {"EmptyLine"},
  167.         {"button",{ecs.colors.red,0xFFFFFF,"OK"}}
  168.       )--]]
  169. --      print("you lose")
  170.       messageBox("GAME OVER","You lose")
  171.       gamestate = false
  172.     elseif field[x][y][1] == 0 then
  173.       if field[x][y][0] == 0 then
  174.         if (x~=0)and(x~=size+1)and(y~=0)and(y~=size+1) then
  175.           field[x][y][1] = 1
  176.           openfield = openfield + 1
  177.           usecell(x-1,y-1,0)
  178.           usecell(x-1,y,0)
  179.           usecell(x-1,y+1,0)
  180.           usecell(x,y-1,0)
  181.           usecell(x,y+1,0)
  182.           usecell(x+1,y-1,0)
  183.           usecell(x+1,y,0)
  184.           usecell(x+1,y+1,0)
  185.           gpu.set(x*2-1,2+y,"  ")
  186.         end
  187.       else
  188.         gpu.setForeground(cnum[field[x][y][0]])
  189.         gpu.set(x*2-1,2+y,tostring("."..field[x][y][0]))
  190.         gpu.setForeground(0xFFFFFF)
  191.         field[x][y][1] = 1
  192.         openfield = openfield + 1
  193.       end
  194.     end
  195.   elseif type == 1 then
  196.     if field[x][y][1] == 0 then
  197.       if (flags > 0) then
  198.         gpu.setBackground(0xFFFF00)
  199.         flags = flags - 1
  200.         field[x][y][1] = 2
  201.         gpu.set(x*2-1,2+y,"  ")
  202.         gpu.setBackground(0x999999)
  203.       end
  204.     elseif field[x][y][1] == 1 then
  205.       tmp = 0
  206.       if field[x-1][y-1][1] == 2 then
  207.         tmp = tmp + 1
  208.       end
  209.       if field[x-1][y][1] == 2 then
  210.         tmp = tmp + 1
  211.       end
  212.       if field[x-1][y+1][1] == 2 then
  213.         tmp = tmp + 1
  214.       end
  215.       if field[x][y-1][1] == 2 then
  216.         tmp = tmp + 1
  217.       end
  218.       if field[x][y+1][1] == 2 then
  219.         tmp = tmp + 1
  220.       end
  221.       if field[x+1][y-1][1] == 2 then
  222.         tmp = tmp + 1
  223.       end
  224.       if field[x+1][y][1] == 2 then
  225.         tmp = tmp + 1
  226.       end
  227.       if field[x+1][y+1][1] == 2 then
  228.         tmp = tmp + 1
  229.       end
  230.       if tmp == field[x][y][0] then
  231. ----
  232.         if field[x-1][y-1][1] == 0 then
  233.           usecell(x-1,y-1,0)
  234.         end
  235.         if field[x-1][y][1] == 0 then
  236.           usecell(x-1,y,0)
  237.         end
  238.         if field[x-1][y+1][1] == 0 then
  239.           usecell(x-1,y+1,0)
  240.         end
  241.         if field[x][y-1][1] == 0 then
  242.           usecell(x,y-1,0)
  243.         end
  244.         if field[x][y+1][1] == 0 then
  245.           usecell(x,y+1,0)
  246.         end
  247.         if field[x+1][y-1][1] == 0 then
  248.           usecell(x+1,y-1,0)
  249.         end
  250.         if field[x+1][y][1] == 0 then
  251.           usecell(x+1,y,0)
  252.         end
  253.         if field[x+1][y+1][1] == 0 then
  254.           usecell(x+1,y+1,0)
  255.         end
  256. ----
  257.       end
  258.     else
  259.       gpu.setBackground(0x666666)
  260. --      gpu.setBack
  261.       flags = flags + 1
  262.       field[x][y][1] = 0
  263.       gpu.set(x*2-1,2+y,"  ")
  264.       gpu.setBackground(0x999999)
  265.     end
  266.   end
  267. end
  268. gpu.setBackground(0x333333) -- Парочка костылей
  269. gpu.set(size*2-4,1,"00:00")
  270. gpu.set(1,1,((flags<100 and "0")or "")..((flags<10 and "0")or "")..flags..sym)
  271. gamestate = true
  272. while gamestate do
  273.   _,_,x,y,type = event.pull("touch")
  274.   if not tir then
  275.     startTimer()
  276.     tir=true
  277.   end
  278.   x = math.floor((x+1)/2)
  279.   y = y-2
  280.   if (x>0)and(x<size+1)and(y>0)and(y<size+1) then
  281.     gpu.setBackground(0x999999)
  282.     usecell(x,y,type)
  283.   end
  284.   win = false
  285.   if openfield >= size*size-mines then
  286.     win = true
  287.   end
  288.   if flags == 0 then
  289.     win = true
  290.     for i = 1,size do
  291.       for j = 1,size do
  292.         if ((field[i][j][0] == -1)and(field[i][j][1]==2))or((field[i][j][0] ~= -1)and(field[i][j][1]==1)) then
  293.         else
  294.           win = false
  295.         end
  296.       end
  297.     end
  298.   end
  299.   if win then
  300.     gamestate = false
  301.     messageBox("congratulation","you win",0x00CC00)
  302.   end
  303.   gpu.setBackground(0x333333)
  304.   gpu.setForeground(0xffffff)
  305.   gpu.set(1,1,((flags<100 and "0")or "")..((flags<10 and "0")or "")..flags..sym)
  306. end
  307. stopTimer()
  308. gpu.setBackground(0x000000)
  309. gpu.setForeground(0xffffff)
  310. gpu.setResolution(SAVEW,SAVEH)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement