Guest User

ms

a guest
Jan 12th, 2014
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.34 KB | None | 0 0
  1. local DIFFICULTY = 0.13
  2. -- Change value between 0..1.
  3. -- 1 would be field full of mines, and 0 empty
  4. -- or provide it with launch parameter
  5.  
  6. local mList = {"top","right","left","bottom","back","front"}
  7. local mon = peripheral.wrap(mList[1])
  8. local i=2
  9. -- Try all sides of computer to find
  10. -- where monitor is
  11. while (mon == nil) and (i <= #mList) do
  12.   mon = peripheral.wrap(mList[i])
  13.   i = i+1
  14. end
  15. if mon == nil then
  16.   print("You need to attach some monitors")
  17.   return
  18. end
  19. mon.setTextScale(1.5)
  20. local w, h = mon.getSize()
  21. local arg = {...}
  22. if #arg > 0 then
  23.   DIFFICULTY = 0+arg[1]
  24. end
  25. if DIFFICULTY > 0.99 then
  26.   DIFFICULTY = 0.99
  27. elseif DIFFICULTY < 1.5/(w*h) then
  28.   DIFFICULTY = 1.5/(w*h)
  29. end
  30. math.randomseed(os.time())
  31.  
  32. function time(s)
  33.   return string.format("%ds",s)
  34. end
  35.  
  36. function game()
  37.   local g = {}
  38.   local b
  39.   local mines = 0
  40.   local clears = 0
  41.   local start = os.clock()
  42.   mon.setTextColor(colors.white)
  43.  
  44.   function stepOn(x, y)
  45.     if g[x][y] ~= 1 then
  46.       return
  47.     end
  48.     local sum = 0
  49.     clears = clears-1
  50.     g[x][y] = 0
  51.     for i=math.max(1,x-1),math.min(w,x+1) do
  52.       for j=math.max(1,y-1),math.min(h,y+1) do
  53.         if math.abs(g[i][j]) == 2 then
  54.           sum = sum+1
  55.         end
  56.       end
  57.     end
  58.     mon.setCursorPos(x, y)
  59.     if sum > 0 then
  60.       local color
  61.       if sum == 1 then
  62.         color = colors.green
  63.       elseif sum == 2 then
  64.         color = colors.yellow
  65.       else
  66.         color = colors.red
  67.       end
  68.       mon.setTextColor(color)
  69.       mon.write(string.format("%d",sum))
  70.     else
  71.       mon.write(" ")
  72.       for i=math.max(1,x-1),math.min(w,x+1) do
  73.         for j=math.max(1,y-1),math.min(h,y+1) do
  74.           if (i ~= x) or (j ~= y) then
  75.             stepOn(i, j)
  76.           end
  77.         end
  78.       end
  79.     end
  80.   end
  81.  
  82.   mon.setBackgroundColor(colors.lightGray)
  83.   mon.clear()
  84.   for i=1,w do
  85.     g[i] = {}
  86.     for j=1,h do
  87.       mon.setCursorPos(i,j)
  88.       g[i][j] = 1
  89.       mon.write("#")
  90.     end
  91.   end
  92.   mines = math.floor(w*h*DIFFICULTY)
  93.   clears = w*h-mines
  94.   for n=1,mines do
  95.     repeat
  96.       i = math.floor(math.random()*w)+1
  97.       j = math.floor(math.random()*h)+1
  98.     until (g[i][j] == 1)
  99.     g[i][j] = 2
  100.   end
  101.   mon.setBackgroundColor(colors.black)
  102.  
  103.   -- Game loop
  104.   while clears > 0 do
  105.     local event, side, x, y = os.pullEvent("monitor_touch")
  106.     if event ~= nil then
  107.       mon.setCursorPos(x,y)
  108.       if g[x][y] == 2 then
  109.         mon.setTextColor(colors.red)
  110.         mon.write("X")
  111.         print("You hit a mine, HAHAHA!")
  112.         print("There were "..clears.." empty spots left")
  113.         return
  114.       else
  115.         stepOn(x, y)
  116.       end
  117.     end
  118.    
  119.     sleep(0)
  120.   end
  121.   start = os.clock()-start
  122.   mon.setTextColor(colors.green)
  123.   print("Well done!")
  124.   print("Time: "..time(start))
  125.   for i=1,w do
  126.     for j=1,h do
  127.       mon.setCursorPos(i, j)
  128.       if g[i][j] == 2 then
  129.         mon.write("O")
  130.       else
  131.         mon.write(" ")
  132.       end
  133.     end
  134.   end
  135.   mon.setTextColor(colors.yellow)
  136.   mon.setCursorPos(w/2-4, h/2+1)
  137.   mon.write(" Well done! ")
  138.   mon.setCursorPos(1, 1)
  139.   mon.write("Time: "..time(start))
  140. end
  141.  
  142. -- Program starts
  143. print("Hold Ctrl+T to terminate if stuck\n")
  144. local k
  145. while true do
  146.   game()
  147.   print("\nNew game? [y/n]: ")
  148.   k = read()
  149.   if k ~= "y" then
  150.     break
  151.   end
  152. end
  153. print("Bye bye...")
Advertisement
Add Comment
Please, Sign In to add comment