Advertisement
qwertyMAN_rus

saper

Jan 25th, 2016 (edited)
876
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.51 KB | None | 0 0
  1. -- Автор: qwertyMAN
  2. -- Версия: 0.1 beta
  3.  
  4. local term              = require("term")
  5. local event             = require("event")
  6. local component         = require("component")
  7. local gpu               = component.gpu
  8. local max_mine          = 40                    -- число мин на поле
  9. local display           = {gpu.getResolution()}
  10. local border            = {0,0}                 -- отступ, который не используется
  11. local marker            = {}                    -- отмечает ПКМ мины
  12. local size              = {16,16}               -- размер игрового поля
  13. math.randomseed(os.time())
  14.  
  15. local colors={0x0000ff,0x00ff00,0xff0000,0xffff00,0x8800ff,0x88ff00,0x00ffff,0xff00ff}
  16.  
  17. local function conv_cord(sx,sy)
  18.     return sx*2-1+border[1], sy+border[2]
  19. end
  20.  
  21. -- создаем поле
  22. local area={}
  23. for x=1, size[1] do
  24.     area[x]={}
  25.     for y=1, size[2] do
  26.         area[x][y]={mine=false, n=0}
  27.     end
  28. end
  29.  
  30. -- генерируем мины
  31. for i=1, max_mine do
  32.     while true do
  33.         rand_x, rand_y = math.random(1, size[1]), math.random(1, size[2])
  34.         if not area[rand_x][rand_y].mine then
  35.             area[rand_x][rand_y].mine = true
  36.             break
  37.         end
  38.     end
  39. end
  40.  
  41. -- генерирем числа на пустых клетках
  42. for x=1, size[1] do
  43.     for y=1, size[2] do
  44.         if not area[x][y].mine then
  45.             for i=-1,1 do
  46.                 for j=-1,1 do
  47.                     if x+i>0 and y+j>0 and x+i<size[1]+1 and y+j<size[2]+1 and area[x+i][y+j].mine then
  48.                         area[x][y].n = area[x][y].n+1
  49.                     end
  50.                 end
  51.             end
  52.         end
  53.     end
  54. end
  55.  
  56. gpu.setBackground(0x000000)
  57. gpu.setForeground(0xffffff)
  58.  
  59. -- отображение пустого поля
  60. term.clear()
  61. gpu.setBackground(0xAAAAAA)
  62. for i=1, size[1] do
  63.     for j=1, size[2] do
  64.         local rezerv = {conv_cord(i,j)}
  65.         gpu.set(rezerv[1], rezerv[2], "  ")
  66.     end
  67. end
  68. gpu.setBackground(0x000000)
  69.  
  70. local function click(x,y)
  71.     local x = math.modf((x+1)/2)
  72.     return x,y
  73. end
  74.  
  75. local function open(x,y)
  76.     local not_sorting_tb        = {}
  77.     local sorting_tb            = {}
  78.     not_sorting_tb[1]       = {x,y}
  79.     gpu.setBackground(0xffffff)
  80.     local rezerv = {conv_cord(x,y)}
  81.     gpu.set(rezerv[1], rezerv[2], "  ")
  82.     while true do
  83.         -- выход из цикла
  84.         if #not_sorting_tb == 0 then
  85.             gpu.setBackground(0x000000)
  86.             gpu.setForeground(0xffffff)
  87.             return
  88.         else
  89.             local nx,ny = not_sorting_tb[1][1], not_sorting_tb[1][2]
  90.             -- смотрим какого вида ячейка
  91.             if area[nx][ny] then
  92.                 if area[nx][ny].n>0 then -- если не пустая
  93.                     gpu.setForeground(colors[area[nx][ny].n])
  94.                     local rezerv = {conv_cord(nx,ny)}
  95.                     gpu.set(rezerv[1], rezerv[2], " "..area[nx][ny].n)
  96.                 else
  97.                     -- если пустая
  98.                     for i=-1,1 do
  99.                         for j=-1,1 do
  100.                             local mx,my = nx+i, ny+j
  101.                             -- если ячейка существует
  102.                             if mx>=1 and my>=1 and mx<=size[1] and my<=size[2] then
  103.                                 local swich = true
  104.                                 -- проверяем есть ли она в бд
  105.                                 for n=1, #sorting_tb do
  106.                                     if mx==sorting_tb[n][1] and my==sorting_tb[n][2] then
  107.                                         swich = false
  108.                                     end
  109.                                 end
  110.                                 for n=1, #not_sorting_tb do
  111.                                     if mx==not_sorting_tb[n][1] and my==not_sorting_tb[n][2] then
  112.                                         swich = false
  113.                                     end
  114.                                 end
  115.                                 if swich then
  116.                                     local rezerv = {conv_cord(mx,my)}
  117.                                     gpu.set(rezerv[1], rezerv[2], "  ")
  118.                                     not_sorting_tb[#not_sorting_tb+1]={mx,my}
  119.                                 end
  120.                             end
  121.                         end
  122.                     end
  123.                 end
  124.                 area[nx][ny]=false
  125.             end
  126.             sorting_tb[#sorting_tb+1] = not_sorting_tb[1]
  127.             table.remove(not_sorting_tb,1)
  128.         end
  129.     end
  130. end
  131.  
  132. -- тело программы
  133. while true do
  134.     local _,_,x,y,key,nick = event.pull("touch")
  135.     local x,y = click(x,y)
  136.     if key == 0 then
  137.         local swich = true
  138.         for i=1, #marker do
  139.             if x == marker[i][1] and y == marker[i][2] then
  140.                 swich = false
  141.             end
  142.         end
  143.         if area[x][y] and area[x][y].mine and swich then
  144.             -- покажим мины
  145.             gpu.setBackground(0xff0000)
  146.             gpu.setForeground(0x000000)
  147.             for i=1, size[1] do
  148.                 for j=1, size[2] do
  149.                     if area[i][j] and area[i][j].mine then
  150.                         local rezerv = {conv_cord(i,j)}
  151.                         gpu.set(rezerv[1], rezerv[2], " m")
  152.                     end
  153.                 end
  154.             end
  155.             gpu.setBackground(0x000000)
  156.             gpu.setForeground(0xffffff)
  157.             os.sleep(2)
  158.             term.clear()
  159.             print("game over")
  160.             os.sleep(2)
  161.             term.clear()
  162.             return
  163.         elseif swich then
  164.             open(x,y)
  165.             -- проверяем выигрыш
  166.             local timer = 0
  167.             for i=1, size[1] do
  168.                 for j=1, size[2] do
  169.                     if area[i][j] then
  170.                         timer = timer+1
  171.                     end
  172.                 end
  173.             end
  174.             if timer==max_mine then
  175.                 -- покажим мины
  176.                 gpu.setBackground(0xff0000)
  177.                 gpu.setForeground(0x000000)
  178.                 for i=1, size[1] do
  179.                     for j=1, size[2] do
  180.                         if area[i][j] and area[i][j].mine then
  181.                             local rezerv = {conv_cord(i,j)}
  182.                             gpu.set(rezerv[1], rezerv[2], " m")
  183.                         end
  184.                     end
  185.                 end
  186.                 gpu.setBackground(0x000000)
  187.                 gpu.setForeground(0xffffff)
  188.                 -- поздравления
  189.                 os.sleep(2)
  190.                 term.clear()
  191.                 print("You win!")
  192.                 os.sleep(2)
  193.                 term.clear()
  194.                 return
  195.             end
  196.         end
  197.     elseif key == 1 then
  198.         local swich = true
  199.         for i=#marker, 1, -1  do
  200.             if x == marker[i][1] and y == marker[i][2] then
  201.                 table.remove(marker,i)
  202.                 gpu.setBackground(0xAAAAAA)
  203.                 local rezerv = {conv_cord(x,y)}
  204.                 gpu.set(rezerv[1], rezerv[2], "  ")
  205.                 swich = false
  206.             end
  207.         end
  208.         if swich and area[x][y] then
  209.             marker[#marker+1]={x,y}
  210.             gpu.setBackground(0xffaa00)
  211.             local rezerv = {conv_cord(x,y)}
  212.             gpu.set(rezerv[1], rezerv[2], "  ")
  213.         end
  214.         gpu.setBackground(0x000000)
  215.     end
  216. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement