module/src/grid-selection-strategy.js
/**
     * Creates a strategy that places ships at unique random points within the grid.
     * @type {GridSelectionStrategy}
     * @readonly
     */
Creates a strategy that places ships at unique random points within the grid.
Creates a strategy that places ships at unique random points within the grid .
static get RANDOM_PLACEMENT() {
         as such, our implementation will take the following crude strategy:
         sort our ships by area (size)
         iterate each ship
           randomly iterate every unique available point in the grid where available means no other ship intersects this point
             iterate every rotation around that point
               validate the ship is contained within the grid
               validate the ship does not intersect any other placed ships
               place the ship and continue to the next ship
             on failing to place the ship in any rotation; contine to next available position
           on failing to place the ship at any available point; restart placement from scratch
           unless we reach n restarts, where n is arbitrarily large to ensure our algorithm is halting
        return playerState => {
             devnote: we take a copy of the original ships since we're going to sort them
            const ships = [...playerState.grid.ships];
             sort our ships by largest first
             todo: this should be part of random placement
             ships.sort((a, b) => a.geometry.area - b.geometry.area);
            const placements = getRandomPlacement(ships, playerState.grid.dimensions);
             if we got this far then let's assign the placement positions of all our ships
            for (let [ship, [position, rotation]] of placements) {
                ship.position = position;
                ship.rotation = rotation;
            }
        };
    }
static get RANDOM_PLACEMENT ( ) { return playerState => { const ships = [ ... playerState . grid . ships ] ; const placements = getRandomPlacement ( ships , playerState . grid . dimensions ) ; for ( let [ ship , [ position , rotation ] ] of placements ) { ship . position = position ; ship . rotation = rotation ; } } ; }
Creates a strategy that places ships at unique random points within the grid.
Creates a strategy that places ships at unique random points within the grid .
// devnote: essentially here we're entering into bin packing world, realistically // though, our domain of packing is so sparse we'll never need to consider bin // packing and arguably, if we cannot sparsely (or non-uniformly) arrange our // ships then we're dealing with a grid that is soo dense that this becomes // a race to who will luck out the most and avoid any holes in the grid. // // as such, our implementation will take the following crude strategy: // sort our ships by area (size) // iterate each ship //   randomly iterate every unique available point in the grid where available means no other ship intersects this point //     iterate every rotation around that point //       validate the ship is contained within the grid //       validate the ship does not intersect any other placed ships //       place the ship and continue to the next ship //     on failing to place the ship in any rotation; contine to next available position //   on failing to place the ship at any available point; restart placement from scratch //   unless we reach n restarts, where n is arbitrarily large to ensure our algorithm is halting // devnote: we take a copy of the original ships since we're going to sort them // sort our ships by largest first // todo: this should be part of random placement // ships.sort((a, b) => a.geometry.area - b.geometry.area); // if we got this far then let's assign the placement positions of all our ships