From de5300fc92cc9a77cdd641dd6f28b6de55bdb4de Mon Sep 17 00:00:00 2001 From: fieldsher <56013637+fieldsher@users.noreply.github.com> Date: Sun, 13 Mar 2022 15:18:50 -0700 Subject: [PATCH] Update ChessEngineHelper.py --- ChessHelpers/ChessEngineHelper.py | 86 +++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/ChessHelpers/ChessEngineHelper.py b/ChessHelpers/ChessEngineHelper.py index 827939c..e81258b 100644 --- a/ChessHelpers/ChessEngineHelper.py +++ b/ChessHelpers/ChessEngineHelper.py @@ -224,3 +224,89 @@ def score_material(self, board): count_black += 1 score -= self.piece_score[piece_type] return score + def control_diagonals(board): + # This procedure determines if diagonals are controlled by bishops or queen + + # Define figures capable of controlling the diagonals + diagonal_figures = [b, q] + + # set initial heuristic to zero + diagonal_heuristics = 0 + + # Convert board + chess_board = MakeMatrix().convert_to_matrix(board) + + # Define diagonals + black_diagonal = [chess_board[1,1], chess_board[2,2], chess_board[3,3], chess_board[4,4], + chess_board[5,5], chess_board[6,6], chess_board[7,7], chess_board[8,8]] + + white_diagonal = [chess_board[1,8], chess_board[2,7], chess_board[3,6], chess_board[4,5], + chess_board[5,4], chess_board[6,3], chess_board[7,2], chess_board[8,1]] + + + for cell in black_diagonal: + for figure in diagonal_figures: + if figure == cell: + diagonal_heuristics += 3 + for cell in white_diagonal: + for figure in diagonal_figures: + if figure == cell: + diagonal_heuristics += 3 + + + return diagonal_heuristics + + def control_center(board, side): + # This procedure will give heuristic points for control of central squares + + # Convert board + chess_board = MakeMatrix().convert_to_matrix(board) + + # define central squares: e4, e5, d4, d5 + central_squares = [chess_board[4,4], chess_board[4,5], chess_board[5,4], chess_board[5,5]] + + # Define squares can be used to control central squares by pawns + white_pawn_squares = [chess_board[3,3], chess_board[4,3], chess_board[5,3], chess_board[6,3], + chess_board[4,3], chess_board[4,4], chess_board[4,5], chess_board[4,6]] + + black_pawn_squares = [chess_board[3,6], chess_board[4,6], chess_board[5,6], chess_board[6,6], + chess_board[3,5], chess_board[4,5], chess_board[5,5], chess_board[5,6]] + + # Define squares can be used to control central squares by knights + knight_squares = [chess_board[3,2], chess_board[4,2], chess_board[5,2], chess_board[6,2], + chess_board[2,3], chess_board[3,3], chess_board[6,3], chess_board[7,3], + chess_board[2,4], chess_board[7,4], chess_board[2,5], chess_board[7,5], + chess_board[2,6], chess_board[3,6], chess_board[6,6], chess_board[7,6], + chess_board[3,7], chess_board[4,7], chess_board[5,7], chess_board[6,7]] + + # Set control center heuristics to 0 + ccHeuristic = 0 + + # Give points for each piece in central square (pawn or knight) + + for square in central_squares: + if square == p: + ccHeuristic +=1 + elif square == n: + ccHeuristic +=2 + + # Give points for white pawns in controlling positions + if side == white: + for square in white_pawn_squares: + if square == p: + ccHeuristic +=1 + + # Give points for black pawns in controlling positions + if side == black: + for square in black_pawn_squares: + if square == p: + ccHeuristic +=1 + + + # Give points for knights controlling central squares + + for square in knight_squares: + if square == n: + ccHeuristic += 2 + + return ccHeuristic