Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Minesweeper for CC: Tweaked
- -- Click to reveal cells, right-click to flag
- local WIDTH = 10
- local HEIGHT = 10
- local MINES = 10
- local grid = {}
- local revealed = {}
- local flagged = {}
- local gameOver = false
- local won = false
- local function initGame()
- for y = 1, HEIGHT do
- grid[y] = {}
- revealed[y] = {}
- flagged[y] = {}
- for x = 1, WIDTH do
- grid[y][x] = 0
- revealed[y][x] = false
- flagged[y][x] = false
- end
- end
- local minesPlaced = 0
- while minesPlaced < MINES do
- local x = math.random(1, WIDTH)
- local y = math.random(1, HEIGHT)
- if grid[y][x] ~= 9 then
- grid[y][x] = 9
- minesPlaced = minesPlaced + 1
- end
- end
- for y = 1, HEIGHT do
- for x = 1, WIDTH do
- if grid[y][x] ~= 9 then
- local count = 0
- for dy = -1, 1 do
- for dx = -1, 1 do
- local ny, nx = y + dy, x + dx
- if ny >= 1 and ny <= HEIGHT and nx >= 1 and nx <= WIDTH and grid[ny][nx] == 9 then
- count = count + 1
- end
- end
- end
- grid[y][x] = count
- end
- end
- end
- end
- local function getAdjacentCells(x, y)
- local cells = {}
- for dy = -1, 1 do
- for dx = -1, 1 do
- if dx ~= 0 or dy ~= 0 then
- local nx, ny = x + dx, y + dy
- if nx >= 1 and nx <= WIDTH and ny >= 1 and ny <= HEIGHT then
- table.insert(cells, {nx, ny})
- end
- end
- end
- end
- return cells
- end
- local function reveal(x, y)
- if x < 1 or x > WIDTH or y < 1 or y > HEIGHT or revealed[y][x] or flagged[y][x] then
- return
- end
- revealed[y][x] = true
- if grid[y][x] == 9 then
- gameOver = true
- return
- end
- if grid[y][x] == 0 then
- for _, cell in ipairs(getAdjacentCells(x, y)) do
- reveal(cell[1], cell[2])
- end
- end
- end
- local function toggleFlag(x, y)
- if not revealed[y][x] then
- flagged[y][x] = not flagged[y][x]
- end
- end
- local function drawBoard()
- term.clear()
- term.setCursorPos(1, 1)
- print("=== MINESWEEPER ===")
- print()
- for y = 1, HEIGHT do
- for x = 1, WIDTH do
- if not revealed[y][x] then
- if flagged[y][x] then
- term.setTextColor(colors.red)
- write("F")
- else
- term.setTextColor(colors.white)
- write("?")
- end
- elseif grid[y][x] == 9 then
- term.setTextColor(colors.red)
- write("*")
- else
- term.setTextColor(colors.lime)
- write(grid[y][x])
- end
- term.setTextColor(colors.white)
- end
- print()
- end
- print()
- end
- local function checkWin()
- local revealedCount = 0
- for y = 1, HEIGHT do
- for x = 1, WIDTH do
- if revealed[y][x] then
- revealedCount = revealedCount + 1
- end
- end
- end
- return revealedCount == (WIDTH * HEIGHT - MINES)
- end
- local function run()
- initGame()
- while not gameOver and not won do
- drawBoard()
- local event, button, x, y = os.pullEvent("mouse_click")
- if button == 1 then
- reveal(x, y - 2)
- elseif button == 2 then
- toggleFlag(x, y - 2)
- end
- if checkWin() then
- won = true
- end
- end
- drawBoard()
- if gameOver then
- print("GAME OVER - You hit a mine!")
- for y = 1, HEIGHT do
- for x = 1, WIDTH do
- revealed[y][x] = true
- end
- end
- drawBoard()
- else
- print("YOU WIN!")
- end
- end
- run()
Advertisement
Add Comment
Please, Sign In to add comment