Skip to content

Commit 170a0e1

Browse files
BNAndraskotp
andauthored
Add queen-attack exercise (#190)
Co-authored-by: Victor Goff <[email protected]>
1 parent 4ed3c5d commit 170a0e1

File tree

7 files changed

+205
-0
lines changed

7 files changed

+205
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,14 @@
247247
"prerequisites": [],
248248
"difficulty": 2
249249
},
250+
{
251+
"slug": "queen-attack",
252+
"name": "Queen Attach",
253+
"uuid": "c728a685-1b4e-4d77-9273-6ee42ca4fb0e",
254+
"practices": [],
255+
"prerequisites": [],
256+
"difficulty": 3
257+
},
250258
{
251259
"slug": "word-count",
252260
"name": "Word Count",
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
```text
12+
a b c d e f g h
13+
8 _ _ _ _ _ _ _ _ 8
14+
7 _ _ _ _ _ _ _ _ 7
15+
6 _ _ _ _ _ _ _ _ 6
16+
5 _ _ W _ _ _ _ _ 5
17+
4 _ _ _ _ _ _ _ _ 4
18+
3 _ _ _ _ _ _ _ _ 3
19+
2 _ _ _ _ _ B _ _ 2
20+
1 _ _ _ _ _ _ _ _ 1
21+
a b c d e f g h
22+
```
23+
24+
You are also able to answer whether the queens can attack each other.
25+
In this case, that answer would be yes, they can, because both pieces share a diagonal.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"queen_attack.vim"
8+
],
9+
"test": [
10+
"queen_attack.vader"
11+
],
12+
"example": [
13+
".meta/example.vim"
14+
]
15+
},
16+
"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.",
17+
"source": "J Dalbey's Programming Practice problems",
18+
"source_url": "https://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html"
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function! Queen(row, column) abort
2+
if a:row < 0
3+
throw 'row not positive'
4+
elseif a:column < 0
5+
throw 'column not positive'
6+
elseif a:row > 7
7+
throw 'row not on board'
8+
elseif a:column > 7
9+
throw 'column not on board'
10+
endif
11+
12+
return {'row': a:row, 'column': a:column, 'CanAttack': function('DoCanAttack')}
13+
endfunction
14+
15+
function! DoCanAttack(other) dict
16+
return self.row == a:other.row ||
17+
\ self.column == a:other.column ||
18+
\ (abs(a:other.column - self.column) == abs(a:other.row - self.row))
19+
endfunction
Lines changed: 49 additions & 0 deletions
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"
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
2+
Execute (queen with a valid position):
3+
let g:row = 2
4+
let g:column = 2
5+
let g:queen = Queen(g:row, g:column)
6+
AssertEqual v:t_dict, type(queen)
7+
8+
Execute (queen must have positive row):
9+
let g:row = -2
10+
let g:column = 2
11+
let g:expected = "row not positive"
12+
AssertThrows call Queen(g:row, g:column)
13+
AssertEqual g:expected, g:vader_exception
14+
15+
Execute (queen must have row on board):
16+
let g:row = 8
17+
let g:column = 4
18+
let g:expected = "row not on board"
19+
AssertThrows call Queen(g:row, g:column)
20+
AssertEqual g:expected, g:vader_exception
21+
22+
Execute (queen must have positive column):
23+
let g:row = 2
24+
let g:column = -2
25+
let g:expected = "column not positive"
26+
AssertThrows call Queen(g:row, g:column)
27+
AssertEqual g:expected, g:vader_exception
28+
29+
Execute (queen must have column on board):
30+
let g:row = 4
31+
let g:column = 8
32+
let g:expected = "column not on board"
33+
AssertThrows call Queen(g:row, g:column)
34+
AssertEqual g:expected, g:vader_exception
35+
36+
Execute (cannot attack):
37+
let g:black_queen = Queen(6, 6)
38+
let g:white_queen = Queen(2, 4)
39+
let g:expected = 0
40+
AssertEqual g:expected, g:black_queen.CanAttack(g:white_queen)
41+
42+
Execute (can attack on same row):
43+
let g:black_queen = Queen(2, 6)
44+
let g:white_queen = Queen(2, 4)
45+
let g:expected = 1
46+
AssertEqual g:expected, g:black_queen.CanAttack(g:white_queen)
47+
48+
Execute (can attack on same column):
49+
let g:black_queen = Queen(2, 5)
50+
let g:white_queen = Queen(4, 5)
51+
let g:expected = 1
52+
AssertEqual g:expected, g:black_queen.CanAttack(g:white_queen)
53+
54+
Execute (can attack on first diagonal):
55+
let g:black_queen = Queen(0, 4)
56+
let g:white_queen = Queen(2, 2)
57+
let g:expected = 1
58+
AssertEqual g:expected, g:black_queen.CanAttack(g:white_queen)
59+
60+
Execute (can attack on second diagonal):
61+
let g:black_queen = Queen(3, 1)
62+
let g:white_queen = Queen(2, 2)
63+
let g:expected = 1
64+
AssertEqual g:expected, g:black_queen.CanAttack(g:white_queen)
65+
66+
Execute (can attack on third diagonal):
67+
let g:black_queen = Queen(1, 1)
68+
let g:white_queen = Queen(2, 2)
69+
let g:expected = 1
70+
AssertEqual g:expected, g:black_queen.CanAttack(g:white_queen)
71+
72+
Execute (can attack on fourth diagonal):
73+
let g:black_queen = Queen(0, 6)
74+
let g:white_queen = Queen(1, 7)
75+
let g:expected = 1
76+
AssertEqual g:expected, g:black_queen.CanAttack(g:white_queen)
77+
78+
Execute (cannot attack if falling diagonals are only the same when reflected across the longest falling diagonal):
79+
let g:black_queen = Queen(2, 5)
80+
let g:white_queen = Queen(4, 1)
81+
let g:expected = 0
82+
AssertEqual g:expected, g:black_queen.CanAttack(g:white_queen)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function! Queen(row, column) abort
2+
" your code goes here
3+
endfunction

0 commit comments

Comments
 (0)