-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCells.lua
More file actions
147 lines (128 loc) · 4.62 KB
/
Cells.lua
File metadata and controls
147 lines (128 loc) · 4.62 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
local Cell = {
colours = {
["-1"] = Color3.fromRGB(100, 100, 100),
[""] = Color3.fromRGB(103, 103, 103),
["X"] = Color3.fromRGB(255, 0, 0),
["F"] = Color3.fromRGB(112, 40, 40),
[" "] = Color3.fromRGB(255, 252, 244),
["1"] = Color3.fromRGB(255, 224, 202),
["2"] = Color3.fromRGB(255, 208, 160),
["3"] = Color3.fromRGB(255, 190, 147),
["4"] = Color3.fromRGB(255, 186, 129),
["5"] = Color3.fromRGB(255, 172, 117),
["6"] = Color3.fromRGB(255, 147, 114),
["7"] = Color3.fromRGB(255, 133, 96),
["8"] = Color3.fromRGB(255, 112, 112)
}
}
local minefield = workspace:WaitForChild("Minefield"):WaitForChild("SurfaceGui"):WaitForChild("Minefield")
function Cell.GetSurroundingMines(button : TextButton)
local mines = 0
local x = tonumber(string.split(button.Name, "|")[1])
local y = tonumber(string.split(button.Name, "|")[2])
for i = x-1, x+1 do
for j = y-1, y+1 do
if i < 1 or j < 1 or i > minefield:GetAttribute("Width") or j > minefield:GetAttribute("Height") then continue end
local surroundingButton = button.Parent:FindFirstChild(i.."|"..j)
if surroundingButton:GetAttribute("Mine") then
mines += 1
end
if surroundingButton:GetAttribute("Flagged") then
mines -= 1
end
end
end
button:SetAttribute("NearbyMines", mines)
return mines
end
function Cell.Flood(button : TextButton)
if not button or button:GetAttribute("Mine") or button:GetAttribute("Revealed") then return end
button:SetAttribute("Revealed", true)
Cell.UpdateCell(button, false)
local x = tonumber(string.split(button.Name, "|")[1])
local y = tonumber(string.split(button.Name, "|")[2])
if button:GetAttribute("NearbyMines") == 0 then
Cell.Flood(button.Parent:FindFirstChild(tostring(x).."|"..tostring(y+1))) -- top
Cell.Flood(button.Parent:FindFirstChild(tostring(x+1).."|"..tostring(y+1))) -- top right
Cell.Flood(button.Parent:FindFirstChild(tostring(x+1).."|"..tostring(y))) -- right
Cell.Flood(button.Parent:FindFirstChild(tostring(x+1).."|"..tostring(y-1))) -- bottom right
Cell.Flood(button.Parent:FindFirstChild(tostring(x).."|"..tostring(y-1))) -- bottom
Cell.Flood(button.Parent:FindFirstChild(tostring(x-1).."|"..tostring(y-1))) -- bottom left
Cell.Flood(button.Parent:FindFirstChild(tostring(x-1).."|"..tostring(y))) -- left
Cell.Flood(button.Parent:FindFirstChild(tostring(x-1).."|"..tostring(y+1))) -- top left
end
end
function Cell.UpdateCell(button : TextButton, flag : boolean)
if flag and not button:GetAttribute("Revealed") then
if button:GetAttribute("Flagged") then
button.Text = ""
else
button.Text = "F"
end
button:SetAttribute("Flagged", not button:GetAttribute("Flagged"))
elseif not button:GetAttribute("Flagged") then
if button:GetAttribute("Mine") then
button.Text = "X"
elseif button:GetAttribute("NearbyMines") == 0 then
button.Text = " "
Cell.Flood(button)
else
button.Text = tostring(button:GetAttribute("NearbyMines"))
end
if not minefield:GetAttribute("Static") then
end
button:SetAttribute("Revealed", true)
end
if Cell.colours[button.Text] then
button.BackgroundColor3 = Cell.colours[button.Text]
else
button.BackgroundColor3 = Cell.colours["-1"]
end
if button.Text == "X" then
return false
else
return true
end
end
function Cell.CheckWin()
for i=1, minefield:GetAttribute("Width") do
for j=1, minefield:GetAttribute("Height") do
local button = minefield:FindFirstChild(i.."|"..j)
if not button:GetAttribute("Revealed") and not button:GetAttribute("Mine") then
return false
end
end
end
Cell.Win()
return true
end
function Cell.Win()
minefield.BackgroundColor3 = Color3.fromRGB(66, 204, 75)
for i=1, minefield:GetAttribute("Width") do
for j=1, minefield:GetAttribute("Height") do
local button : TextButton = minefield:FindFirstChild(i.."|"..j)
if button:GetAttribute("Mine") and not button:GetAttribute("Flagged") then
button.Text = "F"
button.BackgroundColor3 = Color3.fromRGB(50, 107, 55)
elseif not button:GetAttribute("Mine") and not button:GetAttribute("Flagged") then
Cell.UpdateCell(button, false)
elseif button:GetAttribute("Flagged") then
button.BackgroundColor3 = Color3.fromRGB(50, 107, 55)
end
end
end
end
function Cell.Lose()
minefield.BackgroundColor3 = Color3.fromRGB(204, 24, 24)
for i=1, minefield:GetAttribute("Width") do
for j=1, minefield:GetAttribute("Height") do
local button : TextButton = minefield:FindFirstChild(i.."|"..j)
if button:GetAttribute("Mine") and button:GetAttribute("Flagged") then
button.BackgroundColor3 = Color3.fromRGB(50, 107, 55)
elseif button:GetAttribute("Mine") then
Cell.UpdateCell(button, false)
end
end
end
end
return Cell