Sha256: f29796369ac0406db5c0f4567ac51ea3d71748d9ccd010c03bf8ce70ff58c848

Contents?: true

Size: 1.35 KB

Versions: 20

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';
}

function 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

20 entries across 20 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.179 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.178 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.177 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.176 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.175 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.174 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.173 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.172 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.171 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.170 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.169 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.167 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.166 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.165 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.164 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.163 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.162 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.161 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.160 tracks/ecmascript/exercises/queen-attack/example.js