Skip to content

Commit 861c3b7

Browse files
authored
new exercise: queen-attack (#147)
1 parent 55094d1 commit 861c3b7

14 files changed

+8880
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,14 @@
351351
"prerequisites": [],
352352
"difficulty": 8
353353
},
354+
{
355+
"slug": "queen-attack",
356+
"name": "Queen Attack",
357+
"uuid": "b1b0e448-be17-4bca-a04a-bf87308b651a",
358+
"practices": [],
359+
"prerequisites": [],
360+
"difficulty": 6
361+
},
354362
{
355363
"slug": "knapsack",
356364
"name": "Knapsack",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Instructions append
2+
3+
A queen must be placed on a valid position on the board.
4+
Two queens cannot share the same position.
5+
6+
The queens shown below are at their [default starting positions](https://en.wikipedia.org/wiki/Rules_of_chess#Initial_setup). That's the 1st rank (row 7) for the white queen and the 8th rank (row 0) for the black queen. Both queens start in the d file (column 3).
7+
8+
```text
9+
a b c d e f g h
10+
8 _ _ _ B _ _ _ _ 8
11+
7 _ _ _ _ _ _ _ _ 7
12+
6 _ _ _ _ _ _ _ _ 6
13+
5 _ _ _ _ _ _ _ _ 5
14+
4 _ _ _ _ _ _ _ _ 4
15+
3 _ _ _ _ _ _ _ _ 3
16+
2 _ _ _ _ _ _ _ _ 2
17+
1 _ _ _ W _ _ _ _ 1
18+
a b c d e f g h
19+
```
20+
21+
# Positions argument
22+
23+
The function will receive only a single unsigned 32bit number as argument. Both rows and columns are encoded into it:
24+
25+
- The chess notation files a..h become zero-indexed columns 0..7
26+
- The chess notation ranks 8..1 become zero-indexed rows 0..7
27+
28+
```
29+
[ 32bit ]
30+
[ 8bit ][ 8bit ][ 8bit ][ 8bit ]
31+
^ ^ ^ ^
32+
| | | +---- black row
33+
| | +------------ black column
34+
| +-------------------- white row
35+
+---------------------------- white column
36+
```
37+
38+
So the aforementioned positions white D1 and black D8 would result in the numbers white column 3, white row 7, black column 3, black row 0; as binary number, this would be expressed as
39+
40+
`00000011 00000111 00000011 00000000`
41+
42+
So the `$positions` argument would be 50791168.
43+
44+
You can use integer division or bit shifting and division remainder or bitwise and to get at every one value.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Instructions
2+
3+
Given the position of two queens on a chess board, indicate whether or not they are positioned so that they can attack each other.
4+
5+
In the game of chess, a queen can attack pieces which are on the same row, column, or diagonal.
6+
7+
A chessboard can be represented by an 8 by 8 array.
8+
9+
So if you are told the white queen is at `c5` (zero-indexed at column 2, row 3) and the black queen at `f2` (zero-indexed at column 5, row 6), then you know that the set-up is like so:
10+
11+
![A chess board with two queens. Arrows emanating from the queen at c5 indicate possible directions of capture along file, rank and diagonal.](https://assets.exercism.org/images/exercises/queen-attack/queen-capture.svg)
12+
13+
You are also able to answer whether the queens can attack each other.
14+
In this case, that answer would be yes, they can, because both pieces share a diagonal.
15+
16+
## Credit
17+
18+
The chessboard image was made by [habere-et-dispertire][habere-et-dispertire] using LaTeX and the [chessboard package][chessboard-package] by Ulrike Fischer.
19+
20+
[habere-et-dispertire]: https://exercism.org/profiles/habere-et-dispertire
21+
[chessboard-package]: https://github.com/u-fischer/chessboard
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"root": true,
3+
"extends": "@exercism/eslint-config-javascript",
4+
"env": {
5+
"jest": true
6+
},
7+
"overrides": [
8+
{
9+
"files": [".meta/proof.ci.js", ".meta/exemplar.js", "*.spec.js"],
10+
"excludedFiles": ["custom.spec.js"],
11+
"extends": "@exercism/eslint-config-javascript/maintainers"
12+
}
13+
]
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"authors": [
3+
"atk"
4+
],
5+
"files": {
6+
"solution": [
7+
"queen-attack.wat"
8+
],
9+
"test": [
10+
"queen-attack.spec.js"
11+
],
12+
"example": [
13+
".meta/proof.ci.wat"
14+
],
15+
"invalidator": [
16+
"package.json"
17+
]
18+
},
19+
"blurb": "Given the position of two queens on a chess board, indicate whether or not they are positioned so that they can attack each other.",
20+
"source": "J Dalbey's Programming Practice problems",
21+
"source_url": "https://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html",
22+
"custom": {
23+
"version.tests.compatibility": "jest-27",
24+
"flag.tests.task-per-describe": false,
25+
"flag.tests.may-run-long": false,
26+
"flag.tests.includes-optional": false
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
(module
2+
(memory (export "mem") 2)
3+
(data (i32.const 0) "_ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _\n")
4+
5+
(global $White i32 (i32.const 87))
6+
(global $Black i32 (i32.const 66))
7+
8+
;;
9+
;; Checks a queen positioning for validity and the ability to attack
10+
;;
11+
;; @param {i32} $positions
12+
;; (4*8bit for white row, white column, black row, black column, each 0-7)
13+
;;
14+
;; @returns {i32} -1 if invalid, 0 if cannot attack, 1 if it can attack
15+
;;
16+
(func $canAttack (export "canAttack") (param $positions i32) (result i32)
17+
;; any position > 7
18+
(if (i32.or (i32.and (local.get $positions) (i32.const 4177066232))
19+
;; identical positions
20+
(i32.eqz (i32.xor (i32.shr_u (local.get $positions) (i32.const 16))
21+
(i32.and (local.get $positions) (i32.const 65535)))))
22+
(then (return (i32.const -1))))
23+
;; horizontal
24+
(i32.or (i32.or (i32.eq (i32.and (local.get $positions) (i32.const 255))
25+
(i32.and (i32.shr_u (local.get $positions) (i32.const 16)) (i32.const 255)))
26+
;; vertical
27+
(i32.eq (i32.and (i32.shr_u (local.get $positions) (i32.const 8)) (i32.const 255))
28+
(i32.and (i32.shr_u (local.get $positions) (i32.const 24)) (i32.const 255))))
29+
;; diagonal
30+
(f32.eq (f32.abs (f32.convert_i32_s (i32.sub
31+
(i32.and (local.get $positions) (i32.const 255))
32+
(i32.and (i32.shr_u (local.get $positions) (i32.const 16)) (i32.const 255)))))
33+
(f32.abs (f32.convert_i32_s (i32.sub
34+
(i32.and (i32.shr_u (local.get $positions) (i32.const 8)) (i32.const 255))
35+
(i32.and (i32.shr_u (local.get $positions) (i32.const 24)) (i32.const 255)))))))
36+
)
37+
38+
;;
39+
;; Prints the chess board to linear memory
40+
;;
41+
;; @param {i32} $positions
42+
;; (4*8bit for white row, white column, black row, black column, each 0-7)
43+
;;
44+
;; @returns {(i32,i32)} offset and length of chess board string in memory
45+
;;
46+
(func (export "printBoard") (param $positions i32) (result i32 i32)
47+
(i32.store8 (i32.or
48+
(i32.shl (i32.and (i32.shr_u (local.get $positions)
49+
(i32.const 16)) (i32.const 7)) (i32.const 1))
50+
(i32.shl (i32.and (i32.shr_u (local.get $positions)
51+
(i32.const 24)) (i32.const 7)) (i32.const 4)))
52+
(global.get $White))
53+
(i32.store8 (i32.or
54+
(i32.shl (i32.and (local.get $positions) (i32.const 7)) (i32.const 1))
55+
(i32.shl (i32.and (i32.shr_u (local.get $positions) (i32.const 8))
56+
(i32.const 7)) (i32.const 4)))
57+
(global.get $Black))
58+
(i32.const 0) (i32.const 128)
59+
)
60+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[3ac4f735-d36c-44c4-a3e2-316f79704203]
13+
description = "Test creation of Queens with valid and invalid positions -> queen with a valid position"
14+
15+
[4e812d5d-b974-4e38-9a6b-8e0492bfa7be]
16+
description = "Test creation of Queens with valid and invalid positions -> queen must have positive row"
17+
18+
[f07b7536-b66b-4f08-beb9-4d70d891d5c8]
19+
description = "Test creation of Queens with valid and invalid positions -> queen must have row on board"
20+
21+
[15a10794-36d9-4907-ae6b-e5a0d4c54ebe]
22+
description = "Test creation of Queens with valid and invalid positions -> queen must have positive column"
23+
24+
[6907762d-0e8a-4c38-87fb-12f2f65f0ce4]
25+
description = "Test creation of Queens with valid and invalid positions -> queen must have column on board"
26+
27+
[33ae4113-d237-42ee-bac1-e1e699c0c007]
28+
description = "Test the ability of one queen to attack another -> cannot attack"
29+
30+
[eaa65540-ea7c-4152-8c21-003c7a68c914]
31+
description = "Test the ability of one queen to attack another -> can attack on same row"
32+
33+
[bae6f609-2c0e-4154-af71-af82b7c31cea]
34+
description = "Test the ability of one queen to attack another -> can attack on same column"
35+
36+
[0e1b4139-b90d-4562-bd58-dfa04f1746c7]
37+
description = "Test the ability of one queen to attack another -> can attack on first diagonal"
38+
39+
[ff9b7ed4-e4b6-401b-8d16-bc894d6d3dcd]
40+
description = "Test the ability of one queen to attack another -> can attack on second diagonal"
41+
42+
[0a71e605-6e28-4cc2-aa47-d20a2e71037a]
43+
description = "Test the ability of one queen to attack another -> can attack on third diagonal"
44+
45+
[0790b588-ae73-4f1f-a968-dd0b34f45f86]
46+
description = "Test the ability of one queen to attack another -> can attack on fourth diagonal"
47+
48+
[543f8fd4-2597-4aad-8d77-cbdab63619f8]
49+
description = "Test the ability of one queen to attack another -> cannot attack if falling diagonals are only the same when reflected across the longest falling diagonal"
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
audit=false
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Exercism
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default {
2+
presets: ["@exercism/babel-preset-javascript"],
3+
plugins: [],
4+
};

0 commit comments

Comments
 (0)