mirror of
https://github.com/Kiritow/OpenComputerScripts.git
synced 2024-03-22 13:10:46 +08:00
Update Minesweeper. Happy game!
This commit is contained in:
parent
2ed503620a
commit
98d7763251
117
MineSweeper.lua
117
MineSweeper.lua
|
@ -3,7 +3,12 @@
|
||||||
|
|
||||||
require("kgui")
|
require("kgui")
|
||||||
require("libevent")
|
require("libevent")
|
||||||
|
require("queue")
|
||||||
local component=require("component")
|
local component=require("component")
|
||||||
|
local shell=require("shell")
|
||||||
|
|
||||||
|
local args=shell.parse(...)
|
||||||
|
local argc=#args
|
||||||
|
|
||||||
local gpu=GetGPU()
|
local gpu=GetGPU()
|
||||||
|
|
||||||
|
@ -70,8 +75,6 @@ local function generateBlankMask(line,col,val)
|
||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local function printMap(base,mask)
|
local function printMap(base,mask)
|
||||||
gpu:clear()
|
gpu:clear()
|
||||||
for i=1,base.col+2,1 do
|
for i=1,base.col+2,1 do
|
||||||
|
@ -84,7 +87,13 @@ local function printMap(base,mask)
|
||||||
for j=1,base.col,1 do
|
for j=1,base.col,1 do
|
||||||
if(mask[i][j]>0) then
|
if(mask[i][j]>0) then
|
||||||
if(base[i][j]>=0) then
|
if(base[i][j]>=0) then
|
||||||
|
if(base[i][j]>0) then
|
||||||
|
gpu:pushfg(0x0000FF)
|
||||||
|
gpu:pushbg(0xFFFFFF)
|
||||||
gpu:set(i+1,j+1,tostring(base[i][j]))
|
gpu:set(i+1,j+1,tostring(base[i][j]))
|
||||||
|
gpu:popbg()
|
||||||
|
gpu:popfg()
|
||||||
|
end
|
||||||
else
|
else
|
||||||
gpu:pushfg(0xFF0000)
|
gpu:pushfg(0xFF0000)
|
||||||
gpu:set(i+1,j+1,"X")
|
gpu:set(i+1,j+1,"X")
|
||||||
|
@ -94,6 +103,10 @@ local function printMap(base,mask)
|
||||||
gpu:pushfg(0xFFFF00)
|
gpu:pushfg(0xFFFF00)
|
||||||
gpu:set(i+1,j+1,"?")
|
gpu:set(i+1,j+1,"?")
|
||||||
gpu:popfg()
|
gpu:popfg()
|
||||||
|
else
|
||||||
|
gpu:pushbg(0xFFFFFF)
|
||||||
|
gpu:set(i+1,j+1," ")
|
||||||
|
gpu:popbg()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -105,17 +118,37 @@ local function printMap(base,mask)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function SetStatus(mp,str)
|
||||||
|
gpu:set(mp.line+3,1,">>" .. str .. "<<")
|
||||||
|
end
|
||||||
|
|
||||||
-- Game
|
-- Game
|
||||||
local function main()
|
local function main()
|
||||||
-- printMap(generateMap(10,10,5),generateBlankMask(10,10,true))
|
-- printMap(generateMap(10,10,5),generateBlankMask(10,10,true))
|
||||||
local mp=generateMap(10,10,5)
|
local maxline=10
|
||||||
local mask=generateBlankMask(10,10,0)
|
local maxcol=10
|
||||||
|
local maxmine=5
|
||||||
|
if(argc==3) then
|
||||||
|
maxline=tonumber(args[1])
|
||||||
|
maxcol=tonumber(args[2])
|
||||||
|
maxmine=tonumber(args[3])
|
||||||
|
end
|
||||||
|
|
||||||
|
local mp=generateMap(maxline,maxcol,maxmine)
|
||||||
|
local mask=generateBlankMask(maxline,maxcol,0)
|
||||||
|
local marked=0
|
||||||
|
local marked_right=0
|
||||||
|
|
||||||
printMap(mp,mask)
|
printMap(mp,mask)
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
|
SetStatus(mp,"Marked " .. marked .. "/" .. maxmine)
|
||||||
|
|
||||||
local e=WaitMultipleEvent("touch","interrupted")
|
local e=WaitMultipleEvent("touch","interrupted")
|
||||||
if(e.event=="interrupted") then break end
|
if(e.event=="interrupted") then
|
||||||
|
SetStatus(mp,"Game stopped. Click to exit.")
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
local line=e.y-1
|
local line=e.y-1
|
||||||
local col=e.x-1
|
local col=e.x-1
|
||||||
|
@ -123,23 +156,92 @@ local function main()
|
||||||
if(not (line<1 or col<1 or line>mp.line or col>mp.col) ) then
|
if(not (line<1 or col<1 or line>mp.line or col>mp.col) ) then
|
||||||
if(e.button==0) then
|
if(e.button==0) then
|
||||||
if(mp[line][col]<0) then
|
if(mp[line][col]<0) then
|
||||||
|
-- Game over
|
||||||
mask=generateBlankMask(mp.line,mp.col,1)
|
mask=generateBlankMask(mp.line,mp.col,1)
|
||||||
|
|
||||||
printMap(mp,mask)
|
printMap(mp,mask)
|
||||||
os.sleep(5)
|
SetStatus(mp,"Game Over! Click to exit.")
|
||||||
return
|
return
|
||||||
elseif(mask[line][col]==0) then
|
elseif(mask[line][col]==0) then
|
||||||
|
if(mp[line][col]>0) then
|
||||||
mask[line][col]=1
|
mask[line][col]=1
|
||||||
|
else
|
||||||
|
-- If click on 0, then try to unlock other blocks with 0 (BFS)
|
||||||
|
local bus=Queue.new()
|
||||||
|
local x={}
|
||||||
|
x.line=line
|
||||||
|
x.col=col
|
||||||
|
bus:push(x)
|
||||||
|
|
||||||
|
local cnt=1
|
||||||
|
while(not bus:empty()) do
|
||||||
|
SetStatus(mp,"Counting " .. cnt)
|
||||||
|
--os.sleep(0)
|
||||||
|
|
||||||
|
local x=bus:pop()
|
||||||
|
cnt=cnt+1
|
||||||
|
|
||||||
|
if(x.line>1 and
|
||||||
|
mp[x.line-1][x.col]==0 and mask[x.line-1][x.col]==0) then
|
||||||
|
local t={}
|
||||||
|
t.line=x.line-1
|
||||||
|
t.col=x.col
|
||||||
|
mask[x.line-1][x.col]=1
|
||||||
|
bus:push(t)
|
||||||
|
end
|
||||||
|
|
||||||
|
if(x.line<mp.line and
|
||||||
|
mp[x.line+1][x.col]==0 and mask[x.line+1][x.col]==0) then
|
||||||
|
local t={}
|
||||||
|
t.line=x.line+1
|
||||||
|
t.col=x.col
|
||||||
|
mask[x.line+1][x.col]=1
|
||||||
|
bus:push(t)
|
||||||
|
end
|
||||||
|
|
||||||
|
if(x.col>1 and
|
||||||
|
mp[x.line][x.col-1]==0 and mask[x.line][x.col-1]==0) then
|
||||||
|
local t={}
|
||||||
|
t.line=x.line
|
||||||
|
t.col=x.col-1
|
||||||
|
mask[x.line][x.col-1]=1
|
||||||
|
bus:push(t)
|
||||||
|
end
|
||||||
|
|
||||||
|
if(x.col<mp.col and
|
||||||
|
mp[x.line][x.col+1]==0 and mask[x.line][x.col+1]==0) then
|
||||||
|
local t={}
|
||||||
|
t.line=x.line
|
||||||
|
t.col=x.col+1
|
||||||
|
mask[x.line][x.col+1]=1
|
||||||
|
bus:push(t)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
printMap(mp,mask)
|
printMap(mp,mask)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
if(mask[line][col]==0) then
|
if(mask[line][col]==0) then
|
||||||
mask[line][col]=-1
|
mask[line][col]=-1
|
||||||
|
marked=marked+1
|
||||||
|
if(mp[line][col]<0) then
|
||||||
|
marked_right=marked_right+1
|
||||||
|
end
|
||||||
elseif(mask[line][col]==-1) then
|
elseif(mask[line][col]==-1) then
|
||||||
mask[line][col]=0
|
mask[line][col]=0
|
||||||
|
marked=marked-1
|
||||||
|
if(mp[line][col]<0) then
|
||||||
|
marked_right=marked_right-1
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
printMap(mp,mask)
|
printMap(mp,mask)
|
||||||
|
|
||||||
|
if(marked_right==maxmine and marked==maxmine) then
|
||||||
|
SetStatus(mp,"Game Finish. Thank you for playing. Click to exit.")
|
||||||
|
return
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -149,3 +251,6 @@ end
|
||||||
print("Mine Sweeper")
|
print("Mine Sweeper")
|
||||||
print("Author: Github/Kiritow")
|
print("Author: Github/Kiritow")
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
WaitEvent("touch")
|
||||||
|
gpu:clear()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user