forked from exercism/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
27 lines (23 loc) · 777 Bytes
/
example.py
File metadata and controls
27 lines (23 loc) · 777 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def board(pos1, pos2):
validate_position(pos1, pos2)
x1, y1 = pos1
x2, y2 = pos2
b = [['_'] * 8 for i in range(8)]
b[x1][y1] = 'W'
b[x2][y2] = 'B'
return [''.join(r) for r in b]
def can_attack(pos1, pos2):
validate_position(pos1, pos2)
x1, y1 = pos1
x2, y2 = pos2
dx = x1 - x2 if x1 >= x2 else x2 - x1
dy = y1 - y2 if y1 >= y2 else y2 - y1
if dx == dy or dx == 0 or dy == 0:
return True
return False
def validate_position(pos1, pos2):
if any(x < 0 or x > 7 for x in pos1 + pos2):
raise ValueError('Invalid queen position: queen out of the board')
if pos1 == pos2:
raise ValueError('Invalid queen position: both queens in the same '
'square: {0}'.format(pos1))