def test_check(self):
        shape = (1, 1, 9, 9) if self.data_format == 'NCHW' else (1, 9, 9, 1)
        board = np.empty(shape, dtype=np.int32)

        ph = tf.placeholder(tf.int32, shape=shape)
        pseudo_effect = BlackPseudoOuEffect(
            data_format=self.data_format, use_cudnn=self.use_cudnn
        )(ph)
        long_check = WhiteLongCheckLayer()(ph, pseudo_effect)

        # 利きの長い駒のみに限定する
        # UM,RYの長い利きのみで、短い利きは対象に含めない
        white_pieces = [Piece.WHITE_KY, Piece.WHITE_KA, Piece.WHITE_HI,
                        Piece.WHITE_UM, Piece.WHITE_RY]

        for direction in get_eight_directions():
            with self.test_session() as sess:
                for i, j, k, piece in product(range(9), range(9), range(1, 9),
                                              white_pieces):
                    x, y = self._get_position(
                        direction=direction, i=i, j=j, k=k
                    )
                    if x not in range(9) or y not in range(9):
                        continue

                    # OUから見てdirectionの方向に相手の駒を配置
                    board[:] = Piece.EMPTY
                    if self.data_format == 'NCHW':
                        board[0, 0, i, j] = Piece.BLACK_OU
                        board[0, 0, x, y] = piece
                    else:
                        board[0, i, j, 0] = Piece.BLACK_OU
                        board[0, x, y, 0] = piece

                    with self.subTest(direction=direction, i=i, j=j, k=k,
                                      piece=piece):
                        check = sess.run(long_check, feed_dict={ph: board})

                        for key, value in check.items():
                            # ブロードキャストが正しく行われるために4次元が必要
                            self.assertTupleEqual((1, 1, 1, 1), value.shape)

                            if key == direction:
                                d = get_opposite_direction(direction=direction)
                                if piece == Piece.WHITE_KY:
                                    self.assertEqual(value,
                                                     d == Direction.DOWN)
                                elif piece in (Piece.WHITE_KA, Piece.WHITE_UM):
                                    self.assertEqual(
                                        value, d in get_diagonal_directions()
                                    )
                                elif piece in (Piece.WHITE_HI, Piece.WHITE_RY):
                                    self.assertEqual(
                                        value, d in get_cross_directions()
                                    )
                                else:
                                    # ここには到達しないはず
                                    raise ValueError()
                            else:
                                self.assertFalse(value)
Ejemplo n.º 2
0
    def test_long_check(self):
        """
        長い利きで王手されている場合に王以外の駒が動ける場所の判定のテスト
        :return:
        """
        shape = (1, 1, 9, 9) if self.data_format == 'NCHW' else (1, 9, 9, 1)
        board = np.empty(shape, dtype=np.int32)

        ph = tf.placeholder(tf.int32, shape=shape)
        pseudo_effect = BlackPseudoOuEffect(data_format=self.data_format,
                                            use_cudnn=self.use_cudnn)(ph)
        all_check, long_check = WhiteAllCheckLayer()(ph, pseudo_effect)
        available_square = CheckAvailableSquareLayer()(pseudo_effect,
                                                       all_check)
        available_square = tf.squeeze(available_square)

        long_piece_list = [
            Piece.WHITE_KY, Piece.WHITE_KA, Piece.WHITE_HI, Piece.WHITE_UM,
            Piece.WHITE_RY
        ]

        with self.test_session() as sess:
            for tmp in product(get_eight_directions(), range(9), range(9),
                               range(1, 9), long_piece_list):
                direction, i, j, k, piece = tmp

                # 短い利きは含めない
                if (piece == Piece.WHITE_UM
                        and direction in get_cross_directions()):
                    continue
                elif (piece == Piece.WHITE_RY
                      and direction == get_diagonal_directions()):
                    continue

                x, y = self._get_position(direction=direction, i=i, j=j, k=k)
                if x not in range(9) or y not in range(9):
                    continue

                board[:] = Piece.EMPTY
                if self.data_format == 'NCHW':
                    board[0, 0, i, j] = Piece.BLACK_OU
                    board[0, 0, x, y] = piece
                else:
                    board[0, i, j, 0] = Piece.BLACK_OU
                    board[0, x, y, 0] = piece

                with self.subTest(direction=direction, i=i, j=j, piece=piece):
                    square, check = sess.run(
                        [available_square, long_check[direction]],
                        feed_dict={ph: board})

                    self.assertTupleEqual(square.shape, (9, 9))

                    if check:
                        for u, v in zip(*self._get_range(i=i, j=j, x=x, y=y)):
                            self.assertTrue(square[u, v])
                            square[u, v] = False
                        self.assertFalse(np.any(square))
                    else:
                        self.assertTrue(np.all(square))
    def test_check(self):
        """
        短い利きでの王手があるかの判定のテスト

        :return:
        """
        shape = (1, 1, 9, 9) if self.data_format == 'NCHW' else (1, 9, 9, 1)
        board = np.empty(shape, dtype=np.int32)

        ph = tf.placeholder(tf.int32, shape=shape)
        pseudo_effect = BlackPseudoOuEffect(data_format=self.data_format,
                                            use_cudnn=self.use_cudnn)(ph)
        short_check = WhiteShortCheckLayer()(ph, pseudo_effect)

        # 利きの短い駒のみに限定する
        # UM,RYの短い利きのみで、長い利きは対象に含めない
        white_pieces = [
            Piece.WHITE_FU, Piece.WHITE_KE, Piece.WHITE_GI, Piece.WHITE_KI,
            Piece.WHITE_TO, Piece.WHITE_NY, Piece.WHITE_NK, Piece.WHITE_NG,
            Piece.WHITE_UM, Piece.WHITE_RY
        ]
        direction_table = make_base_table()

        for direction in chain(get_eight_directions(),
                               [Direction.RIGHT_UP_UP, Direction.LEFT_UP_UP]):
            with self.test_session() as sess:
                for i, j, piece in product(range(9), range(9), white_pieces):
                    x, y = self._get_position(direction=direction, i=i, j=j)
                    if x not in range(9) or y not in range(9):
                        continue

                    # OUから見てdirectionの方向に相手の駒を配置
                    board[:] = Piece.EMPTY
                    if self.data_format == 'NCHW':
                        board[0, 0, i, j] = Piece.BLACK_OU
                        board[0, 0, x, y] = piece
                    else:
                        board[0, i, j, 0] = Piece.BLACK_OU
                        board[0, x, y, 0] = piece

                    with self.subTest(direction=direction,
                                      i=i,
                                      j=j,
                                      piece=piece):
                        check = sess.run(short_check, feed_dict={ph: board})

                        for key, value in check.items():
                            # ブロードキャストが正しく行われるために4次元が必要
                            self.assertTupleEqual((1, 1, 1, 1), value.shape)

                            if key == direction:
                                d = get_opposite_direction(direction=direction)
                                index = piece - Piece.WHITE_FU
                                flag = direction_table[d][index]
                                self.assertEqual(value, bool(flag))
                            else:
                                self.assertFalse(value)
            pass
