ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

Published at Aug 05 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.

To run the tests, run the command `busted`

from within the exercise directory.

For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language page.

J Dalbey's Programming Practice problems http://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
local Queen = require('queen-attack')
describe('collatz-conjecture', function()
it('queen with a valid position', function()
assert.has_no_error(function()
Queen({ row = 2, column = 2 })
end)
end)
it('queen must have a positive row', function()
assert.has_error(function()
Queen({ row = -2, column = 2 })
end)
end)
it('queen must have row on board', function()
assert.has_error(function()
Queen({ row = 8, column = 4 })
end)
end)
it('queen must have positive column', function()
assert.has_error(function()
Queen({ row = 2, column = -2 })
end)
end)
it('queen must have column on board', function()
assert.has_error(function()
Queen({ row = 4, column = 8 })
end)
end)
it('can not attack', function()
local q1 = Queen({ row = 2, column = 4 })
local q2 = Queen({ row = 6, column = 6 })
assert.is_false(q1.can_attack(q2))
end)
it('can attack on same row', function()
local q1 = Queen({ row = 2, column = 4 })
local q2 = Queen({ row = 2, column = 6 })
assert.is_true(q1.can_attack(q2))
end)
it('can attack on same column', function()
local q1 = Queen({ row = 4, column = 5 })
local q2 = Queen({ row = 2, column = 5 })
assert.is_true(q1.can_attack(q2))
end)
it('can attack on first diagonal', function()
local q1 = Queen({ row = 2, column = 2 })
local q2 = Queen({ row = 0, column = 4 })
assert.is_true(q1.can_attack(q2))
end)
it('can attack on second diagonal', function()
local q1 = Queen({ row = 2, column = 2 })
local q2 = Queen({ row = 3, column = 1 })
assert.is_true(q1.can_attack(q2))
end)
it('can attack on third diagonal', function()
local q1 = Queen({ row = 2, column = 2 })
local q2 = Queen({ row = 1, column = 1 })
assert.is_true(q1.can_attack(q2))
end)
it('can attack on fourth diagonal', function()
local q1 = Queen({ row = 2, column = 2 })
local q2 = Queen({ row = 5, column = 5 })
assert.is_true(q1.can_attack(q2))
end)
end)
```

```
function on_board(v)
return v >= 0 and v <= 7
end
return function(pos)
if not on_board(pos.row) or not on_board(pos.column) then
error("Invalid position")
end
pos.can_attack = function(qo)
local slope = math.abs((pos.row - qo.row) / (pos.column - qo.column))
return pos.row == qo.row or pos.column == qo.column or slope == 1
end
return pos
end
```

A huge amount can be learned from reading other peopleâ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments