mirror of
https://gitea.smigz.com/smiggiddy/odin-codeprojects.git
synced 2024-12-26 22:30:44 -05:00
feat: gameboard logic/tests complete
This commit is contained in:
parent
670dd0f480
commit
904c4901da
2 changed files with 64 additions and 7 deletions
|
@ -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 };
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue