vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1  
2 local Sweep_Mines = 10;
3 local Sweep_FlagsDown = 0;
4 local Sweep_NumExposed = 0;
5 local Sweep_GameIsOver = false;
6  
7 function Sweep_OnLoad()
8 -- Register the slash command
9 SLASH_SWEEP1 = "/sweep";
10 SlashCmdList["SWEEP"] = function(msg)
11 Sweep_SlashCommand(msg);
12 end
13  
14 Sweep_Write("Val's Sweep loaded. Type |cffff0000/sweep|r to play or |cffff0000/sweep help|r for instructions.");
15 Sweep_NewGame();
16 end
17  
18 function Sweep_SlashCommand(msg)
19 if(msg == nil or msg == "") then
20 Sweep_Toggle();
21 elseif(msg == "help") then
22 Sweep_Help();
23 end
24 end
25  
26 function Sweep_Help()
27 Sweep_Write("Help for Val's Sweep");
28 Sweep_Write("=================");
29 Sweep_Write("- Expose a square by clicking it. If you expose a mine, you lose.");
30 Sweep_Write("- A number on a square tells how many mines are adjacent to the square.");
31 Sweep_Write("- If you think a square contains a mine, right-click to mark it.");
32 Sweep_Write("- When all non-mined squares are exposed, you win.");
33 Sweep_Write("- There are " .. Sweep_Mines .. " mines per game.");
34 end
35  
36 function Sweep_Toggle()
37 if(getglobal("Sweep_Frame"):IsShown()) then
38 getglobal("Sweep_Frame"):Hide();
39 else
40 getglobal("Sweep_Frame"):Show();
41 end
42 end
43  
44 function Sweep_NewGame()
45  
46 Sweep_FlagsDown = 0;
47 Sweep_NumExposed = 0;
48 Sweep_GameIsOver = false;
49  
50 for i = 1,64 do
51 getglobal("Sweep_Cell" .. i).mined = false;
52 getglobal("Sweep_Cell" .. i).flagged = false;
53 getglobal("Sweep_Cell" .. i).exposed = false;
54 getglobal("Sweep_Cell" .. i).adjacent = 0;
55 getglobal("Sweep_Cell" .. i .. "Label"):SetText("");
56 getglobal("Sweep_Cell" .. i):SetBackdropColor(0.0, 0.0, 0.0);
57 end
58  
59 local minesToLay = Sweep_Mines;
60 local cellNum;
61 math.randomseed = 3;
62 while(minesToLay > 0) do
63 cellNum = math.random(1,64);
64 if(not getglobal("Sweep_Cell" .. cellNum).mined) then
65 getglobal("Sweep_Cell" .. cellNum).mined = true;
66 --getglobal("Sweep_Cell" .. cellNum .. "Label"):SetText("*");
67 minesToLay = minesToLay - 1;
68 end
69 end
70 end
71  
72 function Sweep_CellOnClick(arg1)
73 local frame = this:GetName();
74  
75 if(Sweep_GameIsOver) then
76 return;
77 end
78  
79 if(arg1 == "LeftButton") then
80 if(this.mined) then
81 Sweep_GameOver(frame);
82 elseif(not this.flagged) then
83 Sweep_Expose(this);
84 end
85 elseif(arg1 == "RightButton") then
86 if(this.flagged) then
87 this.flagged = false;
88 Sweep_FlagsDown = Sweep_FlagsDown - 1;
89 if(this.exposed) then
90 if(this.adjacent > 0) then
91 this:SetBackdropColor(0.5, 0.0, 0.0);
92 getglobal(frame.."Label"):SetText(this.adjacent);
93 else
94 this:SetBackdropColor(0.0, 0.5, 0.0);
95 getglobal(frame.."Label"):SetText("");
96 end
97 else
98 this:SetBackdropColor(0.0, 0.0, 0.0);
99 getglobal(frame.."Label"):SetText("");
100 end
101  
102 else
103 this.flagged = true;
104 Sweep_FlagsDown = Sweep_FlagsDown + 1;
105 getglobal(frame.."Label"):SetText("F");
106 this:SetBackdropColor(0.0, 0.0, 0.5);
107 Sweep_CheckVictory();
108 end
109 end
110 end
111  
112 function Sweep_Expose(frame)
113  
114 if(frame.exposed or frame.mined or frame.flagged) then
115 return;
116 end
117  
118 local adjacent = 0;
119 local id = frame:GetID();
120  
121 local is_top = (id < 9);
122 local is_left = (math.mod(id-1, 8) == 0);
123 local is_right = (math.mod(id-1, 8) == 7);
124 local is_bottom = (id > 56);
125  
126 frame.exposed = true;
127 Sweep_NumExposed = Sweep_NumExposed + 1;
128  
129  
130 if(not is_top) then
131  
132 if(getglobal("Sweep_Cell"..id-8).mined) then
133 adjacent = adjacent + 1;
134 end
135  
136  
137 if(not is_left) then
138 if(getglobal("Sweep_Cell"..id-9).mined) then
139 adjacent = adjacent + 1;
140 end
141 end
142  
143 if(not is_right) then
144 if(getglobal("Sweep_Cell"..id-7).mined) then
145 adjacent = adjacent + 1;
146 end
147 end
148 end
149  
150 if(not is_left) then
151 if(getglobal("Sweep_Cell"..id-1).mined) then
152 adjacent = adjacent + 1;
153 end
154  
155 end
156  
157 if(not is_right) then
158 if(getglobal("Sweep_Cell"..id+1).mined) then
159 adjacent = adjacent + 1;
160 end
161  
162 end
163  
164 if(not is_bottom) then
165 if(getglobal("Sweep_Cell"..id+8).mined) then
166 adjacent = adjacent + 1;
167 end
168  
169  
170 if(not is_left) then
171 if(getglobal("Sweep_Cell"..id+7).mined) then
172 adjacent = adjacent + 1;
173 end
174 end
175  
176 if(not is_right) then
177 if(getglobal("Sweep_Cell"..id+9).mined) then
178 adjacent = adjacent + 1;
179 end
180 end
181 end
182  
183 if(adjacent > 0) then
184 getglobal("Sweep_Cell" .. id .. "Label"):SetText(adjacent);
185 frame:SetBackdropColor(0.5, 0.0, 0.0);
186 else
187 frame:SetBackdropColor(0.0, 0.5, 0.0);
188  
189 if(not is_top) then
190  
191 Sweep_Expose(getglobal("Sweep_Cell"..id-8));
192  
193  
194 if(not is_left) then
195 Sweep_Expose(getglobal("Sweep_Cell"..id-9));
196 end
197  
198 if(not is_right) then
199 Sweep_Expose(getglobal("Sweep_Cell"..id-7));
200 end
201 end
202  
203 if(not is_left) then
204 Sweep_Expose(getglobal("Sweep_Cell"..id-1));
205 end
206  
207 if(not is_right) then
208 Sweep_Expose(getglobal("Sweep_Cell"..id+1));
209 end
210  
211 if(not is_bottom) then
212 Sweep_Expose(getglobal("Sweep_Cell"..id+8));
213  
214  
215 if(not is_left) then
216 Sweep_Expose(getglobal("Sweep_Cell"..id+7));
217 end
218  
219 if(not is_right) then
220 Sweep_Expose(getglobal("Sweep_Cell"..id+9));
221 end
222 end
223 end
224  
225 frame.adjacent = adjacent;
226  
227 Sweep_CheckVictory();
228  
229 end
230  
231 function Sweep_GameOver(frame)
232 Sweep_Write("BOMB! Game over.");
233  
234 for i = 1,64 do
235 if(getglobal("Sweep_Cell" .. i).mined and not getglobal("Sweep_Cell" .. i).flagged) then
236 getglobal("Sweep_Cell" .. i .. "Label"):SetText("*");
237 end
238 end
239  
240 getglobal(frame.."Label"):SetText("X");
241 Sweep_GameIsOver = true;
242 end
243  
244 function Sweep_CheckVictory()
245 if(Sweep_NumExposed + Sweep_FlagsDown == 64) then
246 Sweep_Write("You Win!");
247 Sweep_GameIsOver = true;
248 end
249 end
250  
251 function Sweep_Write(msg)
252 if (DEFAULT_CHAT_FRAME) then
253 DEFAULT_CHAT_FRAME:AddMessage(msg);
254 end
255 end