Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defn not-nil? [x]
- (not (nil? x)))
- (defn get-max [coll]
- (if (empty? coll) 0
- (apply max coll)))
- (defn minimax
- "Minimax algorithm using only main heuristic."
- [boards player-num x depth]
- (if (or (nil? boards)
- (nil? (board/insert boards x player-num))) nil
- (if (= depth 0)
- (heuristic (board/insert boards x player-num) player-num x)
- (- (heuristic (board/insert boards x player-num) player-num x)
- (get-max (filter not-nil?
- (map #(minimax
- (board/insert boards x player-num)
- (- 3 player-num)
- %
- (- depth 1))
- [0 1 2 3 4 5 6])))))))
- (defn negamax
- [boards player-num x alpha beta depth]
- (if (or (nil? boards)
- (nil? (board/insert boards x player-num))) nil
- (if (= depth 0)
- (heuristic (board/insert boards x player-num) player-num x)
- (- (heuristic (board/insert boards x player-num) player-num x)
- (last (take-while #(<= % beta)
- (reductions #(max % (- (or (negamax
- (board/insert boards x player-num)
- (- 3 player-num)
- %2
- (- beta) (- %) (dec depth)) (- %))))
- alpha [0 1 2 3 4 5 6])))))))
- (defn get-highest-index [coll]
- (apply max-key second
- (filter #(not-nil? (second %)) coll)))
- (defn make-move
- "Generate next move using minimax
- and second heuristic if needed."
- [boards player-num depth]
- (first (get-highest-index
- (map-indexed vector (map #(negamax boards player-num % -INF +INF depth)
- [0 1 2 3 4 5 6])))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement