コード例 #1
0
def test_pos_transform():
    pos_pairs = [((0, 0), "A1", 0x00), ((7, 0), "H1", 0x70),
                 ((0, 7), "A8", 0x07), ((7, 7), "H8", 0x77),
                 ((3, 4), "D5", 0x34)]

    for tupl, sp, ip in pos_pairs:
        ok(to_tuple_pos(sp)) == tupl
        ok(to_tuple_pos(ip)) == tupl

        ok(to_str_pos(tupl)) == sp
        ok(to_str_pos(ip)) == sp

        ok(to_int_pos(sp)) == ip
        ok(to_int_pos(tupl)) == ip

    for x in range(8):
        for y in range(8):
            ok(to_tuple_pos(to_str_pos((x, y)))) == (x, y)
            ok(to_tuple_pos(to_int_pos((x, y)))) == (x, y)
コード例 #2
0
ファイル: chess_tests.py プロジェクト: ddovbii/python-classes
def test_pos_transform():
    pos_pairs = [
        ((0, 0), "A1", 0x00),
        ((7, 0), "H1", 0x70),
        ((0, 7), "A8", 0x07),
        ((7, 7), "H8", 0x77),
        ((3, 4), "D5", 0x34),
    ]

    for tupl, sp, ip in pos_pairs:
        ok(to_tuple_pos(sp)) == tupl
        ok(to_tuple_pos(ip)) == tupl

        ok(to_str_pos(tupl)) == sp
        ok(to_str_pos(ip)) == sp

        ok(to_int_pos(sp)) == ip
        ok(to_int_pos(tupl)) == ip

    for x in range(8):
        for y in range(8):
            ok(to_tuple_pos(to_str_pos((x, y)))) == (x, y)
            ok(to_tuple_pos(to_int_pos((x, y)))) == (x, y)
コード例 #3
0
ファイル: chess.py プロジェクト: medfedeff/python-classes
 def __str__(self):
     return "{}({!r}, {!r})".format(self.__class__.__name__,
                                    to_str_pos(self.pos), self.color)
コード例 #4
0
ファイル: chess.py プロジェクト: medfedeff/python-classes
def _get_next_step(board, for_color, level=2, pstep="", hits_enabled=True):
    debug = False
    moves = []
    best_evaluate = None

    hit_checked = set()

    if 0 == level:
        if debug:
            print pstep + "empty"
        return [], position_evaluate(board)

    for piece in list(board):
        if piece.color == for_color:
            for mv in board.get_all_moves(piece):

                if debug:
                    print pstep + "mv ", piece, to_str_pos(mv)

                ppos = piece.ipos
                board.move(piece, mv)
                if level == 1:
                    next_moves = []
                    new_val = position_evaluate(board)
                else:
                    next_moves, new_val = _get_next_step(
                        board, rev_color[for_color], level - 1, pstep + "    ")
                board.move(piece, ppos)

                if is_better_move(best_evaluate, new_val, for_color):
                    best_evaluate = new_val
                    moves = [(piece.ipos, mv)] + next_moves

            if hits_enabled:
                for hit in board.get_all_hits(piece):
                    if hit in hit_checked:
                        continue
                    else:
                        figures, evals = zip(*hit_order(board, hit, for_color))
                        best_after_this_color_move = (min if for_color == 'W'
                                                      else max)(evals[::2])

                        # need find best move after this position, disable hits to avoid internal loop
                        # apply hits
                        if evals[1::2]:
                            best_after_other_color_move = (max if for_color
                                                           == 'W' else min)(
                                                               evals[1::2])
                        else:
                            assert False

                        b = board.copy()
                        for figure in figures[:evals.index(
                                best_after_other_color_move)]:
                            b.remove(hit)
                            print figure, to_str_pos(hit)
                            b.move(b.get(figure.ipos), hit)
                        _, new_eval = _get_next_step(b, for_color, 1,
                                                     pstep + "    ", False)

                        if is_better_move(best_evaluate, new_eval, for_color):
                            best_evaluate = new_eval
                            moves = [(figures[0].ipos, hit)] + next_moves

                        if is_better_move(best_evaluate,
                                          best_after_this_color_move,
                                          for_color):
                            best_evaluate = best_after_this_color_move
                            moves = [(figures[0].ipos, hit)] + next_moves

    return moves, best_evaluate if best_evaluate is not None else position_evaluate(
        board)
コード例 #5
0
ファイル: chess.py プロジェクト: medfedeff/python-classes
            tp, pos = text_piece.split(" ")
            color, ttp = tp
            pieces.append(text_to_piece[ttp](pos, color))
        return cls(pieces)


import contextlib


@contextlib.contextmanager
def time_it():
    t = time.time()
    yield
    print time.time() - t


if __name__ == "__main__":
    board = Board(knorre_vs_neumann())

    t = time.time()
    for frm, to in get_next_step(board, "W", 2)[0]:
        print to_str_pos(fro), to_str_pos(to)
    print time.time() - t

    # print "best val =", val
    # for frm, to in moves:
    #     print to_str_pos(frm), to_str_pos(to)

    #for f,c in hit_order(board, to_int_pos("E5"), "W"):
    #    print f, c
コード例 #6
0
ファイル: chess.py プロジェクト: ddovbii/python-classes
 def __str__(self):
     return "{}({!r}, {!r})".format(
             self.__class__.__name__, to_str_pos(self.pos), self.color)
コード例 #7
0
ファイル: chess.py プロジェクト: ddovbii/python-classes
def _get_next_step(board, for_color, level=2, pstep="", hits_enabled=True):
    debug = False
    moves = []
    best_evaluate = None

    hit_checked = set()

    if 0 == level:
        if debug:
            print pstep + "empty"
        return [], position_evaluate(board)

    for piece in list(board):
        if piece.color == for_color:
            for mv in board.get_all_moves(piece):

                if debug:
                    print pstep + "mv ", piece, to_str_pos(mv)

                ppos = piece.ipos
                board.move(piece, mv)
                if level == 1:
                    next_moves = []
                    new_val = position_evaluate(board)
                else:
                    next_moves, new_val = _get_next_step(board, rev_color[for_color], level - 1, pstep + "    ")
                board.move(piece, ppos)

                if is_better_move(best_evaluate, new_val, for_color):
                    best_evaluate = new_val
                    moves = [(piece.ipos, mv)] + next_moves

            if hits_enabled:
                for hit in board.get_all_hits(piece):
                    if hit in hit_checked:
                        continue
                    else:
                        figures, evals = zip(*hit_order(board, hit, for_color))
                        best_after_this_color_move = (min if for_color == 'W' else max)(evals[::2])
                        
                        # need find best move after this position, disable hits to avoid internal loop
                        # apply hits
                        if evals[1::2]:
                            best_after_other_color_move = (max if for_color == 'W' else min)(evals[1::2])
                        else:
                            assert False

                        b = board.copy()
                        for figure in figures[:evals.index(best_after_other_color_move)]:
                            b.remove(hit)
                            print figure, to_str_pos(hit)
                            b.move(b.get(figure.ipos), hit)
                        _, new_eval = _get_next_step(b, for_color, 1, pstep + "    ", False)

                        if is_better_move(best_evaluate, new_eval, for_color):
                            best_evaluate = new_eval
                            moves = [(figures[0].ipos, hit)] + next_moves

                        if is_better_move(best_evaluate, best_after_this_color_move, for_color):
                            best_evaluate = best_after_this_color_move
                            moves = [(figures[0].ipos, hit)] + next_moves

    return moves, best_evaluate if best_evaluate is not None else position_evaluate(board)
コード例 #8
0
ファイル: chess.py プロジェクト: ddovbii/python-classes
    def parse(cls, *text_pieces):
        pieces = []
        for text_piece in text_pieces:
            tp, pos = text_piece.split(" ")
            color, ttp = tp
            pieces.append(text_to_piece[ttp](pos, color))
        return cls(pieces)


import contextlib
@contextlib.contextmanager
def time_it():    
    t = time.time()
    yield
    print time.time() - t

if __name__ == "__main__":
    board = Board(knorre_vs_neumann())

    t = time.time()
    for frm, to in get_next_step(board, "W", 2)[0]:
        print to_str_pos(fro), to_str_pos(to)
    print time.time() - t

    # print "best val =", val
    # for frm, to in moves:
    #     print to_str_pos(frm), to_str_pos(to)

    #for f,c in hit_order(board, to_int_pos("E5"), "W"):
    #    print f, c