|
| 1 | +import { |
| 2 | + PointQueue, |
| 3 | +} from "./PointQueue.js" |
| 4 | +import { |
| 5 | + PointList, |
| 6 | +} from "./PointList.js" |
| 7 | +import { |
| 8 | + PointSet, |
| 9 | +} from "./PointSet.js" |
| 10 | +import { |
| 11 | + hasStone, |
| 12 | + BLACK, |
| 13 | + WHITE, |
| 14 | + COLORS, |
| 15 | + TERRITORY_B, |
| 16 | + TERRITORY_W, |
| 17 | + TERRITORY, |
| 18 | + REMOVED_B, |
| 19 | + REMOVED_W, |
| 20 | + TOGGLE_B, |
| 21 | + TOGGLE_W, |
| 22 | + TOGGLE_STUFF, |
| 23 | +} from "../util.js" |
| 24 | + |
| 25 | +export function count(board) { |
| 26 | + let acc = createAcc(board.length) |
| 27 | + for (let y = 0; y < board.length; y++) { |
| 28 | + let row = board[y] |
| 29 | + for (let x = 0; x < row.length; x++) { |
| 30 | + if (acc[y][x] === -1) { |
| 31 | + colorEmptyTerritory(board, acc, x, y) |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + return acc |
| 36 | +} |
| 37 | + |
| 38 | +function updateToggleStonesAt(board, x, y, color, pointsChecked, pointsToCheck) { |
| 39 | + let dim = board.length |
| 40 | + if (Math.min(x, y) < 0 || Math.max(x, y) >= dim) { |
| 41 | + return |
| 42 | + } |
| 43 | + let c = board[y][x] |
| 44 | + if (c !== color) { |
| 45 | + return |
| 46 | + } |
| 47 | + if (pointsChecked.has(x, y)) { |
| 48 | + return |
| 49 | + } |
| 50 | + pointsChecked.add(x, y) |
| 51 | + pointsToCheck.offer(x, y) |
| 52 | +} |
| 53 | + |
| 54 | +export function toggleStonesAt(board, xx, yy) { |
| 55 | + let color = board[yy][xx] |
| 56 | + if (!(color & TOGGLE_STUFF)) { |
| 57 | + return board |
| 58 | + } |
| 59 | + let dim = board.length |
| 60 | + let result = new PointList(dim) |
| 61 | + let pointsToCheck = new PointQueue(dim) |
| 62 | + let pointsChecked = new PointSet(dim) |
| 63 | + pointsChecked.add(xx, yy) |
| 64 | + pointsToCheck.offer(xx, yy) |
| 65 | + while (!pointsToCheck.isEmpty()) { |
| 66 | + let ptId = pointsToCheck.poll() |
| 67 | + let y = Math.trunc(ptId / dim) |
| 68 | + let x = ptId % dim |
| 69 | + result.add(x, y) |
| 70 | + updateToggleStonesAt(board, x, y - 1, color, pointsChecked, pointsToCheck) |
| 71 | + updateToggleStonesAt(board, x, y + 1, color, pointsChecked, pointsToCheck) |
| 72 | + updateToggleStonesAt(board, x - 1, y, color, pointsChecked, pointsToCheck) |
| 73 | + updateToggleStonesAt(board, x + 1, y, color, pointsChecked, pointsToCheck) |
| 74 | + } |
| 75 | + let updated = board.slice() |
| 76 | + result.forEach((x, y) => { |
| 77 | + if (updated[y] === board[y]) { |
| 78 | + updated[y] = board[y].slice() |
| 79 | + } |
| 80 | + updated[y][x] = toggleRemoved(updated[y][x]) |
| 81 | + }) |
| 82 | + return updated |
| 83 | +} |
| 84 | + |
| 85 | +export function resetCounting(board) { |
| 86 | + let dim = board.length |
| 87 | + let updated = board.slice() |
| 88 | + for (let y = 0; y < dim; y++) { |
| 89 | + updated[y] = board[y].slice() |
| 90 | + for (let x = 0; x < dim; x++) { |
| 91 | + updated[y][x] = resurrect(board[y][x]) & COLORS |
| 92 | + } |
| 93 | + } |
| 94 | + return updated |
| 95 | +} |
| 96 | + |
| 97 | +function updateFindColor(dim, x, y, pointsChecked, pointsToCheck) { |
| 98 | + if (Math.min(x, y) < 0 || Math.max(x, y) >= dim) { |
| 99 | + return |
| 100 | + } |
| 101 | + if (pointsChecked.has(x, y)) { |
| 102 | + return |
| 103 | + } |
| 104 | + pointsChecked.add(x, y) |
| 105 | + pointsToCheck.offer(x, y) |
| 106 | +} |
| 107 | + |
| 108 | +function findColor(board, xx, yy) { |
| 109 | + if (hasStone(board[yy][xx])) { |
| 110 | + return board[yy][xx] |
| 111 | + } |
| 112 | + let dim = board.length |
| 113 | + let pointsChecked = new PointSet(dim) |
| 114 | + let color = -1 |
| 115 | + pointsChecked.add(xx, yy) |
| 116 | + let pointsToCheck = new PointQueue(dim) |
| 117 | + pointsToCheck.offer(xx, yy) |
| 118 | + while (!pointsToCheck.isEmpty()) { |
| 119 | + let ptId = pointsToCheck.poll() |
| 120 | + let y = Math.trunc(ptId / dim) |
| 121 | + let x = ptId % dim |
| 122 | + if (hasStone(board[y][x])) { |
| 123 | + if (color === -1 || color === board[y][x]) { |
| 124 | + color = board[y][x] |
| 125 | + } else { |
| 126 | + return 0 // disputed area |
| 127 | + } |
| 128 | + } |
| 129 | + updateFindColor(dim, x, y - 1, pointsChecked, pointsToCheck) |
| 130 | + updateFindColor(dim, x, y + 1, pointsChecked, pointsToCheck) |
| 131 | + updateFindColor(dim, x - 1, y, pointsChecked, pointsToCheck) |
| 132 | + updateFindColor(dim, x + 1, y, pointsChecked, pointsToCheck) |
| 133 | + } |
| 134 | + return color |
| 135 | +} |
| 136 | + |
| 137 | +function updateColorEmptyTerritory(board, x, y, found, acc, pointsToCheck) { |
| 138 | + let dim = board.length |
| 139 | + if (Math.min(x, y) < 0 || Math.max(x, y) >= dim) { |
| 140 | + return |
| 141 | + } |
| 142 | + if (acc[y][x] !== -1) { |
| 143 | + return |
| 144 | + } |
| 145 | + let color = board[y][x] |
| 146 | + if (!isEmpty(color)) { |
| 147 | + return |
| 148 | + } |
| 149 | + acc[y][x] = !found ? 0 : getTerritoryMarker(found, color) |
| 150 | + pointsToCheck.offer(x, y) |
| 151 | +} |
| 152 | + |
| 153 | +function colorEmptyTerritory(board, acc, xx, yy) { |
| 154 | + if (hasStone(board[yy][xx])) { |
| 155 | + acc[yy][xx] = board[yy][xx] |
| 156 | + return |
| 157 | + } |
| 158 | + let found = findColor(board, xx, yy) |
| 159 | + if (found === -1) { // empty board |
| 160 | + for (let row of acc) { |
| 161 | + row.fill(0) |
| 162 | + } |
| 163 | + return |
| 164 | + } |
| 165 | + let dim = board.length |
| 166 | + let pointsToCheck = new PointQueue(dim) |
| 167 | + acc[yy][xx] = getTerritoryMarker(found, board[yy][xx]) |
| 168 | + pointsToCheck.offer(xx, yy) |
| 169 | + while (!pointsToCheck.isEmpty()) { |
| 170 | + let ptId = pointsToCheck.poll() |
| 171 | + let y = Math.trunc(ptId / dim) |
| 172 | + let x = ptId % dim |
| 173 | + updateColorEmptyTerritory(board, x, y - 1, found, acc, pointsToCheck) |
| 174 | + updateColorEmptyTerritory(board, x, y + 1, found, acc, pointsToCheck) |
| 175 | + updateColorEmptyTerritory(board, x - 1, y, found, acc, pointsToCheck) |
| 176 | + updateColorEmptyTerritory(board, x + 1, y, found, acc, pointsToCheck) |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +function getTerritoryMarker(found, empty) { |
| 181 | + if ((empty & asRemoved(found)) !== 0) { |
| 182 | + return found // resurrect |
| 183 | + } |
| 184 | + return asTerritory(found) | (empty & ~TERRITORY) |
| 185 | +} |
| 186 | + |
| 187 | +function asTerritory(color) { |
| 188 | + if (color === BLACK) { |
| 189 | + return TERRITORY_B |
| 190 | + } |
| 191 | + if (color === WHITE) { |
| 192 | + return TERRITORY_W |
| 193 | + } |
| 194 | + return color |
| 195 | +} |
| 196 | + |
| 197 | +function asRemoved(color) { |
| 198 | + if (color === BLACK) { |
| 199 | + return REMOVED_B |
| 200 | + } |
| 201 | + if (color === WHITE) { |
| 202 | + return REMOVED_W |
| 203 | + } |
| 204 | + return color |
| 205 | +} |
| 206 | + |
| 207 | +function toggleRemoved(color) { |
| 208 | + if (color & TOGGLE_B) { |
| 209 | + return color ^ TOGGLE_B |
| 210 | + } |
| 211 | + if (color & TOGGLE_W) { |
| 212 | + return color ^ TOGGLE_W |
| 213 | + } |
| 214 | + return color |
| 215 | +} |
| 216 | + |
| 217 | +function resurrect(color) { |
| 218 | + if (color & REMOVED_B) { |
| 219 | + return color ^ TOGGLE_B |
| 220 | + } |
| 221 | + if (color & REMOVED_W) { |
| 222 | + return color ^ TOGGLE_W |
| 223 | + } |
| 224 | + return color |
| 225 | +} |
| 226 | + |
| 227 | +function createAcc(dim) { |
| 228 | + let result = Array(dim) |
| 229 | + for (let y = 0; y < dim; y++) { |
| 230 | + result[y] = new Int32Array(dim).fill(-1) |
| 231 | + } |
| 232 | + return result |
| 233 | +} |
| 234 | + |
| 235 | +function isEmpty(color) { |
| 236 | + return (color & COLORS) === 0 |
| 237 | +} |
0 commit comments