【问题标题】:How write correct condition? (FEN chess)如何写正确的条件? (分棋)
【发布时间】:2021-07-12 18:27:59
【问题描述】:

我正在尝试在 python 中构建一个简单的国际象棋游戏,我创建了一个包含棋子位置的字典。我想把字典改成Forsyth–Edwards Notation (FEN)(棋子的位置)。

curr_position = {'a8': 'r', 'b8': 'n', 'c8': 'b', 'd8': 'q', 'e8': 'k', 'f8': 'b', 'g8': 'n', 'h8': 'r', 'a7': 'p', 'b7': 'p', 'c7': 'p', 'd7': 'p', 'e7': 'p', 'f7': 'p', 'g7': 'p', 'h7': 'p', 'a6': None, 'b6': None, 'c6': None, 'd6': None, 'e6': None, 'f6': None, 'g6': None, 'h6': None, 'a5': None, 'b5': None, 'c5': None, 'd5': None, 'e5': None, 'f5': None, 'g5': None, 'h5': None, 'a4': None, 'b4': None, 'c4': None, 'd4': None, 'e4': None, 'f4': None, 'g4': None, 'h4': None, 'a3': None, 'b3': None, 'c3': None, 'd3': None, 'e3': None, 'f3': None, 'g3': None, 'h3': None, 'a2': 'P', 'b2': 'P', 'c2': 'P', 'd2': 'P', 'e2': 'P', 'f2': 'P', 'g2': 'P', 'h2': 'P', 'a1': 'R', 'b1': 'N', 'c1': 'B', 'd1': 'Q', 'e1': 'K', 'f1': 'B', 'g1': 'N', 'h1': 'R'}

上面的字典应该在 FEN 中返回类似这样的内容:

"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR"

我写的

def curr_fen():
    positions2 = ""
    pos_temp = 0
    prev_value = 0
    for ix, value in enumerate(curr_position.values()):
        if ix % 8 == 0 and ix != 0:
            positions2 += "/"
            pos_temp = 0
        if not value:
            pos_temp += 1
            positions2 += str(pos_temp)
        else:
            positions2 += value
    return positions2

print(curr_fen())

返回

"nbqkbnr/pppppppp/12345678/12345678/12345678/12345678/PPPPPPPP/RNBQKBNR" 

这是不正确的。如何调整我的函数以返回所需的输出?

【问题讨论】:

  • 你调试过这个吗?似乎在 GUI 调试器中单步调试代码(就像 PyCharm 中非常好的免费调试器一样)会阐明代码给出该输出的原因,这应该可以帮助您找出要修复的问题。我认为在发布之前调试代码可能很重要,这样您就可以准确地解释代码正在做什么以及您希望它做什么。
  • 我们不应该去异地资源来回答您的问题。您的问题应包含对 FEN 格式规则的口头描述 - 包含链接是可以的,但我们不必依赖它。
  • 感谢您的回答 :) Fen 一条记录包含棋盘的第 8 部分,由“/”分隔,负责棋盘中的一行。如果你在场上有一个棋子,你从顶部开始你正在添加这个棋子的第一个字母,比如“p”(小写字母代表白色,大代表黑色)如果该字段是空的,你正在检查下一个也是空的,并且你正在添加它的总和。例如,如果你在一行中间有两个棋子,则记录应如下所示“/3pp3/”,这意味着我有 3 个空字段的两个棋子和另外 3 个空字段在一行。

标签: python dictionary chess


【解决方案1】:

当您逐步通过棋盘位置时,您将使用pos_temp 跟踪连续的空方格

每当你发现一个有棋子的方块或你移动到下一个等级(如果之前的等级完全是空的)时,应该将连续的空方块的数量添加到字符串中。

pos_temp 在添加到字符串时需要重置为零。

我对您的代码做了一些添加和删除。

def curr_fen():
    positions2 = ""
    pos_temp = 0
    prev_value = 0
    for ix, value in enumerate(curr_position.values()):
        if ix % 8 == 0 and ix != 0:
            if pos_temp > 0:
                positions2 = positions2 + str(pos_temp)
            positions2 += "/"
            pos_temp = 0
        if not value:
            pos_temp += 1
            # positions2 += str(pos_temp)
        else:
            if pos_temp > 0:
                positions2 = positions2 + str(pos_temp)
                pos_temp = 0
            positions2 += value

    return positions2

您的解决方案依赖于按特定顺序构建的字典。这里有几个不依赖于原始字典顺序的替代方案。它们都有转换原始字典的中间步骤。

import collections
def f(d):
    rank_order = '87654321'
    file_order = 'abcdefgh'
    new = collections.defaultdict(dict)
    for (file,rank),piece in d.items():
        new[rank].update({file:piece})
    board = []
    for rank in rank_order:
        s = ''
        temp = 0
        for file in file_order:
            piece = new[rank][file]
            if piece is None:
                temp += 1
            else:
                if temp > 0:
                    s = f'{s}{temp}'
                    temp = 0
                s = f'{s}{piece}'
        if temp > 0:
            s = f'{s}{temp}'
        board.append(s)
    return '/'.join(board)

import itertools
def g(d):
    rank_order = '87654321'
    file_order = 'abcdefgh'
    new1 = collections.defaultdict(list)
    for (file,rank),piece in d.items():
        new1[rank].append((file,piece))
    board = []
    for rank in rank_order:
        s = ''
        positions = new1[rank]
        positions.sort()
        for piece,items in itertools.groupby(positions,key=lambda x:x[1]):
            n_pieces = len(list(items))
            if piece is None:
                s = f'{s}{n_pieces}'
            else:
                s = f'{s}{piece * n_pieces}'
        board.append(s)
    return '/'.join(board)

【讨论】:

    猜你喜欢
    • 2022-10-14
    • 2021-02-04
    • 1970-01-01
    • 2017-07-27
    • 2020-03-22
    • 1970-01-01
    • 2018-11-14
    • 2021-07-05
    • 2021-04-10
    相关资源
    最近更新 更多