ElijahCrafter

Minesweeper

Jul 12th, 2026
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.41 KB | Source Code | 0 0
  1. -- Minesweeper for CC: Tweaked
  2. -- Click to reveal cells, right-click to flag
  3.  
  4. local WIDTH = 10
  5. local HEIGHT = 10
  6. local MINES = 10
  7.  
  8. local grid = {}
  9. local revealed = {}
  10. local flagged = {}
  11. local gameOver = false
  12. local won = false
  13.  
  14. local function initGame()
  15.   for y = 1, HEIGHT do
  16.     grid[y] = {}
  17.     revealed[y] = {}
  18.     flagged[y] = {}
  19.     for x = 1, WIDTH do
  20.       grid[y][x] = 0
  21.       revealed[y][x] = false
  22.       flagged[y][x] = false
  23.     end
  24.   end
  25.  
  26.   local minesPlaced = 0
  27.   while minesPlaced < MINES do
  28.     local x = math.random(1, WIDTH)
  29.     local y = math.random(1, HEIGHT)
  30.     if grid[y][x] ~= 9 then
  31.       grid[y][x] = 9
  32.       minesPlaced = minesPlaced + 1
  33.     end
  34.   end
  35.  
  36.   for y = 1, HEIGHT do
  37.     for x = 1, WIDTH do
  38.       if grid[y][x] ~= 9 then
  39.         local count = 0
  40.         for dy = -1, 1 do
  41.           for dx = -1, 1 do
  42.             local ny, nx = y + dy, x + dx
  43.             if ny >= 1 and ny <= HEIGHT and nx >= 1 and nx <= WIDTH and grid[ny][nx] == 9 then
  44.               count = count + 1
  45.             end
  46.           end
  47.         end
  48.         grid[y][x] = count
  49.       end
  50.     end
  51.   end
  52. end
  53.  
  54. local function getAdjacentCells(x, y)
  55.   local cells = {}
  56.   for dy = -1, 1 do
  57.     for dx = -1, 1 do
  58.       if dx ~= 0 or dy ~= 0 then
  59.         local nx, ny = x + dx, y + dy
  60.         if nx >= 1 and nx <= WIDTH and ny >= 1 and ny <= HEIGHT then
  61.           table.insert(cells, {nx, ny})
  62.         end
  63.       end
  64.     end
  65.   end
  66.   return cells
  67. end
  68.  
  69. local function reveal(x, y)
  70.   if x < 1 or x > WIDTH or y < 1 or y > HEIGHT or revealed[y][x] or flagged[y][x] then
  71.     return
  72.   end
  73.  
  74.   revealed[y][x] = true
  75.  
  76.   if grid[y][x] == 9 then
  77.     gameOver = true
  78.     return
  79.   end
  80.  
  81.   if grid[y][x] == 0 then
  82.     for _, cell in ipairs(getAdjacentCells(x, y)) do
  83.       reveal(cell[1], cell[2])
  84.     end
  85.   end
  86. end
  87.  
  88. local function toggleFlag(x, y)
  89.   if not revealed[y][x] then
  90.     flagged[y][x] = not flagged[y][x]
  91.   end
  92. end
  93.  
  94. local function drawBoard()
  95.   term.clear()
  96.   term.setCursorPos(1, 1)
  97.   print("=== MINESWEEPER ===")
  98.   print()
  99.  
  100.   for y = 1, HEIGHT do
  101.     for x = 1, WIDTH do
  102.       if not revealed[y][x] then
  103.         if flagged[y][x] then
  104.           term.setTextColor(colors.red)
  105.           write("F")
  106.         else
  107.           term.setTextColor(colors.white)
  108.           write("?")
  109.         end
  110.       elseif grid[y][x] == 9 then
  111.         term.setTextColor(colors.red)
  112.         write("*")
  113.       else
  114.         term.setTextColor(colors.lime)
  115.         write(grid[y][x])
  116.       end
  117.       term.setTextColor(colors.white)
  118.     end
  119.     print()
  120.   end
  121.   print()
  122. end
  123.  
  124. local function checkWin()
  125.   local revealedCount = 0
  126.   for y = 1, HEIGHT do
  127.     for x = 1, WIDTH do
  128.       if revealed[y][x] then
  129.         revealedCount = revealedCount + 1
  130.       end
  131.     end
  132.   end
  133.   return revealedCount == (WIDTH * HEIGHT - MINES)
  134. end
  135.  
  136. local function run()
  137.   initGame()
  138.  
  139.   while not gameOver and not won do
  140.     drawBoard()
  141.  
  142.     local event, button, x, y = os.pullEvent("mouse_click")
  143.     if button == 1 then
  144.       reveal(x, y - 2)
  145.     elseif button == 2 then
  146.       toggleFlag(x, y - 2)
  147.     end
  148.  
  149.     if checkWin() then
  150.       won = true
  151.     end
  152.   end
  153.  
  154.   drawBoard()
  155.   if gameOver then
  156.     print("GAME OVER - You hit a mine!")
  157.     for y = 1, HEIGHT do
  158.       for x = 1, WIDTH do
  159.         revealed[y][x] = true
  160.       end
  161.     end
  162.     drawBoard()
  163.   else
  164.     print("YOU WIN!")
  165.   end
  166. end
  167.  
  168. run()
Advertisement
Add Comment
Please, Sign In to add comment