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.159 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.158 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.157 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.156 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.155 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.154 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.153 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.152 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.151 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.150 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.149 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.148 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.147 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.146 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.145 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.144 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.143 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.142 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.141 tracks/ecmascript/exercises/queen-attack/example.js
trackler-2.2.1.140 tracks/ecmascript/exercises/queen-attack/example.js