Sha256: b9b2ceb4c84010a0fc2e4fa41e5e8f739b89ea8f83f5754038ea2912aec1dacd

Contents?: true

Size: 1.41 KB

Versions: 68

Compression:

Stored size: 1.41 KB

Contents

'use strict';


module.exports = function (passedInOptions) {
  var options = passedInOptions || {white: [0, 3], black: [7, 3]};

  if (options.white[0] === options.black[0] && options.white[1] === options.black[1]) {
    throw String('Queens cannot share the same space');
  }

  this.white = options.white;
  this.black = options.black;

  this.canAttack = function () {
    var canAttack = false;

    if (this.white[0] === this.black[0]) {
      canAttack = true;
    }
    if (this.white[1] === this.black[1]) {
      canAttack = true;
    }

    var yDistance = this.white[0] - this.black[0];
    var xDistance = this.white[1] - this.black[1];

    if (xDistance === yDistance) {
      canAttack = true;
    }

    if (xDistance === -yDistance) {
      canAttack = true;
    }

    return canAttack;
  };

  this.boardRepresentation = function () {
    var boardRepresentation = '';

    for (var i = 0; i < 8; i++) {
      for (var j = 0; j < 8; j++) {
        if (this.white[0] === i && this.white[1] === j) {
          boardRepresentation += 'W';
        } else if (this.black[0] === i && this.black[1] === j) {
          boardRepresentation += 'B';
        } else {
          boardRepresentation += '_';
        }

        if (j < 7) { boardRepresentation += ' '; }
      }
      boardRepresentation += '\n';
    }

    return boardRepresentation;
  };

  this.toString = function () {
    return this.boardRepresentation();
  };
};

Version data entries

68 entries across 68 versions & 1 rubygems

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