简体   繁体   English

python chess board.attackers如何使用

[英]python chess board.attackers how to use

I'm playing around with some python chess code, its for fun.我正在玩一些 python 国际象棋代码,只是为了好玩。
However i have difficulty in using the board.attackers function.但是我很难使用 board.attackers function。
I'm not sure what it accepts我不确定它接受什么
This code below makes use of the chess lib described here:下面的代码使用了此处描述的国际象棋库:
http://python-chess.readthedocs.io/en/latest/core.html http://python-chess.readthedocs.io/en/latest/core.html

in the docs example code exists where they put in chess.F3 though i need some conversion from move to chess.xx i guess?.在文档中,示例代码存在于他们放入 chess.F3 的位置,尽管我猜我需要从 move 到 chess.xx 的一些转换?

def staticAnalysis(board, move, my_color):
  score = random.random()
  board.push(move)
 
  for (piece, value) in [(chess.PAWN, 1), 
                       (chess.BISHOP, 4), 
                       (chess.KING, 0), 
                       (chess.QUEEN, 10), 
                       (chess.KNIGHT, 5),
                       (chess.ROOK, 3)]:
    score += len(board.pieces(piece, my_color)) * value
    score -= len(board.pieces(piece, not my_color)) * value
     
  score += 100 if board.is_checkmate() else 0
  score += 10 if board.is_capture(move)  else 0

  # to get proposed destination of the move, removes piece, thus Ph1 becomes h1
  square =  str(move)[-2:]  

  # but its not accepted ?.. what can be used to do achieve this ?.
  myAttackers = board.attackers(not my_color,  square)
  score +=len(attackers)*-2

return score

For us to have a better idea, you should define your functions and variables.为了让我们有更好的想法,您应该定义您的函数和变量。 A bit of googling found the docs here: https://python-chess.readthedocs.io/en/latest/一些谷歌搜索在这里找到了文档: https://python-chess.readthedocs.io/en/latest/

Based on this snippet in the docs attackers = board.attackers(chess.WHITE, chess.F3) , it looks like you need to pass the color and square not as a string but as the chess.根据文档attackers = board.attackers(chess.WHITE, chess.F3)中的这个片段,看起来您需要传递颜色和正方形而不是作为字符串,而是作为chess. function. function。

Ok its more simple then i thought..好的,它比我想的更简单..

print(chess.B2)
#equals
print ((ord("B")-ord("A"))+(2-1)*8)

There are built-in methods that take care of converting between a square's str name and int value already:已经有内置方法负责在正方形的str名称和int值之间进行转换:

>>> chess.square_name(21)
'f3'
>>> chess.parse_square('f3')
21

However, your solution using ord() is technically more efficient as it runs in O(1) whereas chess.parse_square calls SQUARES.index(...) and runs in O(n).但是,您使用ord()的解决方案在技术上更高效,因为它在 O(1) 中运行,而chess.parse_square调用SQUARES.index(...)并在 O(n) 中运行。 But no need to reinvent the wheel here.但无需在这里重新发明轮子。


For more context, chess.Square is simply an alias for a Python int .有关更多上下文, chess.Square只是 Python int的别名。 Here is the actual source code where it's defined:这是定义它的实际源代码

Square = int
SQUARES = [
    A1, B1, C1, D1, E1, F1, G1, H1,
    A2, B2, C2, D2, E2, F2, G2, H2,
    A3, B3, C3, D3, E3, F3, G3, H3,
    A4, B4, C4, D4, E4, F4, G4, H4,
    A5, B5, C5, D5, E5, F5, G5, H5,
    A6, B6, C6, D6, E6, F6, G6, H6,
    A7, B7, C7, D7, E7, F7, G7, H7,
    A8, B8, C8, D8, E8, F8, G8, H8,
] = range(64)

This is shorthand for chess.A1 = 0 , chess.B1 = 1 , ... , chess.H8 = 63 .这是chess.A1 = 0 , chess.B1 = 1 , ... , chess.H8 = 63的简写。

In your code, you are providing attackers() with a string (because you define square = str(move)[-2:] ), not a chess.Square (an int ), which is causing your error.在您的代码中,您为attackers()提供了一个字符串(因为您定义了square = str(move)[-2:] ),而不是chess.Square (一个int ),这会导致您的错误。

You can replace the line where you define square with this, so its type is actually chess.Square (here I know the type of move is chess.Move since you use board.push(move) earlier):你可以用这个替换你定义square的那一行,所以它的类型实际上是chess.Square (这里我知道move的类型是chess.Move因为你之前使用board.push(move) ):

square = move.to_square  # a ``chess.Square``

On a side note, I noticed your piece values look a bit off.附带一提,我注意到你的单品价值看起来有点偏差。 Maybe you wanted them like this:也许你希望他们这样:

[(chess.PAWN, 1),
(chess.BISHOP, 3),
(chess.KING, 0),
(chess.QUEEN, 9),
(chess.KNIGHT, 3),
(chess.ROOK, 5)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM