Guest User

Nim

a guest
Nov 13th, 2014
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns nim.core
  2.   (:gen-class))
  3.  
  4. (defn winning-heaps? [heaps]
  5.   (and
  6.    (or
  7.     (and (every? #(> 2 %) heaps) (odd? (reduce + heaps)))
  8.     (zero? (reduce bit-xor heaps)))
  9.    (not (zero? (reduce + heaps)))))
  10.  
  11. (defn new-heaps [n min max]
  12.   (let [heaps (vec (repeatedly n #(+ min (rand-int (- (inc max) min)))))]
  13.     (if (winning-heaps? heaps)
  14.       (let [first-row (nth heaps 0)]
  15.         (if (== first-row max)
  16.           (assoc heaps 0 (dec first-row))
  17.           (assoc heaps 0 (inc first-row))))
  18.       heaps)))
  19.  
  20. (defn all-moves [heaps]
  21.   (->> heaps
  22.        (map-indexed (fn [index heap]
  23.                       (for [new-heap (range heap)]
  24.                         (assoc heaps index new-heap))))
  25.        (apply concat)))
  26.  
  27. (defn best-move [heaps]
  28.   (if (winning-heaps? heaps)
  29.     (first (for [new-heaps (all-moves heaps)
  30.                  :when (= (reduce + new-heaps) (dec (reduce + heaps)))] new-heaps))
  31.     (first (for [new-heaps (all-moves heaps)
  32.                  :when (winning-heaps? new-heaps)] new-heaps))))
  33.  
  34. (defn legal-move? [heaps move]
  35.   (some #(= move %) (all-moves heaps)))
  36.  
  37. (defn game-over? [heaps]
  38.   (every? zero? heaps))
  39.  
  40. (defn heap-string
  41.   [heaps]
  42.   (clojure.string/join (map-indexed (fn [row count]
  43.                                       (str row ": " (clojure.string/join (repeat count "I")) "\n"))
  44.                                     heaps)))
  45. (defn prompt
  46.   [msg]
  47.   (print (str msg " "))
  48.   (flush)
  49.   (read-line))
  50.  
  51. (defn prompt-int
  52.   [msg]
  53.   (try
  54.     (Integer. (prompt msg))
  55.     (catch Exception e (prompt-int msg))))
  56.  
  57. (defn input-move
  58.   [heaps]
  59.   (println (heap-string heaps))
  60.   (let [row (prompt-int "From which row do you want to remove?")
  61.         amount (prompt-int "How many do you want to leave?")]
  62.     (when (and (neg? row) (>= row (count heaps)))
  63.         (println "Illegal move!")
  64.         (input-move heaps))
  65.     (let [new-heaps (assoc heaps row amount)]
  66.       (if-not (legal-move? heaps new-heaps)
  67.         (do
  68.           (println "Illegal move!")
  69.           (input-move heaps))
  70.         new-heaps))))
  71.  
  72. (defn -main
  73.   [& args]
  74.   (loop [myturn false
  75.          heaps (new-heaps 3 3 9)]
  76.     (cond
  77.      (and (game-over? heaps) myturn) (println "You lost!")
  78.      (and (game-over? heaps) (not myturn)) (println "You won!")
  79.      :else (if myturn
  80.              (recur false (best-move heaps))
  81.              (recur true (input-move heaps))))))
Advertisement
Add Comment
Please, Sign In to add comment