Skip to content

Commit

Permalink
Add queen-attack exercise (#190)
Browse files Browse the repository at this point in the history
Co-authored-by: Victor Goff <[email protected]>
  • Loading branch information
BNAndras and kotp authored Nov 5, 2023
1 parent 4ed3c5d commit 170a0e1
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,14 @@
"prerequisites": [],
"difficulty": 2
},
{
"slug": "queen-attack",
"name": "Queen Attach",
"uuid": "c728a685-1b4e-4d77-9273-6ee42ca4fb0e",
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "word-count",
"name": "Word Count",
Expand Down
25 changes: 25 additions & 0 deletions exercises/practice/queen-attack/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Instructions

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 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:

```text
a b c d e f g h
8 _ _ _ _ _ _ _ _ 8
7 _ _ _ _ _ _ _ _ 7
6 _ _ _ _ _ _ _ _ 6
5 _ _ W _ _ _ _ _ 5
4 _ _ _ _ _ _ _ _ 4
3 _ _ _ _ _ _ _ _ 3
2 _ _ _ _ _ B _ _ 2
1 _ _ _ _ _ _ _ _ 1
a b c d e f g h
```

You are also 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.
19 changes: 19 additions & 0 deletions exercises/practice/queen-attack/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"queen_attack.vim"
],
"test": [
"queen_attack.vader"
],
"example": [
".meta/example.vim"
]
},
"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.",
"source": "J Dalbey's Programming Practice problems",
"source_url": "https://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html"
}
19 changes: 19 additions & 0 deletions exercises/practice/queen-attack/.meta/example.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function! Queen(row, column) abort
if a:row < 0
throw 'row not positive'
elseif a:column < 0
throw 'column not positive'
elseif a:row > 7
throw 'row not on board'
elseif a:column > 7
throw 'column not on board'
endif

return {'row': a:row, 'column': a:column, 'CanAttack': function('DoCanAttack')}
endfunction

function! DoCanAttack(other) dict
return self.row == a:other.row ||
\ self.column == a:other.column ||
\ (abs(a:other.column - self.column) == abs(a:other.row - self.row))
endfunction
49 changes: 49 additions & 0 deletions exercises/practice/queen-attack/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[3ac4f735-d36c-44c4-a3e2-316f79704203]
description = "Test creation of Queens with valid and invalid positions -> queen with a valid position"

[4e812d5d-b974-4e38-9a6b-8e0492bfa7be]
description = "Test creation of Queens with valid and invalid positions -> queen must have positive row"

[f07b7536-b66b-4f08-beb9-4d70d891d5c8]
description = "Test creation of Queens with valid and invalid positions -> queen must have row on board"

[15a10794-36d9-4907-ae6b-e5a0d4c54ebe]
description = "Test creation of Queens with valid and invalid positions -> queen must have positive column"

[6907762d-0e8a-4c38-87fb-12f2f65f0ce4]
description = "Test creation of Queens with valid and invalid positions -> queen must have column on board"

[33ae4113-d237-42ee-bac1-e1e699c0c007]
description = "Test the ability of one queen to attack another -> cannot attack"

[eaa65540-ea7c-4152-8c21-003c7a68c914]
description = "Test the ability of one queen to attack another -> can attack on same row"

[bae6f609-2c0e-4154-af71-af82b7c31cea]
description = "Test the ability of one queen to attack another -> can attack on same column"

[0e1b4139-b90d-4562-bd58-dfa04f1746c7]
description = "Test the ability of one queen to attack another -> can attack on first diagonal"

[ff9b7ed4-e4b6-401b-8d16-bc894d6d3dcd]
description = "Test the ability of one queen to attack another -> can attack on second diagonal"

[0a71e605-6e28-4cc2-aa47-d20a2e71037a]
description = "Test the ability of one queen to attack another -> can attack on third diagonal"

[0790b588-ae73-4f1f-a968-dd0b34f45f86]
description = "Test the ability of one queen to attack another -> can attack on fourth diagonal"

[543f8fd4-2597-4aad-8d77-cbdab63619f8]
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"
82 changes: 82 additions & 0 deletions exercises/practice/queen-attack/queen_attack.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@

Execute (queen with a valid position):
let g:row = 2
let g:column = 2
let g:queen = Queen(g:row, g:column)
AssertEqual v:t_dict, type(queen)

Execute (queen must have positive row):
let g:row = -2
let g:column = 2
let g:expected = "row not positive"
AssertThrows call Queen(g:row, g:column)
AssertEqual g:expected, g:vader_exception

Execute (queen must have row on board):
let g:row = 8
let g:column = 4
let g:expected = "row not on board"
AssertThrows call Queen(g:row, g:column)
AssertEqual g:expected, g:vader_exception

Execute (queen must have positive column):
let g:row = 2
let g:column = -2
let g:expected = "column not positive"
AssertThrows call Queen(g:row, g:column)
AssertEqual g:expected, g:vader_exception

Execute (queen must have column on board):
let g:row = 4
let g:column = 8
let g:expected = "column not on board"
AssertThrows call Queen(g:row, g:column)
AssertEqual g:expected, g:vader_exception

Execute (cannot attack):
let g:black_queen = Queen(6, 6)
let g:white_queen = Queen(2, 4)
let g:expected = 0
AssertEqual g:expected, g:black_queen.CanAttack(g:white_queen)

Execute (can attack on same row):
let g:black_queen = Queen(2, 6)
let g:white_queen = Queen(2, 4)
let g:expected = 1
AssertEqual g:expected, g:black_queen.CanAttack(g:white_queen)

Execute (can attack on same column):
let g:black_queen = Queen(2, 5)
let g:white_queen = Queen(4, 5)
let g:expected = 1
AssertEqual g:expected, g:black_queen.CanAttack(g:white_queen)

Execute (can attack on first diagonal):
let g:black_queen = Queen(0, 4)
let g:white_queen = Queen(2, 2)
let g:expected = 1
AssertEqual g:expected, g:black_queen.CanAttack(g:white_queen)

Execute (can attack on second diagonal):
let g:black_queen = Queen(3, 1)
let g:white_queen = Queen(2, 2)
let g:expected = 1
AssertEqual g:expected, g:black_queen.CanAttack(g:white_queen)

Execute (can attack on third diagonal):
let g:black_queen = Queen(1, 1)
let g:white_queen = Queen(2, 2)
let g:expected = 1
AssertEqual g:expected, g:black_queen.CanAttack(g:white_queen)

Execute (can attack on fourth diagonal):
let g:black_queen = Queen(0, 6)
let g:white_queen = Queen(1, 7)
let g:expected = 1
AssertEqual g:expected, g:black_queen.CanAttack(g:white_queen)

Execute (cannot attack if falling diagonals are only the same when reflected across the longest falling diagonal):
let g:black_queen = Queen(2, 5)
let g:white_queen = Queen(4, 1)
let g:expected = 0
AssertEqual g:expected, g:black_queen.CanAttack(g:white_queen)
3 changes: 3 additions & 0 deletions exercises/practice/queen-attack/queen_attack.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function! Queen(row, column) abort
" your code goes here
endfunction

0 comments on commit 170a0e1

Please sign in to comment.