JSFiddle - React, Tailwind, and code Playground
by Nicolas PUJOL
JavaScript
"use strict";
var battleShipsGame = function() {
const DIRECTION_VERTICAL = 1,
GRID_SIZE = 10;
var x,
grid = (x = i => Array(GRID_SIZE).fill(i))(x(0)),
ships = [5, 4, 4];
function addShips(ships) {
ships.forEach(function(ship) {
placeShip(ship);
});
}
function createShipPosition(ship) {
let dir = Math.floor(Math.random() * 2),
col,
row;
if (dir === DIRECTION_VERTICAL) {
col = Math.floor(Math.random() * GRID_SIZE);
row = Math.floor(Math.random() * (GRID_SIZE - ship + 1));
} else {
col = Math.floor(Math.random() * (GRID_SIZE - ship + 1));
row = Math.floor(Math.random() * GRID_SIZE);
}
return {
'col': col,
'row': row,
'dir': dir,
'size': ship
};
}
function isValidShipPosition(pos) {
var i;
console.log(pos)
for (i = 0; i < ) {
}
return true;
}
function placeShip(ship) {
var position,
isValidPosition = false;
do {
position = createShipPosition(ship);
if (isValidShipPosition(position)) {
}
isValidPosition = true;
} while (!isValidPosition);
}
addShips(ships);
/*return {
'getGrid': function() {
return this.grid;
},
'grid':
//function() {}
/*grid status
0: no fire
1: no fire no empty
2: fire and touched
3: fire and empty
/*function placeBoats() {
}
//console.log(grid)
};*/
};
console.log(battleShipsGame())