Skip to content
This repository has been archived by the owner on Jun 16, 2021. It is now read-only.

Commit

Permalink
Add is_suicidal method
Browse files Browse the repository at this point in the history
  • Loading branch information
brilee committed Oct 27, 2016
1 parent 460cec1 commit a05f378
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
19 changes: 19 additions & 0 deletions go.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,25 @@ def is_eyeish(board, c):
else:
return color

def is_suicidal(position, move):
potential_libs = set()
for n in NEIGHBORS[move]:
neighbor_group_id = position.lib_tracker.group_index[n]
if neighbor_group_id == MISSING_GROUP_ID:
# at least one liberty after playing here, so not a suicide
return False
neighbor_group = position.lib_tracker.groups[neighbor_group_id]
if neighbor_group.color == position.to_play:
potential_libs |= neighbor_group.liberties
elif len(neighbor_group.liberties) == 1:
# would capture an opponent group if they only had one lib.
return False
# it's possible to suicide by connecting several friendly groups
# each of which had one liberty.
potential_libs -= set([move])
return not potential_libs


def is_reasonable(position, move):
'Checks that a move is both legal and not self-eye filling'
if is_eyeish(position.board, move) == position.to_play:
Expand Down
27 changes: 26 additions & 1 deletion tests/test_go.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,32 @@ def test_is_reasonable(self):
for move in reasonable_moves:
self.assertTrue(go.is_reasonable(position, move))
for move in unreasonable_moves:
self.assertFalse(go.is_reasonable(position, move))
self.assertFalse(go.is_reasonable(position, move), str(move))

def test_is_suicidal(self):
board = load_board('''
...O.O...
....O....
XO.....O.
OXO...OXO
O.XO.OX.O
OXO...OOX
XO.......
......XXO
.....XOO.
''')
position = Position(
board=board,
to_play=BLACK,
)
suicidal_moves = pc_set('E9 H5')
nonsuicidal_moves = pc_set('B5 J1 A9')
for move in suicidal_moves:
assert(position.board[move] == go.EMPTY) #sanity check my coordinate input
self.assertTrue(go.is_suicidal(position, move), str(move))
for move in nonsuicidal_moves:
assert(position.board[move] == go.EMPTY) #sanity check my coordinate input
self.assertFalse(go.is_suicidal(position, move), str(move))


class TestLibertyTracker(unittest.TestCase):
Expand Down

0 comments on commit a05f378

Please sign in to comment.