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

# glennj's solution

## to Queen Attack in the Lua Track

Published at Mar 09 2019 · 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)``````
``````local Queen = function(position)
assert(
position
and 0 <= position.row    and position.row    <= 7
and 0 <= position.column and position.column <= 7
, 'invalid position'
)
return {
position = position,
can_attack = function (other)
local dx = math.abs(position.row - other.position.row)
local dy = math.abs(position.column - other.position.column)
return dx == 0 or dy == 0 or dx == dy
end
}
end

return Queen``````