|
1 | 1 | import {
|
2 | 2 | PointQueue,
|
3 | 3 | } from "./PointQueue.js"
|
4 |
| -import { |
5 |
| - PointList, |
6 |
| -} from "./PointList.js" |
7 | 4 | import {
|
8 | 5 | PointSet,
|
9 | 6 | } from "./PointSet.js"
|
@@ -57,28 +54,24 @@ export function toggleStonesAt(board, xx, yy) {
|
57 | 54 | return board
|
58 | 55 | }
|
59 | 56 | let dim = board.length
|
60 |
| - let result = new PointList(dim) |
61 | 57 | let pointsToCheck = new PointQueue(dim)
|
62 | 58 | let pointsChecked = new PointSet(dim)
|
63 | 59 | pointsChecked.add(xx, yy)
|
64 | 60 | pointsToCheck.offer(xx, yy)
|
| 61 | + let updated = board.slice() |
65 | 62 | while (!pointsToCheck.isEmpty()) {
|
66 | 63 | let ptId = pointsToCheck.poll()
|
67 | 64 | let y = Math.trunc(ptId / dim)
|
68 | 65 | let x = ptId % dim
|
69 |
| - result.add(x, y) |
| 66 | + if (updated[y] === board[y]) { |
| 67 | + updated[y] = board[y].slice() |
| 68 | + } |
| 69 | + updated[y][x] = toggleRemoved(board[y][x]) |
70 | 70 | updateToggleStonesAt(board, x, y - 1, color, pointsChecked, pointsToCheck)
|
71 | 71 | updateToggleStonesAt(board, x, y + 1, color, pointsChecked, pointsToCheck)
|
72 | 72 | updateToggleStonesAt(board, x - 1, y, color, pointsChecked, pointsToCheck)
|
73 | 73 | updateToggleStonesAt(board, x + 1, y, color, pointsChecked, pointsToCheck)
|
74 | 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 | 75 | return updated
|
83 | 76 | }
|
84 | 77 |
|
|
0 commit comments