Skip to content

Instantly share code, notes, and snippets.

@paytonrules
Created January 21, 2016 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paytonrules/1f7e9662972422a60ae2 to your computer and use it in GitHub Desktop.
Save paytonrules/1f7e9662972422a60ae2 to your computer and use it in GitHub Desktop.
(defn- minimax-square
[{:keys [board current-player opposing-player player-turn square] :as params}]
(let [new-board (board/take-square board player-turn square)]
(cond
(board/winner? new-board current-player) 10
(board/winner? new-board opposing-player) -10
(board/tie? new-board current-player opposing-player) 0
:else (let [next-player (next-player current-player opposing-player player-turn)
next-board-states (board/open-squares new-board)]
(minimax-for-open-postions
(assoc params :board new-board :player-turn next-player) next-board-states)))))
(defn- minimax-for-open-postions [{:keys [current-player player-turn] :as params} open-squares]
(let [scores (map #(minimax-square (assoc params :square %)) open-squares)]
(if (= player-turn current-player)
(apply max scores)
(apply min scores))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment