Commit 6d962ad0 authored by satan's avatar satan
Browse files

fix: setCell preventing cell modification during war

parent 9199a551
......@@ -60,12 +60,12 @@ export default {
function edit(g: Grid, [x, y]: [x: number, y: number]) {
const old = getCell(g, x, y);
if (old === Cell.UNKNOWN) {
setCell(g, x, y, Cell.SHIP);
setCell(g, x, y, Cell.SHIP, { editing: true });
return;
}
if (old === Cell.SHIP) {
setCell(g, x, y, Cell.UNKNOWN);
setCell(g, x, y, Cell.UNKNOWN, { editing: true });
return;
}
}
......
......@@ -110,11 +110,19 @@ export function getCell(grid: Grid, x: number, y: number) {
return cell;
}
export function setCell(grid: Grid, x: number, y: number, cell: Cell) {
if (getCell(grid, x - 1, y - 1) === Cell.SHIP) return;
if (getCell(grid, x + 1, y - 1) === Cell.SHIP) return;
if (getCell(grid, x + 1, y + 1) === Cell.SHIP) return;
if (getCell(grid, x - 1, y + 1) === Cell.SHIP) return;
export function setCell(
grid: Grid,
x: number,
y: number,
cell: Cell,
{ editing = false }: { editing?: boolean } = {}
) {
if (editing && cell === Cell.SHIP) {
if (getCell(grid, x - 1, y - 1) === Cell.SHIP) return;
if (getCell(grid, x + 1, y - 1) === Cell.SHIP) return;
if (getCell(grid, x + 1, y + 1) === Cell.SHIP) return;
if (getCell(grid, x - 1, y + 1) === Cell.SHIP) return;
}
grid.set(`${x | 0},${y | 0}`, cell);
return true;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment