-
Notifications
You must be signed in to change notification settings - Fork 2
/
MineSweeper.lua
256 lines (222 loc) · 7.09 KB
/
MineSweeper.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
-- Mine Sweeper Game
-- Author: Github/Kiritow
require("libgpu")
require("libevent")
require("queue")
local component=require("component")
local shell=require("shell")
local args=shell.parse(...)
local argc=#args
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<all) do
local cl=math.random(1,line)
local cc=math.random(1,col)
if(t[cl][cc]==nil) then
t[cl][cc]=-1
cnt=cnt+1
end
end
local function val(xl,xc)
if(t[xl][xc]==nil) then return 0
elseif(t[xl][xc]<0) then return 1
else return 0
end
end
for i=1,line,1 do
for j=1,col,1 do
if(t[i][j]==nil) then
t[i][j]=val(i-1,j-1)+val(i-1,j)+val(i-1,j+1)+
val(i,j-1)+val(i,j+1)+
val(i+1,j-1)+val(i+1,j)+val(i+1,j+1)
end
end
end
t.line=line
t.col=col
t.all=all
return t
end
local function debugPrintMap(t)
gpu:clear()
for i=1,t.line,1 do
for j=1,t.col,1 do
if(t[i][j]>=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]>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:popbg()
gpu:popfg()
end
else
gpu:pushfg(0xFF0000)
gpu:set(i+1,j+1,"X")
gpu:popfg()
end
elseif(mask[i][j]<0) then
gpu:pushfg(0xFFFF00)
gpu:set(i+1,j+1,"?")
gpu:popfg()
else
gpu:pushbg(0xFFFFFF)
gpu:set(i+1,j+1," ")
gpu:popbg()
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
local function SetStatus(mp,str)
gpu:set(mp.line+3,1,">>" .. str .. "<<")
end
-- Game
local function main()
-- printMap(generateMap(10,10,5),generateBlankMask(10,10,true))
local maxline=10
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)
while true do
SetStatus(mp,"Marked " .. marked .. "/" .. maxmine)
local e=WaitMultipleEvent("touch","interrupted")
if(e.event=="interrupted") then
SetStatus(mp,"Game stopped. Click to exit.")
return
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(e.button==0) then
if(mp[line][col]<0) then
-- Game over
mask=generateBlankMask(mp.line,mp.col,1)
printMap(mp,mask)
SetStatus(mp,"Game Over! Click to exit.")
return
elseif(mask[line][col]==0) then
if(mp[line][col]>0) then
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)
end
else
if(mask[line][col]==0) then
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
mask[line][col]=0
marked=marked-1
if(mp[line][col]<0) then
marked_right=marked_right-1
end
end
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
print("Mine Sweeper")
print("Author: Github/Kiritow")
main()
WaitEvent("touch")
gpu:clear()