-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSweepMine.lua
More file actions
334 lines (275 loc) · 8.79 KB
/
Copy pathSweepMine.lua
File metadata and controls
334 lines (275 loc) · 8.79 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
local sweepframe = CreateFrame("Frame", "SweepMineFrame", UIParent, "BasicFrameTemplate")
local sweepreset = CreateFrame("Button", "SweepResetButton", sweepframe, "UIPanelButtonTemplate")
print("[|cFF9370DBSweepMine|r] Welcome to SweepMine - use /sweepmine to open the frame!")
-- Globals
SweepBoard = {}
SweepBombs = {}
TotalSweepBombs = 0
MissplacedBombs = 0
SweepRevealedSpaces = 0
SweepMineGameInProgress = false
InvalidBombSpaces = {}
-- Make a title for the window
sweepframe.TitleText:SetText("SweepMine")
sweepframe.TitleText:SetPoint("TOP", sweepframe, "TOP", 0, -6);
sweepframe.TitleText:SetTextColor(0.8, 0, 0.8, 1);
-- Make our frame draggable so that people can move it where they want
sweepframe:SetMovable(true)
sweepframe:EnableMouse(true)
sweepframe:RegisterForDrag("LeftButton")
sweepframe:SetScript("OnDragStart", sweepframe.StartMoving)
sweepframe:SetScript("OnDragStop", function(self)
self:StopMovingOrSizing()
end)
sweepframe:SetClampedToScreen(true)
-- The code below makes the frame visible.
sweepframe:SetPoint("CENTER")
sweepframe:SetSize(180, 225)
-- Make the reset button visable too
sweepreset:SetPoint("BOTTOM", sweepframe, "BOTTOM", 0, 10)
sweepreset:SetSize(80, 20)
sweepreset:SetText("Reset Game")
local function bombGenerator(bombsToPlace)
-- If this is our first run through
if MissplacedBombs == 0 then
for i = 1, bombsToPlace do
-- Randomly pick two coords
local x = math.random(8)
local y = math.random(8)
if InvalidBombSpaces[x..","..y] == nil then
-- If this isn't already a bomb location
if SweepBombs[x..","..y] == nil then
SweepBombs[x..","..y] = true
SweepBoard[x][y] = 1
TotalSweepBombs = TotalSweepBombs + 1
else
-- If a bomb location is already occupied, add one to the counter
-- so that it will be re-placed
-- print("[|cFF9370DBSweepMine|r] Conflict Found, will re-place bomb...")
MissplacedBombs = MissplacedBombs + 1
end
else
-- This spot is invalid due to first click rules
MissplacedBombs = MissplacedBombs + 1
end
end
-- If this function is being called a second time
else
for i = 1, bombsToPlace do
-- Random Coords
local x = math.random(8)
local y = math.random(8)
if InvalidBombSpaces[x..","..y] == nil then
-- If this isn't already a bomb location then we successfully placed the bomb
if SweepBombs[x..","..y] == nil then
SweepBombs[x..","..y] = true
SweepBoard[x][y] = 1
TotalSweepBombs = TotalSweepBombs + 1
-- The only difference is we remove missplaced bombs as we fix them
MissplacedBombs = MissplacedBombs - 1
end
end
end
end
end
-- Generate a 2D gameboard
local function generateBoard()
-- Init Board and Bombs tables
SweepBoard = {}
SweepBombs = {}
print("[|cFF9370DBSweepMine|r] Generating SweepMine Board...")
-- Create a 2D array of all 0s
for x = 1,8 do
SweepBoard[x] = {}
for y = 1,8 do
SweepBoard[x][y] = 0
end
end
-- Randomly place bombs on the board
-- A "Bomb" is just a 1 in the 2D array
bombGenerator(10)
while MissplacedBombs > 0 do
-- print("[|cFF9370DBSweepMine|r] Re-placing Missplaced Bombs...")
bombGenerator(MissplacedBombs)
end
print("[|cFF9370DBSweepMine|r] A SweepMine Board has been generated!")
print("[|cFF9370DBSweepMine|r] There are |cFF9370DB"..TotalSweepBombs.."|r total bombs!")
end
-- Check if a user has won. A user wins if they flag all the bombs.
local function verifyWin()
if SweepRevealedSpaces == 64 - TotalSweepBombs then
for x = 1, 8 do
for y = 1, 8 do
local framename = "x"..x.."y"..y
-- If Bomb or Flagged Bomb
if SweepBoard[x][y] == 1 or SweepBoard[x][y] == 3 then
_G[framename]:SetText("B")
_G[framename]:Disable()
end
end
end
message('You Win! Press the reset button or to play again!')
end
end
-- Flag or Unflag a Space
local function flagSpace(x, y, frame)
if SweepBoard[x][y] == 0 then
frame:SetText("F")
SweepBoard[x][y] = 2
elseif SweepBoard[x][y] == 1 then
frame:SetText("F")
SweepBoard[x][y] = 3
elseif SweepBoard[x][y] == 2 then
frame:SetText("")
SweepBoard[x][y] = 0
elseif SweepBoard[x][y] == 3 then
frame:SetText("")
SweepBoard[x][y] = 1
end
end
-- Reveals a space when clicked, also displays the numBombs close to the clicked square
local function revealSpace(x, y, frame)
-- How many bombs are adjacent to the current square
local adjBombs = 0
-- Prevents players from left clicking on any flagged spaces.
if SweepBoard[x][y] == 3 or SweepBoard[x][y] == 2 then
return
end
-- If player clicks on a bomb, it's game over
if SweepBoard[x][y] == 1 then
frame:SetText("B")
-- Show where the bombs were and disable the board
for x = 1, 8 do
for y = 1, 8 do
local framename = "x"..x.."y"..y
-- If Bomb or Flagged Bomb
if SweepBoard[x][y] == 1 or SweepBoard[x][y] == 3 then
_G[framename]:SetText("B")
_G[framename]:Disable()
-- If flagged empty space, remove the F
elseif SweepBoard[x][y] == 2 then
_G[framename]:SetText("")
_G[framename]:Disable()
-- If empty space, just disable it
elseif SweepBoard[x][y] == 0 then
_G[framename]:Disable()
end
end
end
message('You blew up! Press the reset button to play again!')
return
end
-- Game Tile States
-- COVERED_EMPTY = 0
-- COVERED_MINE = 1
-- FLAG_EMPTY = 2
-- FLAG_MINE = 3
-- Is there a bomb around us?
for xcoord = x-1, x+1 do
for ycoord = y-1, y+1 do
-- Make sure we're not trying to access out of bounds
if xcoord ~= 0 and xcoord ~= 9 then
if ycoord ~= 0 and ycoord ~= 9 then
if SweepBoard[xcoord][ycoord] == 1 or SweepBoard[xcoord][ycoord] == 3 then
adjBombs = adjBombs + 1
end
end
end
end
end
frame:SetText(tostring(adjBombs))
frame:Disable()
SweepRevealedSpaces = SweepRevealedSpaces + 1
verifyWin()
end
-- Create a button
local function MakeButtons(buttonX, buttonY)
-- The base coords to make the grid
local baseCoordsX = -70
local baseCoordsY = -30
-- Init locals
local x
local y
-- Create a button and set the size
local frame = CreateFrame("Button", "x"..buttonX.."y"..buttonY, sweepframe, "UIPanelButtonTemplate")
frame:SetSize(20,20)
-- Set point using size and where we are in the grid
x = (baseCoordsX + (20 * (buttonX-1)))
y = (baseCoordsY - (20 * (buttonY-1)))
frame:SetPoint("TOP", sweepframe, x, y)
frame:RegisterForClicks("LeftButtonDown", "RightButtonDown")
frame:SetScript("OnClick", function(self, button, down)
-- Grab the name of the frame so that we can edit it
local name = self:GetName()
-- The frame's X and Y can be derived from the name
local framex = tonumber(name:sub(2, 2))
local framey = tonumber(name:sub(4, 4))
-- Depending on what button the user clicks we do other stuff
if button == "RightButton" then
if SweepMineGameInProgress == true then
flagSpace(framex, framey, self)
end
elseif button == "LeftButton" then
if SweepMineGameInProgress == false then
SweepBetterStart(framex, framey, self)
else
revealSpace(framex, framey, self)
end
end
end)
end
-- Make the button grid
for x = 1, 8 do
for y = 1, 8 do
local buttonX, buttonY = x, y
MakeButtons(buttonX, buttonY);
end
end
-- If the reset button is pressed, reset the game entirely
-- It has to be at the bottom because Lua doesn't have function hoisting
sweepreset:SetScript("OnClick", function(self, button, down)
-- Initialize the buttons
for x = 1, 8 do
for y = 1, 8 do
local framename = "x"..x.."y"..y
_G[framename]:SetText("")
_G[framename]:Enable()
end
end
-- Make a new GameState
TotalSweepBombs = 0
MissplacedBombs = 0
SweepRevealedSpaces = 0
SweepMineGameInProgress = false
InvalidBombSpaces = {}
print("[|cFF9370DBSweepMine|r] SweepMine has been reset!")
end)
function SweepBetterStart(x, y, frame)
-- Take all the squares around the clicked square and make them invalid
for xcoord = x-1, x+1 do
for ycoord = y-1, y+1 do
-- Make sure we're not trying to access out of bounds
if xcoord ~= 0 and xcoord ~= 9 then
if ycoord ~= 0 and ycoord ~= 9 then
InvalidBombSpaces[xcoord..","..ycoord] = true
end
end
end
end
generateBoard()
frame:SetText("0")
frame:Disable()
SweepRevealedSpaces = SweepRevealedSpaces + 1
SweepMineGameInProgress = true
end
--Make a Slash Command so that we can open the frame again if we close it
SLASH_VER1 = '/sweepmine'
function SlashCmdList.VER(msg, editBox)
if sweepframe:IsVisible() then
sweepframe:Hide()
else
sweepframe:Show()
end
end
-- Hide the frame on /reload and on WoW start-up
sweepframe:Hide()