Skip to content

Commit

Permalink
Merge pull request #986 from AInnuganti/distance
Browse files Browse the repository at this point in the history
Add square_manhattan_distance and square_knight_distance functions
  • Loading branch information
niklasf committed Apr 16, 2023
2 parents 2f7ada0 + a2f1588 commit 5a3cda8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
26 changes: 25 additions & 1 deletion chess/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,34 @@ def square_rank(square: Square) -> int:

def square_distance(a: Square, b: Square) -> int:
"""
Gets the distance (i.e., the number of king steps) from square *a* to *b*.
Gets the Chebyshev distance (i.e., the number of king steps) from square *a* to *b*.
"""
return max(abs(square_file(a) - square_file(b)), abs(square_rank(a) - square_rank(b)))

def square_manhattan_distance(a: Square, b: Square) -> int:
"""
Gets the Manhattan/Taxicab distance (i.e., the number of orthogonal king steps) from square *a* to *b*.
"""
return abs(square_file(a) - square_file(b)) + abs(square_rank(a) - square_rank(b))

def square_knight_distance(a: Square, b: Square) -> int:
"""
Gets the Knight distance (i.e., the number of knight moves) from square *a* to *b*.
"""
dx = abs(square_file(a) - square_file(b))
dy = abs(square_rank(a) - square_rank(b))

if dx + dy == 1:
return 3
elif dx == dy == 2:
return 4
elif dx == dy == 1:
if BB_SQUARES[a] & BB_CORNERS or BB_SQUARES[b] & BB_CORNERS: # Special case only for corner squares
return 4

m = math.ceil(max(dx / 2, dy / 2, (dx + dy) / 3))
return m + ((m + dx + dy) % 2)

def square_mirror(square: Square) -> Square:
"""Mirrors the square vertically."""
return square ^ 0x38
Expand Down
25 changes: 25 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,31 @@ def test_parse_square(self):
with self.assertRaises(ValueError):
self.assertEqual(chess.parse_square("a0"))

def test_square_distance(self):
self.assertEqual(chess.square_distance(chess.A1, chess.A1), 0)
self.assertEqual(chess.square_distance(chess.A1, chess.H8), 7)
self.assertEqual(chess.square_distance(chess.E1, chess.E8), 7)
self.assertEqual(chess.square_distance(chess.A4, chess.H4), 7)
self.assertEqual(chess.square_distance(chess.D4, chess.E5), 1)

def test_square_manhattan_distance(self):
self.assertEqual(chess.square_manhattan_distance(chess.A1, chess.A1), 0)
self.assertEqual(chess.square_manhattan_distance(chess.A1, chess.H8), 14)
self.assertEqual(chess.square_manhattan_distance(chess.E1, chess.E8), 7)
self.assertEqual(chess.square_manhattan_distance(chess.A4, chess.H4), 7)
self.assertEqual(chess.square_manhattan_distance(chess.D4, chess.E5), 2)

def test_square_knight_distance(self):
self.assertEqual(chess.square_knight_distance(chess.A1, chess.A1), 0)
self.assertEqual(chess.square_knight_distance(chess.A1, chess.H8), 6)
self.assertEqual(chess.square_knight_distance(chess.G1, chess.F3), 1)
self.assertEqual(chess.square_knight_distance(chess.E1, chess.E8), 5)
self.assertEqual(chess.square_knight_distance(chess.A4, chess.H4), 5)
self.assertEqual(chess.square_knight_distance(chess.A1, chess.B1), 3)
self.assertEqual(chess.square_knight_distance(chess.A1, chess.C3), 4)
self.assertEqual(chess.square_knight_distance(chess.A1, chess.B2), 4)
self.assertEqual(chess.square_knight_distance(chess.C1, chess.B2), 2)


class MoveTestCase(unittest.TestCase):

Expand Down

0 comments on commit 5a3cda8

Please sign in to comment.