How to use the chess.square_file function in chess

To help you get started, we’ve selected a few chess examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github emdoyle / chess_ai / pychess_utils.py View on Github external
def dir_and_dist(square1, square2):
	rank1 = chess.square_rank(square1)
	rank2 = chess.square_rank(square2)
	file1 = chess.square_file(square1)
	file2 = chess.square_file(square2)

	horiz = 'W' if file1 > file2 else 'E'
	vert = 'S' if rank1 > rank2 else 'N'

	filedist = abs(file1 - file2)
	rankdist = abs(rank1 - rank2)
	diag = filedist == rankdist
	straight = filedist == 0 or rankdist == 0
	knight = (filedist == 1 and rankdist == 2) or (filedist == 2 and rankdist == 1)
	far = 'F' if rankdist == 2 else 'S'

	if diag:
		dist = filedist
		return {'dir': '{}{}'.format(vert, horiz), 'dist': dist}
	elif straight:
github niklasf / python-chess / chess / gaviota.py View on Github external
x = flip_we(x)
        y = flip_we(y)

    if chess.square_rank(x) > 3:
        x = flip_ns(x)
        y = flip_ns(y)

    rowx = chess.square_rank(x)
    colx = chess.square_file(x)

    if rowx > colx:
        x = flip_nw_se(x)
        y = flip_nw_se(y)

    rowy = chess.square_rank(y)
    coly = chess.square_file(y)

    if rowx == colx and rowy > coly:
        x = flip_nw_se(x)
        y = flip_nw_se(y)

    return x, y
github fsmosca / Python-Easy-Chess-GUI / python_easy_chess_gui.py View on Github external
def get_col(self, s):
        """ Returns col given square s """
        return chess.square_file(s)
github niklasf / python-chess / chess / svg.py View on Github external
x = (file_index if not flipped else 7 - file_index) * SQUARE_SIZE + margin
            svg.append(_text(file_name, x, 0, SQUARE_SIZE, margin))
            svg.append(_text(file_name, x, margin + 8 * SQUARE_SIZE, SQUARE_SIZE, margin))
        for rank_index, rank_name in enumerate(chess.RANK_NAMES):
            y = (7 - rank_index if not flipped else rank_index) * SQUARE_SIZE + margin
            svg.append(_text(rank_name, 0, y, margin, SQUARE_SIZE))
            svg.append(_text(rank_name, margin + 8 * SQUARE_SIZE, y, margin, SQUARE_SIZE))

    for arrow in arrows:
        try:
            tail, head, color = arrow.tail, arrow.head, arrow.color  # type: ignore
        except AttributeError:
            tail, head = arrow  # type: ignore
            color = "#888"

        tail_file = chess.square_file(tail)
        tail_rank = chess.square_rank(tail)
        head_file = chess.square_file(head)
        head_rank = chess.square_rank(head)

        xtail = margin + (tail_file + 0.5 if not flipped else 7.5 - tail_file) * SQUARE_SIZE
        ytail = margin + (7.5 - tail_rank if not flipped else tail_rank + 0.5) * SQUARE_SIZE
        xhead = margin + (head_file + 0.5 if not flipped else 7.5 - head_file) * SQUARE_SIZE
        yhead = margin + (7.5 - head_rank if not flipped else head_rank + 0.5) * SQUARE_SIZE

        if (head_file, head_rank) == (tail_file, tail_rank):
            ET.SubElement(svg, "circle", {
                "cx": str(xhead),
                "cy": str(yhead),
                "r": str(SQUARE_SIZE * 0.9 / 2),
                "stroke-width": str(SQUARE_SIZE * 0.1),
                "stroke": color,
github niklasf / python-chess / chess / gaviota.py View on Github external
if chess.square_rank(x) > 3:
        x = flip_ns(x)
        y = flip_ns(y)
        ret |= 2

    rowx = chess.square_rank(x)
    colx = chess.square_file(x)

    if rowx > colx:
        x = flip_nw_se(x)
        y = flip_nw_se(y)
        ret |= 4

    rowy = chess.square_rank(y)
    coly = chess.square_file(y)
    if rowx == colx and rowy > coly:
        x = flip_nw_se(x)
        y = flip_nw_se(y)
        ret |= 4

    return ret
github SamRagusa / Batch-First / batch_first / __init__.py View on Github external
diffs[(-j, -j)] = j + 48

    diffs[(1, 2)] = 56
    diffs[(1, -2)] = 57
    diffs[(-1, 2)] = 58
    diffs[(-1, -2)] = 59
    diffs[(2, 1)] = 60
    diffs[(2, -1)] = 61
    diffs[(-2, 1)] = 62
    diffs[(-2, -1)] = 63

    filter_table = np.zeros([64,64,6], dtype=np.uint8)

    for square1 in chess.SQUARES:
        for square2 in chess.SQUARES:
            file_diff = chess.square_file(square2) - chess.square_file(square1)
            rank_diff = chess.square_rank(square2) - chess.square_rank(square1)
            if not diffs.get((file_diff, rank_diff)) is None:
                filter_table[square1, square2] = diffs[(file_diff, rank_diff)]

                if rank_diff == 1 and file_diff in [1,0,-1]:
                    filter_table[square1, square2, 2:5] = 3*(1+file_diff) + np.arange(64,67)
    return filter_table
github niklasf / python-chess / chess / gaviota.py View on Github external
def norm_kkindex(x: chess.Square, y: chess.Square) -> Tuple[int, int]:
    if chess.square_file(x) > 3:
        x = flip_we(x)
        y = flip_we(y)

    if chess.square_rank(x) > 3:
        x = flip_ns(x)
        y = flip_ns(y)

    rowx = chess.square_rank(x)
    colx = chess.square_file(x)

    if rowx > colx:
        x = flip_nw_se(x)
        y = flip_nw_se(y)

    rowy = chess.square_rank(y)
    coly = chess.square_file(y)

    if rowx == colx and rowy > coly:
        x = flip_nw_se(x)
        y = flip_nw_se(y)

    return x, y

chess

A chess library with move generation and validation, Polyglot opening book probing, PGN reading and writing, Gaviota tablebase probing, Syzygy tablebase probing, and XBoard/UCI engine communication.

GPL-3.0
Latest version published 2 months ago

Package Health Score

82 / 100
Full package analysis