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