html--扫雷

打印 上一主题 下一主题

主题 529|帖子 529|积分 1587

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="UTF-8">
  5.   <meta name="Generator" content="EditPlus庐">
  6.   <meta name="Author" content="">
  7.   <meta name="Keywords" content="">
  8.   <meta name="Description" content="">
  9.   <title>Document</title>
  10.   <style>html {
  11.   font-size: 10px;
  12. }
  13. body {
  14.   counter-reset: total cleared bombs flagged;
  15.   font-size: 1.6rem;
  16.   font-family: sans-serif;
  17. }
  18. H1 {
  19.   font-size:2rem;
  20.   margin:1rem auto;
  21.   width:fit-content
  22. }
  23. #game {
  24.   overflow:hidden;
  25.   position:relative;
  26.   margin:0 auto;
  27.   background:lightgrey;
  28.   width:fit-content;
  29.   height:fit-content;
  30.   padding:1rem;
  31.   border:0.3rem outset lightgrey;
  32.   border-radius:1rem;
  33. }
  34. /*
  35. grid square.
  36. */
  37. .square {
  38.   position: relative;
  39.   float: left;
  40.   display: block;
  41.   width: 2rem;
  42.   height: 2rem;
  43.   border: 1px grey solid;
  44.   background-color: lightgrey;
  45.   text-align: center;
  46. }
  47. /*
  48. these will be the "buttons"
  49. that hide the grid.
  50. */
  51. .square > label,
  52. .square > .flag {
  53.   position: absolute;
  54.   left: -0.1rem;
  55.   top: -0.1rem;
  56.   right: -0.1rem;
  57.   bottom: -0.1rem;
  58.   border: 0.2rem outset grey;
  59.   background-color: darkgrey;
  60. }
  61. /*
  62. switch for flag mode and clear mode
  63. */
  64. #flag-mode {
  65.   display:none;
  66. }
  67. /*
  68. buttons for global settings
  69. */
  70. [for="flag-mode"],
  71. [for="show-bombs"] {
  72.   display:block;
  73.   margin:0 auto;
  74.   width:fit-content;
  75.   background:lightgrey;
  76.   padding:0 0.5rem;
  77.   border:2px outset grey;
  78.   border-radius:0.5rem;
  79.   margin:0.2rem auto;
  80. }
  81. #flag-mode:not(:checked) + [for="flag-mode"]:after {
  82.   content:" OFF ";
  83.   font-weight:bold;
  84. }
  85. #flag-mode:checked + [for="flag-mode"]:after {
  86.   content:" ON ";
  87.   font-weight:bold;
  88. }
  89. /*
  90. checkboxes to control state of each square
  91. these need to be "visible"
  92. for the counter to work.
  93. push them far off to the left.
  94. */
  95. .open-square,.flag-square {
  96.   position: absolute;
  97.   left: -1000rem;
  98. }
  99. /*
  100. count the total clearable
  101. */
  102. .open-square:not(.bomb) {
  103.   counter-increment:total;
  104. }
  105. /*
  106. count the number of bombs
  107. */
  108. .open-square.bomb {
  109.   counter-increment:bombs;
  110. }
  111. /*
  112. hide the label "button" when it's clicked
  113. */
  114. .open-square:checked + .square > label {
  115.   display: none;
  116. }
  117. /*
  118. show the number of surounding bombs (if any)
  119. with different colors for the count values
  120. */
  121. :not(.bomb)[data-count] + .square:before {
  122.   line-height:2rem;
  123.   font-weight:bold;
  124. }
  125. :not(.bomb)[data-count="1"] + .square:before {
  126.   content:"1";
  127.   color: blue;
  128. }
  129. :not(.bomb)[data-count="2"] + .square:before {
  130.   content:"2";
  131.   color: green;
  132. }
  133. :not(.bomb)[data-count="3"] + .square:before {
  134.   content:"3";
  135.   color: red;
  136. }
  137. :not(.bomb)[data-count="4"] + .square:before {
  138.   content:"4";
  139.   color: darkblue;
  140. }
  141. :not(.bomb)[data-count="5"] + .square:before {
  142.   content:"5";
  143.   color: brown;
  144. }
  145. :not(.bomb)[data-count="6"] + .square:before {
  146.   content:"6";
  147.   color: cyan;
  148. }
  149. :not(.bomb)[data-count="7"] + .square:before {
  150.   content:"7";
  151.   color: black;
  152. }
  153. :not(.bomb)[data-count="8"] + .square:before {
  154.   content:8;
  155.   color: grey;
  156. }
  157. /*
  158. bomb square.
  159. */
  160. .bomb + .square {
  161.   background-color: red
  162. }
  163. .bomb + .square:before {
  164.   content: "馃挘";
  165.   line-height: 2rem;
  166.   font-size: 1.2rem;
  167. }
  168. /*
  169. default hide the flag
  170. */
  171. .flag {
  172.   display:none;
  173. }
  174. /*
  175. show the flag if square is flagged
  176. and count it
  177. */
  178. .flag-square:checked ~ .flag {
  179.   display:block;
  180.   counter-increment:flagged;
  181. }
  182. /*
  183. display flag as needed
  184. */
  185. .flag-square:checked ~ .flag-label:before,
  186. .flag-square:checked ~ .flag:before {
  187. content:"馃毄"
  188. }
  189. /*
  190. hide the label when not in flag mode
  191. */
  192. #flag-mode:not(:checked) ~ .square > .flag-label {
  193.   display:none;
  194. }
  195. /*
  196. only show the flag if the square isn't in "show" mode.
  197. */
  198. #flag-mode:checked ~ .show-square:not(:checked) + .square > .flag-label {
  199.   display:block;
  200. }
  201. /*
  202. increment cleared counter
  203. for each non-bomb checked
  204. and keep the total intact
  205. */
  206. .open-square:not(.bomb):checked {
  207.   counter-increment: total cleared;
  208. }
  209. /*
  210. various counter displays
  211. */
  212. .bombs {
  213.   width:50%;
  214.   text-align:center;
  215. }
  216. .score {
  217.   clear:both;
  218.   float:right;
  219.   text-align:right;
  220.   width:50%;
  221.   text-align:center;
  222. }
  223. /*
  224. track the bombs
  225. */
  226. .bombs:after {
  227.   content: counter(bombs) "馃毄" counter(flagged);
  228.   line-height:3rem;
  229.   border:0.2rem inset lightgrey;
  230.   padding:0.2rem 0.5rem;
  231.   background-color:white;
  232.   border-radius:0.5rem;
  233. }
  234. /*
  235. track the total cleareable & cleared
  236. */
  237. .score:after {
  238.   content: counter(cleared) " of " counter(total);
  239.   line-height:3rem;
  240.   border:0.2rem inset lightgrey;
  241.   padding:0.2rem 0.5rem;
  242.   background-color:white;
  243.   border-radius:0.5rem;
  244. }
  245. /*
  246. default setting is the win state
  247. */
  248. .message {
  249.   clear: left;
  250.   color: limegreen;
  251.   text-align:center;
  252.   font-weight:bold;
  253. }
  254. .message:before {
  255.   content: "(: YOU WIN! :) "
  256. }
  257. /*
  258.   block user interaction with
  259.   game board after a win.
  260. */
  261. .pane
  262. {
  263.   display:block;
  264.   position:absolute;
  265.   left:0;
  266.   right:0;
  267.   top:0;
  268.   bottom:0;
  269. }
  270. /*
  271. override for the playing state
  272. */
  273. .open-square:not(.grouped):not(.bomb):not(:checked) ~ .message:before,
  274. .group-master:not(:checked) ~ .message:before
  275. {
  276.   content: none;
  277. }
  278. /*
  279.   play state. hide the blocking pane.
  280. */
  281. .open-square:not(.grouped):not(.bomb):not(:checked) ~ .pane,
  282. .group-master:not(:checked) ~ .pane,
  283. .lost-game
  284. {
  285.   display: none;
  286. }
  287. /*
  288. finally, the losing state
  289. */
  290. .open-square.bomb:checked ~ .message {
  291.   color: red;
  292. }
  293. /*
  294.   reinstate the blocking pane
  295. */
  296. .open-square.bomb:checked ~ .pane
  297. {
  298.   position:relative;
  299.   display:block;
  300. }
  301. .open-square.bomb:checked ~ .message:before {
  302.   content: " ): You Lose :(" !important;
  303. }
  304. /*
  305. display thre show bombs button if you've lost
  306. */
  307. .open-square.bomb:checked ~ .lost-game
  308. {
  309.   position:relative;
  310.   display:block;
  311.   z-index:2
  312. }
  313. /*
  314. display any hidden bombs
  315. */
  316. #show-bombs:checked ~ .bomb + .square > label {
  317.   display:none;
  318. }
  319. /*
  320. mark any falsely flagged squares as wrong.
  321. */
  322. #show-bombs:checked ~ .open-square:not(.bomb) + .square > .flag:after {
  323.   content: "鉂?;
  324.   position:absolute;
  325.   top:-0.4rem;
  326.   left:-0.1rem;
  327. }
  328.   </style>
  329. </head>
  330. <body>
  331. <div id="game">
  332.   <h1>Bomb CSSweeper</h1>
  333.   <input type="checkbox" id="show-bombs" hidden>
  334.   <input type="checkbox" id="flag-mode">
  335.   <label for="flag-mode">馃毄mode</label>
  336.   <div class="pane"></div>
  337.   <div class="score">Cleared:
  338.     <br>
  339.   </div>
  340.   <div class="bombs">Bombs:
  341.     <br>
  342.   </div>
  343.   <div class="message"></div>
  344.   <div class="lost-game">
  345.     <label for="show-bombs">Show Bombs</label>
  346.   </div>
  347. </div>
  348. <script>
  349. /*
  350.   Using Javascript to :
  351.   - generate a board of randomized size.
  352.   - randomly place bombs
  353.   - insert custom CSS to display board size properly
  354.   - find and group empty & ajacent squares
  355.     for fast clearing
  356.   - insert custom CSS to handle the group clearing.
  357. */
  358. // randomize board size and bomb count
  359. var rows = Math.floor(10 + (Math.random() * 5));
  360. var cols = Math.floor(10 + (Math.random() * 5));
  361. var bombs = Math.floor(15 + (Math.random() * 10));
  362. var i, s, l, fi, fs, fl;
  363. var game = document.getElementById('game');
  364. var pane = game.querySelector('.pane');
  365. var grid = [];
  366. //add stylesheet handle board size
  367. var style = document.createElement('style');
  368. //row wrapping.
  369. style.innerHTML = ".square:nth-of-type(" + cols + "n+1) {clear:left;}\n";
  370. game.insertBefore(style, pane);
  371. //add the grid elements
  372. for (var r = 1; r <= rows; r++) {
  373.   grid[r] = [];
  374.   for (var c = 1; c <= cols; c++) {
  375.     //controls the showing of a grid
  376.     i = grid[r][c] = document.createElement('input');
  377.     i.type = 'checkbox';
  378.     i.classList.add('open-square')
  379.     i.id = 'open-r' + r + '-c' + c;
  380.     i.r = r;
  381.     i.c = c;
  382.     game.insertBefore(i, pane);
  383.     //the actual grid square
  384.     s = document.createElement('span');
  385.     s.classList.add('square');
  386.     game.insertBefore(s, pane);
  387.     //hide the square contents
  388.     //and switches the show control
  389.     l = document.createElement('label');
  390.     l.htmlFor = i.id;
  391.     l.classList.add('open-label');
  392.     s.appendChild(l);
  393.     //flag switch control
  394.     fi = document.createElement('input');
  395.     fi.type = "checkbox";
  396.     fi.classList.add('flag-square');
  397.     fi.id = 'flag-r' + r + '-c' + c;
  398.     s.appendChild(fi);
  399.     //only visible is squre "flagged"
  400.     //hide the square contents
  401.     //and the show label
  402.     fs = document.createElement('span');
  403.     fs.classList.add('flag');
  404.     s.appendChild(fs);
  405.     //only visible in "flag mode"
  406.     //(if square not "shown")
  407.     //and switches the flag control
  408.     fl = document.createElement('label');
  409.     fl.htmlFor = fi.id;
  410.     fl.classList.add('flag-label');
  411.     s.appendChild(fl);
  412.   }
  413. }
  414. //randomly assign bomb state to some
  415. for (var b = 0; b < bombs; b++) {
  416.   do {
  417.     r = 1 + (Math.random() * rows) >> 0;
  418.     c = 1 + (Math.random() * cols) >> 0;
  419.     console.log(r, c, grid[r][c]);
  420.   } while (grid[r][c].classList.contains('bomb'));
  421.   i = grid[r][c];
  422.   i.classList.add('bomb');
  423.   //also increment
  424.   //surronding square's count
  425.   //to indicate number of adjacent
  426.   //bombs.
  427.   var list = getSurrounding(i)
  428.   for (r = 0; r < list.length; r++) {
  429.     i = list[r];
  430.     var count = Number(i.getAttribute('data-count')) || 0;
  431.     i.setAttribute('data-count', ++count);
  432.     i = i.nextElementSibling;
  433.     i.setAttribute('data-count', count);
  434.   }
  435. }
  436. //locate the groups of "empty" squares
  437. var group = 1;
  438. //while an ungrouped empty square exists
  439. while (i = document.querySelector('.open-square:not([data-count]):not(.grouped)')) {
  440.   //create an array of empty squares to group
  441.   var check = [i];
  442.   do {
  443.     //grab a square off the check list
  444.     i = check.pop();
  445.     //get surrounding squares
  446.     list = getSurrounding(i)
  447.     for (r = 0; r < list.length; r++) {
  448.       i = list[r];
  449.       //if they're not part of the group
  450.       if (!i.classList.contains('group' + group)) {
  451.         //mark them as so
  452.         i.classList.add('grouped');
  453.         i.classList.add('group' + group);
  454.         //if they're also empty
  455.         if (!i.getAttribute('data-count')) {
  456.           //add the m to the group check array
  457.           check.push(i);
  458.           //and point their label to the group master switch
  459.           i = document.querySelector('[for="' + i.id + '"]');
  460.           i.htmlFor = 'group' + group;
  461.         }
  462.       }
  463.     }
  464.     //repeat until all empty squares are grouped
  465.   } while (check.length);
  466.   //create the master switch
  467.   i = document.createElement('input');
  468.   i.type = 'checkbox';
  469.   i.classList.add('group-master');
  470.   i.hidden = true;
  471.   i.id = 'group' + group;
  472.   //add styles to disply all squares in the group
  473.   //and increment the cleared counter accordingly
  474.   style.innerHTML += '#group' + group + ':checked ~ .group' + group + ' + .square > label { display:none; }\n' + '#group' + group + ':checked ~ .group' + group + ' { counter-increment:cleared total }\n';
  475.   game.insertBefore(i, style);
  476.   //and move on to the next group (if any)
  477.   group++;
  478. }
  479. //grabs squares around a given square, inclusive
  480. function getSurrounding(i) {
  481.   var rs = Math.max(i.r - 1, 1);
  482.   var re = Math.min(i.r + 1, rows);
  483.   var cs = Math.max(i.c - 1, 1);
  484.   var ce = Math.min(i.c + 1, cols);
  485.   var result = [];
  486.   for (r = rs; r <= re; r++) {
  487.     for (c = cs; c <= ce; c++) {
  488.       result.push(grid[r][c]);
  489.     }
  490.   }
  491.   return result;
  492. }
  493. </script>
  494. </body>
  495. </html>
复制代码





免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

缠丝猫

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表