 # sbreining-gozego's solution

## to Queen Attack in the TypeScript Track

Published at Jun 02 2020 · 0 comments
Instructions
Test suite
Solution

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 TypeScript to install the necessary dependencies:

https://exercism.io/tracks/typescript/installation

## Requirements

Install assignment dependencies:

``````\$ yarn install
``````

## Making the test suite pass

Execute the tests with:

``````\$ yarn test
``````

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable 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.test.ts

``````import QueenAttack from "./queen-attack"

describe("Queens", () => {
it("initialized with specific placement", () => {
const queens = new QueenAttack({ white: [3, 7], black: [6, 1] })
expect(queens.white).toEqual([3, 7])
expect(queens.black).toEqual([6, 1])
})

xit("cannot occupy the same space", () => {
const positioning: { black: [number, number]; white: [number, number] } = {
black: [2, 4],
white: [2, 4]
}
const expectedError = "Queens cannot share the same space"
expect(() => new QueenAttack(positioning)).toThrow(expectedError)
})

xit("toString representation", () => {
const positioning: { black: [number, number]; white: [number, number] } = {
white: [2, 4],
black: [6, 6]
}
const queens = new QueenAttack(positioning)
const board = [
"_ _ _ _ _ _ _ _",
"_ _ _ _ _ _ _ _",
"_ _ _ _ W _ _ _",
"_ _ _ _ _ _ _ _",
"_ _ _ _ _ _ _ _",
"_ _ _ _ _ _ _ _",
"_ _ _ _ _ _ B _",
"_ _ _ _ _ _ _ _\n"
].join("\n")
expect(queens.toString()).toEqual(board)
})

xit("queens cannot attack", () => {
const queens = new QueenAttack({ white: [2, 3], black: [4, 7] })
expect(queens.canAttack()).toEqual(false)
})

xit("queens can attack when they are on the same row", () => {
const queens = new QueenAttack({ white: [2, 4], black: [2, 7] })
expect(queens.canAttack()).toEqual(true)
})

xit("queens can attack when they are on the same column", () => {
const queens = new QueenAttack({ white: [5, 4], black: [2, 4] })
expect(queens.canAttack()).toEqual(true)
})

xit("queens can attack diagonally", () => {
const queens = new QueenAttack({ white: [1, 1], black: [6, 6] })
expect(queens.canAttack()).toEqual(true)
})

xit("queens can attack another diagonally", () => {
const queens = new QueenAttack({ white: [0, 6], black: [1, 7] })
expect(queens.canAttack()).toEqual(true)
})

xit("queens can attack yet another diagonally", () => {
const queens = new QueenAttack({ white: [4, 1], black: [6, 3] })
expect(queens.canAttack()).toEqual(true)
})

xit("queens can attack on a north-east/south-west diagonal", () => {
const queens = new QueenAttack({ white: [7, 0], black: [0, 7] })
expect(queens.canAttack()).toEqual(true)
})

xit("queens can attack on another ne/sw diagonal", () => {
const queens = new QueenAttack({ white: [2, 6], black: [5, 3] })
expect(queens.canAttack()).toEqual(true)
})
})``````
``````type Queen = 'white' | 'black';
type BoardLocation = [number, number];

export default class QueenAttack {

constructor(locations: Record<Queen, BoardLocation>) {
if (this.isSameSquare(locations.black, locations.white)) {
throw Error('Queens cannot share the same space');
}

this._black = locations.black;
this._white = locations.white;
}

public canAttack(): boolean {
return this.isSameRow() || this.isSameColumn() || this.isSameDiagonal();
}

public toString(): string {
const board = [];

for (let i = 0; i < this.SIDE_LENGTH; ++i) {
board.push(this.getRowAsString(i));
}

return board.join('\n') + '\n';
}

private getRowAsString(rowNumber: number): string {
const row = ['_', '_', '_', '_', '_', '_', '_', '_'];

if (rowNumber === this._black[this.ROW]) row[this._black[this.COLUMN]] = 'B';
if (rowNumber === this._white[this.ROW]) row[this._white[this.COLUMN]] = 'W';

return row.join(' ');
}

private isSameSquare(square1: BoardLocation, square2: BoardLocation): boolean {
const [row1, column1] = square1;
const [row2, column2] = square2;

return row1 === row2 && column1 === column2;
}

private isSameRow(): boolean {
return this._black[this.ROW] === this._white[this.ROW];
}

private isSameColumn(): boolean {
return this._black[this.COLUMN] === this._white[this.COLUMN];
}

private isSameDiagonal(): boolean {
const rowDelta = Math.abs(this._black[this.ROW] - this._white[this.ROW]);
const columnDelta = Math.abs(this._black[this.COLUMN] - this._white[this.COLUMN]);

return rowDelta === columnDelta;
}

get black(): BoardLocation {
return this._black;
}

get white(): BoardLocation {
return this._white;
}
}``````