Ejemplo n.º 1
0
    def get_handicap(sgf):
        """
        sgfファイルの初期ハンディキャップを適用した盤を返す

        Parameters
        ----------
        sgf : str
            sgfの棋譜データコンテンツ
        
        Returns
        -------
        game_state : GameState
            ハンディキャップ適用後の盤
        first_move_done : bool
            ハンディキャップ適用があったか(盤面が空でないか)
        """
        go_board = Board(19, 19)
        first_move_done = False
        move = None
        game_state = GameState.new_game(19)
        if sgf.get_handicap() != None and sgf.get_handicap() != 0:
            for setup in sgf.get_root().get_setup_stones():
                for move in setup:
                    row, col = move
                    go_board.place_stone(Player.black, Point(row + 1, col + 1))
            first_move_done = True
            game_state = GameState(go_board, Player.white, None, move)

        return game_state, first_move_done
Ejemplo n.º 2
0
 def test_capture_is_not_suicide(self):
     board = Board(19, 19)
     board.place_stone(Player.black, Point(1, 1))
     board.place_stone(Player.black, Point(2, 2))
     board.place_stone(Player.black, Point(1, 3))
     board.place_stone(Player.white, Point(2, 1))
     board.place_stone(Player.white, Point(1, 2))
     self.assertIsNone(board.get(Point(1, 1)))
     self.assertEqual(Player.white, board.get(Point(2, 1)))
     self.assertEqual(Player.white, board.get(Point(1, 2)))
Ejemplo n.º 3
0
 def get_handicap(self, sgf):
     go_board = Board(19, 19)
     first_move_done = False
     game_state = GameState.new_game(19)
     if sgf.get_handicap() != None and sgf.get_handicap() != 0:
         for setup in sgf.get_root().get_setup_stones():
             for move in setup:
                 row, col = move
                 go_board.place_stone(Player.black, Point(row + 1, col + 1))
         first_move_done = True
         game_state = GameState(go_board, Player.white, None, move)
     return game_state, first_move_done
Ejemplo n.º 4
0
    def test_not_self_capture(self):
        # o.o..
        # x.xo.
        board = Board(5, 5)
        board.place_stone(Player.black, Point(1, 1))
        board.place_stone(Player.black, Point(1, 3))
        board.place_stone(Player.white, Point(2, 1))
        board.place_stone(Player.white, Point(2, 3))
        board.place_stone(Player.white, Point(1, 4))

        self.assertFalse(board.is_self_capture(Player.black, Point(1, 2)))
Ejemplo n.º 5
0
 def test_remove_liberties(self):
     board = Board(5, 5)
     board.place_stone(Player.black, Point(3, 3))
     board.place_stone(Player.white, Point(2, 2))
     white_string = board.get_go_string(Point(2, 2))
     six.assertCountEqual(
         self,
         [Point(2, 3), Point(2, 1),
          Point(1, 2), Point(3, 2)], white_string.liberties)
     board.place_stone(Player.black, Point(3, 2))
     white_string = board.get_go_string(Point(2, 2))
     six.assertCountEqual(
         self,
         [Point(2, 3), Point(2, 1), Point(1, 2)], white_string.liberties)
Ejemplo n.º 6
0
    def test_empty_triangle(self):
        board = Board(5, 5)
        board.place_stone(Player.black, Point(1, 1))
        board.place_stone(Player.black, Point(1, 2))
        board.place_stone(Player.black, Point(2, 2))
        board.place_stone(Player.white, Point(2, 1))

        black_string = board.get_go_string(Point(1, 1))
        six.assertCountEqual(
            self,
            [Point(3, 2), Point(2, 3), Point(1, 3)], black_string.liberties)
Ejemplo n.º 7
0
    def get_handicap(sgf):  # Get handicap stones
        go_board = Board(19, 19)

        first_move_done = False
        move = None
        game_state = GameState.new_game(19)
        board_ext = Board_Ext(game_state.board)
        if sgf.get_handicap() is not None and sgf.get_handicap() != 0:
            for setup in sgf.get_root().get_setup_stones():
                for move in setup:
                    row, col = move
                    go_board.place_stone(Player.black,
                                         Point(row + 1,
                                               col + 1))  # black gets handicap
                    #My inserting Nail
                    point = Point(row + 1, col + 1)
                    ret = board_ext.place_stone_ext(
                        go_board, 'b', point)  # Handicap for black Player
                    #### Nail

            first_move_done = True
            game_state = GameState(go_board, Player.white, None, move)
        return game_state, first_move_done, board_ext
