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
| const tools = { $: function (selector) { let elems = document.querySelectorAll(selector); if (elems.length === 1) { return elems[0]; } return elems; }, randomIntRange: function (min, max) { return parseInt(Math.random() * (max - min) + min, 10); }, randomColor: function () { return ( "rgb(" + randomIntRange(0, 255) + ", " + randomIntRange(0, 255) + ", " + randomIntRange(0, 255) + ")" ); }, compareGrid(g1, g2) { if (g1.dataset.col === g2.dataset.col && g1.dataset.row === g2.dataset.row) { return true; } else { return false; } }, };
Object.assign(window, tools);
var coloredGrids = [];
window.onload = function () { $("#init-submit").onclick = function (event) { event.preventDefault(); let rows = $("input[name=rows]").value; let cols = $("input[name=cols]").value; if (rows && cols) { $(".init").style.display = "none"; initGrids(rows, cols); } else { alert("请输入正确的数字"); } }; };
function initGrids(rows, cols) { let table = document.createElement("table"); table.className = "sketchpad";
document.addEventListener("keyup", handleKeyPress);
document.oncontextmenu = function (event) { event.preventDefault(); };
table.addEventListener("mousedown", handleClick, false);
for (let i = 0; i < rows; i++) { let tr = document.createElement("tr"); tr.className = "sketchpad-grid-row"; for (let j = 0; j < cols; j++) { let td = document.createElement("td"); td.className = "sketchpad-grid-col"; td.setAttribute("data-row", i); td.setAttribute("data-col", j); tr.appendChild(td); } table.appendChild(tr); }
$(".stage").appendChild(table); }
function handleKeyPress(event) { if (event.keyCode !== 67) { return; }
if (coloredGrids.length) { coloredGrids.forEach(function (grid, index) { grid.style.backgroundColor = randomColor(); }); } }
function handleClick(event) { if (event.button === 0) { event.target.style.backgroundColor = randomColor(); coloredGrids.push(event.target); } if (event.button === 2) { event.target.style.backgroundColor = ""; let index = coloredGrids.indexOf(event.target); coloredGrids.splice(index, 1); }
let colorIslands = countColorIslands(); let message = document.createElement("p"); message.className = "message"; message.innerHTML = "当前颜色岛数量为: <span>" + colorIslands + "<span>"; $(".container").appendChild(message); setTimeout(function() { $(".container").removeChild(message); }, 1000); }
function countColorIslands() { let temp = coloredGrids.slice(0); let visited = Array(temp.length).fill(false); let result = [];
function dfs(grid, index) { visited[index] = true; getColoredNeighbors(grid).forEach((i) => { if (!visited[i]) dfs(coloredGrids[i], i); }); };
temp.forEach((grid, index) => { if (!visited[index]) { result.push(grid); dfs(grid, index); } });
return result.length; }
function getColoredNeighbors(elem) { let x = +elem.dataset.col; let y = +elem.dataset.row; let neighbors = [];
for (let i = (x - 1 < 0 ? 0 : x - 1); i <= x + 1; i++) { for (let j = (y - 1 < 0 ? 0 : y - 1); j <= y + 1; j++) { if (!(i === x && j === y) && (i === x || j === y)) { if ($("td[data-col='" + i + "'][data-row='" + j + "']").style.backgroundColor !== "") { coloredGrids.forEach((grid, index) => { if (compareGrid($("td[data-col='" + i + "'][data-row='" + j + "']"), grid)) { neighbors.push(index); } }); } } } }
return neighbors; }
|