如何防止除创建中断之外的尝试



我正在构建一个国际象棋可视化工具。下面是一个示例游戏。我必须建立一个"try-except"子句,说明如果有一个游戏以白色勾选黑色结束,程序应该怎么做,因为回合的格式不是"回合,白色,黑色"(例如1。e4c5(,而是"变白"(例如88。Bd6#(

try-except子句有效,只是当我使用它时,它会碰到最后一个回合,抛出一个值错误,然后不打印板数据。我如何重建我的尝试,除非它仍然打印棋盘数据,而不仅仅是游戏数据?

from pprint import pprint
game_data = '1. e4 c5 2. Nf3 d6 3. d4 cxd4 4. Nxd4 Nf6 5. Nc3 g6 6. f3 Bg7 7. Be3 O-O 8. Qd2  Nc6 9. O-O-O d5 10. exd5 Nxd5 11. Nxc6 bxc6 12. Bd4 e5 13. Bc5 Be6 14. Ne4 f5 15. Bxf8 Qxf8 16. Ng5 Bh6 17. Bc4 Rb8 18. h4 Qc5 19. Bb3 a5 20. Kb1 Qb6 21. c4 Nc7 22. Qd6 Bxg5 23. hxg5 Qb7 24. Qe7 Bf7 25. Rd7 Rf8 26. Rxc7 Qb8 27. c5 Qe8 28 Bxf7+ Qxf7 29. Rxh7 Qxe7 30. Rcxe7 Rb8 31. Reg7+ Kf8 32. Rh8+ Kxg7 33. Rxb8 e4 34. fxe4 fxe4 35. Re8 a4 36. Rxe4 a3 37. b3 Kh7 38. Ra4 Kg8 39. Rxa3 Kf7 40. Ra6 Kg7 41. Rxc6 Kh7 42. b4 Kg7 43. b5 Kh7 44. b6 Kg7 45. b7 Kh8 46. b8=B Kh7 47. a4 Kg7 48. a5 Kg8 49. a6 Kg7 50. a7 Kh7 51. a8=B Kg7 52. Rf6 Kh7 53. c6 Kg8 54. c7 Kh7 55. c8=B Kg7 56. Be4 Kh7 57. Rxg6 Kh8 58. Rh6+ Kg7 59. Be5+ Kf8 60. Rh8+ Kf7 61. g6+ Ke7 62. Re8+ Kxe8 63. g7 Kf7 64. Bd5+ Ke7 65. g8=B Kf8 66. g4 Ke7 67. g5 Ke8 68. g6 Kf8 69. g7+ Ke8 70. Bh1 Ke7 71. Bgd5 Kd8 72. Bcb7 Ke7 73. g8=B Kf8 74. Kc2 Ke7 75. Kd3 Kd7 76. Kd4 Ke7 77. Bde6 Kf8 78. Bf6 Ke8 79. Bd7+ Kxd7 80. Kd5 Ke8 81. Kd6 Kf8 82. Bc4 Ke8 83. Bg7 Kd8 84. Bd4 Ke8 85. Ke6 Kf8 86. Kf6 Ke8 87. Bhc6+ Kd8 88. Bb6#'
game_data_split = game_data.split('. ')
print(game_data_split)
alphabet = 'abcdefgh'
#below is just a chess board
board = [
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
]
for i, item in enumerate(game_data_split):
#the below try except is because some games have white checkmating on the final move which means there will only be {move no}{white move} and no {black move}
try:
turn,white,black = item.split()
except ValueError:
if i != len(game_data_split) -1:
raise
turn,white = item.split()
if white.endswith('+'):
x = alphabet.index(white[-3])
y = int(white[-2]) -1
board[y][x] += 0.01
elif 'O-O' in white:
x = 5
y = 0
board[y][x] += 5
x = 6
y = 0
board[y][x] += 5
x = 7
y = 0
board[y][x] += 5
elif 'O-O-O' in white:
x = 0
y = 0
board[y][x] += 5
x = 1
y = 0
board[y][x] += 5
x = 2
y = 0
board[y][x] += 5
elif white.endswith('#'):
x = alphabet.index(white[-3])
y = int(white[-2]) -1
board[y][x] += 10
else:
x = alphabet.index(white[-2])
y = int(white[-1]) - 1
board[y][x] += 100
if black.endswith('+'):
x = alphabet.index(black[-3])
y = int(black[-2]) -1
board[y][x] += .01
elif 'O-O' in black:
x = 0
y = 7
board[y][x] += 5
x = 1
y = 7
board[y][x] += 5
x = 2
y = 7
board[y][x] += 5
elif 'O-O-O' in black:
x = 5
y = 7
board[y][x] += 5
x = 6
y = 7
board[y][x] += 5
x = 7
y = 7
board[y][x] += 5
elif black.endswith('#'):
x = alphabet.index(black[-3])
y = int(black[-2]) -1
board[y][x] += 10
else:
x = alphabet.index(black[-2])
y = int(black[-1]) - 1
board[y][x] +=1000
print(turn)
pprint(board)

尝试使用finally: pass:

try:
turn,white,black = item.split()
except ValueError:
if i != len(game_data_split) -1:
raise
turn,white = item.split()
finally:
pass

最新更新