Guest User

Untitled

a guest
Oct 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. (fn [player board]
  2. (let [column
  3. (fn [[x y]]
  4. (for [newy [0 1 2]]
  5. [x newy]))
  6.  
  7. row
  8. (fn [[x y]]
  9. (for [newx [0 1 2]]
  10. [newx y]))
  11.  
  12. diags
  13. (fn [[x y :as pos]]
  14. (let [up [[0 2] [1 1] [2 0]]
  15. down [[0 0] [1 1] [2 2]]]
  16. (filter #(some (partial = pos) %) [up down])))
  17.  
  18. triples
  19. (fn [pos]
  20. (into (diags pos) [(row pos) (column pos)]))
  21.  
  22. winning?
  23. (fn [board triple]
  24. (every? #(= player (get-in board %)) triple))
  25.  
  26. empties
  27. (fn [board]
  28. (for [x [0 1 2]
  29. y [0 1 2]
  30. :when (= :e (get-in board [x y]))]
  31. [x y]))
  32. ]
  33. (apply hash-set
  34. (filter
  35. (fn [pos]
  36. (let [new-board (assoc-in board pos player)
  37. lines (triples pos)]
  38. (some #(winning? new-board %) lines)))
  39.  
  40. (empties board)))))
Add Comment
Please, Sign In to add comment