From 1b1d674e681527096f8895126450bf041f2a94f6 Mon Sep 17 00:00:00 2001 From: Kiritow <1362050620@qq.com> Date: Sat, 10 Mar 2018 00:19:56 +0800 Subject: [PATCH] Add mine sweeper game --- MineSweeper.lua | 144 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 MineSweeper.lua diff --git a/MineSweeper.lua b/MineSweeper.lua new file mode 100644 index 0000000..700dc67 --- /dev/null +++ b/MineSweeper.lua @@ -0,0 +1,144 @@ +-- Mine Sweeper Game +-- Author: Github/Kiritow + +require("kgui") +require("libevent") +local component=require("component") + +local gpu=GetGPU() + +local function generateMap(line,col,all) + local t={} + for i=0,line+1,1 do + t[i]={} + end + + local cnt=0 + while(cnt=0) then + gpu:set(i,j,tostring(t[i][j])) + else + gpu:set(i,j,"X") + end + end + end +end + +local function generateBlankMask(line,col,val) + local t={} + for i=1,line,1 do + t[i]={} + for j=1,col,1 do + t[i][j]=val + end + end + t.line=line + t.col=col + return t +end + + + +local function printMap(base,mask) + gpu:clear() + for i=1,base.col+2,1 do + gpu:set(1,i,"-") + end + + for i=1,base.line,1 do + gpu:set(i+1,1,"|") + + for j=1,base.col,1 do + if(mask[i][j]) then + if(base[i][j]>=0) then + gpu:set(i+1,j+1,tostring(base[i][j])) + else + gpu:set(i+1,j+1,"X") + end + end + end + + gpu:set(i+1,base.col+2,"|") + end + + for i=1,base.col+2,1 do + gpu:set(base.line+2,i,"-") + end +end + +-- Game +local function main() + -- printMap(generateMap(10,10,5),generateBlankMask(10,10,true)) + local mp=generateMap(10,10,5) + local mask=generateBlankMask(10,10,false) + + printMap(mp,mask) + + while true do + local e=WaitMultipleEvent("touch","interrupted") + if(e.event=="interrupted") then break end + + local line=e.y-1 + local col=e.x-1 + + if(not (line<1 or col<1 or line>mp.line or col>mp.col) ) then + if(mp[line][col]<0) then + -- GameOver + --[[ for i=1,line,1 do + for j=1,col,1 do + if(mp[i][j]<0) then + mask[i][j]=true + end + end + end + --]] + mask=generateBlankMask(mp.line,mp.col,true) + + printMap(mp,mask) + os.sleep(5) + return + elseif(not mask[line][col]) then + mask[line][col]=true + printMap(mp,mask) + end + end + end +end + + +print("Mine Sweeper") +print("Author: Github/Kiritow") +main() \ No newline at end of file