diff --git a/battleship/src/components/gameboard.js b/battleship/src/components/gameboard.js index a223274..2ed75f9 100644 --- a/battleship/src/components/gameboard.js +++ b/battleship/src/components/gameboard.js @@ -32,7 +32,10 @@ class Gameboard { let cords = this.generateCoordinates(this.shipSize[i]); ship.coordinates = cords; - if (!this.checkForDuplicateCoordinates(ship)) { + if ( + !this.checkForDuplicateCoordinates(ship) && + !this.checkForAdjacentCoordinates(ship) + ) { break; } } @@ -66,7 +69,6 @@ class Gameboard { checkForDuplicateCoordinates(ship) { // needs to save coordinates to a list - let ships = []; let duplicates = false; if (this.ships.length > 0) { @@ -86,6 +88,63 @@ class Gameboard { return duplicates; } + checkForAdjacentCoordinates(ship) { + // needs to save coordinates to a list + let adjacent = false; + let test = []; + + const adj = [ + [-1, 1], + [-1, 0], + [-1, -1], + [0, -1], + [1, -1], + [1, 0], + [1, 1], + [0, 1], + ]; + + for (let y = 1; y < 11; y++) { + let rowData = []; + for (let x = 1; x < 11; x++) { + let adjPositions = []; + for (const [adjX, adjY] of adj) { + const tempX = x + adjX; + const tempY = y + adjY; + + if (tempX >= 1 && tempX < 11 && tempY >= 1 && tempY < 11) { + adjPositions.push([tempX, tempY]); + } + } + rowData.push(adjPositions); + } + test.push(rowData); + } + + if (this.ships.length > 0) { + this.ships.forEach((s) => { + for (let i = 0; i < ship.coordinates.length; i++) { + let x = ship.coordinates[i][1]; + let y = ship.coordinates[i][0]; + + let adjacentCords = test[x - 1][y - 1]; + + for (let t = 0; t < adjacentCords.length; t++) { + let check = s.coordinates.find( + (s) => + JSON.stringify(s) === + JSON.stringify(adjacentCords[t]), + ); + if (check) { + adjacent = true; + } + } + } + }); + } + return adjacent; + } + receiveAttack(coordinate) { let shipIndex; diff --git a/battleship/src/website.js b/battleship/src/website.js index d8dfdb8..93eda05 100644 --- a/battleship/src/website.js +++ b/battleship/src/website.js @@ -1,3 +1,5 @@ +import { forEach } from 'lodash'; + export default class Website { constructor(game) { this.game = game; @@ -26,6 +28,10 @@ export default class Website { let misses = player.gameboard.scoreboard.misses; let hits = player.gameboard.scoreboard.hits; + let shipCoordinates = player.gameboard.ships.map( + (ship) => ship.coordinates, + ); + let allShipCoordinates = shipCoordinates.concat(...shipCoordinates); for (let i = 1; i < 11; i++) { for (let j = 1; j < 11; j++) { @@ -35,6 +41,14 @@ export default class Website { cell.style.border = '1px solid black'; div.appendChild(cell); + if (player.playerName === 'Player1') { + allShipCoordinates.forEach((ship) => { + if (JSON.stringify([j, i]) === JSON.stringify(ship)) { + cell.style.backgroundColor = 'blue'; + } + }); + } + if (hits.has(JSON.stringify([j, i]))) { cell.style.backgroundColor = 'red'; } else if (misses.has(JSON.stringify([j, i]))) { @@ -44,11 +58,6 @@ export default class Website { } } - // player.gameboard.scoreboard.misses - // .entries() - // .array.forEach((element) => { - // console.log(element); - // }); return div; }