sync: pushing latest code to repo

This commit is contained in:
Mike 2024-03-21 05:58:07 -04:00
parent e7ac5c84f9
commit ef506b304d
3 changed files with 48 additions and 11 deletions

View file

@ -1,14 +1,30 @@
export default class Game { export default class Game {
constructor(players) { constructor(players) {
this.player1 = players.player1; this.player1 = players.player1;
this.palyer2 = players.player2; this.player2 = players.player2;
this.main(); this.main();
} }
main() { main() {
// TODO take turns picking points to shoot a ship // TODO take turns picking points to shoot a ship
this.player1.gameboard.receiveAttack([1, 2]); // this.player1.gameboard.receiveAttack([1, 2]);
this.player1.gameboard.receiveAttack([3, 8]); // this.player1.gameboard.receiveAttack([3, 8]);
console.log(this.player1.gameboard); let count = 0;
while (count < 100) {
let move = this.player2.computerMoves(
this.player1.gameboard.scoreboard,
);
this.player1.gameboard.receiveAttack(move);
this.player1.gameboard.shipStatus();
console.log(
this.player1.gameboard.scoreboard,
move,
this.player1.gameboard.sunkShipCount,
);
if (this.player1.gameboard.sunkShipCount === 5) return;
count += 1;
}
} }
} }

View file

@ -12,6 +12,7 @@ class Gameboard {
// Create Ships // Create Ships
this.createShips(); this.createShips();
// Tracks the opponents attempts on the players gameboard
this.scoreboard = { this.scoreboard = {
hits: new Set(), hits: new Set(),
misses: new Set(), misses: new Set(),
@ -20,15 +21,15 @@ class Gameboard {
settings() { settings() {
this.shipCount = 5; this.shipCount = 5;
this.shipSize = [2, 3, 3, 4, 5];
} }
createShips() { createShips() {
let shipSize = [2, 3, 3, 4, 5]; for (let i = 0; i < this.shipSize.length; i++) {
for (let i = 0; i < shipSize.length; i++) { let ship = new Ship(this.shipSize[i]);
let ship = new Ship(shipSize[i]);
while (true) { while (true) {
let cords = this.generateCoordinates(shipSize[i]); let cords = this.generateCoordinates(this.shipSize[i]);
ship.coordinates = cords; ship.coordinates = cords;
if (!this.checkForDuplicateCoordinates(ship)) { if (!this.checkForDuplicateCoordinates(ship)) {
@ -95,18 +96,19 @@ class Gameboard {
if (attacked) { if (attacked) {
shipIndex = i; shipIndex = i;
this.ships[i].hit(); this.ships[shipIndex].hit();
this.scoreboard.hits.add(coordinate); this.scoreboard.hits.add(JSON.stringify(coordinate));
return; return;
} }
} }
if (!shipIndex) this.scoreboard.misses.add(coordinate); if (!shipIndex) this.scoreboard.misses.add(JSON.stringify(coordinate));
} }
shipStatus() { shipStatus() {
for (let i = 0; i < this.ships.length; i++) { for (let i = 0; i < this.ships.length; i++) {
if (this.ships[i].isSunk()) { if (this.ships[i].isSunk()) {
console.log(this.ships[i]);
this.sunkShipCount += 1; this.sunkShipCount += 1;
this.ships.splice(i, 1); this.ships.splice(i, 1);
} }

View file

@ -4,6 +4,25 @@ class Player {
this.gameboard = new Gameboard(); this.gameboard = new Gameboard();
this.playerName = name; this.playerName = name;
} }
computerMoves(scoreboard) {
function coordinates() {
let x = Math.floor(Math.random() * 10) + 1;
let y = Math.floor(Math.random() * 10) + 1;
return [x, y];
}
while (true) {
let potentialMove = coordinates();
console.log(
scoreboard.misses.has(JSON.stringify(potentialMove)),
potentialMove,
);
if (!scoreboard.misses.has(JSON.stringify(potentialMove))) {
return potentialMove;
}
}
}
} }
export { Player }; export { Player };