Files
python/exercises/practice/queen-attack/.meta/example.py

23 lines
781 B
Python
Raw Normal View History

class Queen:
def __init__(self, row, column):
if row < 0:
2021-11-23 14:05:17 +01:00
raise ValueError('row not positive')
if not 0 <= row <= 7:
2021-11-23 14:05:17 +01:00
raise ValueError('row not on board')
if column < 0:
2021-11-23 14:05:17 +01:00
raise ValueError('column not positive')
if not 0 <= column <= 7:
2021-11-23 14:05:17 +01:00
raise ValueError('column not on board')
self.row = row
self.column = column
2014-04-08 00:37:51 -03:00
def can_attack(self, another_queen):
2021-11-23 14:05:17 +01:00
idx = abs(self.row - another_queen.row)
edx = abs(self.column - another_queen.column)
if idx == edx == 0:
raise ValueError('Invalid queen position: both queens in the same square')
2021-11-23 14:05:17 +01:00
elif idx == edx or idx == 0 or edx == 0:
return True
else:
return False