🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of Nia11

Nia11's solution

to Queen Attack in the Lua Track

Published at Dec 23 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)
queen={}
queen.__index=queen

setmetatable(queen,{
	__call=function(cls,...)
		return cls.new(...)
	end
})


function queen.new(pos)
	local self = setmetatable({},queen)
	assert(pos.row>=0 and pos.row<=7 and pos.column>=0 and pos.column<=7,'invalid board placing!')
	self.row=pos.row
	self.column=pos.column
	function self.can_attack(target)
		for i=0,7 do
			if 	self.row-i==target.row and self.column-i==target.column or
				self.row-i==target.row and self.column==target.column or
				self.row-i==target.row and self.column+i==target.column or
				self.row==target.row and self.column-i==target.column or
				self.row==target.row and self.column+i==target.column or
				self.row+i==target.row and self.column-i==target.column or
				self.row+i==target.row and self.column==target.column or
				self.row+i==target.row and self.column+i==target.column 
			then 
				return true
			end
		end
		
		return false
	end
	return self
end

return queen

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?