Ejemplo n.º 8
0
    def game(self) -> GameState:
        if not self._game:
            black = batch_translate_labels_to_coordinates(self.initial_black)
            white = batch_translate_labels_to_coordinates(self.initial_white)
            move_points = batch_translate_labels_to_coordinates(self.moves)
            player = Player.black if self.initial_player == 'b' else Player.white

            board = Board(19, 19)
            for b in black:
                board.place_stone(Player.black, b)
            for w in white:
                board.place_stone(Player.white, w)

            self._game = GameState(board, player, None, None)
            for move_point in move_points:
                move = Move.pass_turn() if not move_point else Move.play(
                    move_point)
                self._game = self._game.apply_move(move)

        return self._game
Ejemplo n.º 9
0
 def test_capture_two_stones(self):
     board = Board(19, 19)
     board.place_stone(Player.black, Point(2, 2))
     board.place_stone(Player.black, Point(2, 3))
     board.place_stone(Player.white, Point(1, 2))
     board.place_stone(Player.white, Point(1, 3))
     self.assertEqual(Player.black, board.get(Point(2, 2)))
     self.assertEqual(Player.black, board.get(Point(2, 3)))
     board.place_stone(Player.white, Point(3, 2))
     board.place_stone(Player.white, Point(3, 3))
     self.assertEqual(Player.black, board.get(Point(2, 2)))
     self.assertEqual(Player.black, board.get(Point(2, 3)))
     board.place_stone(Player.white, Point(2, 1))
     board.place_stone(Player.white, Point(2, 4))
     self.assertIsNone(board.get(Point(2, 2)))
     self.assertIsNone(board.get(Point(2, 3)))
Ejemplo n.º 10
0
    def test_not_self_capture_is_other_capture(self):
        # xx...
        # oox..
        # x.o..
        board = Board(5, 5)
        board.place_stone(Player.black, Point(3, 1))
        board.place_stone(Player.black, Point(3, 2))
        board.place_stone(Player.black, Point(2, 3))
        board.place_stone(Player.black, Point(1, 1))
        board.place_stone(Player.white, Point(2, 1))
        board.place_stone(Player.white, Point(2, 2))
        board.place_stone(Player.white, Point(1, 3))

        self.assertFalse(board.is_self_capture(Player.black, Point(1, 2)))
