mirror of
https://gitea.smigz.com/smiggiddy/odin-codeprojects.git
synced 2024-12-26 22:30:44 -05:00
feat: add adjacent rules
This commit is contained in:
parent
00a52f578f
commit
39607492f8
2 changed files with 75 additions and 7 deletions
|
@ -32,7 +32,10 @@ class Gameboard {
|
||||||
let cords = this.generateCoordinates(this.shipSize[i]);
|
let cords = this.generateCoordinates(this.shipSize[i]);
|
||||||
ship.coordinates = cords;
|
ship.coordinates = cords;
|
||||||
|
|
||||||
if (!this.checkForDuplicateCoordinates(ship)) {
|
if (
|
||||||
|
!this.checkForDuplicateCoordinates(ship) &&
|
||||||
|
!this.checkForAdjacentCoordinates(ship)
|
||||||
|
) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -66,7 +69,6 @@ class Gameboard {
|
||||||
|
|
||||||
checkForDuplicateCoordinates(ship) {
|
checkForDuplicateCoordinates(ship) {
|
||||||
// needs to save coordinates to a list
|
// needs to save coordinates to a list
|
||||||
let ships = [];
|
|
||||||
let duplicates = false;
|
let duplicates = false;
|
||||||
|
|
||||||
if (this.ships.length > 0) {
|
if (this.ships.length > 0) {
|
||||||
|
@ -86,6 +88,63 @@ class Gameboard {
|
||||||
return duplicates;
|
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) {
|
receiveAttack(coordinate) {
|
||||||
let shipIndex;
|
let shipIndex;
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import { forEach } from 'lodash';
|
||||||
|
|
||||||
export default class Website {
|
export default class Website {
|
||||||
constructor(game) {
|
constructor(game) {
|
||||||
this.game = game;
|
this.game = game;
|
||||||
|
@ -26,6 +28,10 @@ export default class Website {
|
||||||
|
|
||||||
let misses = player.gameboard.scoreboard.misses;
|
let misses = player.gameboard.scoreboard.misses;
|
||||||
let hits = player.gameboard.scoreboard.hits;
|
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 i = 1; i < 11; i++) {
|
||||||
for (let j = 1; j < 11; j++) {
|
for (let j = 1; j < 11; j++) {
|
||||||
|
@ -35,6 +41,14 @@ export default class Website {
|
||||||
cell.style.border = '1px solid black';
|
cell.style.border = '1px solid black';
|
||||||
div.appendChild(cell);
|
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]))) {
|
if (hits.has(JSON.stringify([j, i]))) {
|
||||||
cell.style.backgroundColor = 'red';
|
cell.style.backgroundColor = 'red';
|
||||||
} else if (misses.has(JSON.stringify([j, i]))) {
|
} 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;
|
return div;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue