Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- โค้ดต้นฉบับภาษา Julia จาก https://rosettacode.org/wiki/Minesweeper_game#Julia
- โค้ด Lua เริ่มที่บรรทัด 125
- mutable struct Field
- size::Tuple{Int, Int}
- numbers::Array{Int, 2}
- possible_mines::Array{Bool, 2}
- actual_mines::Array{Bool, 2}
- visible::Array{Bool, 2}
- end
- function Field(x, y)
- size = (x, y)
- actual_mines = convert(Array{Bool, 2}, rand(x, y) .< 0.15)
- possible_mines = zeros(Bool, x, y)
- numbers = zeros(Int, x, y)
- visible = zeros(Bool, x, y)
- for i = 1:x
- for j = 1:y
- n = 0
- for di = -1:1
- for dj = -1:1
- n += (0 < di+i <= x && 0 < dj+j <= y) ?
- (actual_mines[di+i, dj+j] ? 1 : 0) : 0
- end
- end
- numbers[i, j] = n
- end
- end
- return Field(size, numbers, possible_mines, actual_mines, visible)
- end
- function printfield(f::Field; showall = false)
- spaces = Int(floor(log(10, f.size[2])))
- str = " "^(4+spaces)
- for i in 1:f.size[1]
- str *= string(" ", i, " ")
- end
- str *= "\n" * " "^(4+spaces) * "___"^f.size[1] * "\n"
- for j = 1:f.size[2]
- str *= " " * string(j) * " "^(floor(log(10, j)) > 0 ? 1 : spaces+1) * "|"
- for i = 1:f.size[1]
- if showall
- str *= f.actual_mines[i, j] ? " * " : " "
- else
- if f.visible[i, j]
- str *= " " * string(f.numbers[i, j] > 0 ? f.numbers[i, j] : " ") * " "
- elseif f.possible_mines[i, j]
- str *= " ? "
- else
- str *= " . "
- end
- end
- end
- str *= "\r\n"
- end
- println("Found " * string(length(f.possible_mines[f.possible_mines.==true])) *
- " of " * string(length(f.actual_mines[f.actual_mines.==true])) * " mines.\n")
- print(str)
- end
- function parse_input(str::String)
- input = split(chomp(str), " ")
- mode = input[1]
- println(str)
- coords = length(input) > 1 ? (parse(Int,input[2]), parse(Int,input[3])) : (0, 0)
- return mode, coords
- end
- function eval_input(f::Field, str::String)
- mode, coords = parse_input(str)
- (coords[1] > f.size[1] || coords[2] > f.size[2]) && return true
- if mode == "o"
- reveal(f, coords...) || return false
- elseif mode == "m" && f.visible[coords...] == false
- f.possible_mines[coords...] = !f.possible_mines[coords...]
- elseif mode == "close"
- error("You closed the game.")
- end
- return true
- end
- function reveal(f::Field, x::Int, y::Int)
- (x > f.size[1] || y > f.size[2]) && return true # check for index out of bounds
- f.actual_mines[x, y] && return false # check for mines
- f.visible[x, y] = true
- if f.numbers[x, y] == 0
- for di = -1:1
- for dj = -1:1
- if (0 < di+x <= f.size[1] && 0 < dj+y <= f.size[2]) &&
- f.actual_mines[x+di, y+dj] == false &&
- f.visible[x+di, y+dj] == false
- reveal(f, x+di, y+dj)
- end
- end
- end
- end
- return true
- end
- function play()
- print("\nWelcome to Minesweeper\n\nEnter the gridsize x y:\n")
- s = split(readline(), " ")
- f = Field(parse.(Int,s)...)
- won = false
- while true
- printfield(f)
- print("\nWhat do you do? (\"o x y\" to reveal a field; \"m x y\" to toggle a mine; close)\n")
- eval_input(f, readline()) || break
- print("_"^80 * "\n")
- (f.actual_mines == f.possible_mines ||
- f.visible == .!f.actual_mines) && (won = true; break)
- end
- println(won ? "You won the game!" : "You lost the game:\n")
- printfield(f, showall = true)
- end
- play()
- ]]
- -- แปลงเป็น Lua
- math.randomseed(os.time())
- function Field(x, y)
- size = {x, y}
- actual_mines = {}
- possible_mines = {}
- numbers = {}
- visible = {}
- for r = 1, y do
- actual_mines[r] = {}
- possible_mines[r] = {}
- numbers[r] = {}
- visible[r] = {}
- for c = 1, x do
- --actual_mines[r][c] = math.random(x*y)<=(x*y/3) and true or false
- actual_mines[r][c] = false
- possible_mines[r][c] = false
- numbers[r][c] = 0
- visible[r][c] = false
- end
- end
- local mines = ("%.f"):format(x * y * 0.15)
- for i = 1, mines do
- local x = math.random(1, x)
- local y = math.random(1, y)
- while actual_mines[y][x] do
- x = math.random(1, x)
- y = math.random(1, y)
- end
- actual_mines[y][x] = true
- end
- for i = 1, x do
- for j = 1, y do
- local n = 0
- for di = -1, 1 do
- for dj = -1,1 do
- dii, djj = i + di, j + dj
- n = n + ((0 < dii and dii <= x and 0 < djj and djj <= y) and (actual_mines[djj][dii] and 1 or 0) or 0)
- end
- end
- numbers[j][i] = n
- end
- end
- print(is(numbers))
- print(is(actual_mines))
- return {
- size = size,
- numbers = numbers,
- possible_mines = possible_mines,
- actual_mines = actual_mines,
- visible = visible
- }
- end
- function printfield(field, showall)
- showall = showall or false
- local spaces = math.floor(math.log(field.size[2], 10))
- local str = {(" "):rep(4+spaces)}
- for i = 1, f.size[1] do
- str[#str+1] = " " .. i .. " "
- end
- str[#str+1] = "\n" .. (" "):rep(4+spaces) .. ("___"):rep(field.size[1]) .. "\n"
- for j = 1, f.size[2] do
- str[#str+1] = " " .. j .. (" "):rep((math.floor(math.log(j, 10)) > 0) and 1 or (spaces+1)) .. "|"
- for i = 1, f.size[1] do
- if showall then
- str[#str+1] = field.actual_mines[j][i] and " * " or " "
- else
- if field.visible[j][i] then
- str[#str+1] = " " .. (field.numbers[j][i] > 0 and field.numbers[j][i] or " ") .. " "
- elseif field.possible_mines[j][i] then
- str[#str+1] = " ? "
- else
- str[#str+1] = " . "
- end
- end
- end
- str[#str+1] = "\r\n"
- end
- local possible, actual = 0, 0
- for j = 1, field.size[2] do
- for i = 1, field.size[1] do
- possible = possible + (field.possible_mines[j][i] and 1 or 0)
- actual = actual + (field.actual_mines[j][i] and 1 or 0)
- end
- end
- print("Found " .. possible ..
- " of " .. actual .. " mines.\n")
- print(table.concat(str))
- end
- function parse_input(str)
- local input = {}
- if str:sub(-2)=="\n\r" then
- str = str:sub(1, -2)
- elseif ({["\n"]=true, ["\r"]=true})[str:sub(-1)] then
- str = str:sub(1, -1)
- end
- for s in str:gmatch("[^%s]+") do
- input[#input+1] = s
- end
- mode = input[1]
- print(str)
- coords = #input == 3 and {tonumber(input[2]) or 0, tonumber(input[3]) or 0} or {0, 0}
- return mode, coords
- end
- function eval_input(field, str)
- local mode, coords = parse_input(str)
- if coords[1] > field.size[1] or coords[2] > field.size[2] then
- return true
- end
- if mode == "o" then
- return reveal(field, coords[1], coords[2]) or false
- elseif mode == "m" and field.visible[coords[2]][coords[1]] == false then
- field.possible_mines[coords[2]][coords[1]] = not field.possible_mines[coords[2]][coords[1]]
- elseif mode == "c" then
- print("You closed the game.")
- os.exit()
- end
- return true
- end
- function reveal(field, x, y)
- if (x > field.size[1] or y > field.size[2]) then
- return true -- check for index out of bounds
- end
- if (x < 1 or y < 1) then
- return true -- check for index out of bounds
- end
- if field.actual_mines[y][x] then
- return false -- check for mines
- end
- field.visible[y][x] = true
- if field.numbers[y][x] == 0 then
- for di = -1, 1 do
- for dj = -1, 1 do
- local dix, djy = di+x, dj+y
- if (0 < dix and dix <= field.size[1] and 0 < djy and djy <= field.size[2]) and field.actual_mines[djy][dix] == false and f.visible[djy][dix] == false then
- reveal(f, dix, djy)
- end
- end
- end
- end
- return true
- end
- function play()
- print("\nWelcome to Minesweeper\n\nEnter the gridsize x y:")
- local x, y = io.read("n", "n")
- f = Field(x or 1, y or 1)
- local won = false
- while true do
- printfield(f)
- print("\nWhat do you do? (\"o x y\" to reveal a field; \"m x y\" to toggle a mine; \"c\" to close)")
- if not eval_input(f, io.read()) then
- break
- end
- print(("_"):rep(80))
- local comp = {true, true}
- for j = 1, y do
- for i = 1, x do
- if comp[1] and (f.actual_mines[j][i]~=f.possible_mines[j][i]) then
- comp[1] = false
- end
- if comp[2] and (f.visible[j][i]==f.actual_mines[j][i]) then
- comp[2] = false
- end
- if not (comp[1] or comp[2]) then
- break
- end
- end
- end
- if comp[1] or comp[2] then
- won = true
- break
- end
- end
- print(won and "You won the game!" or "You lost the game:\n")
- printfield(f, true)
- end
- play()
Add Comment
Please, Sign In to add comment