src/createInitializedGameBoardFromQueryString.js
/**
 * This creates a GameBoard and initializes it with values from the query string.
 */
This creates a GameBoard and initializes it with values from the query string.
This creates a GameBoard and initializes it with values from the query string .
function createInitializedGameBoardFromQueryString(qs) {
  const board = new GameBoard(qs.get('w'), qs.get('h'), qs.get('l'), qs.get('s'));
  decodeMoveSequence(getMoveSequence(qs), board.width).forEach(playMoveFromQueryString);
  board.on('change', throttle(updateUrl));
  return board;
  function updateUrl() {
    let sequence = encodeMoveSequence(board.positions, board.width);
    qs.set('m', sequence.mainQuery);
    if (sequence.overflow) {
      window.location.hash = '#o=' + sequence.overflow;
    } else {
      window.location.hash = '';
    }
  }
  function playMoveFromQueryString(m) {
    try {
      board.play(m[0], m[1]);
    } catch (e) {
      console.error(e.message);
    }
  }
}
function createInitializedGameBoardFromQueryString ( qs ) { const board = new GameBoard ( qs . get ( 'w' ) , qs . get ( 'h' ) , qs . get ( 'l' ) , qs . get ( 's' ) ) ; decodeMoveSequence ( getMoveSequence ( qs ) , board . width ) . forEach ( playMoveFromQueryString ) ; board . on ( 'change' , throttle ( updateUrl ) ) ; return board ; function updateUrl ( ) { let sequence = encodeMoveSequence ( board . positions , board . width ) ; qs . set ( 'm' , sequence . mainQuery ) ; if ( sequence . overflow ) { window . location . hash = '#o=' + sequence . overflow ; } else { window . location . hash = '' ; } } function playMoveFromQueryString ( m ) { try { board . play ( m [ 0 ] , m [ 1 ] ) ; } catch ( e ) { console . error ( e . message ) ; } } }
This creates a GameBoard and initializes it with values from the query string.
This creates a GameBoard and initializes it with values from the query string .
// first, replay the moves from the query string // then start listen to board changes and update query string as necessary: // Query string could not be related to have a valid move.