Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_meta_section(self, board, fen, rank, game_over):
padding = ' '
padding_alt = ' '
just_played = game_over or (
chess.WHITE
if len(board.san_move_stack_white) > len(board.san_move_stack_black)
else chess.BLACK
)
if rank == 0:
positions = fen.split(' ')[0]
ranks = positions.split('/')
# Calculate advantage pieces
(captured_white, captured_black) = self._get_captured_pieces(positions)
(white_advantage, black_advantage) = self._diff_pieces(captured_white, captured_black)
advantage_text = ''.join(map(self.get_piece, list(white_advantage)))
# Calculate advantage score
diff_score = self._score_pieces(white_advantage) - self._score_pieces(black_advantage)
score_text = '+{}'.format(diff_score) if diff_score > 0 else ''
return '{}{}{}{}'.format(padding, Colors.DULL_GRAY, advantage_text, score_text)
if rank == 1:
return ' {}'.format(self.get_user())
occupancies_white = map(lambda bb : bb & colors[chess.WHITE], occupancies)
col = pos % 8
# since you got the queen pose, add the mobility of queen
feature[177] = get_north_mobility(board, pos)
feature[178] = get_north_east_mobility(board, pos)
feature[179] = get_east_mobility(board, pos)
feature[180] = get_south_east_mobility(board, pos)
feature[181] = get_south_mobility(board, pos)
feature[182] = get_south_west_mobility(board, pos)
feature[183] = get_west_mobility(board, pos)
feature[184] = get_north_west_mobility(board, pos)
feature[23] = float(row) / 8.0
feature[24] = float(col) / 8.0
white_attack = list(board.attackers(chess.WHITE, pos))
min_val = 10
for lol in range(len(white_attack)):
curr_val = board.piece_type_at(white_attack[lol])
if curr_val < min_val:
min_val = curr_val
feature[25] = min_val
black_attack = list(board.attackers(chess.BLACK, pos))
min_val = 10
for lol in range(len(black_attack)):
curr_val = board.piece_type_at(black_attack[lol])
if curr_val < min_val:
min_val = curr_val
feature[26] = min_val
# white rooks
d5Found, e6Found = False, False
bpSqList = bb.pieces(chess.PAWN, chess.BLACK)
if chess.D5 in bpSqList:
d5Found = True
if chess.E6 in bpSqList:
e6Found = True
# Return early if we found a pattern
if d5Found and e6Found:
return True
# Case 2: wpawn at d5/e4 and bpawn at d6/e5 pattern
# Find wpawn at d5/e4
d5Found, e4Found = False, False
wpSqList = bb.pieces(chess.PAWN, chess.WHITE)
if chess.D5 in wpSqList:
d5Found = True
if chess.E4 in wpSqList:
e4Found = True
# We only check the black pattern when we found the white pattern
# Find bpawn at d6/e5
if d5Found and e4Found:
d6Found, e5Found = False, False
bpSqList = bb.pieces(chess.PAWN, chess.BLACK)
if chess.D6 in bpSqList:
d6Found = True
if chess.E5 in bpSqList:
e5Found = True
if d6Found and e5Found:
def eval(self, vec, board):
v = {'1-0': 1, '0-1': -1, '1/2-1/2': 0, '*': None}[board.result()]
if v is not None:
return (v if board.turn == chess.WHITE else -v), True
# Result doesn't check for repetitions unless we add claim_draw=True,
# but even then it doesn't quite do what we want.
if board.is_repetition(count=2):
return 0, True
return self.args.model.get_eval(vec, board), False
def board_to_feature(board):
feature = np.zeros(384)
"START: GLOBAL FEATURES"
# side-to-move
feature[0] = board.turn
# castling rights
feature[1] = board.has_kingside_castling_rights(chess.WHITE)
feature[2] = board.has_queenside_castling_rights(chess.WHITE)
feature[3] = board.has_kingside_castling_rights(chess.BLACK)
feature[4] = board.has_queenside_castling_rights(chess.BLACK)
# material configuration
feature[5] = len(board.pieces(chess.KING, chess.WHITE))
feature[6] = len(board.pieces(chess.QUEEN, chess.WHITE))
feature[7] = len(board.pieces(chess.ROOK, chess.WHITE))
feature[8] = len(board.pieces(chess.BISHOP, chess.WHITE))
feature[9] = len(board.pieces(chess.KNIGHT, chess.WHITE))
feature[10] = len(board.pieces(chess.PAWN, chess.WHITE))
feature[11] = len(board.pieces(chess.KING, chess.BLACK))
feature[12] = len(board.pieces(chess.QUEEN, chess.BLACK))
feature[13] = len(board.pieces(chess.ROOK, chess.BLACK))
feature[14] = len(board.pieces(chess.BISHOP, chess.BLACK))
feature[15] = len(board.pieces(chess.KNIGHT, chess.BLACK))
def write_pgn (pgn_dir, name, moves, outcome, winner):
dirs = pgn_dir
if not os.path.exists(dirs):
print('making directories {}'.format(dirs))
os.makedirs(dirs)
path = os.path.join(dirs, name) + '.pgn'
pgn = [chess.Board().variation_san(moves)]
if outcome:
if winner == chess.WHITE:
pgn.append('1-0')
else:
pgn.append('0-1')
else:
pgn.append('1/2-1/2')
with open(path, 'w') as out_file:
print('opened {}'.format(path))
print(' '.join(pgn), file=out_file)
print('closed {}'.format(path))
best_move = self.get_best_move(board)
output = OrderedDict([
('fen', board.fen()),
('fullmoveNumber', board.fullmove_number),
('result', board.result()),
('isGameOver', board.is_game_over()),
('isCheckmate',board.is_checkmate()),
('isStalemate', board.is_stalemate()),
('isInsufficientMaterial', board.is_insufficient_material()),
('isSeventyfiveMoves', board.is_seventyfive_moves()),
('isFivefoldRepetition', board.is_fivefold_repetition()),
('white', OrderedDict([
('hasKingsideCastlingRights', board.has_kingside_castling_rights(chess.WHITE)),
('hasQueensideCastlingRights', board.has_queenside_castling_rights(chess.WHITE)),
])),
('black', OrderedDict([
('hasKingsideCastlingRights', board.has_kingside_castling_rights(chess.BLACK)),
('hasQueensideCastlingRights', board.has_queenside_castling_rights(chess.BLACK)),
])),
('turn', OrderedDict([
('color', 'white' if board.turn is chess.WHITE else 'black'),
('isInCheck', board.is_check()),
('bestMove', best_move),
('legalMoves', [move.uci() for move in board.legal_moves]),
('canClaimDraw', board.can_claim_draw()),
('canClaimFiftyMoves', board.can_claim_fifty_moves()),
('canClaimThreefoldRepetition', board.can_claim_threefold_repetition()),
return False
if self.turn == chess.WHITE or self.kings & self.occupied_co[chess.BLACK] & chess.BB_RANK_8:
return True
black_kings = self.kings & self.occupied_co[chess.BLACK]
if not black_kings:
return True
black_king = chess.msb(black_kings)
# White has reached the backrank. The game is over if black can not
# also reach the backrank on the next move. Check if there are any
# safe squares for the king.
targets = chess.BB_KING_ATTACKS[black_king] & chess.BB_RANK_8
return all(self.attackers_mask(chess.WHITE, target) for target in chess.scan_forward(targets))
def hash_castling(self, board: chess.Board) -> int:
zobrist_hash = 0
# Hash in the castling flags.
if board.has_kingside_castling_rights(chess.WHITE):
zobrist_hash ^= self.array[768]
if board.has_queenside_castling_rights(chess.WHITE):
zobrist_hash ^= self.array[768 + 1]
if board.has_kingside_castling_rights(chess.BLACK):
zobrist_hash ^= self.array[768 + 2]
if board.has_queenside_castling_rights(chess.BLACK):
zobrist_hash ^= self.array[768 + 3]
return zobrist_hash