43 lines
958 B
JavaScript
43 lines
958 B
JavaScript
|
import CellSet;
|
||
|
import Grid;
|
||
|
import Toolbox;
|
||
|
|
||
|
return {
|
||
|
ize: colorize,
|
||
|
paint: paint
|
||
|
};
|
||
|
|
||
|
function colorize(row, column, color) {
|
||
|
var grid = Grid.get();
|
||
|
grid.data[row][column] = color || Toolbox.color();
|
||
|
Grid.cell(row, column).className = grid.data[row][column];
|
||
|
grid.missing.remove(row, column);
|
||
|
}
|
||
|
|
||
|
function paint(i0, j0) {
|
||
|
var originColor = Grid.get().data[i0][j0];
|
||
|
var done = CellSet.make();
|
||
|
var queue = [{i: i0, j: j0}];
|
||
|
while(queue.length > 0) {
|
||
|
var p0 = queue[0];
|
||
|
colorize(p0.i, p0.j);
|
||
|
done.add(p0.i, p0.j);
|
||
|
extend(p0, queue, done, originColor);
|
||
|
queue.shift();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function extend(p0, queue, done, originColor) {
|
||
|
var size = Grid.get().size;
|
||
|
for(var d = -1; d < 2; d += 2) {
|
||
|
[{i: p0.i + d, j: p0.j}, {i: p0.i, j: p0.j + d}].forEach(function(p1) {
|
||
|
if(p1.i >= 0 && p1.i < size && p1.j >= 0 && p1.j < size
|
||
|
&& !done.contains(p1.i, p1.j)
|
||
|
&& Grid.get().data[p1.i][p1.j] == originColor) {
|
||
|
queue.push(p1);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|