有没有一种方法可以检查棋子字典验证器在同一个正方形上是否有两个棋子?

agxfikkp  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(276)

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


# 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()的函数,该函数接受一个dictionary参数并根据电路板是否有效返回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))
wfsdck30

wfsdck301#

在你的圈子里 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

相关问题