Ejemplo n.º 4
0
    def test_case1(self):
        """
        動作確認中に発覚したものについて調べる
         .  n  .  .  .  .  .  n  .
         l  .  k  .  g  .  s  .  l
         .  s  .  p  .  .  .  .  .
         .  p  .  g  p  p  r  p  p
         .  .  p  .  .  R  p  P  .
         p  .  .  G  .  P  G  .  .
         P  P  P  .  .  .  .  B  P
         .  B  .  .  .  .  .  .  L
         L  N  S  .  K  .  S  N  .

         p*3

        :return:
        """
        board = np.array([[28, 15, 28, 14, 28, 28, 0, 1, 28],
                          [16, 28, 28, 14, 0, 28, 4, 28, 2],
                          [28, 17, 28, 19, 14, 6, 28, 28, 3],
                          [28, 28, 28, 14, 5, 0, 28, 28, 28],
                          [28, 20, 28, 14, 28, 28, 28, 28, 7],
                          [28, 28, 14, 20, 28, 6, 28, 28, 28],
                          [28, 21, 28, 28, 14, 28, 0, 28, 3],
                          [16, 28, 17, 14, 28, 28, 0, 4, 2],
                          [28, 15, 28, 28, 28, 14, 0, 28, 1]],
                         dtype=np.int32)
        shape = (1, 1, 9, 9) if self.data_format == 'NCHW' else (1, 9, 9, 1)
        board = np.reshape(board, shape)

        pseudo_effect = BlackPseudoOuEffect(data_format=self.data_format,
                                            use_cudnn=self.use_cudnn)(board)

        all_check, long_check = WhiteAllCheckLayer()(board, pseudo_effect)

        with self.test_session() as sess:
            all_check_, long_check_ = sess.run([all_check, long_check])

        for direction, check in all_check_.items():
            with self.subTest(direction=direction):
                self.assertFalse(check)

        for direction, check in long_check_.items():
            with self.subTest(direction=direction):
                self.assertFalse(check)
    def test_available_square(self):
        pseudo_effect = BlackPseudoOuEffect(data_format=self.data_format,
                                            use_cudnn=self.use_cudnn)(
                                                self.board)

        all_check, long_check = WhiteAllCheckLayer()(self.board, pseudo_effect)

        available_square = CheckAvailableSquareLayer()(pseudo_effect,
                                                       all_check)

        available_square = tf.squeeze(available_square)
        with self.test_session() as sess:
            square = sess.run(available_square)

        for i, j in product(range(9), repeat=2):
            # 3,8にある龍を捕る
            available = i == 2 and j == 7
            with self.subTest(i=i, j=j):
                self.assertEqual(square[i, j], available)
    def test_case2_check(self):
        """
        王手が正しく判定されているかを確認

        :return:
        """
        pseudo_effect = BlackPseudoOuEffect(data_format=self.data_format,
                                            use_cudnn=self.use_cudnn)(
                                                self.board)

        all_check, long_check = WhiteAllCheckLayer()(self.board, pseudo_effect)

        with self.test_session() as sess:
            all_check_, long_check_ = sess.run([all_check, long_check])

        for direction, check in all_check_.items():
            with self.subTest(direction=direction):
                self.assertEqual(direction == Direction.RIGHT_DOWN, check)

        for direction, check in long_check_.items():
            with self.subTest(direction=direction):
                self.assertFalse(check)
Ejemplo n.º 7
0
    def test_short_check(self):
        """
        短い利きで王手されている場合に王以外の駒が動ける場所の判定をテスト
        :return:
        """
        shape = (1, 1, 9, 9) if self.data_format == 'NCHW' else (1, 9, 9, 1)
        board = np.empty(shape, dtype=np.int32)

        ph = tf.placeholder(tf.int32, shape=shape)
        pseudo_effect = BlackPseudoOuEffect(data_format=self.data_format,
                                            use_cudnn=self.use_cudnn)(ph)
        all_check, _ = WhiteAllCheckLayer()(ph, pseudo_effect)
        available_square = CheckAvailableSquareLayer()(pseudo_effect,
                                                       all_check)
        available_square = tf.squeeze(available_square)
        short_check = WhiteShortCheckLayer()(ph, pseudo_effect)

        with self.test_session() as sess:
            for direction in chain(
                    get_eight_directions(),
                [Direction.RIGHT_UP_UP, Direction.LEFT_UP_UP]):
                for i, j, piece in product(range(9), range(9),
                                           range(Piece.WHITE_FU, Piece.EMPTY)):
                    piece = Piece(piece)

                    if piece == Piece.WHITE_OU:
                        # OUで王手はない
                        continue
                    # 長い利きはここではテストしない
                    if piece == Piece.WHITE_KY and direction == Direction.UP:
                        continue
                    elif (piece in (Piece.WHITE_KA, Piece.WHITE_UM)
                          and direction in get_diagonal_directions()):
                        continue
                    elif (direction in get_cross_directions()
                          and piece in (Piece.WHITE_HI, Piece.WHITE_RY)):
                        continue

                    x, y = self._get_position(direction=direction, i=i, j=j)
                    if x not in range(9) or y not in range(9):
                        continue

                    board[:] = Piece.EMPTY
                    if self.data_format == 'NCHW':
                        board[0, 0, i, j] = Piece.BLACK_OU
                        board[0, 0, x, y] = piece
                    else:
                        board[0, i, j, 0] = Piece.BLACK_OU
                        board[0, x, y, 0] = piece

                    with self.subTest(direction=direction,
                                      i=i,
                                      j=j,
                                      piece=piece):
                        square, check = sess.run(
                            [available_square, short_check[direction]],
                            feed_dict={ph: board})

                        self.assertTupleEqual(square.shape, (9, 9))

                        if check:
                            self.assertTrue(square[x, y])
                            square[x, y] = False
                            self.assertFalse(np.any(square))
                        else:
                            self.assertTrue(np.all(square))
Ejemplo n.º 8
0
    def test_pin1(self):
        shape = (1, 1, 9, 9) if self.data_format == 'NCHW' else (1, 9, 9, 1)
        board = np.empty(shape, dtype=np.int32)

        ph = tf.placeholder(tf.int32, shape=shape)
        pseudo_effect = BlackPseudoOuEffect(data_format=self.data_format,
                                            use_cudnn=self.use_cudnn)(ph)
        long_effect = {
            direction: WhiteNaiveLongEffectLayer(direction=direction,
                                                 data_format=self.data_format,
                                                 use_cudnn=self.use_cudnn)(ph)
            for direction in get_eight_directions()
        }
        pinned_board = BlackPinLayer(data_format=self.data_format)(
            ph, pseudo_effect, long_effect)
        offset = tf.squeeze(pinned_board - ph)

        long_piece_list = (Piece.WHITE_KY, Piece.WHITE_KA, Piece.WHITE_HI,
                           Piece.WHITE_UM, Piece.WHITE_RY)
        pinned_piece_list = [
            Piece(p) for p in range(Piece.BLACK_FU, Piece.WHITE_FU)
            if p != Piece.BLACK_OU
        ]

        with self.test_session() as sess:
            for tmp in product(get_eight_directions(), range(9), range(9),
                               range(1, 8)):
                direction, i, j, k = tmp

                offset_value = Piece.SIZE + PinDirection[direction.name] * 14

                # ピンされる駒の移動
                x, y = self._get_position(direction=direction, i=i, j=j, k=k)
                if x not in range(9) or y not in range(9):
                    continue

                for l in range(k + 1, 9):
                    # 利きの長い駒の位置
                    u, v = self._get_position(direction=direction,
                                              i=i,
                                              j=j,
                                              k=l)
                    if u not in range(9) or v not in range(9):
                        continue

                    board[:] = Piece.EMPTY
                    if self.data_format == 'NCHW':
                        board[0, 0, i, j] = Piece.BLACK_OU
                    else:
                        board[0, i, j, 0] = Piece.BLACK_OU

                    for long_piece in long_piece_list:
                        pinned_piece = np.random.choice(pinned_piece_list)
                        if self.data_format == 'NCHW':
                            board[0, 0, x, y] = pinned_piece
                            board[0, 0, u, v] = long_piece
                        else:
                            board[0, x, y, 0] = pinned_piece
                            board[0, u, v, 0] = long_piece

                        if long_piece == Piece.WHITE_KY:
                            pin = direction == Direction.UP
                        elif long_piece in (Piece.WHITE_KA, Piece.WHITE_UM):
                            pin = direction in get_diagonal_directions()
                        elif long_piece in (Piece.WHITE_HI, Piece.WHITE_RY):
                            pin = direction in get_cross_directions()
                        else:
                            # ここには到達しないはず
                            raise ValueError(direction)

                        with self.subTest(direction=direction,
                                          i=i,
                                          j=j,
                                          k=k,
                                          l=l,
                                          pinned_piece=pinned_piece,
                                          long_piece=long_piece):
                            o = sess.run(offset, feed_dict={ph: board})

                            if pin:
                                self.assertEqual(o[x, y], offset_value)
                                # 他がすべて0であることを確認
                                o[x, y] = 0
                                self.assertTrue(np.all(o == 0))
                            else:
                                self.assertTrue(np.all(o == 0))