Ejemplo n.º 11
0
    def process_zip(self, zip_file_name, data_file_name, game_list):
        tar_file = self.unzip_data(zip_file_name)
        zip_file = tarfile.open(self.data_dir + '/' + tar_file)
        name_list = zip_file.getnames()
        total_examples = self.num_total_examples(zip_file, game_list,
                                                 name_list)

        shape = self.encoder.shape()
        feature_shape = np.insert(shape, 0, np.asarray([total_examples]))
        features = np.zeros(feature_shape)
        labels = np.zeros((total_examples, ))

        counter = 0

        board = Board(19, 19)  #Nail
        board_ext = Board_Ext(board)  # Nail
        for index in game_list:

            name = name_list[index + 1]
            if not name.endswith('.sgf'):
                raise ValueError(name + ' is not a valid sgf')
            sgf_content = zip_file.extractfile(name).read()
            sgf = Sgf_game.from_string(sgf_content)

            game_state, first_move_done, board_ext = self.get_handicap(sgf)
            # if first_move_done :  # Nail ignore handicap
            #      continue  # Ignore games with handicap
            if self.encoder.name(
            )[:2] == 'my' and first_move_done == False:  # Not handicap
                board_ext = Board_Ext(game_state.board)  #inserted Nail
            for item in sgf.main_sequence_iter():
                color, move_tuple = item.get_move()
                point = None
                if color is not None:
                    if move_tuple is not None:
                        row, col = move_tuple
                        point = Point(row + 1, col + 1)
                        move = Move.play(point)

                    else:
                        move = Move.pass_turn()
                    if first_move_done and point is not None:
                        encode = True
                        # # Data only for debute Nail
                        # if self.count_stones_debute is not None and \
                        #     self.count_stones_middle is None and \
                        #     self.count_stones_end is None and \
                        #     board_ext.count_stones() > self.count_stones_debute:
                        #     encode = False
                        # # Data for middle game Nail
                        # if self.count_stones_debute is not None and \
                        #         self.count_stones_middle is not None and \
                        #         self.count_stones_end is None and \
                        #         self.board_ext.count_stones() <= self.count_stones_debute and board_ext.count_stones() > self.count_stones_middle:
                        #     encode = False
                        #
                        # # Data for end
                        # if self.count_stones_middle is not None and \
                        #         self.count_stones_end is not None and \
                        #         self.board_ext.count_stones() <= self.count_stones_middle and board_ext.count_stones() > self.count_stones_end:
                        #     encode = False
                        if encode == True:  #Nail
                            if self.encoder.name()[:2] == 'my':
                                features[counter] = self.encoder.encode(
                                    game_state, board_ext)  #Nail
                            else:
                                features[counter] = self.encoder.encode(
                                    game_state)

                            labels[counter] = self.encoder.encode_point(point)
                            counter += 1

                    game_state = game_state.apply_move(move)
                    if self.encoder.name()[:2] == 'my':
                        board_ext.place_stone_ext(game_state.board, color,
                                                  point)  # Inserted Nail
                    # Nail
                    first_move_done = True

        feature_file_base = self.data_dir + '/' + data_file_name + '_features_%d'
        label_file_base = self.data_dir + '/' + data_file_name + '_labels_%d'

        chunk = 0  # Due to files with large content, split up after chunksize
        chunksize = 1024
        start_time_all = time.time()

        while features.shape[0] >= chunksize:
            start_time = time.time()
            feature_file = feature_file_base % chunk
            label_file = label_file_base % chunk
            chunk += 1
            current_features, features = features[:chunksize], features[
                chunksize:]
            current_labels, labels = labels[:chunksize], labels[chunksize:]
            np.save(feature_file, current_features)
            np.save(label_file, current_labels)
            # Inserted Nail
            print("Chunk = ", chunk, " File for training Current_features: ",
                  feature_file)
            print("Time per one file = ",
                  (time.time() - start_time_all) / 1000, ' seconds')
        print('Files preparation with proccess_zip is over\n')
        print('Full Time = ', (time.time() - start_time_all) / 1000,
              ' seconds')
        print("End chunk = ", chunk)
Ejemplo n.º 12
0
 def __init__(self, board_size):
     self.board_width, self.board_height = board_size
     self.num_planes = 5
     board = Board(num_rows=self.board_width, num_cols=self.board_height)
     board_ext = Board_Ext(board)
Ejemplo n.º 13
0
 def test_scoring(self):
     # .w.ww
     # wwww.
     # bbbww
     # .bbbb
     # .b.b.
     board = Board(5, 5)
     board.place_stone(Player.black, Point(1, 2))
     board.place_stone(Player.black, Point(1, 4))
     board.place_stone(Player.black, Point(2, 2))
     board.place_stone(Player.black, Point(2, 3))
     board.place_stone(Player.black, Point(2, 4))
     board.place_stone(Player.black, Point(2, 5))
     board.place_stone(Player.black, Point(3, 1))
     board.place_stone(Player.black, Point(3, 2))
     board.place_stone(Player.black, Point(3, 3))
     board.place_stone(Player.white, Point(3, 4))
     board.place_stone(Player.white, Point(3, 5))
     board.place_stone(Player.white, Point(4, 1))
     board.place_stone(Player.white, Point(4, 2))
     board.place_stone(Player.white, Point(4, 3))
     board.place_stone(Player.white, Point(4, 4))
     board.place_stone(Player.white, Point(5, 2))
     board.place_stone(Player.white, Point(5, 4))
     board.place_stone(Player.white, Point(5, 5))
     territory = scoring.evaluate_territory(board)
     self.assertEqual(9, territory.num_black_stones)
     self.assertEqual(4, territory.num_black_territory)
     self.assertEqual(9, territory.num_white_stones)
     self.assertEqual(3, territory.num_white_territory)
     self.assertEqual(0, territory.num_dame)