Debian Codegraph @

samples/mines.svg

raw
1 <?xml version="1.0" standalone="no"?>
2 <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
3 "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
4
5 <!--
6
7 Licensed to the Apache Software Foundation (ASF) under one or more
8 contributor license agreements. See the NOTICE file distributed with
9 this work for additional information regarding copyright ownership.
10 The ASF licenses this file to You under the Apache License, Version 2.0
11 (the "License"); you may not use this file except in compliance with
12 the License. You may obtain a copy of the License at
13
14 http://www.apache.org/licenses/LICENSE-2.0
15
16 Unless required by applicable law or agreed to in writing, software
17 distributed under the License is distributed on an "AS IS" BASIS,
18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
21
22 -->
23 <!-- ====================================================================== -->
24 <!-- Minesweeper in SVG -->
25 <!-- -->
26 <!-- @author [email protected] -->
27 <!-- @version $Id$ -->
28 <!-- ====================================================================== -->
29
30 <svg width="450" height="500" viewBox="0 0 450 500"
31 xmlns="http://www.w3.org/2000/svg"
32 xmlns:xlink="http://www.w3.org/1999/xlink">
33
34 <script type="text/ecmascript">
35 <![CDATA[
36 //
37 // Global declarations
38 //
39 var mines0 = 8;
40 var mines1 = 40;
41 var mines2 = 99;
42
43 var boardSize = 1;
44 var numAcross = 16;
45 var numDown = 16;
46 var numMines = 40;
47
48 var correctFlags = 0;
49 var incorrectFlags = 0;
50 var minesLeft;
51 var board = document.getElementById("board")
52 var cover = document.getElementById("cover")
53
54 var boardArray = new Array(numAcross*numDown);
55 var shownArray = new Array(numAcross*numDown);
56 var flagArray = new Array(numAcross*numDown);
57 var isGameOver = true;
58 var xlinkNS = "http://www.w3.org/1999/xlink";
59 var svgNS = "http://www.w3.org/2000/svg";
60
61 function initBoard(evt) {
62 isGameOver = false;
63 minesLeft = numMines;
64 correctFlags = 0;
65
66 updateText("mines", "Mines Left: "+minesLeft);
67 updateText("boardsz", "Board Size: "+numAcross+"x"+numDown);
68 updateText("title", "");
69 updateText("working", "Working");
70
71 for(var y=0; y<numDown; y++) {
72 for(var x=0; x<numAcross; x++) {
73 boardArray[x+y*numAcross] = 0;
74 shownArray[x+y*numAcross] = false;
75 flagArray [x+y*numAcross] = false;
76 }
77 }
78 while (cover.lastChild != null) {
79 cover.removeChild(cover.lastChild);
80 }
81 while (board.lastChild != null) {
82 board.removeChild(board.lastChild);
83 }
84
85 for (var i=0; i<numMines; i++) {
86 var index = Math.round(Math.random()*(numDown*numAcross));
87 if ((index >= (numDown*numAcross)) ||
88 (boardArray[index]!=0)) {
89 i--;
90 } else {
91 boardArray[index] = -1;
92 }
93 }
94
95 for(var y=0; y<numDown; y++) {
96
97 var coverRow = document.createElementNS(svgNS, "g");
98 var boardRow = document.createElementNS(svgNS, "g");
99
100 var sum=0;
101 for (y1=y-1; y1<=y+1; y1++) {
102 if ((y1 >= 0) &&
103 (y1 < numDown) &&
104 (boardArray[y1*numAcross] == -1))
105 sum++;
106 }
107
108 for(var x=0; x<numAcross; x++) {
109 if (x-2 >= 0) {
110 for (y1=y-1; y1<=y+1; y1++) {
111 if ((y1 >= 0) &&
112 (y1 < numDown) &&
113 (boardArray[x-2+y1*numAcross] == -1))
114 sum--;
115 }
116 }
117 if (x+1 < numAcross) {
118 for (y1=y-1; y1<=y+1; y1++) {
119 if ((y1 >= 0) &&
120 (y1 < numDown) &&
121 (boardArray[x+1+y1*numAcross] == -1))
122 sum++;
123 }
124 }
125
126 if (boardArray[x+y*numAcross] == 0)
127 boardArray[x+y*numAcross] = sum;
128
129 var square = document.createElementNS(svgNS, "use");
130 square.setAttributeNS(null, "x", ""+(x*10));
131 square.setAttributeNS(null, "y", ""+(y*10));
132 square.setAttributeNS(null, "onclick",
133 "handleClick(evt, "+x+","+y+")");
134
135 var cov = square.cloneNode(true);
136 cov.setAttributeNS(null, "id", "cover"+x+"-"+y);
137 cov.setAttributeNS(xlinkNS, "href", "#blank");
138 var g = document.createElementNS(svgNS, "g");
139 g.appendChild(cov);
140 coverRow.appendChild(g);
141
142 square.setAttributeNS(null, "id", "square"+x+"-"+y);
143 if (boardArray[x+y*numAcross] == -1)
144 square.setAttributeNS(xlinkNS, "href", "#bomb");
145 else
146 square.setAttributeNS(xlinkNS, "href", "#square"+sum);
147 var g = document.createElementNS(svgNS, "g");
148 g.appendChild(square);
149 boardRow.appendChild(g);
150 }
151 cover.appendChild(coverRow);
152 board.appendChild(boardRow);
153 }
154
155 updateText("working", "");
156 updateText("title", "SVG Mines");
157 }
158
159 function handleClick(evt, x, y) {
160 if (isGameOver) return;
161 if ((evt.button == 1) ||
162 (evt.shiftKey)) {
163 placeFlag(x, y);
164 }
165 else if ((evt.button == 2) ||
166 (evt.ctrlKey)) {
167 showConnected(x, y);
168 }
169 else if (evt.button == 0) {
170 showSpot(x, y)
171 }
172 }
173
174 function placeFlag(x ,y) {
175 var idx = x + y*numAcross;
176 if (shownArray[idx]) return;
177 var elt = document.getElementById("cover"+x+"-"+y);
178 var nelt = elt.cloneNode(true);
179 if (flagArray[idx]) {
180 nelt.setAttributeNS(xlinkNS, "href", "#blank");
181 flagArray[idx] = false;
182 if (boardArray[idx] == -1) correctFlags--;
183 else incorrectFlags--;
184 minesLeft++;
185 } else {
186 nelt.setAttributeNS(xlinkNS, "href", "#flag");
187 flagArray[idx] = true;
188 if (boardArray[idx] == -1) correctFlags++;
189 else incorrectFlags++;
190 minesLeft--;
191 }
192
193 elt.parentNode.replaceChild(nelt, elt);
194 updateText("mines", "Mines Left: "+minesLeft);
195
196 if ((correctFlags == numMines) && (incorrectFlags == 0)) {
197 youWin();
198 }
199 }
200
201 function showSpot(x, y) {
202 var idx = x + y*numAcross;
203 if (shownArray[idx]) return;
204
205 var elt = document.getElementById("cover"+x+"-"+y);
206 elt.parentNode.parentNode.removeChild(elt.parentNode);
207 shownArray[idx] = true;
208
209 var val = boardArray[idx];
210 if (val == -1) {
211 gameOver();
212 } else if (val == 0) {
213 showConnected(x, y);
214 }
215 }
216
217 function showConnected(x, y) {
218 for (var dy=-1; dy<=1; dy++) {
219 if (y+dy < 0) continue;
220 if (y+dy >= numDown ) continue;
221 for (var dx=-1; dx<=1; dx++) {
222 if (x+dx < 0) continue;
223 if (x+dx >= numDown ) continue;
224 var idx = x+dx + (y+dy)*numAcross;
225 if (shownArray[idx]) continue;
226 if (flagArray[idx]) continue;
227
228 var elt = document.getElementById("cover"+(x+dx)+"-"+(y+dy));
229 elt.parentNode.parentNode.removeChild(elt.parentNode);
230 shownArray[idx] = true;
231
232 var val = boardArray[idx];
233 if (val == -1) {
234 gameOver();
235 } else if (val == 0) {
236 showConnected(x+dx, y+dy);
237 }
238 }
239 }
240 }
241
242 function gameOver() {
243 isGameOver=true;
244 updateText("title", "Game Over");
245 showBoard();
246 }
247
248 function showBoard() {
249 while (cover.lastChild != null) {
250 cover.removeChild(cover.lastChild);
251 }
252 var nc = numAcross*numDown;
253 for (var i=0; i<nc; i++)
254 shownArray[i] = true;
255 }
256
257 function youWin() {
258 isGameOver=true;
259 updateText("title", "You Win");
260 }
261
262 function updateText(id, lbl) {
263 var elt = document.getElementById(id);
264 var grp = elt.parentNode;
265 var newelt = elt.cloneNode(true);
266
267 if (newelt.firstChild == null) {
268 newelt.appendChild(document.createTextNode(lbl));
269 } else {
270 newelt.replaceChild(document.createTextNode(lbl), newelt.firstChild);
271 }
272 grp.replaceChild(newelt, elt);
273 }
274
275 function largerBoard(evt) {
276 if (boardSize == 1) return;
277 boardSize++;
278 if (boardSize == 1) {
279 numAcross = 16;
280 numDown = 16;
281 numMines = mines1;
282 }
283 else if (boardSize == 2) {
284 numAcross = 30;
285 numDown = 16;
286 numMines = mines2;
287 }
288 updateText("boardsz", "Board Size: "+numAcross+"x"+numDown);
289 updateText("title", "");
290 updateText("working", "Working");
291 setTimeout("initBoard()", 50);
292 }
293
294 function smallerBoard(evt) {
295 if (boardSize == 0) return;
296 boardSize--;
297 if (boardSize == 0) {
298 numAcross = 8;
299 numDown = 8;
300 numMines = mines0;
301 }
302 else if (boardSize == 1) {
303 numAcross = 16;
304 numDown = 16;
305 numMines = mines1;
306 }
307 updateText("boardsz", "Board Size: "+numAcross+"x"+numDown);
308 updateText("title", "");
309 updateText("working", "Working");
310 setTimeout("initBoard()", 50);
311 }
312
313 function newGame(evt) {
314 updateText("title", "");
315 updateText("working", "Working");
316 setTimeout("initBoard()", 50);
317 }
318
319 ]]>
320 </script>
321
322 <defs>
323 <radialGradient id="bombGrad" gradientUnits="userSpaceOnUse"
324 cx="5" cy="6.5" fx="4" fy="5" r="2.5">
325 <stop offset="0%" stop-color="#FFF"/>
326 <stop offset="20%" stop-color="#888"/>
327 <stop offset="100%" stop-color="#000"/>
328 </radialGradient>
329 <path id="empty" style="fill:#AAA; stroke:#DDD; stroke-width:0.5;"
330 d="M0.25,0.25 h9.5 v9.5 h-9.5 v-9.5"/>
331
332 <g id="blank"
333 ><rect style="fill:#AAA" width="10" height="10"
334 /><path style="fill:#DDD"
335 d="M0,0 h10 l-1,1 h-8 v8 l-1,1 v-10"
336 /><path style="fill:#888"
337 d="M10,10 h-10 l1,-1 h8 v-8 l1,-1 v10"
338 /></g>
339
340 <use id="square0" xlink:href="#empty"/>
341 <g id="square1" style="text-anchor:middle; font-size:8;"
342 ><use xlink:href="#empty"
343 /><text x="5" y="8" fill="#008" pointer-events="none">1</text></g>
344 <g id="square2" style="text-anchor:middle; font-size:8;"
345 ><use xlink:href="#empty"
346 /><text x="5" y="8" fill="#00C" pointer-events="none">2</text></g>
347 <g id="square3" style="text-anchor:middle; font-size:8;"
348 ><use xlink:href="#empty"
349 /><text x="5" y="8" fill="#40F" pointer-events="none">3</text></g>
350 <g id="square4" style="text-anchor:middle; font-size:8;"
351 ><use xlink:href="#empty"
352 /><text x="5" y="8" fill="#80C" pointer-events="none">4</text></g>
353 <g id="square5" style="text-anchor:middle; font-size:8;"
354 ><use xlink:href="#empty"
355 /><text x="5" y="8" fill="#C0C" pointer-events="none">5</text></g>
356 <g id="square6" style="text-anchor:middle; font-size:8;"
357 ><use xlink:href="#empty"
358 /><text x="5" y="8" fill="#F08" pointer-events="none">6</text></g>
359 <g id="square7" style="text-anchor:middle; font-size:8;"
360 ><use xlink:href="#empty"
361 /><text x="5" y="8" fill="#F04" pointer-events="none">7</text></g>
362 <g id="square8" style="text-anchor:middle; font-size:8;"
363 ><use xlink:href="#empty"
364 /><text x="5" y="8" fill="#F00" pointer-events="none">8</text></g>
365
366 <g id="bomb"
367 ><use xlink:href="#empty"
368 /><rect fill="#000" x="4" y="3" width="2" height="2"
369 /><circle fill="url(#bombGrad)" cx="5" cy="6.5" r="2.5"
370 /><path fill="none" stroke="#000" stroke-width="0.3"
371 d="M5,3 c0,-1.5 1.5,-1.5 1.5,0 c0,1.5 1.5,1.5 1.5,0"
372 /></g>
373
374 <g id="flag"
375 ><use xlink:href="#blank"
376 /><path d="M3.5,2 h.5 v6.5 h-1 l.5,-6.5"
377 /><path fill="crimson" d="M4,2 l4,1.5 l-4,1.5 z"
378 /></g>
379 </defs>
380
381 <g id="body">
382 <text id="title" x="25" y="60" font-size="40"
383 pointer-events="none">SVG Mines</text>
384 <text id="mines" x="250" y="30" font-size="20"
385 pointer-events="none">Mines Left: </text>
386
387 <text id="boardsz" x="250" y="54" font-size="20"
388 pointer-events="none">Board Size: </text>
389 <g transform="translate(242, 38)" fill="black">
390 <path onclick="largerBoard(evt)"
391 d="M2.5,0 l-2.5,7 h5 z"/>
392 <path onclick="smallerBoard(evt)"
393 d="M0,9 h5 l-2.5,7 z"/>
394 </g>
395
396 <g text-anchor="middle" >
397 <g id="help" >
398 <desc xml:space="preserve"
399 >Left: show
400 Right or Shift: place flag
401 Middle or Control: show surround</desc>
402 <circle cx="430" cy="18" r="12"
403 fill="grey" stroke="lightgrey" stroke-width="2" />
404 <text font-size="15" fill="blue" x="430" y="25"
405 pointer-events="none">?</text>
406 </g>
407
408 <g onclick="newGame()">
409 <desc xml:space="preserve">Start new game</desc>
410 <circle cx="430" cy="48" r="12"
411 fill="grey" stroke="lightgrey" stroke-width="2" />
412 <text font-size="10" fill="blue" x="430" y="53"
413 pointer-events="none">NG</text>
414 </g>
415 </g>
416
417 <g id="field" transform="translate(25,75) scale(2)"
418 onload="initBoard(evt)">
419 <g id="board">
420 </g>
421
422 <g id="cover">
423 </g>
424 </g>
425 <text id="working" fill="green" x="25" y="60" font-size="40"
426 pointer-events="none">Working</text>
427 </g>
428
429 <!-- ============================================================= -->
430 <!-- Batik sample mark -->
431 <!-- ============================================================= -->
432 <use xlink:href="batikLogo.svg#Batik_Tag_Box" />
433 </svg>