diff --git a/battleship/src/app.js b/battleship/src/app.js index 1fc9a84..186c2b4 100644 --- a/battleship/src/app.js +++ b/battleship/src/app.js @@ -1,14 +1,30 @@ export default class Game { constructor(players) { this.player1 = players.player1; - this.palyer2 = players.player2; + this.player2 = players.player2; this.main(); } main() { // TODO take turns picking points to shoot a ship - this.player1.gameboard.receiveAttack([1, 2]); - this.player1.gameboard.receiveAttack([3, 8]); - console.log(this.player1.gameboard); + // this.player1.gameboard.receiveAttack([1, 2]); + // this.player1.gameboard.receiveAttack([3, 8]); + 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; + } } } diff --git a/battleship/src/components/gameboard.js b/battleship/src/components/gameboard.js index 5b614e7..d15c16e 100644 --- a/battleship/src/components/gameboard.js +++ b/battleship/src/components/gameboard.js @@ -12,6 +12,7 @@ class Gameboard { // Create Ships this.createShips(); + // Tracks the opponents attempts on the players gameboard this.scoreboard = { hits: new Set(), misses: new Set(), @@ -20,15 +21,15 @@ class Gameboard { settings() { this.shipCount = 5; + this.shipSize = [2, 3, 3, 4, 5]; } createShips() { - let shipSize = [2, 3, 3, 4, 5]; - for (let i = 0; i < shipSize.length; i++) { - let ship = new Ship(shipSize[i]); + for (let i = 0; i < this.shipSize.length; i++) { + let ship = new Ship(this.shipSize[i]); while (true) { - let cords = this.generateCoordinates(shipSize[i]); + let cords = this.generateCoordinates(this.shipSize[i]); ship.coordinates = cords; if (!this.checkForDuplicateCoordinates(ship)) { @@ -95,18 +96,19 @@ class Gameboard { if (attacked) { shipIndex = i; - this.ships[i].hit(); - this.scoreboard.hits.add(coordinate); + this.ships[shipIndex].hit(); + this.scoreboard.hits.add(JSON.stringify(coordinate)); return; } } - if (!shipIndex) this.scoreboard.misses.add(coordinate); + if (!shipIndex) this.scoreboard.misses.add(JSON.stringify(coordinate)); } shipStatus() { for (let i = 0; i < this.ships.length; i++) { if (this.ships[i].isSunk()) { + console.log(this.ships[i]); this.sunkShipCount += 1; this.ships.splice(i, 1); } diff --git a/battleship/src/components/player.js b/battleship/src/components/player.js index 3970185..4cbaa3d 100644 --- a/battleship/src/components/player.js +++ b/battleship/src/components/player.js @@ -4,6 +4,25 @@ class Player { this.gameboard = new Gameboard(); 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 };