def test_parsing_9x9(self): self.assertEqual(coords.parse_sgf_coords('aa'), (0, 0)) self.assertEqual(coords.parse_sgf_coords('ac'), (2, 0)) self.assertEqual(coords.parse_sgf_coords('ca'), (0, 2)) self.assertEqual(coords.parse_sgf_coords(''), None) self.assertEqual(coords.unparse_sgf_coords(None), '') self.assertEqual( 'aa', coords.unparse_sgf_coords(coords.parse_sgf_coords('aa'))) self.assertEqual( 'sa', coords.unparse_sgf_coords(coords.parse_sgf_coords('sa'))) self.assertEqual( (1, 17), coords.parse_sgf_coords(coords.unparse_sgf_coords( (1, 17)))) self.assertEqual(coords.parse_kgs_coords('A1'), (8, 0)) self.assertEqual(coords.parse_kgs_coords('A9'), (0, 0)) self.assertEqual(coords.parse_kgs_coords('C2'), (7, 2)) self.assertEqual(coords.parse_kgs_coords('J2'), (7, 8)) self.assertEqual(coords.parse_pygtp_coords((1, 1)), (8, 0)) self.assertEqual(coords.parse_pygtp_coords((1, 9)), (0, 0)) self.assertEqual(coords.parse_pygtp_coords((3, 2)), (7, 2)) self.assertEqual(coords.unparse_pygtp_coords((8, 0)), (1, 1)) self.assertEqual(coords.unparse_pygtp_coords((0, 0)), (1, 9)) self.assertEqual(coords.unparse_pygtp_coords((7, 2)), (3, 2)) self.assertEqual(coords.to_human_coord((0, 8)), 'J9') self.assertEqual(coords.to_human_coord((8, 0)), 'A1')
def handle_node(pos, node): 'A node can either add B+W stones, play as B, or play as W.' props = node.properties black_stones_added = [coords.parse_sgf_coords( c) for c in props.get('AB', [])] white_stones_added = [coords.parse_sgf_coords( c) for c in props.get('AW', [])] if black_stones_added or white_stones_added: return add_stones(pos, black_stones_added, white_stones_added) # If B/W props are not present, then there is no move. But if it is present and equal to the empty string, then the move was a pass. elif 'B' in props: black_move = coords.parse_sgf_coords(props.get('B', [''])[0]) return pos.play_move(black_move, color=go.BLACK) elif 'W' in props: white_move = coords.parse_sgf_coords(props.get('W', [''])[0]) return pos.play_move(white_move, color=go.WHITE) else: return pos
def test_pass(self): self.assertEqual(coords.parse_sgf_coords(''), None) self.assertEqual(coords.unflatten_coords(81), None) self.assertEqual(coords.parse_kgs_coords('pass'), None) self.assertEqual(coords.parse_pygtp_coords((0, 0)), None) self.assertEqual(coords.unparse_sgf_coords(None), '') self.assertEqual(coords.flatten_coords(None), 81) self.assertEqual(coords.to_human_coord(None), 'pass') self.assertEqual(coords.unparse_pygtp_coords(None), (0, 0))
def test_topleft(self): self.assertEqual(coords.parse_sgf_coords('ia'), (0, 8)) self.assertEqual(coords.unflatten_coords(8), (0, 8)) self.assertEqual(coords.parse_kgs_coords('J9'), (0, 8)) self.assertEqual(coords.parse_pygtp_coords((9, 9)), (0, 8)) self.assertEqual(coords.unparse_sgf_coords((0, 8)), 'ia') self.assertEqual(coords.flatten_coords((0, 8)), 8) self.assertEqual(coords.to_human_coord((0, 8)), 'J9') self.assertEqual(coords.unparse_pygtp_coords((0, 8)), (9, 9))
def test_upperleft(self): self.assertEqual(coords.parse_sgf_coords('aa'), (0, 0)) self.assertEqual(coords.unflatten_coords(0), (0, 0)) self.assertEqual(coords.parse_kgs_coords('A9'), (0, 0)) self.assertEqual(coords.parse_pygtp_coords((1, 9)), (0, 0)) self.assertEqual(coords.unparse_sgf_coords((0, 0)), 'aa') self.assertEqual(coords.flatten_coords((0, 0)), 0) self.assertEqual(coords.to_human_coord((0, 0)), 'A9') self.assertEqual(coords.unparse_pygtp_coords((0, 0)), (1, 9))
def test_make_dataset_from_sgf(self): with tempfile.NamedTemporaryFile() as sgf_file, \ tempfile.NamedTemporaryFile() as record_file: sgf_file.write(TEST_SGF.encode('utf8')) sgf_file.seek(0) preprocessing.make_dataset_from_sgf(sgf_file.name, record_file.name) recovered_data = self.extract_data(record_file.name) start_pos = go.Position() first_move = coords.parse_sgf_coords('fd') next_pos = start_pos.play_move(first_move) second_move = coords.parse_sgf_coords('cf') expected_data = [ (features.extract_features(start_pos), preprocessing._one_hot(coords.flatten_coords(first_move)), -1), (features.extract_features(next_pos), preprocessing._one_hot(coords.flatten_coords(second_move)), -1) ] self.assertEqualData(expected_data, recovered_data)
def handle_node(pos, node): 'A node can either add B+W stones, play as B, or play as W.' props = node.properties black_stones_added = [ coords.parse_sgf_coords(c) for c in props.get('AB', []) ] white_stones_added = [ coords.parse_sgf_coords(c) for c in props.get('AW', []) ] if black_stones_added or white_stones_added: return add_stones(pos, black_stones_added, white_stones_added) # If B/W props are not present, then there is no move. But if it is present and equal to the empty string, then the move was a pass. elif 'B' in props: black_move = coords.parse_sgf_coords(props.get('B', [''])[0]) return pos.play_move(black_move, color=go.BLACK) elif 'W' in props: white_move = coords.parse_sgf_coords(props.get('W', [''])[0]) return pos.play_move(white_move, color=go.WHITE) else: return pos
def get_next_move(node): props = node.next.properties if 'W' in props: return coords.parse_sgf_coords(props['W'][0]) else: return coords.parse_sgf_coords(props['B'][0])