Python 是否有一种方法可以检查是否有两个棋子在同一个方块上,以供国际象棋字典验证器使用?

Python 是否有一种方法可以检查是否有两个棋子在同一个方块上,以供国际象棋字典验证器使用?,python,dictionary,duplicates,Python,Dictionary,Duplicates,有没有办法检查同一个正方形上是否有两块我试着用同样的方法来计算每种颜色的块数,但是没有用。我知道这本字典不能有重复的键。有没有办法检查重复的钥匙 #count how many times each sqaure is mentioned (for ex: if g7 is mentioned twice it should detect it) countSquares.setdefault(m, 0) countSquares[m] = countSquare

有没有办法检查同一个正方形上是否有两块我试着用同样的方法来计算每种颜色的块数,但是没有用。我知道这本字典不能有重复的键。有没有办法检查重复的钥匙

    #count how many times each sqaure is mentioned (for ex: if g7 is mentioned twice it should detect it)
    countSquares.setdefault(m, 0)
        countSquares[m] = countSquares[m] + 1
    #check if more than one piece are in the same square
    for j in countSquares:
        if countSquares.get(j, 0) > 1:
            print('Multiple pieces cannot exist in the same square!!')
            check = False
另外,我想知道是否有任何方法可以使代码的行数减少或运行速度加快。我知道这个特定程序的速度不是问题,但我想知道,如果我写一个类似但更大的程序,是否有办法使它更快

我试图解决的实践问题:

