From 670dd0f4804ea266484b816f3afa2665e4131fb9 Mon Sep 17 00:00:00 2001 From: Mike Smith <89040888+smiggiddy@users.noreply.github.com> Date: Fri, 15 Mar 2024 12:12:11 -0400 Subject: [PATCH] feat: more testing --- battleship/package-lock.json | 6 +- battleship/package.json | 3 + battleship/src/components/gameboard.js | 86 ++++++++++++++++++++++++++ battleship/src/components/ship.js | 22 +++---- battleship/tests/gameboard.test.js | 54 +++++++++++++++- battleship/tests/ship.test.js | 20 +++--- 6 files changed, 164 insertions(+), 27 deletions(-) diff --git a/battleship/package-lock.json b/battleship/package-lock.json index e91a9b1..a22bb9b 100644 --- a/battleship/package-lock.json +++ b/battleship/package-lock.json @@ -8,6 +8,9 @@ "name": "webpack-template", "version": "1.0.0", "license": "ISC", + "dependencies": { + "lodash": "^4.17.21" + }, "devDependencies": { "@babel/core": "^7.23.9", "@babel/preset-env": "^7.23.9", @@ -6956,8 +6959,7 @@ "node_modules/lodash": { "version": "4.17.21", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", - "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", - "dev": true + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==" }, "node_modules/lodash.debounce": { "version": "4.0.8", diff --git a/battleship/package.json b/battleship/package.json index 21da802..2c4f89d 100644 --- a/battleship/package.json +++ b/battleship/package.json @@ -29,5 +29,8 @@ "webpack-cli": "^5.1.4", "webpack-dev-server": "^4.15.1", "webpack-merge": "^5.10.0" + }, + "dependencies": { + "lodash": "^4.17.21" } } diff --git a/battleship/src/components/gameboard.js b/battleship/src/components/gameboard.js index e69de29..baba4c4 100644 --- a/battleship/src/components/gameboard.js +++ b/battleship/src/components/gameboard.js @@ -0,0 +1,86 @@ +import { Ship } from './ship'; +let _ = require('lodash'); + +class Gameboard { + constructor() { + // some code + this.ships = []; + + // load settings + this.settings(); + + // Create Ships + this.createShips(); + } + + settings() { + this.shipCount = 5; + } + + createShips() { + let shipSize = [2, 3, 3, 4, 5]; + for (let i = 0; i < shipSize.length; i++) { + let ship = new Ship(shipSize[i]); + + while (true) { + let cords = this.generateCoordinates(shipSize[i]); + ship.coordinates = cords; + + if (!this.checkForDuplicateCoordinates()) { + break; + } + } + this.ships.push(ship); + } + } + + generateCoordinates(size) { + const coordinates = []; + const direction = + Math.floor(Math.random() * 2) === 1 ? 'vertical' : 'horizontal'; + // if direction == vertial.. x should be the same.. else y will be the same + let x = Math.floor(Math.random() * 10) + 1; + let y = Math.floor(Math.random() * 10) + 1; + let startingPoint = Math.floor(Math.random() * 9) + 1; + + if (startingPoint > 10 - size) startingPoint -= size; + + for (let i = 1; i < size + 1; i++) { + if (direction === 'vertical') { + y = i + startingPoint; + coordinates.push([x, y]); + } else { + x = i + startingPoint; + coordinates.push([x, y]); + } + } + + return coordinates; + } + + checkForDuplicateCoordinates() { + // needs to save coordinates to a list + let ships = []; + let duplicates = false; + + if (this.ships.length > 0) { + this.ships.forEach((ship) => { + for (let i = 0; i < ship.coordinates.length; i++) { + let check = ships.find( + (s) => + JSON.stringify(s) === + JSON.stringify(ship.coordinates[i]), + ); + if (!check) ships.push(ship.coordinates[i]); + if (check) { + duplicates = true; + } + } + }); + } + + return duplicates; + } +} + +export { Gameboard }; diff --git a/battleship/src/components/ship.js b/battleship/src/components/ship.js index bc8da0b..811114e 100644 --- a/battleship/src/components/ship.js +++ b/battleship/src/components/ship.js @@ -1,16 +1,16 @@ class Ship { - constructor(length) { - this.length = length; - this.hits = 0; - } + constructor(length) { + this.length = length; + this.hits = 0; + } - hit() { - this.hits += 1 - } + hit() { + this.hits += 1; + } - isSunk() { - return this.hits === this.length - } + isSunk() { + return this.hits === this.length; + } } -export { Ship } +export { Ship }; diff --git a/battleship/tests/gameboard.test.js b/battleship/tests/gameboard.test.js index c504ad3..7435b3c 100644 --- a/battleship/tests/gameboard.test.js +++ b/battleship/tests/gameboard.test.js @@ -1,4 +1,52 @@ -it('should place ships at random coordinates', () => { - const gb = new Gameboard(); +import { Gameboard } from '../src/components/gameboard'; -}) +const gb = new Gameboard(); +it('should place ships at random coordinates', () => { + // wait +}); + +it('should return random coordinates', () => { + const size = 3; + const coordinates = gb.generateCoordinates(size); + + for (let i = 0; i < coordinates.length; i++) { + expect(coordinates[i][0]).toBeLessThan(11); + expect(coordinates[i][1]).toBeLessThan(11); + expect(coordinates[i][0]).toBeGreaterThan(0); + expect(coordinates[i][1]).toBeGreaterThan(0); + } + // expect(coordinates).toBe(3); + expect(coordinates).toHaveLength(size); +}); + +it('should create 5 ships', () => { + expect(gb.ships).toHaveLength(5); +}); + +it('should create 5 ships with lengths of 2,3,3,4,5', () => { + const size = [2, 3, 3, 4, 5]; + for (let i = 0; i < 5; i++) { + expect(gb.ships[i].length).toBe(size[i]); + } +}); + +it('should contain unique random coordinates for each ship', () => { + let ships = []; + let count = 0; + let duplicates = false; + + gb.ships.forEach((ship) => { + for (let i = 0; i < ship.coordinates.length; i++) { + let test = ships.find( + (s) => + JSON.stringify(s) === JSON.stringify(ship.coordinates[i]), + ); + if (!test) ships.push(ship.coordinates[i]); + if (test) { + duplicates = true; + } + } + console.log(ship.coordinates); + }); + expect(duplicates).toBeFalsy(); +}); diff --git a/battleship/tests/ship.test.js b/battleship/tests/ship.test.js index b047a96..b297e82 100644 --- a/battleship/tests/ship.test.js +++ b/battleship/tests/ship.test.js @@ -1,29 +1,27 @@ -import { Ship } from "../src/components/ship"; - +import { Ship } from '../src/components/ship'; it('should have length, number of times hit, and if sunk', () => { // Small ship - const _ship = new Ship(2) + const _ship = new Ship(2); expect(_ship).toEqual({ length: 2, hits: 0, - }) - -}) + }); +}); it('if hit, hit count should increase', () => { const _ship = new Ship(2); _ship.hit(); expect(_ship.hits).toBe(1); -}) +}); it('is not sunk', () => { const _ship = new Ship(2); - expect(_ship.isSunk()).toBeFalsy() -}) + expect(_ship.isSunk()).toBeFalsy(); +}); it('is sunk', () => { const _ship = new Ship(2); _ship.hits = 2; - expect(_ship.isSunk()).toBeTruthy() -}) + expect(_ship.isSunk()).toBeTruthy(); +});