Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local DIFFICULTY = 0.13
- -- Change value between 0..1.
- -- 1 would be field full of mines, and 0 empty
- -- or provide it with launch parameter
- local mList = {"top","right","left","bottom","back","front"}
- local mon = peripheral.wrap(mList[1])
- local i=2
- -- Try all sides of computer to find
- -- where monitor is
- while (mon == nil) and (i <= #mList) do
- mon = peripheral.wrap(mList[i])
- i = i+1
- end
- if mon == nil then
- print("You need to attach some monitors")
- return
- end
- mon.setTextScale(1.5)
- local w, h = mon.getSize()
- local arg = {...}
- if #arg > 0 then
- DIFFICULTY = 0+arg[1]
- end
- if DIFFICULTY > 0.99 then
- DIFFICULTY = 0.99
- elseif DIFFICULTY < 1.5/(w*h) then
- DIFFICULTY = 1.5/(w*h)
- end
- math.randomseed(os.time())
- function time(s)
- return string.format("%ds",s)
- end
- function game()
- local g = {}
- local b
- local mines = 0
- local clears = 0
- local start = os.clock()
- mon.setTextColor(colors.white)
- function stepOn(x, y)
- if g[x][y] ~= 1 then
- return
- end
- local sum = 0
- clears = clears-1
- g[x][y] = 0
- for i=math.max(1,x-1),math.min(w,x+1) do
- for j=math.max(1,y-1),math.min(h,y+1) do
- if math.abs(g[i][j]) == 2 then
- sum = sum+1
- end
- end
- end
- mon.setCursorPos(x, y)
- if sum > 0 then
- local color
- if sum == 1 then
- color = colors.green
- elseif sum == 2 then
- color = colors.yellow
- else
- color = colors.red
- end
- mon.setTextColor(color)
- mon.write(string.format("%d",sum))
- else
- mon.write(" ")
- for i=math.max(1,x-1),math.min(w,x+1) do
- for j=math.max(1,y-1),math.min(h,y+1) do
- if (i ~= x) or (j ~= y) then
- stepOn(i, j)
- end
- end
- end
- end
- end
- mon.setBackgroundColor(colors.lightGray)
- mon.clear()
- for i=1,w do
- g[i] = {}
- for j=1,h do
- mon.setCursorPos(i,j)
- g[i][j] = 1
- mon.write("#")
- end
- end
- mines = math.floor(w*h*DIFFICULTY)
- clears = w*h-mines
- for n=1,mines do
- repeat
- i = math.floor(math.random()*w)+1
- j = math.floor(math.random()*h)+1
- until (g[i][j] == 1)
- g[i][j] = 2
- end
- mon.setBackgroundColor(colors.black)
- -- Game loop
- while clears > 0 do
- local event, side, x, y = os.pullEvent("monitor_touch")
- if event ~= nil then
- mon.setCursorPos(x,y)
- if g[x][y] == 2 then
- mon.setTextColor(colors.red)
- mon.write("X")
- print("You hit a mine, HAHAHA!")
- print("There were "..clears.." empty spots left")
- return
- else
- stepOn(x, y)
- end
- end
- sleep(0)
- end
- start = os.clock()-start
- mon.setTextColor(colors.green)
- print("Well done!")
- print("Time: "..time(start))
- for i=1,w do
- for j=1,h do
- mon.setCursorPos(i, j)
- if g[i][j] == 2 then
- mon.write("O")
- else
- mon.write(" ")
- end
- end
- end
- mon.setTextColor(colors.yellow)
- mon.setCursorPos(w/2-4, h/2+1)
- mon.write(" Well done! ")
- mon.setCursorPos(1, 1)
- mon.write("Time: "..time(start))
- end
- -- Program starts
- print("Hold Ctrl+T to terminate if stuck\n")
- local k
- while true do
- game()
- print("\nNew game? [y/n]: ")
- k = read()
- if k ~= "y" then
- break
- end
- end
- print("Bye bye...")
Advertisement
Add Comment
Please, Sign In to add comment