Hi all
I made a naïve attempt to implement the minimax algorithm in Clojure.
I would appreciate any comment on style, wrong (or right) use of
idioms etc.
Specifically, can I create a “contract” for the function I use, like
heuristic, to formalize minimax requirement from it?

Thanks
Tzach

Pseudocode from http://en.wikipedia.org/wiki/Minimax:

function integer minimax(node, depth)
    if node is a terminal node or depth == 0:
        return the heuristic value of node
    α = -∞
    for child in node:                       # evaluation is identical
for both players
        α = max(α, -minimax(child, depth-1))
    return α

My take in Clojure:

(defn minimax [pos depth player]
  "minimax implementation, return a pair of the best value and move.
   Require the following functions:
    heuristic - return the heuristic value of a pos
    movegen - return a sequence of legal moves
    next-pos - return the new position after a move was done"

  (let [moves (movegen pos player)]
    (if (or (empty? moves) (zero? depth))
      [(heuristic pos player) nil]
      (apply max-key first
             (cons
              [-9999 nil]
              (for [m moves :let [n-pos (next-pos pos m)]]
                [ (- (first (minimax n-pos (dec depth) (opposite player)))) m ]
                ))))))

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to