在本章中,我们使用字典值{'1h':'bking','6c':'wqueen','2g':'bbishop','5h':'bqueen','3e':'wking}来表示棋盘。编写一个名为isValidChessBoard()的函数,该函数接受字典参数,并根据电路板是否有效返回True或False。 一个有效的董事会将有一个黑人国王和一个白人国王。每个玩家最多只能有16个棋子,最多8个棋子,并且所有棋子必须在“1a”到“8h”的有效空间内;也就是说,一块不能在空间“9z”上。作品名称以“w”或“b”开头,代表白色或黑色,然后是“兵”、“骑士”、“主教”、“车”、“女王”或“国王”。此函数应检测错误何时导致不正确的棋盘

提前谢谢

import pprint
chessBoard = {'a1': 'wrook', 'b1': 'wknight', 'c1': 'wbishop', 'd1': 'wqueen', 'e1': 'wking', 'f1': 'wbishop', 'g1': 'wknight', 'h1': 'wrook',
              'a2': 'wpawn', 'b2': 'wpawn', 'c2': 'wpawn', 'd2': 'wpawn', 'e2': 'wpawn', 'f2': 'wpawn', 'g2': 'wpawn', 'h2': 'wpawn',
              'a8': 'brook', 'b8': 'bknight', 'c8': 'bbishop', 'd8': 'bqueen', 'e8': 'bking', 'f8': 'bbishop', 'g8': 'bknight', 'h8': 'brook',
              'a7': 'bpawn', 'b7': 'bpawn', 'c7': 'bpawn', 'd7': 'bpawn', 'e7': 'bpawn', 'f7': 'bpawn', 'g7': 'bpawn', 'g7': 'bpawn'}
validBoard = ['a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8',
              'b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8',
              'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8',
              'd1', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8',
              'e1', 'e2', 'e3', 'e4', 'e5', 'e6', 'e7', 'e8',
              'f1', 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8',
              'g1', 'g2', 'g3', 'g4', 'g5', 'g6', 'g7', 'g8',
              'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8']
whitePieces = ['wking', 'wqueen', 'wbishop', 'wknight', 'wrook', 'wpawn']
blackPieces = ['bking', 'bqueen', 'bbishop', 'bknight', 'brook', 'bpawn']
pieces = {'white': whitePieces, 'black': blackPieces}
count = {}
countSquares = {}

def isValidChessBoard(board):
    check = True
    for m, n in board.items():
        # Checks if the places occupied are a valid place on the chess board
        if m not in validBoard:
            print('{} is an invalid place'.format(m))
            check = False
        #keep count of every type of piece
        count.setdefault(n, 0)
        count[n] = count[n] + 1
        #count how many times each sqaure is mentioned (for ex: if g7 is mentioned twice it should detect it)
        countSquares.setdefault(m, 0)
        countSquares[m] = countSquares[m] + 1
    #check if more than one piece are in the same square
    for j in countSquares:
        if countSquares.get(j, 0) > 1:
            print('Multiple pieces cannot exist in the same square!!')
            check = False
    #for loop that checks for black pieces and white pieces
    for v in pieces.keys():
        num = 0
        for k in pieces[v]:
            num = num + count.get(k, 0) #keeps count of total pieces per colour
            # conditions to check for, for each colour
            if k == 'bking' or k == 'wking':
                if count.get(k, 0) == 1:
                    pass
                else:
                    print('invalid number of kings for {}'.format(v))
                    check = False
            if k == 'bpawn' or k == 'wpawn':
                if count.get(k, 0) <= 8:
                    pass
                else:
                    print('invalid number of pawns for {}'.format(v))
                    check = False
        print('{} has: {} pieces'.format(v, num))
        #check for total number of pieces per colour
        if num <= 16:
            pass
        else:
            print('invalid total number of pieces for {}'.format(v))
            check = False
    pprint.pprint(countSquares)

    if check:
        return True
    else:
        return False

print(isValidChessBoard(chessBoard))
导入pprint
棋盘={'a1':'wrook','b1':'wknight','c1':'wbishop','d1':'wqueen','e1':'wking','f1':'wbishop','g1':'wknight','h1':'wrook',
‘a2’:‘wpawn’、‘b2’:‘wpawn’、‘c2’:‘wpawn’、‘d2’:‘wpawn’、‘e2’:‘wpawn’、‘f2’:‘wpawn’、‘g2’:‘wpawn’、‘h2’:‘wpawn’,
‘a8’:‘布鲁克’、‘b8’:‘bknight’、‘c8’:‘bbishop’、‘d8’:‘bqueen’、‘e8’:‘bking’、‘f8’:‘bbishop’、‘g8’:‘bknight’、‘h8’:‘布鲁克’,
‘a7’:‘bpawn’、‘b7’:‘bpawn’、‘c7’:‘bpawn’、‘d7’:‘bpawn’、‘e7’:‘bpawn’、‘f7’:‘bpawn’、‘g7’:‘bpawn’、‘g7’:‘bpawn’)
有效板=['a1','a2','a3','a4','a5','a6','a7','a8',
‘b1’、‘b2’、‘b3’、‘b4’、‘b5’、‘b6’、‘b7’、‘b8’,
“c1”、“c2”、“c3”、“c4”、“c5”、“c6”、“c7”、“c8”,
“d1”、“d2”、“d3”、“d4”、“d5”、“d6”、“d7”、“d8”,
‘e1’、‘e2’、‘e3’、‘e4’、‘e5’、‘e6’、‘e7’、‘e8’,
‘f1’、‘f2’、‘f3’、‘f4’、‘f5’、‘f6’、‘f7’、‘f8’,
“g1”、“g2”、“g3”、“g4”、“g5”、“g6”、“g7”、“g8”,
‘h1’、‘h2’、‘h3’、‘h4’、‘h5’、‘h6’、‘h7’、‘h8’]
whitePieces=['wking','wqueen','wbishop','wknight','wrook','wpawn']
黑色碎片=['bking','Bkueen','bbishop','bknight','brook','bpawn']
碎片={'white':whitePieces,'black':blackPieces}
计数={}
countSquares={}
def isValidChessBoard(板):
检查=正确
对于板中的m,n.items():
#检查所占用的位置是否为棋盘上的有效位置
如果m不在有效板中:
打印({}是无效的位置。格式(m))
检查=错误
#数一数每种类型的物品
count.setdefault(n,0)
计数[n]=计数[n]+1
#计算每个sqaure被提及的次数(例如:如果g7被提及两次,它应该检测到)
countSquares.setdefault(m,0)
countSquares[m]=countSquares[m]+1
#检查是否有多个零件在同一个正方形中
对于平方中的j:
如果countSquares.get(j,0)>1:
打印('同一个正方形中不能存在多个碎片!!')
检查=错误
#用于检查黑色片段和白色片段的循环
对于分段的v.keys():
num=0
对于k的碎片[v]:
num=num+count.get(k,0)#记录每种颜色的总件数
#每种颜色的检查条件
如果k='bking'或k='wking':
如果count.get(k,0)==1:
通过
其他:
打印({}的国王数无效。格式(v))
检查=错误
如果k='bpawn'或k='wpawn':

如果循环中的count.get(k,0),
j
将是一个键值对,这就是为什么
countSquares.get(j,0)
并没有达到您期望的效果

要修复您的解决方案,请执行以下操作:

     for k, v in countSquares:
        if countSquares.get(k) > 1:
            print('Multiple pieces cannot exist in the same square!!')
            check = False
或者您可以使用:

        if not all(v == 1 for v in countSquares.values()):
            print('Multiple pieces cannot exist in the same square!!')
            check = False
这个问题非常适合,虽然我想它在这里也适用。。。