 # brunnock's solution

## to Queen Attack in the JavaScript Track

Published at Aug 19 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

Given the position of two queens on a chess board, indicate whether or not they are positioned so that they can attack each other.

In the game of chess, a queen can attack pieces which are on the same row, column, or diagonal.

A chessboard can be represented by an 8 by 8 array.

So if you're told the white queen is at (2, 3) and the black queen at (5, 6), then you'd know you've got a set-up like so:

``````_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ W _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ B _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
``````

You'd also be able to answer whether the queens can attack each other. In this case, that answer would be yes, they can, because both pieces share a diagonal.

## Setup

Go through the setup instructions for JavaScript to install the necessary dependencies:

http://exercism.io/languages/javascript/installation

## Running the test suite

The provided test suite uses Jasmine. You can install it by opening a terminal window and running the following command:

``````npm install -g jasmine
``````

Run the test suite from the exercise directory with:

``````jasmine queen-attack.spec.js
``````

In many test suites all but the first test have been marked "pending". Once you get a test passing, activate the next one by changing `xit` to `it`.

## Source

J Dalbey's Programming Practice problems http://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### queen-attack.spec.js

``````var Queens = require('./queen-attack');

describe('Queens', function () {
it('has the correct default positions', function () {
var queens = new Queens();
expect(queens.white).toEqual([0, 3]);
expect(queens.black).toEqual([7, 3]);
});

xit('initialized with specific placement', function () {
var queens = new Queens({white: [3, 7], black: [6, 1]});
expect(queens.white).toEqual([3, 7]);
expect(queens.black).toEqual([6, 1]);
});

xit('cannot occupy the same space', function () {
var positioning = {white: [2, 4], black: [2, 4]};

try {
var queens = new Queens(positioning);
expect(queens.white).toEqual([2, 4]);
expect(queens.black).toEqual([2, 4]);
} catch (error) {
expect(error).toEqual('Queens cannot share the same space');
}
});

xit('toString representation', function () {
var positioning = {white: [2, 4], black: [6, 6]};
var queens = new Queens(positioning);
var board = '_ _ _ _ _ _ _ _\n' +
'_ _ _ _ _ _ _ _\n' +
'_ _ _ _ W _ _ _\n' +
'_ _ _ _ _ _ _ _\n' +
'_ _ _ _ _ _ _ _\n' +
'_ _ _ _ _ _ _ _\n' +
'_ _ _ _ _ _ B _\n' +
'_ _ _ _ _ _ _ _\n'
;
expect(queens.toString()).toEqual(board);
});

xit('queens cannot attack', function () {
var queens = new Queens({ white: [2, 3], black: [4, 7] });
expect(queens.canAttack()).toEqual(false);
});

xit('queens can attack when they are on the same row', function () {
var queens = new Queens({ white: [2, 4], black: [2, 7] });
expect(queens.canAttack()).toEqual(true);
});

xit('queens can attack when they are on the same column', function () {
var queens = new Queens({ white: [5, 4], black: [2, 4] });
expect(queens.canAttack()).toEqual(true);
});

xit('queens can attack diagonally', function () {
var queens = new Queens({ white: [1, 1], black: [6, 6] });
expect(queens.canAttack()).toEqual(true);
});

xit('queens can attack another diagonally', function () {
var queens = new Queens({ white: [0, 6], black: [1, 7] });
expect(queens.canAttack()).toEqual(true);
});

xit('queens can attack yet another diagonally', function () {
var queens = new Queens({ white: [4, 1], black: [6, 3] });
expect(queens.canAttack()).toEqual(true);
});

xit('queens can attack on a north-east/south-west diagonal', function () {
var queens = new Queens({ white: [7, 0], black: [0, 7] });
expect(queens.canAttack()).toEqual(true);
});

xit('queens can attack on another ne/sw diagonal', function () {
var queens = new Queens({ white: [2, 6], black: [5, 3] });
expect(queens.canAttack()).toEqual(true);
});
});``````
``````class Queens {
constructor(pieces={white:[0,3], black:[7,3]}) {
if (typeof(pieces) === 'object') {
if (pieces.white.join('') === pieces.black.join('')) {
throw('Queens cannot share the same space');
} else {
this.white = pieces.white;
this.black = pieces.black;
}
}
this.board = Array(8).fill().map( ()=> Array(8).fill('_') );
this.board[this.white][this.white]='W';
this.board[this.black][this.black]='B';
}
toString() {
return (this.board.map( x=>x.join(' ')  ).join('\n') +'\n' );
}
canAttack() {
if (this.white === this.black) return (true);
if (this.white === this.black) return (true);
if ((this.white-this.white) === (this.black-this.black)) return (true);
if ((this.white+this.white) === (this.black+this.black)) return (true);
return(false);
}

}
module.exports = Queens;``````