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