Sha256: 2d4871c3e43a7df579d88da7e571f88d26fee80aadc5a0ad894a7a052a9f9cf5

Contents?: true

Size: 1.35 KB

Versions: 138

Compression:

Stored size: 1.35 KB

Contents

const W = 8;
const H = 8;
const STARTING = { black: [7, 3], white: [0, 3] };

function samePosition({ white, black }) {
  return white[0] === black[0] && white[1] === black[1];
}

function buildRow(cell, colCount) {
  return Array(...Array(colCount)).map(() => cell);
}

function concatRows(row, rowCount) {
  return [...Array.prototype.concat.apply(buildRow(row, rowCount)).join('')];
}

function constructBoard() {
  let row = buildRow('_ ', W).join('');
  row = `${row.substring(0, row.length - 1)}\n`;
  return concatRows(row, H);
}

function placePieces(self) {
  const board = self.board;
  board[(self.black[0] * W * 2) + (self.black[1] * 2)] = 'B';
  board[(self.white[0] * W * 2) + (self.white[1] * 2)] = 'W';
}

const QueenAttack = (params = STARTING) => {
  const self = this instanceof QueenAttack ? this : Object.getPrototypeOf(QueenAttack);
  if (samePosition(params)) {
    throw new Error('Queens cannot share the same space');
  }

  self.black = params.black;
  self.white = params.white;
  self.board = constructBoard();
  placePieces(self);

  self.canAttack = () => {
    if (self.black[0] === self.white[0] || self.black[1] === self.white[1]) {
      return true;
    }
    return Math.abs(self.black[0] - self.white[0]) === Math.abs(self.black[1] - self.white[1]);
  };

  self.toString = () => self.board.join('');

  return self;
};

export default QueenAttack;

Version data entries

138 entries across 138 versions & 1 rubygems

Version Path
trackler-2.2.1.98 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.97 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.96 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.95 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.94 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.93 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.92 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.91 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.90 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.89 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.88 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.87 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.86 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.85 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.84 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.83 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.82 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.81 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.80 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.79 tracks/ecmascript/exercises/queen-attack/example.js