Created
April 28, 2023 05:58
-
-
Save lxsmnsyc/b57fddb7963c5dfca26e1c26bca7a7fa to your computer and use it in GitHub Desktop.
Minesweeper in LOVE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local random = math.random | |
local function newMap(width, height, mines) | |
local map, symbol, hide = {}, {}, {} | |
for x = 1, width do | |
map[x] = {} | |
symbol[x] = {} | |
hide[x] = {} | |
for y = 1, height do | |
map[x][y] = 0 | |
symbol[x][y] = 0 | |
hide[x][y] = 0 | |
end | |
end | |
for c = 1, mines do | |
local rx, ry = random(width), random(height) | |
while(map[rx][ry] ~= 0) do | |
rx, ry = random(width), random(height) | |
end | |
map[rx][ry] = 1 | |
for tx = -1, 1 do | |
for ty = -1, 1 do | |
local dx, dy = rx + tx, ry + ty | |
if(symbol[dx] and symbol[dx][dy]) then | |
if(map[dx][dy] == 0) then | |
symbol[dx][dy] = symbol[dx][dy] + 1 | |
end | |
end | |
end | |
end | |
end | |
return map, symbol, hide | |
end | |
local function pickTile(mineMap, hideMap, x, y, width, height) | |
if(mineMap[x][y] == 0) then | |
local toVisit = 1 | |
local visitStackX = {x} | |
local visitStackY = {y} | |
local vc = 0 | |
local visited = {} | |
for vx = 1, #hideMap do | |
visited[vx] = {} | |
for vy = 1, #(hideMap[vx]) do | |
visited[vx][vy] = false | |
end | |
end | |
while(vc < toVisit) do | |
vc = vc + 1 | |
local tx, ty = visitStackX[vc], visitStackY[vc] | |
print(tx, ty) | |
if(mineMap[tx][ty] == 0 and not visited[tx][ty]) then | |
hideMap[tx][ty] = 1 | |
if((0 < tx + 1 and tx + 1 <= width) and not visited[tx + 1][ty]) then | |
toVisit = toVisit + 1 | |
visitStackX[toVisit] = tx + 1 | |
visitStackY[toVisit] = ty | |
end | |
if((0 < tx - 1 and tx - 1 <= width) and not visited[tx - 1][ty]) then | |
print("wtf") | |
toVisit = toVisit + 1 | |
visitStackX[toVisit] = tx - 1 | |
visitStackY[toVisit] = ty | |
end | |
if((0 < ty + 1 and ty + 1 <= height) and not visited[tx][ty + 1]) then | |
toVisit = toVisit + 1 | |
visitStackX[toVisit] = tx | |
visitStackY[toVisit] = ty + 1 | |
end | |
if((0 < ty - 1 and ty - 1 <= height) and not visited[tx][ty - 1]) then | |
toVisit = toVisit + 1 | |
visitStackX[toVisit] = tx | |
visitStackY[toVisit] = ty - 1 | |
end | |
visited[tx][ty] = true | |
end | |
end | |
return true | |
end | |
hideMap[x][y] = 1 | |
return false | |
end | |
local setColor = love.graphics.setColor | |
local circle = love.graphics.circle | |
local rectangle = love.graphics.rectangle | |
local gprint = love.graphics.print | |
local font = love.graphics.getFont() | |
local function drawMineMap(tbl, tileSize) | |
for x = 1, #tbl do | |
local axis = tbl[x] | |
for y = 1, #axis do | |
if(axis[y] == 1) then | |
local half = tileSize*0.5 | |
local tx, ty = (x - 1)*tileSize + half, (y - 1)*tileSize + half | |
setColor(1, 0.25, 0.25, 0.75) | |
circle("fill", tx, ty, half) | |
setColor(1, 0.25, 0.25, 1) | |
circle("line", tx, ty, half) | |
end | |
end | |
end | |
end | |
local function drawSymbolMap(tbl, tileSize) | |
for x = 1, #tbl do | |
local axis = tbl[x] | |
for y = 1, #axis do | |
local tx, ty = (x - 1)*tileSize, (y - 1)*tileSize | |
setColor(0.25, 0.25, 1, 0.75) | |
rectangle("fill", tx, ty, tileSize, tileSize) | |
setColor(0.25, 0.25, 1, 1) | |
rectangle("line", tx, ty, tileSize, tileSize) | |
if(axis[y] > 0) then | |
local value = axis[y] | |
local fwidth = font:getWidth(value) | |
local fheight = font:getHeight() | |
local half = tileSize*0.5 | |
tx = tx + half - fwidth*0.5 | |
ty = ty + half - fheight*0.5 | |
setColor(0.25, 0.50, 1, 1) | |
gprint(value, tx, ty) | |
end | |
end | |
end | |
end | |
local function drawHideMap(tbl, tileSize) | |
for x = 1, #tbl do | |
local axis = tbl[x] | |
for y = 1, #axis do | |
if(axis[y] == 0) then | |
local tx, ty = (x - 1)*tileSize, (y - 1)*tileSize | |
setColor(0.75, 0.75, 0.75, 1) | |
rectangle("fill", tx, ty, tileSize, tileSize) | |
setColor(0.25, 0.25, 0.25, 1) | |
rectangle("line", tx, ty, tileSize, tileSize) | |
end | |
end | |
end | |
end | |
local MineMap, SymbolMap, HideMap | |
function love.load() | |
MineMap, SymbolMap, HideMap = newMap(25, 25, 16*16) | |
end | |
local floor = math.floor | |
function love.mousepressed(x, y) | |
x = x/600 | |
y = y/600 | |
x = floor(x*25) + 1 | |
y = floor(y*25) + 1 | |
pickTile(MineMap, HideMap, x, y, 25, 25) | |
end | |
function love.draw() | |
drawSymbolMap(SymbolMap, 24) | |
drawMineMap(MineMap, 24) | |
drawHideMap(HideMap, 24) | |
local x, y = love.mouse.getPosition() | |
gprint(floor(x*25/600) + 1, 600, 0) | |
gprint(floor(y*25/600) + 1, 600, 16) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment