🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # ryanplusplus's solution

## to Queen Attack in the Lua Track

Published at Jul 13 2018 · 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)``````
``````return function(pos)
assert(pos.row >= 0, 'row must be positive')
assert(pos.row < 8, 'row must be on the board')
assert(pos.column >= 0, 'column must be positive')
assert(pos.column < 8, 'column must be on the board')

return {
row = pos.row,
column = pos.column,
can_attack = function(other)
local row_difference = math.abs(pos.row - other.row)
local column_difference = math.abs(pos.column - other.column)

local same_row = row_difference == 0
local same_column = column_difference == 0
local diagonal = row_difference == column_difference

return same_row or same_column or diagonal
end
}
end``````

## Community comments

Find this solution interesting? Ask the author a question to learn more.

### What can you learn from this solution?

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?