Advertisement
Guest User

Untitled

a guest
Apr 17th, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (defn not-nil? [x]
  2.   (not (nil? x)))
  3.  
  4. (defn get-max [coll]
  5.   (if (empty? coll) 0
  6.     (apply max coll)))
  7.  
  8. (defn minimax
  9.   "Minimax algorithm using only main heuristic."
  10.   [boards player-num x depth]
  11.   (if (or (nil? boards)
  12.           (nil? (board/insert boards x player-num))) nil
  13.     (if (= depth 0)
  14.       (heuristic (board/insert boards x player-num) player-num x)
  15.       (- (heuristic (board/insert boards x player-num) player-num x)
  16.          (get-max (filter not-nil?
  17.                           (map #(minimax
  18.                                  (board/insert boards x player-num)
  19.                                  (- 3 player-num)
  20.                                  %
  21.                                  (- depth 1))
  22.                                [0 1 2 3 4 5 6])))))))
  23.  
  24. (defn negamax
  25.   [boards player-num x alpha beta depth]
  26.   (if (or (nil? boards)
  27.           (nil? (board/insert boards x player-num))) nil
  28.       (if (= depth 0)
  29.         (heuristic (board/insert boards x player-num) player-num x)
  30.         (- (heuristic (board/insert boards x player-num) player-num x)
  31.         (last (take-while #(<= % beta)
  32.                           (reductions #(max % (- (or (negamax
  33.                                                       (board/insert boards x player-num)
  34.                                                       (- 3 player-num)
  35.                                                       %2
  36.                                                       (- beta) (- %) (dec depth)) (- %))))
  37.                                       alpha [0 1 2 3 4 5 6])))))))
  38.  
  39. (defn get-highest-index [coll]
  40.   (apply max-key second
  41.          (filter #(not-nil? (second %)) coll)))
  42.  
  43. (defn make-move
  44.   "Generate next move using minimax
  45.  and second heuristic if needed."
  46.   [boards player-num depth]
  47.   (first (get-highest-index
  48.           (map-indexed vector (map #(negamax boards player-num % -INF +INF depth)
  49.                                    [0 1 2 3 4 5 6])))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement