From f0f5b2d9966df1fa7b036ab10a07333629841e93 Mon Sep 17 00:00:00 2001 From: Scalaptia Date: Sun, 17 Mar 2024 21:55:15 -0700 Subject: [PATCH] Fix ship collission when placing new ships --- src/components/board.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/components/board.ts b/src/components/board.ts index 8ce89ab..04a882f 100644 --- a/src/components/board.ts +++ b/src/components/board.ts @@ -16,29 +16,41 @@ export const createBoard = (width: number, height: number): Gameboard => { this.ships.push(ship); if (vertical) { + // Check wall collision if (y + ship.length > this.boardGrid.length) { this.ships.pop(); return false; } + + // Check ship collision for (let i = 0; i < ship.length; i++) { if (this.boardGrid[y + i][x].ship) { this.ships.pop(); return false; } + } + // Add ship to board + for (let i = 0; i < ship.length; i++) { this.boardGrid[y + i][x].ship = ship; } } else { + // Check wall collision if (x + ship.length > this.boardGrid[0].length) { this.ships.pop(); return false; } + + // Check ship collision for (let i = 0; i < ship.length; i++) { if (this.boardGrid[y][x + i].ship) { this.ships.pop(); return false; } + } + // Add ship to board + for (let i = 0; i < ship.length; i++) { this.boardGrid[y][x + i].ship = ship; } }