|
10 | 10 | black-king black-queen black-rook |
11 | 11 | black-bishop black-knight black-pawn |
12 | 12 | empty-square white black |
13 | | - piece-type ->piece]] |
| 13 | + piece-type ->piece move-piece |
| 14 | + notation->coords]] |
14 | 15 | #_[taoensso.tufte :refer [defnp p profiled profile]] |
15 | 16 | #_[swordfight.profiling])) |
16 | 17 |
|
|
102 | 103 | (for [[y x] all-coords] |
103 | 104 | [(get-in board [y x]) [y x]])))) |
104 | 105 |
|
| 106 | +(defn board-value-delta [board board' move] |
| 107 | + (let [[move-from move-to] move |
| 108 | + [from-pos to-pos] [move-from (subs move-to 0 2)] |
| 109 | + [from-idx to-idx] (map notation->coords [from-pos to-pos])] |
| 110 | + (- (piece-val-with-bonus (get-in board' to-idx) to-idx) |
| 111 | + (piece-val-with-bonus (get-in board to-idx) to-idx) |
| 112 | + (piece-val-with-bonus (get-in board from-idx) from-idx)))) |
| 113 | + |
105 | 114 | (def checkmated-val {white -100000 black 100000}) |
106 | 115 | (defn stalemated-val [board] (/ (eval-board board) 4)) |
107 | 116 |
|
|
113 | 122 |
|
114 | 123 | (defn move-evaluation [game-state alpha beta depth piece-move] |
115 | 124 | (let [state-after-move (move game-state piece-move) |
| 125 | + extra-board-changes (not= (:board state-after-move) |
| 126 | + (first (move-piece (:board game-state) |
| 127 | + piece-move))) |
| 128 | + evaled-state-after-move (assoc state-after-move |
| 129 | + :board-value |
| 130 | + (if extra-board-changes |
| 131 | + ;; full board reevaluation only after |
| 132 | + ;; castling, en passant and promotions |
| 133 | + (eval-board (:board state-after-move)) |
| 134 | + (+ (:board-value game-state) |
| 135 | + (board-value-delta (:board game-state) |
| 136 | + (:board state-after-move) |
| 137 | + piece-move)))) |
116 | 138 | move-value (if (or (= depth 1) @hurried-move) |
117 | | - (eval-board (:board state-after-move)) |
| 139 | + (:board-value evaled-state-after-move) |
118 | 140 | (second |
119 | | - (best-move state-after-move alpha beta (dec depth))))] |
| 141 | + (best-move evaled-state-after-move alpha beta (dec depth))))] |
120 | 142 | [piece-move move-value])) |
121 | 143 |
|
122 | 144 | (defn best-move [{:keys [board turn] :as game-state} alpha beta depth] |
|
172 | 194 |
|
173 | 195 | (defn computer-move [game-state] |
174 | 196 | (let [moves-cnt (:moves-cnt game-state) |
| 197 | + board-value (eval-board (:board game-state)) |
| 198 | + evaled-board-game-state (assoc game-state :board-value board-value) |
175 | 199 | [square-from square-to] (if (and (< moves-cnt early-game-turns) |
176 | 200 | (false? (:edited game-state))) |
177 | | - (early-move game-state) |
178 | | - (midgame-move game-state))] |
| 201 | + (early-move evaled-board-game-state) |
| 202 | + (midgame-move evaled-board-game-state))] |
179 | 203 | [square-from square-to])) |
0 commit comments