mirror of
https://gitea.smigz.com/smiggiddy/odin-codeprojects.git
synced 2024-12-27 06:40:42 -05:00
sync: pushing latest code to repo
This commit is contained in:
parent
e7ac5c84f9
commit
ef506b304d
3 changed files with 48 additions and 11 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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 };
|
||||
|
|
Loading…
Reference in a new issue