Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def evaluate(node, color, variant="standard"):
score = 0
for field in fields:
piece = node.piece_at(getattr(chess, field))
if piece:
p = str(piece)
if variant=='atomic' and p.lower() == 'k':
if (color == 1 and p == 'k') or (color == -1 and p == 'K'):
adj = (get_adjacent_squares(field))
if color == -1:
attackers = sum([len(node.attackers(chess.BLACK, getattr(chess, a))) for a in adj])
score -= 200 * attackers
elif color == 1:
attackers = sum([len(node.attackers(chess.WHITE, getattr(chess, a))) for a in adj])
score += 200 * attackers
score += get_piece_value(p, variant)
field_coords = field_to_coords(field)
piece_value = variant_pst[variant][p.lower()]
if p.islower():
piece_value = variant_pst[variant][p][::-1]
if p.islower():
score -= piece_value[field_coords[0]][field_coords[1]]
else:
Returns ``None`` if the position was not found in any of the tables.
Otherwise the absolute value is the number of half moves until
forced mate. The value is positive if the side to move is winning,
otherwise it is negative.
In the example position white to move will get mated in 10 half moves:
>>> with chess.gaviota.open_tablebases("data/gaviota") as tablebases:
... tablebases.probe_dtm(chess.Board("8/8/8/8/8/8/8/K2kr3 w - - 0 1"))
...
-10
"""
white = [(square, board.piece_type_at(square), ) for square in chess.SquareSet(board.occupied_co[chess.WHITE])]
self.whiteSquares, self.whiteTypes = zip(*white)
black = [(square, board.piece_type_at(square), ) for square in chess.SquareSet(board.occupied_co[chess.BLACK])]
self.blackSquares, self.blackTypes = zip(*black)
side = 0 if (board.turn == chess.WHITE) else 1
self.realside = side
self.epsq = board.ep_square if board.ep_square else 0
return self._Probe(side, self.epsq)
def __init__(self, board: "ThreeCheckBoardT") -> None:
super().__init__(board)
self.remaining_checks_w = board.remaining_checks[chess.WHITE]
self.remaining_checks_b = board.remaining_checks[chess.BLACK]
def black(self) -> "Score":
"""Get the score from Black's point of view."""
return self.pov(chess.BLACK)
occupancies_black = map(lambda bb : bb & colors[chess.BLACK], occupancies)
occupancies_white = map(lambda bb : bb & colors[chess.WHITE], occupancies)
def start(config: Config):
PlayWithHumanConfig().update_play_config(config.play)
chess_model = PlayWithHuman(config)
env = ChessEnv().reset()
human_is_black = random() < 0.5
chess_model.start_game(human_is_black)
while not env.done:
if (env.board.turn == chess.BLACK) == human_is_black:
action = chess_model.move_by_human(env)
print("You move to: " + action)
else:
action = chess_model.move_by_ai(env)
print("AI moves to: " + action)
board, info = env.step(action)
env.render()
print("Board FEN = " + board.fen())
print("\nEnd of the game.") #spaces after this?
print("Game result:") #and this?
print(env.board.result())
def black(self, *, async_callback=None):
"""
Sets the side to move to be black, engine to play as white and
exits *force* mode.
:return: Nothing.
"""
self.set_side_to_move(chess.BLACK, async_callback)
def example():
board = chess.Board("r3k2r/pp3p2/4p2Q/4q1p1/4P3/P2PK3/6PP/R3R3 w q - 1 2")
print("rook x-ray, black, h3:")
print(xray_rook_attackers(board, chess.BLACK, chess.H3))
board = chess.Board("r1b1r1k1/pp1n1pbp/1qp3p1/B2p4/3P4/Q3PN2/PP2BPPP/R4RK1 b - - 0 1")
print("bishop x-ray, white, d8:")
print(xray_bishop_attackers(board, chess.WHITE, chess.D8))
def binary_encode(board):
""" Returns the board as a binary vector, for eval prediction purposes. """
rows = []
for color in [chess.WHITE, chess.BLACK]:
for ptype in range(chess.PAWN, chess.KING + 1):
mask = board.pieces_mask(ptype, color)
rows.append(list(map(int, bin(mask)[2:].zfill(64))))
ep = [0] * 64
if board.ep_square:
ep[board.ep_square] = 1
rows.append(ep)
rows.append([
int(board.turn),
int(bool(board.castling_rights & chess.BB_A1)),
int(bool(board.castling_rights & chess.BB_H1)),
int(bool(board.castling_rights & chess.BB_A8)),
int(bool(board.castling_rights & chess.BB_H8)),
int(board.is_check())
])
return np.concatenate(rows)
def _material_eval(self, board : chess.Board):
eval = 0.0
eval += len(board.pieces(chess.QUEEN, chess.WHITE)) * 9.0
eval += len(board.pieces(chess.ROOK, chess.WHITE)) * 5.0
eval += len(board.pieces(chess.BISHOP, chess.WHITE)) * 3.25
eval += len(board.pieces(chess.KNIGHT, chess.WHITE)) * 3.0
eval += len(board.pieces(chess.PAWN, chess.WHITE)) * 1.0
eval -= len(board.pieces(chess.QUEEN, chess.BLACK)) * 9.0
eval -= len(board.pieces(chess.ROOK, chess.BLACK)) * 5.0
eval -= len(board.pieces(chess.BISHOP, chess.BLACK)) * 3.25
eval -= len(board.pieces(chess.KNIGHT, chess.BLACK)) * 3.0
eval -= len(board.pieces(chess.PAWN, chess.BLACK)) * 1.0
eval += random.uniform(0.1, -0.1)
moves = list(board.pseudo_legal_moves)
move_val = len(moves)/50.0
if board.turn:
return self.scale_score(eval+move_val)
else:
return self.scale_score(-eval+move_val)