 # bfi3ld's solution

## to Queen Attack in the TypeScript Track

Published at May 31 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)
})
})``````
``````export default class QueenAttack {
white: number[];
black: number[];

constructor(pos: {white:number[], black:number[]}) {
if(pos.white == pos.black && pos.white == pos.black) {
throw new Error("Queens cannot share the same space");
}
this.white = pos.white;
this.black = pos.black;
}

public toString() {
var w =this.white;
var b = this.black;
let board = [];
for(var i =0; i < 8; i++) {
let row = ""
for(var j =0; j < 7; j ++) {
let piece = b == i && b == j ? 'B ' : w == i && w == j ? 'W ': '_ ';
row += piece;
}
row += '_'
board.push(row);
}
board.push("")
return board.join("\n");
}

public canAttack() {
if(Math.abs(this.white - this.black) == Math.abs(this.white - this.black)) {
return true;
}
if(this.white== this.black || this.white == this.black) {
return true;
}
return false;

}
}``````