Queen Attack2026-04-29
My Solution: Exercism Queen Attack solution
Instructions
Determine if two queens on a chessboard can attack each other.
Rules:
- Queens are on an 8x8 board with coordinates
0to7. - Raise
ValueErrorwith appropriate messages for invalid positions (not positive, not on board). - Raise
ValueErrorif both queens are in the same square. - Queens can attack horizontally, vertically, or diagonally.
Solution
class Queen:
def __init__(self, row, column):
if row < 0:
raise ValueError("row not positive")
if row > 7:
raise ValueError("row not on board")
if column < 0:
raise ValueError("column not positive")
if column > 7:
raise ValueError("column not on board")
self.row = row
self.column = column
def can_attack(self, another_queen):
if self.row == another_queen.row and self.column == another_queen.column:
raise ValueError("Invalid queen position: both queens in the same square")
return (
self.row == another_queen.row or
self.column == another_queen.column or
abs(another_queen.row - self.row) == abs(another_queen.column - self.column)
)
Syntax Notes
- Validation happens in
__init__so invalid positions are caught at construction time. can_attack()first checks for the same-square edge case.- Same row:
self.row == another_queen.row - Same column:
self.column == another_queen.column - Diagonal: the absolute differences in row and column are equal.