mirror of
https://gitea.smigz.com/smiggiddy/odin-codeprojects.git
synced 2025-06-27 20:45:35 -04:00
Battleship (#16)
* project: testing * feat: updated testing configs * feat: ship class and test * feat: more testing * feat: gameboard logic/tests complete * feat: add player methods * feat: add main app * sync: pushing latest code to repo * feat: add some basic styling and stuff * feat: added UI logic * feat: basic game is finished * feat: add adjacent rules * feat: basic game complete
This commit is contained in:
parent
d899a773bd
commit
099e6b13e7
23 changed files with 10431 additions and 9218 deletions
77
battleship/tests/gameboard.test.js
Normal file
77
battleship/tests/gameboard.test.js
Normal file
|
@ -0,0 +1,77 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
});
|
||||
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);
|
||||
});
|
18
battleship/tests/player.test.js
Normal file
18
battleship/tests/player.test.js
Normal file
|
@ -0,0 +1,18 @@
|
|||
import { Gameboard } from '../src/components/gameboard';
|
||||
class Player {
|
||||
constructor(name) {
|
||||
this.gameboard = new Gameboard();
|
||||
this.playerName = name;
|
||||
}
|
||||
}
|
||||
|
||||
let p = new Player('Player1');
|
||||
|
||||
it('should contain a gameboard', () => {
|
||||
expect(p.gameboard).toHaveProperty(
|
||||
'ships',
|
||||
'scoreboard',
|
||||
'shipCount',
|
||||
'sunkShipCount',
|
||||
);
|
||||
});
|
27
battleship/tests/ship.test.js
Normal file
27
battleship/tests/ship.test.js
Normal file
|
@ -0,0 +1,27 @@
|
|||
import { Ship } from '../src/components/ship';
|
||||
|
||||
it('should have length, number of times hit, and if sunk', () => {
|
||||
// Small ship
|
||||
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();
|
||||
});
|
||||
|
||||
it('is sunk', () => {
|
||||
const _ship = new Ship(2);
|
||||
_ship.hits = 2;
|
||||
expect(_ship.isSunk()).toBeTruthy();
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue