ElijahCrafter

Minesweeper

Nov 27th, 2025
764
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.69 KB | Source Code | 0 0
  1. -- Minesweeper Game for SimpleOS
  2. -- CC: Tweaked compatible
  3.  
  4. local w, h = term.getSize()
  5.  
  6. -- Game settings
  7. local gridWidth = 9
  8. local gridHeight = 9
  9. local mineCount = 10
  10. local startX = math.floor((w - gridWidth * 2) / 2)
  11. local startY = 3
  12.  
  13. -- Game state
  14. local grid = {}
  15. local revealed = {}
  16. local flagged = {}
  17. local gameOver = false
  18. local gameWon = false
  19. local cursorX, cursorY = 1, 1
  20. local firstClick = true
  21.  
  22. -- Initialize grid
  23. local function initGrid()
  24.     grid = {}
  25.     revealed = {}
  26.     flagged = {}
  27.     for y = 1, gridHeight do
  28.         grid[y] = {}
  29.         revealed[y] = {}
  30.         flagged[y] = {}
  31.         for x = 1, gridWidth do
  32.             grid[y][x] = 0
  33.             revealed[y][x] = false
  34.             flagged[y][x] = false
  35.         end
  36.     end
  37. end
  38.  
  39. -- Place mines (avoiding first click)
  40. local function placeMines(avoidX, avoidY)
  41.     local placed = 0
  42.     while placed < mineCount do
  43.         local x = math.random(1, gridWidth)
  44.         local y = math.random(1, gridHeight)
  45.         if grid[y][x] ~= -1 and not (x == avoidX and y == avoidY) then
  46.             grid[y][x] = -1
  47.             placed = placed + 1
  48.         end
  49.     end
  50.    
  51.     -- Calculate numbers
  52.     for y = 1, gridHeight do
  53.         for x = 1, gridWidth do
  54.             if grid[y][x] ~= -1 then
  55.                 local count = 0
  56.                 for dy = -1, 1 do
  57.                     for dx = -1, 1 do
  58.                         local nx, ny = x + dx, y + dy
  59.                         if nx >= 1 and nx <= gridWidth and ny >= 1 and ny <= gridHeight then
  60.                             if grid[ny][nx] == -1 then
  61.                                 count = count + 1
  62.                             end
  63.                         end
  64.                     end
  65.                 end
  66.                 grid[y][x] = count
  67.             end
  68.         end
  69.     end
  70. end
  71.  
  72. -- Count flags
  73. local function countFlags()
  74.     local count = 0
  75.     for y = 1, gridHeight do
  76.         for x = 1, gridWidth do
  77.             if flagged[y][x] then count = count + 1 end
  78.         end
  79.     end
  80.     return count
  81. end
  82.  
  83. -- Check win condition
  84. local function checkWin()
  85.     for y = 1, gridHeight do
  86.         for x = 1, gridWidth do
  87.             if grid[y][x] ~= -1 and not revealed[y][x] then
  88.                 return false
  89.             end
  90.         end
  91.     end
  92.     return true
  93. end
  94.  
  95. -- Reveal cell
  96. local function reveal(x, y)
  97.     if x < 1 or x > gridWidth or y < 1 or y > gridHeight then return end
  98.     if revealed[y][x] or flagged[y][x] then return end
  99.    
  100.     revealed[y][x] = true
  101.    
  102.     if grid[y][x] == -1 then
  103.         gameOver = true
  104.         -- Reveal all mines
  105.         for my = 1, gridHeight do
  106.             for mx = 1, gridWidth do
  107.                 if grid[my][mx] == -1 then
  108.                     revealed[my][mx] = true
  109.                 end
  110.             end
  111.         end
  112.         return
  113.     end
  114.    
  115.     if grid[y][x] == 0 then
  116.         -- Flood fill
  117.         for dy = -1, 1 do
  118.             for dx = -1, 1 do
  119.                 reveal(x + dx, y + dy)
  120.             end
  121.         end
  122.     end
  123.    
  124.     if checkWin() then
  125.         gameWon = true
  126.     end
  127. end
  128.  
  129. -- Draw game
  130. local function draw()
  131.     term.setBackgroundColor(colors.black)
  132.     term.clear()
  133.    
  134.     -- Title
  135.     term.setTextColor(colors.yellow)
  136.     term.setCursorPos(math.floor(w/2) - 5, 1)
  137.     term.write("MINESWEEPER")
  138.    
  139.     -- Info
  140.     term.setTextColor(colors.white)
  141.     term.setCursorPos(2, 2)
  142.     term.write("Mines: " .. mineCount .. " Flags: " .. countFlags())
  143.     term.setCursorPos(w - 10, 2)
  144.     term.write("[Q] Quit")
  145.    
  146.     -- Draw grid
  147.     local numberColors = {
  148.         colors.blue, colors.green, colors.red, colors.purple,
  149.         colors.brown, colors.cyan, colors.gray, colors.black
  150.     }
  151.    
  152.     for y = 1, gridHeight do
  153.         for x = 1, gridWidth do
  154.             local screenX = startX + (x - 1) * 2
  155.             local screenY = startY + y - 1
  156.             term.setCursorPos(screenX, screenY)
  157.            
  158.             local isSelected = (x == cursorX and y == cursorY)
  159.            
  160.             if revealed[y][x] then
  161.                 if grid[y][x] == -1 then
  162.                     term.setBackgroundColor(colors.red)
  163.                     term.setTextColor(colors.black)
  164.                     term.write("* ")
  165.                 elseif grid[y][x] == 0 then
  166.                     term.setBackgroundColor(colors.lightGray)
  167.                     term.write("  ")
  168.                 else
  169.                     term.setBackgroundColor(colors.lightGray)
  170.                     term.setTextColor(numberColors[grid[y][x]] or colors.black)
  171.                     term.write(grid[y][x] .. " ")
  172.                 end
  173.             elseif flagged[y][x] then
  174.                 term.setBackgroundColor(isSelected and colors.yellow or colors.gray)
  175.                 term.setTextColor(colors.red)
  176.                 term.write("F ")
  177.             else
  178.                 term.setBackgroundColor(isSelected and colors.yellow or colors.gray)
  179.                 term.setTextColor(colors.white)
  180.                 term.write("# ")
  181.             end
  182.         end
  183.     end
  184.    
  185.     -- Controls
  186.     term.setBackgroundColor(colors.black)
  187.     term.setTextColor(colors.gray)
  188.     term.setCursorPos(2, startY + gridHeight + 1)
  189.     term.write("Arrow keys: Move  SPACE: Reveal  F: Flag")
  190. end
  191.  
  192. -- Game over screen
  193. local function showGameOver()
  194.     term.setTextColor(colors.red)
  195.     term.setCursorPos(math.floor(w/2) - 4, startY + gridHeight + 3)
  196.     term.write("GAME OVER!")
  197.     term.setTextColor(colors.white)
  198.     term.setCursorPos(math.floor(w/2) - 8, startY + gridHeight + 4)
  199.     term.write("ENTER=Restart Q=Quit")
  200. end
  201.  
  202. -- Win screen
  203. local function showWin()
  204.     term.setTextColor(colors.lime)
  205.     term.setCursorPos(math.floor(w/2) - 4, startY + gridHeight + 3)
  206.     term.write("YOU WIN!")
  207.     term.setTextColor(colors.white)
  208.     term.setCursorPos(math.floor(w/2) - 8, startY + gridHeight + 4)
  209.     term.write("ENTER=Restart Q=Quit")
  210. end
  211.  
  212. -- Main game loop
  213. local function run()
  214.     initGrid()
  215.     gameOver = false
  216.     gameWon = false
  217.     firstClick = true
  218.     cursorX, cursorY = math.floor(gridWidth/2), math.floor(gridHeight/2)
  219.    
  220.     while true do
  221.         draw()
  222.        
  223.         if gameOver then
  224.             showGameOver()
  225.         elseif gameWon then
  226.             showWin()
  227.         end
  228.        
  229.         local event, key = os.pullEvent("key")
  230.        
  231.         if gameOver or gameWon then
  232.             if key == keys.enter then
  233.                 initGrid()
  234.                 gameOver = false
  235.                 gameWon = false
  236.                 firstClick = true
  237.                 cursorX, cursorY = math.floor(gridWidth/2), math.floor(gridHeight/2)
  238.             elseif key == keys.q then
  239.                 return
  240.             end
  241.         else
  242.             if key == keys.up then
  243.                 cursorY = math.max(1, cursorY - 1)
  244.             elseif key == keys.down then
  245.                 cursorY = math.min(gridHeight, cursorY + 1)
  246.             elseif key == keys.left then
  247.                 cursorX = math.max(1, cursorX - 1)
  248.             elseif key == keys.right then
  249.                 cursorX = math.min(gridWidth, cursorX + 1)
  250.             elseif key == keys.space then
  251.                 if not flagged[cursorY][cursorX] then
  252.                     if firstClick then
  253.                         placeMines(cursorX, cursorY)
  254.                         firstClick = false
  255.                     end
  256.                     reveal(cursorX, cursorY)
  257.                 end
  258.             elseif key == keys.f then
  259.                 if not revealed[cursorY][cursorX] then
  260.                     flagged[cursorY][cursorX] = not flagged[cursorY][cursorX]
  261.                 end
  262.             elseif key == keys.q then
  263.                 return
  264.             end
  265.         end
  266.     end
  267. end
  268.  
  269. run()
  270.  
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment