如何防止尝试概念创建休息
我正在建立一个棋子可视化器。以下是一个示例游戏。我必须在“ try-except”子句中构建程序,如果有一个以白色检查黑色结尾的游戏应该做什么,因为转弯的格式不是“转弯,白色,黑色”(例如1 。
董事会数据。我如何重建尝试,除非它仍然会打印板数据,而不仅仅是游戏数据?
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)
I am building a chess visualizer. Below is an example game. I had to build in a 'try-except' clause for what the program should do if there is a game that ends with white checkmating black, as then the format for a turn is not 'turn, white, black' (for example 1. e4 c5) but rather 'turn, white' (for example 88. Bd6#)
The try except clause works except that when I use it, it hits the last turn, throws up a value error, and then doesn't print the board data. How can I rebuild my try except so that it still prints the board data and not just the game data?
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用
finally: pass
:Try using
finally: pass
: