Advertisement
qwertyMAN_rus

Saper

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