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.139 tracks/javascript/exercises/queen-attack/example.js
trackler-2.2.1.138 tracks/javascript/exercises/queen-attack/example.js
trackler-2.2.1.137 tracks/javascript/exercises/queen-attack/example.js
trackler-2.2.1.136 tracks/javascript/exercises/queen-attack/example.js
trackler-2.2.1.135 tracks/javascript/exercises/queen-attack/example.js
trackler-2.2.1.134 tracks/javascript/exercises/queen-attack/example.js
trackler-2.2.1.133 tracks/javascript/exercises/queen-attack/example.js
trackler-2.2.1.132 tracks/javascript/exercises/queen-attack/example.js
trackler-2.2.1.131 tracks/javascript/exercises/queen-attack/example.js
trackler-2.2.1.130 tracks/javascript/exercises/queen-attack/example.js
trackler-2.2.1.129 tracks/javascript/exercises/queen-attack/example.js
trackler-2.2.1.128 tracks/javascript/exercises/queen-attack/example.js
trackler-2.2.1.127 tracks/javascript/exercises/queen-attack/example.js
trackler-2.2.1.126 tracks/javascript/exercises/queen-attack/example.js
trackler-2.2.1.125 tracks/javascript/exercises/queen-attack/example.js
trackler-2.2.1.124 tracks/javascript/exercises/queen-attack/example.js
trackler-2.2.1.123 tracks/javascript/exercises/queen-attack/example.js
trackler-2.2.1.122 tracks/javascript/exercises/queen-attack/example.js
trackler-2.2.1.121 tracks/javascript/exercises/queen-attack/example.js
trackler-2.2.1.120 tracks/javascript/exercises/queen-attack/example.js