如何写正确的条件? (芬棋)
How write correct condition? (FEN chess)
我正在尝试在 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 中应该 return 类似这样的东西:
"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())
其中return
"nbqkbnr/pppppppp/12345678/12345678/12345678/12345678/PPPPPPPP/RNBQKBNR"
这是不正确的。我如何调整我的功能以 return 所需的输出?
当您逐步浏览棋盘位置时,您会跟踪连续的空 正方形 和 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)
我正在尝试在 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 中应该 return 类似这样的东西:
"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())
其中return
"nbqkbnr/pppppppp/12345678/12345678/12345678/12345678/PPPPPPPP/RNBQKBNR"
这是不正确的。我如何调整我的功能以 return 所需的输出?
当您逐步浏览棋盘位置时,您会跟踪连续的空 正方形 和 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)