这是一个代码,用于确保棋子的数量在每个棋子的最大限制内
This is a code to make sure the number of chess pieces is within the max limit for each piece
为什么此代码 return 值为 false?
它应该 return false 仅当其中一件超过它的最大总数或者如果国王件丢失,最后的循环打印每件的总和并且其中 none 超过了最大限制.但出于某种原因,它仍然 return 是错误的.....
chessboard = {
'1h': 'bking',
'6c': 'wqueen',
'2g': 'bbishop',
'5h': 'bqueen',
'3e': 'wking',
'4c': 'brook',
}
def check_validity(board):
if sum(value == 'wpawn' or value == 'bpawn' for value in board.values()) > 8:
return False
elif sum(value == 'wknight' or value == 'bknight' or value == 'bbishop' or value == 'wbishop' or value == 'wrook' or value == 'brook' for value in board.values()) > 2:
return False
elif sum(value == 'wqueen' or value == 'bqueen' for value in board.values()) > 1:
return False
elif sum(value == 'wking' or value == 'bking' for value in board.values()) != 1:
return False
elif sum(value == b or value == w for value in board.values[0]) > 16:
return False
else:
return True
print(check_validity(chessboard))
for key, values in chessboard.items():
print(values, sum(value == values for value in chessboard.values()))
输出:
False
bking 1
wqueen 1
bbishop 1
bqueen 1
wking 1
brook 1
你的每个总和计算几种类型的棋子,然后看看计数是否高于这些棋子中每个棋子的最大值。在您的示例中,国王的总和为 return 2,高于 1,因此该函数将为 return false。试试这个:
def check_validity(board):
pieces = ['wpawn', 'bpawn', 'wknight', 'bknight', 'bbishop', 'wbishop', 'wrook', 'brook', 'wqueen', 'bqueen', 'wking', 'bking']
max_pieces = [8, 8, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1]
for piece in zip(pieces, max_pieces):
if sum(value == piece[0] for value in board.values()) > piece[1]:
return False
return True
为什么此代码 return 值为 false? 它应该 return false 仅当其中一件超过它的最大总数或者如果国王件丢失,最后的循环打印每件的总和并且其中 none 超过了最大限制.但出于某种原因,它仍然 return 是错误的.....
chessboard = {
'1h': 'bking',
'6c': 'wqueen',
'2g': 'bbishop',
'5h': 'bqueen',
'3e': 'wking',
'4c': 'brook',
}
def check_validity(board):
if sum(value == 'wpawn' or value == 'bpawn' for value in board.values()) > 8:
return False
elif sum(value == 'wknight' or value == 'bknight' or value == 'bbishop' or value == 'wbishop' or value == 'wrook' or value == 'brook' for value in board.values()) > 2:
return False
elif sum(value == 'wqueen' or value == 'bqueen' for value in board.values()) > 1:
return False
elif sum(value == 'wking' or value == 'bking' for value in board.values()) != 1:
return False
elif sum(value == b or value == w for value in board.values[0]) > 16:
return False
else:
return True
print(check_validity(chessboard))
for key, values in chessboard.items():
print(values, sum(value == values for value in chessboard.values()))
输出:
False
bking 1
wqueen 1
bbishop 1
bqueen 1
wking 1
brook 1
你的每个总和计算几种类型的棋子,然后看看计数是否高于这些棋子中每个棋子的最大值。在您的示例中,国王的总和为 return 2,高于 1,因此该函数将为 return false。试试这个:
def check_validity(board):
pieces = ['wpawn', 'bpawn', 'wknight', 'bknight', 'bbishop', 'wbishop', 'wrook', 'brook', 'wqueen', 'bqueen', 'wking', 'bking']
max_pieces = [8, 8, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1]
for piece in zip(pieces, max_pieces):
if sum(value == piece[0] for value in board.values()) > piece[1]:
return False
return True