-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
160 lines (106 loc) · 4.46 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# This file is part of YoBot_Bronze UCI Chess Engine.
# Copyright (C) 2021- Yohaan Seth Nathan (TheYoBots)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the MIT License.
#
# You should have received a copy of the MIT License along with this
# UCI Chess Engine. If not, view this https://opensource.org/licenses/MIT
import logging
import chess
import random
import sys
logger = logging.getLogger(__name__)
def get_best_move(board: chess.Board) -> chess.Move:
my_color = board.turn
global_max_score = -sys.maxsize
best_moves = []
# attacked_pieces = get_attacked_pieces(board, my_color)
# print(attacked_pieces)
for my_candidate_move in list(board.legal_moves):
board.push(my_candidate_move)
bonus = 0
if board.is_checkmate():
return my_candidate_move
if board.is_stalemate():
bonus = +500
if board.can_claim_threefold_repetition():
current_score = __get_board_total_score(board, my_color)
if current_score > 0:
bonus = +1
else:
bonus = -1
is_my_candidate_move_attacked = __is_attacked(board, my_candidate_move.to_square)
candidate_min_score = sys.maxsize
for opponent_candidate_move in list(board.legal_moves):
board.push(opponent_candidate_move)
if board.is_checkmate():
current_score = -9999
else:
current_score = __get_board_total_score(board, my_color) + bonus
if is_my_candidate_move_attacked:
current_score = current_score + 1
candidate_min_score = min(current_score, candidate_min_score)
board.pop()
if candidate_min_score > global_max_score:
global_max_score = candidate_min_score
best_moves = [my_candidate_move]
elif candidate_min_score == global_max_score:
best_moves.append(my_candidate_move)
board.pop()
best_move = random.choice(best_moves)
# Always promote to queen.
if best_move.uci()[-1].isalpha():
best_move.promotion = chess.QUEEN
return best_move
def get_attacked_pieces(board: chess.Board, defending_color: chess.Color):
attacked_pieces = { }
for square, piece in board.piece_map().items():
if board.color_at(square) != defending_color:
continue
attacking_pieces = get_attacking_pieces(board, not defending_color, square)
if len(attacking_pieces) > 0:
defending_pieces = get_defending_pieces(board, defending_color, square)
attacked_pieces[square] = (piece.piece_type, attacking_pieces, defending_pieces)
return attacked_pieces
def __is_attacked(board: chess.Board, square: chess.Square):
return len(board.attacks(square)) > 1
def get_attacking_pieces(board: chess.Board, attacking_color: chess.Color, square: chess.Square) -> [chess.PieceType]:
piece_types = []
attacking_squares = board.attackers(attacking_color, square)
for attacking_square in attacking_squares:
if board.is_pinned(attacking_color, attacking_square) == False:
piece_type = board.piece_type_at(attacking_square)
piece_types.append(piece_type)
return piece_types
def get_defending_pieces(board: chess.Board, defending_color: chess.Color, square: chess.Square) -> [chess.PieceType]:
cloned_board = board.copy()
defending_pieces = get_attacking_pieces(cloned_board, defending_color, square)
del cloned_board
return defending_pieces
def get_board_score(board: chess.Board, color: chess.Color) -> int:
total = 0
pawns = board.pieces(chess.PAWN, color)
bishops = board.pieces(chess.BISHOP, color)
knights = board.pieces(chess.KNIGHT, color)
queens = board.pieces(chess.QUEEN, color)
rooks = board.pieces(chess.ROOK, color)
for _ in pawns:
total += 10
for _ in bishops:
total += 30
for _ in knights:
total += 30
for _ in queens:
total += 90
for _ in rooks:
total += 50
if (board.has_kingside_castling_rights(color)):
total += 1
if (board.has_queenside_castling_rights(color)):
total += 1
return total
def __get_board_total_score(board: chess.Board, color: chess.Color) -> int:
color_score = get_board_score(board, color)
opposite_color_score = get_board_score(board, not color)
return color_score - opposite_color_score