Skip to content
This repository has been archived by the owner on Apr 23, 2022. It is now read-only.

Commit

Permalink
Fixes & tests for AN ambiguity
Browse files Browse the repository at this point in the history
  • Loading branch information
kekalainen committed May 4, 2021
1 parent f53e1ef commit 850221d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/game/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def store_move_an(self, move, previous_legal_moves):
if an == "P":
an = ""

# Specify initial column, file or both for ambiguous moves.
# Specify initial file (column), rank (row) or both for ambiguous moves.
ambiguous_moves = []
for from_xy in previous_legal_moves:
if (
Expand All @@ -125,7 +125,7 @@ def store_move_an(self, move, previous_legal_moves):
else:
if ambiguous_x:
an += str(self.board.width - move.from_y)
if ambiguous_y:
else:
an += ascii_lowercase[move.from_x]

if move.captured_piece:
Expand Down Expand Up @@ -163,7 +163,11 @@ def move_piece_an(self, an):

piece_name = matches.group(1) or "P"
from_x = matches.group(2)
if from_x:
from_x = ascii_lowercase.index(from_x)
from_y = matches.group(3)
if from_y:
from_y = self.board.width - int(from_y)

for from_xy in self.legal_moves:
if self.board.get_piece(from_xy[0], from_xy[1]).name.upper() == piece_name:
Expand Down
5 changes: 5 additions & 0 deletions src/tests/game/game_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ def test_store_move_an_kings_pawn(self):
self.game.store_move_an(move, self.game.legal_moves)
self.assertEqual(self.game.an_moves[-1], "e3")

def test_store_move_an_ambiguous_move(self):
for an in ["a4", "a5", "h4", "h5", "Ra3", "Ra6", "Rhh3"]:
self.game.move_piece_an(an)
self.assertEqual(self.game.an_moves[-1], "Rhh3")

def test_move_piece_an_kings_pawn(self):
self.assertIsNone(self.game.board.get_piece(4, 5))
self.game.move_piece_an("e3")
Expand Down

0 comments on commit 850221d

Please sign in to comment.