是关于一个国际象棋的验证器:
我只完善到检查种类,数量,以及是否在棋谱;
最重要的检查每个种类的棋子,我使用了最简单,最长的一一列举,比较判断的方法,希望可以优化这一段。非常感谢!
s = {'1h':'bking', '6c':'wqueen', '2g':'bbishop', '5h':'bqueen', '3e':'wking'}
# s = {'2h':'wpamn', '3c':'wpamn', '4g':'wpamn', '6h':'wqueen', '7e':'wking','3h':'wpamn', '5c':'wpamn', '1g':'wpamn', '4h':'wqueen', '4e':'wking','9h':'wpamn', '8c':'wpamn', '7g':'wpamn', '7h':'wqueen', '8e':'wking','8h':'wpamn', '7c':'wpamn', '3g':'wpamn', '9e':'wking','1h':'wpamn', '6c':'wpamn', '2g':'wpamn', '5h':'wqueen', '3e':'wking'}
n = []
a = ''
y = []
#创建棋子种类
w = ['wpamn', 'wknight', 'wbishop', 'wrook', 'wqueen', 'wking','bpawn', 'bknight', 'bbishop', 'brook', 'bqueen', 'bking']
r = {'wpamn': 8, 'wknight': 2, 'wbishop': 2, 'wrook': 2, 'wqueen': 1, 'wking': 1,'bpawn': 8, 'bknight': 2, 'bbishop': 2, 'brook': 2, 'bqueen': 1, 'bking': 1}#原本想通过这个来对比判断棋子是否有多的,想了半天没啥结果
def is_valid_chess_board(name):
#最简单最麻烦的一一列举判断棋子是否正确
wp = 0
wk = 0
wb = 0
wr = 0
wq = 0
wki = 0
bp = 0
bk = 0
bb = 0
br = 0
bq = 0
bki = 0
for k, v in name.items():
y.append(v)
for i in y:
if i == 'wpamn':
wp += 1
for i in y:
if i == 'wknight':
wk += 1
for i in y:
if i == 'wbishop':
wb += 1
for i in y:
if i == 'wrook':
wr += 1
for i in y:
if i == 'wqueen':
wq += 1
for i in y:
if i == 'wking':
wki += 1
for i in y:
if i == 'bpamn':
bp += 1
for i in y:
if i == 'bknight':
bk += 1
for i in y:
if i == 'bbishop':
bb += 1
for i in y:
if i == 'brook':
br += 1
for i in y:
if i == 'bqueen':
bq += 1
for i in y:
if i == 'bking':
bki += 1
if wp > 8 or wk > 2 or wb > 2 or wq > 1 or wr > 2 or wki > 1:
print('白棋数量有误,请检查')
if bp > 8 or bk > 2 or bb > 2 or bq > 1 or br > 2 or bki > 1:
print('黑棋数量有误,请检查')
for i in range(1,9): #制造行
for x in ('abcdefgh'): #制造列
a = str(i) + x #拼接起来
n.append(a) #把他们加入列表,方便检查
# for k ,v in r.items():
# print(k,v)
# for k1 , v1 in s.items():
# print(k1,v1)
# if k1 == k:
# if v1 > v:
# print(k1 + '数量错误,多了:' + (v1-v))
# print(name.keys())
# print(n)
for k, v in name.items(): #循环检查是否在棋盘中
if k not in n:
print(v + '在' + k + '位置,不在棋盘了.')
if v not in w:
print(v + '不是国际象棋的棋子') #判断棋子种类是否正确
is_valid_chess_board(s)