javascript - queen-attack

This commit is contained in:
James Walker 2018-10-05 12:43:22 -04:00
parent e28b0dbb4d
commit 69bb37b523
Signed by: walkah
GPG Key ID: 3C127179D6086E93
5 changed files with 7353 additions and 0 deletions

View File

@ -0,0 +1,63 @@
# Queen Attack
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:
```text
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ 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:
[https://exercism.io/tracks/javascript/installation](https://exercism.io/tracks/javascript/installation)
## Requirements
Install assignment dependencies:
```bash
$ npm install
```
## Making the test suite pass
Execute the tests with:
```bash
$ npm 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 `xtest` to `test`.
## Source
J Dalbey's Programming Practice problems [http://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html](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.

7080
javascript/queen-attack/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,80 @@
{
"name": "exercism-javascript",
"version": "0.0.0",
"description": "Exercism exercises in Javascript.",
"author": "Katrina Owen",
"private": true,
"repository": {
"type": "git",
"url": "https://github.com/exercism/javascript"
},
"devDependencies": {
"babel-jest": "^21.2.0",
"babel-plugin-transform-builtin-extend": "^1.1.2",
"babel-preset-env": "^1.4.0",
"eslint": "^3.19.0",
"eslint-config-airbnb": "^15.0.1",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jsx-a11y": "^5.0.1",
"eslint-plugin-react": "^7.0.1",
"jest": "^21.2.1"
},
"jest": {
"modulePathIgnorePatterns": [
"package.json"
]
},
"babel": {
"presets": [
[
"env",
{
"targets": [
{
"node": "current"
}
]
}
]
],
"plugins": [
[
"babel-plugin-transform-builtin-extend",
{
"globals": [
"Error"
]
}
],
[
"transform-regenerator"
]
]
},
"scripts": {
"test": "jest --no-cache ./*",
"watch": "jest --no-cache --watch ./*",
"lint": "eslint .",
"lint-test": "eslint . && jest --no-cache ./* "
},
"eslintConfig": {
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"env": {
"es6": true,
"node": true,
"jest": true
},
"extends": "airbnb",
"rules": {
"import/no-unresolved": "off",
"import/extensions": "off"
}
},
"licenses": [
"MIT"
],
"dependencies": {}
}

View File

@ -0,0 +1,55 @@
const GRID_SIZE = 8;
export default class Queens {
constructor(defaultPositions = {}) {
this.setPosition('white', defaultPositions.white || [0, 3]);
this.setPosition('black', defaultPositions.black || [7, 3]);
}
toString() {
let board = [];
for (let x = 0; x < GRID_SIZE; x += 1) {
const row = [];
for (let y = 0; y < GRID_SIZE; y += 1) {
if (Queens.isEqual(this.white, [x, y])) {
row.push('W');
} else if (Queens.isEqual(this.black, [x, y])) {
row.push('B');
} else {
row.push('_');
}
}
board += `${row.join(' ')}\n`;
}
return board;
}
canAttack() {
if (this.white[0] === this.black[0]) {
return true; // Same Row
} else if (this.white[1] === this.black[1]) {
return true; // Same Column
} else if ((Math.abs(this.white[0] - this.black[0])) ===
(Math.abs(this.white[1] - this.black[1]))) {
return true; // On a diagonal
}
return false;
}
setPosition(color, position) {
if (Queens.isEqual(position, this.getOpponentPosition(color))) {
throw Error('Queens cannot share the same space');
}
this[color] = position;
}
getOpponentPosition(color) {
return (color === 'white') ? this.black : this.white;
}
static isEqual(pos1, pos2) {
return JSON.stringify(pos1) === JSON.stringify(pos2);
}
}

View File

@ -0,0 +1,75 @@
import Queens from './queen-attack';
describe('Queens', () => {
test('has the correct default positions', () => {
const queens = new Queens();
expect(queens.white).toEqual([0, 3]);
expect(queens.black).toEqual([7, 3]);
});
test('initialized with specific placement', () => {
const queens = new Queens({ white: [3, 7], black: [6, 1] });
expect(queens.white).toEqual([3, 7]);
expect(queens.black).toEqual([6, 1]);
});
test('cannot occupy the same space', () => {
const positioning = { white: [2, 4], black: [2, 4] };
const expectedError = 'Queens cannot share the same space';
expect(() => new Queens(positioning)).toThrow(expectedError);
});
test('toString representation', () => {
const positioning = { white: [2, 4], black: [6, 6] };
const queens = new Queens(positioning);
const board = ['_ _ _ _ _ _ _ _',
'_ _ _ _ _ _ _ _',
'_ _ _ _ W _ _ _',
'_ _ _ _ _ _ _ _',
'_ _ _ _ _ _ _ _',
'_ _ _ _ _ _ _ _',
'_ _ _ _ _ _ B _',
'_ _ _ _ _ _ _ _\n'].join('\n');
expect(queens.toString()).toEqual(board);
});
test('queens cannot attack', () => {
const queens = new Queens({ white: [2, 3], black: [4, 7] });
expect(queens.canAttack()).toEqual(false);
});
test('queens can attack when they are on the same row', () => {
const queens = new Queens({ white: [2, 4], black: [2, 7] });
expect(queens.canAttack()).toEqual(true);
});
test('queens can attack when they are on the same column', () => {
const queens = new Queens({ white: [5, 4], black: [2, 4] });
expect(queens.canAttack()).toEqual(true);
});
test('queens can attack diagonally', () => {
const queens = new Queens({ white: [1, 1], black: [6, 6] });
expect(queens.canAttack()).toEqual(true);
});
test('queens can attack another diagonally', () => {
const queens = new Queens({ white: [0, 6], black: [1, 7] });
expect(queens.canAttack()).toEqual(true);
});
test('queens can attack yet another diagonally', () => {
const queens = new Queens({ white: [4, 1], black: [6, 3] });
expect(queens.canAttack()).toEqual(true);
});
test('queens can attack on a north-east/south-west diagonal', () => {
const queens = new Queens({ white: [7, 0], black: [0, 7] });
expect(queens.canAttack()).toEqual(true);
});
test('queens can attack on another ne/sw diagonal', () => {
const queens = new Queens({ white: [2, 6], black: [5, 3] });
expect(queens.canAttack()).toEqual(true);
});
});