From 904c4901da39edfea705e66dc062e4ebdaba605f Mon Sep 17 00:00:00 2001 From: Mike Smith <89040888+smiggiddy@users.noreply.github.com> Date: Fri, 15 Mar 2024 15:46:33 -0400 Subject: [PATCH] feat: gameboard logic/tests complete --- battleship/src/components/gameboard.js | 44 ++++++++++++++++++++++---- battleship/tests/gameboard.test.js | 27 +++++++++++++++- 2 files changed, 64 insertions(+), 7 deletions(-) diff --git a/battleship/src/components/gameboard.js b/battleship/src/components/gameboard.js index baba4c4..750aa89 100644 --- a/battleship/src/components/gameboard.js +++ b/battleship/src/components/gameboard.js @@ -5,12 +5,18 @@ class Gameboard { constructor() { // some code this.ships = []; + this.sunkShipCount = 0; // load settings this.settings(); // Create Ships this.createShips(); + + this.scoreboard = { + hits: new Set(), + misses: new Set(), + }; } settings() { @@ -26,7 +32,7 @@ class Gameboard { let cords = this.generateCoordinates(shipSize[i]); ship.coordinates = cords; - if (!this.checkForDuplicateCoordinates()) { + if (!this.checkForDuplicateCoordinates(ship)) { break; } } @@ -58,29 +64,55 @@ class Gameboard { return coordinates; } - checkForDuplicateCoordinates() { + checkForDuplicateCoordinates(ship) { // needs to save coordinates to a list let ships = []; let duplicates = false; if (this.ships.length > 0) { - this.ships.forEach((ship) => { + this.ships.forEach((s) => { for (let i = 0; i < ship.coordinates.length; i++) { - let check = ships.find( + let check = s.coordinates.find( (s) => JSON.stringify(s) === JSON.stringify(ship.coordinates[i]), ); - if (!check) ships.push(ship.coordinates[i]); if (check) { duplicates = true; } } }); } - return duplicates; } + + receiveAttack(coordinate) { + let shipIndex; + + for (let i = 0; i < this.ships.length; i++) { + let attacked = this.ships[i].coordinates.find( + (c) => JSON.stringify(coordinate) === JSON.stringify(c), + ); + + if (attacked) { + shipIndex = i; + this.ships[i].hit(); + this.scoreboard.hits.add(coordinate); + return; + } + } + + if (!shipIndex) this.scoreboard.misses.add(coordinate); + } + + shipStatus() { + for (let i = 0; i < this.ships.length; i++) { + if (this.ships[i].isSunk()) { + this.sunkShipCount += 1; + this.ships.splice(i, 1); + } + } + } } export { Gameboard }; diff --git a/battleship/tests/gameboard.test.js b/battleship/tests/gameboard.test.js index 7435b3c..a164ca0 100644 --- a/battleship/tests/gameboard.test.js +++ b/battleship/tests/gameboard.test.js @@ -46,7 +46,32 @@ it('should contain unique random coordinates for each ship', () => { duplicates = true; } } - console.log(ship.coordinates); }); expect(duplicates).toBeFalsy(); }); + +it('should show a ship was hit and increase the hit count', () => { + let attacks = [8, 5]; + gb.ships[0].coordinates = [[8, 5]]; + gb.receiveAttack(attacks); + expect(gb.ships[0].hits).toBe(1); +}); + +it('should report if a ship is sunk', () => { + gb.ships[0].length = 1; + expect(gb.ships[0].isSunk()).toBeTruthy(); +}); + +it('should track hits and misses', () => { + expect(gb.scoreboard.hits.size).toBe(1); + expect(gb.scoreboard.misses.size).toBe(0); + + gb.receiveAttack([-1, -1]); + expect(gb.scoreboard.misses.size).toBe(1); +}); + +it('should have a sunk count of 1 and only four ships now', () => { + gb.shipStatus(); + expect(gb.ships).toHaveLength(4); + expect(gb.sunkShipCount).toBe(1); +});