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

# aweidner's solution

## to Queen Attack in the Lua Track

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.

## Running the tests

To run the tests, run the command `busted` from within the exercise directory.

## Further information

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.

## Source

J Dalbey's Programming Practice problems 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.

### queen-attack_spec.lua

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