Skip to content

Instantly share code, notes, and snippets.

@malinatran
Last active August 3, 2016 23:44
Show Gist options
  • Save malinatran/b5b2c4f076042200a00a967441045ac1 to your computer and use it in GitHub Desktop.
Save malinatran/b5b2c4f076042200a00a967441045ac1 to your computer and use it in GitHub Desktop.
(defn is-computer?
[player marker]
(= (.marker player) marker))
(defn calculate-win
[board players depth marker]
(let [winner (get-winner board players)]
(if (is-computer? winner marker)
(- 10 depth)
(- depth 10))))
(defn best-move-and-score
[player moves marker]
(if (is-computer? player marker)
(apply max-key val moves)
(apply min-key val moves)))
(defn best-score
[player moves marker]
(val (best-move-and-score player moves marker)))
(defn best-move
[player moves marker]
(key (best-move-and-score player moves marker)))
(defn get-score
[board players depth marker]
(cond (win? board players) (calculate-win board players depth marker)
(tie? board players) 0
:else (best-score (second players) (score-moves board (switch-player players) (inc depth) marker) marker)))
(defn score-moves
[board players depth marker]
(let [moves (get-empty-cells board)
player (first players)
scores (map #(get-score (mark-cell board % (.marker player)) players depth marker) moves)]
(zipmap moves scores)))
(defn get-minimax-move
[board players marker]
(let [depth 0
player (first players)
scored-moves (score-moves board players depth marker)]
(best-move player scored-moves marker)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment