Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- โค้ดต้นฉบับภาษา Picolisp จาก https://rosettacode.org/wiki/Minesweeper_game#PicoLisp
- โค้ดภาษา Lua เริ่มบรรทัดที่ 65 มีดัดแปลงเอาวิธีจากตัวอย่างแบบของ Julia มาผสม
- # NIL Hidden: Empty field
- # T Hidden: Mine
- # 0-8 Marked: Empty field
- # ? Marked: Mine
- (de minesweeper (DX DY Density)
- (default Density 20)
- (setq *Field (make (do DY (link (need DX)))))
- (use (X Y)
- (do (prinl "Number of mines: " (*/ DX DY Density 100))
- (while
- (get *Field
- (setq Y (rand 1 DY))
- (setq X (rand 1 DX)) ) )
- (set (nth *Field Y X) T) ) )
- (showMines) )
- (de showMines ()
- (for L *Field
- (for F L
- (prin (if (flg? F) "." F)) )
- (prinl) ) )
- (de *NeighborX -1 0 +1 -1 +1 -1 0 +1)
- (de *NeighborY -1 -1 -1 0 0 +1 +1 +1)
- (de c (X Y)
- (if (=T (get *Field Y X))
- "KLABOOM!! You hit a mine."
- (let Visit NIL
- (recur (X Y)
- (when
- (=0
- (set (nth *Field Y X)
- (cnt
- '((DX DY)
- (=T (get *Field (+ Y DY) (+ X DX))) )
- *NeighborX
- *NeighborY ) ) )
- (mapc
- '((DX DY)
- (and
- (get *Field (inc 'DY Y))
- (nth @ (inc 'DX X))
- (not (member (cons DX DY) Visit))
- (push 'Visit (cons DX DY))
- (recurse DX DY) ) )
- *NeighborX
- *NeighborY ) ) ) )
- (showMines) ) )
- (de m (X Y)
- (set (nth *Field Y X) '?)
- (showMines)
- (unless (fish =T *Field)
- "Congratulations! You won!!" ) )
- ]]
- -- แปลงเป็น Lua ดัดแปลงเพิ่มเติมจากโค้ด Julia ด้วย
- local Field, Visit, Mark = {}, {}, {}
- local Mines = {}
- local Neighbor = {
- {-1,-1}, {-1,0}, {-1,1},
- {0,-1}, {0,1},
- {1,-1}, {1,0}, {1,1}
- }
- local total_mines, mines
- local visited = 0
- local end_game = false
- function init_mines(dx, dy)
- for y=1, dy do
- Field[y] = {}
- Visit[y] = {}
- Mark[y] = {}
- for x=1, dx do
- Field[y][x] = false
- Visit[y][x] = false
- Mark[y][x] = false
- end
- end
- total_mines = ("%.f"):format(dx * dy * 0.15)
- mines = tonumber(total_mines)
- for i=1, mines do
- local x = math.random(1, dx)
- local y = math.random(1, dy)
- while Field[y][x] do
- x = math.random(1, dx)
- y = math.random(1, dy)
- end
- Field[y][x] = true
- end
- show_mines()
- end
- function show_mines()
- print("Number of mines: "..mines .."/".. total_mines .."\n")
- io.write(" ")
- for i=1, #Field[1] do
- io.write(i .." ")
- end
- print("\n"..("-"):rep(#Field[1]*3+2))
- for y, row in ipairs(Field) do
- io.write(y .."| ")
- for x, pos in ipairs(row) do
- if end_game then
- io.write(pos==true and "*" or (pos==false and ".") or pos)
- else
- io.write(Mark[y][x] and Mark[y][x] or (type(pos)=="boolean" and "." or pos))
- end
- io.write " "
- end
- print()
- end
- print()
- end
- function Mines.o(x, y)
- if Field[y][x]==true then
- print "KLABOOM!! You hit a mine."
- end_game = true
- else
- visited = visited + 1
- function o(x, y)
- local cnt = 0
- for _, v in ipairs(Neighbor) do
- if Field[y+v[1]] and Field[y+v[1]][x+v[2]]==true then
- cnt = cnt + 1
- end
- end
- Field[y][x] = cnt>0 and cnt or " "
- if Field[y][x]==" " then
- for _, v in ipairs(Neighbor) do
- local dx, dy = x+v[2], y+v[1]
- if Field[dy] and Field[dy][dx]==false then
- if not Visit[dy][dx] then
- Visit[dy][dx] = true
- visited = visited + 1
- end
- o(dx, dy)
- end
- end
- end
- end
- o(x, y)
- end
- show_mines()
- end
- function Mines.m(x, y)
- local tmp = Mark[y][x]
- if not tmp then
- Mark[y][x] = "?"
- visited = visited + 1
- mines = mines - 1
- elseif tmp=="?" then
- Mark[y][x] = false
- visited = visited - 1
- mines = mines + 1
- end
- show_mines()
- end
- io.write("Enter grid size X Y: ")
- local col, row = io.read("n", "n")
- init_mines(col, row)
- local fn, x, y
- while true do
- io.write("Enter command\n 'o x y' for open at position x,y\n 'm x y' for mark at position x,y\n 'c' for close\n: ")
- while true do
- local cmd = io.read()
- if cmd=='c' then
- print("You closed the game.")
- os.exit()
- end
- fn, x, y = cmd:match("([om])%s(%d)%s(%d)")
- x, y = math.tointeger(x), math.tointeger(y)
- if Mines[fn]
- and 0 < x and x <= col
- and 0 < y and y <= row
- then
- break
- end
- end
- Mines[fn](x, y)
- print(visited, col*row-mines)
- if visited >= (col*row)-mines then
- print "Congratulations! You won!!"
- break
- elseif end_game then
- break
- end
- end
Add Comment
Please, Sign In to add comment