mirror of
https://gitea.smigz.com/smiggiddy/odin-codeprojects.git
synced 2024-12-27 06:40:42 -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() {
|
constructor() {
|
||||||
// some code
|
// some code
|
||||||
this.ships = [];
|
this.ships = [];
|
||||||
|
this.sunkShipCount = 0;
|
||||||
|
|
||||||
// load settings
|
// load settings
|
||||||
this.settings();
|
this.settings();
|
||||||
|
|
||||||
// Create Ships
|
// Create Ships
|
||||||
this.createShips();
|
this.createShips();
|
||||||
|
|
||||||
|
this.scoreboard = {
|
||||||
|
hits: new Set(),
|
||||||
|
misses: new Set(),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
settings() {
|
settings() {
|
||||||
|
@ -26,7 +32,7 @@ class Gameboard {
|
||||||
let cords = this.generateCoordinates(shipSize[i]);
|
let cords = this.generateCoordinates(shipSize[i]);
|
||||||
ship.coordinates = cords;
|
ship.coordinates = cords;
|
||||||
|
|
||||||
if (!this.checkForDuplicateCoordinates()) {
|
if (!this.checkForDuplicateCoordinates(ship)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,29 +64,55 @@ class Gameboard {
|
||||||
return coordinates;
|
return coordinates;
|
||||||
}
|
}
|
||||||
|
|
||||||
checkForDuplicateCoordinates() {
|
checkForDuplicateCoordinates(ship) {
|
||||||
// needs to save coordinates to a list
|
// needs to save coordinates to a list
|
||||||
let ships = [];
|
let ships = [];
|
||||||
let duplicates = false;
|
let duplicates = false;
|
||||||
|
|
||||||
if (this.ships.length > 0) {
|
if (this.ships.length > 0) {
|
||||||
this.ships.forEach((ship) => {
|
this.ships.forEach((s) => {
|
||||||
for (let i = 0; i < ship.coordinates.length; i++) {
|
for (let i = 0; i < ship.coordinates.length; i++) {
|
||||||
let check = ships.find(
|
let check = s.coordinates.find(
|
||||||
(s) =>
|
(s) =>
|
||||||
JSON.stringify(s) ===
|
JSON.stringify(s) ===
|
||||||
JSON.stringify(ship.coordinates[i]),
|
JSON.stringify(ship.coordinates[i]),
|
||||||
);
|
);
|
||||||
if (!check) ships.push(ship.coordinates[i]);
|
|
||||||
if (check) {
|
if (check) {
|
||||||
duplicates = true;
|
duplicates = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return duplicates;
|
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 };
|
export { Gameboard };
|
||||||
|
|
|
@ -46,7 +46,32 @@ it('should contain unique random coordinates for each ship', () => {
|
||||||
duplicates = true;
|
duplicates = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log(ship.coordinates);
|
|
||||||
});
|
});
|
||||||
expect(duplicates).toBeFalsy();
|
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