src/GameBoard.js
/**
   * Places a symbol at given position. If position is already occupied,
   * it returns false. Otherwise it returns true.
   */
Places a symbol at given position. If position is already occupied,
it returns false. Otherwise it returns true.
Places a symbol at given position . If position is already occupied it returns false . Otherwise it returns true .
placeSymbol(x, y, symbol) {
    if (x < 0 || x >= this.width || y < 0 || y >= this.height) {
      throw new Error(`Invalid position ${x}, ${y}`);
    }
    if (this.getPosition(x, y)) {
      return false;
    }
    if (!symbol) {
      symbol = this.playerSymbols[this.currentPlayer];
    }
    const pos = new Position(x, y, symbol);
    let row = this.lookup[y];
    if (!row) this.lookup[y] = row = {};
    let col = row[x];
    if (col) {
      throw new Error(`Position ${x}, ${y} is already occupied`);
    }
    row[x] = pos;
    this.positions.push(pos);
    this.currentPlayer = this.positions.length % this.playerSymbols.length;
    this.fire('play');
    this.fire('change');
    return true;
  }
placeSymbol ( x , y , symbol ) { if ( x < 0 || x >= this . width || y < 0 || y >= this . height ) { throw new Error ( ` ${ x } ${ y } ` ) ; } if ( this . getPosition ( x , y ) ) { return false ; } if ( ! symbol ) { symbol = this . playerSymbols [ this . currentPlayer ] ; } const pos = new Position ( x , y , symbol ) ; let row = this . lookup [ y ] ; if ( ! row ) this . lookup [ y ] = row = { } ; let col = row [ x ] ; if ( col ) { throw new Error ( ` ${ x } ${ y } ` ) ; } row [ x ] = pos ; this . positions . push ( pos ) ; this . currentPlayer = this . positions . length % this . playerSymbols . length ; this . fire ( 'play' ) ; this . fire ( 'change' ) ; return true ; }
Places a symbol at given position.
Places a symbol at given position .
// Position is already taken // we should never reach hear, but in case if someone messed up the // `gethPosition()` check, we defensively throw.