程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何写出正确的条件? (奋棋)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何写出正确的条件? (奋棋)?

开发过程中遇到如何写出正确的条件? (奋棋)的问题如何解决?下面主要结合日常开发的经验,给出你关于如何写出正确的条件? (奋棋)的解决方法建议,希望对你解决如何写出正确的条件? (奋棋)有所启发或帮助;

我正在尝试用 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" 

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

解决方法

当您逐步浏览棋盘位置时,您会使用 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,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)

大佬总结

以上是大佬教程为你收集整理的如何写出正确的条件? (奋棋)全部内容,希望文章能够帮你解决如何写出正确的条件? (奋棋)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。