Advertisement
loloof64

Morpion clojure core

Aug 27th, 2013
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns morpion.core
  2.   (:gen-class))
  3.  
  4. (defn -main
  5.   []
  6.   (let [frame ('morpion.graphic/make-frame)]
  7.         (.setVisible frame true)
  8.     )
  9. )
  10.  
  11. (defn is-board?
  12.     "Says whether the board is well defined :
  13.  it must be a vector which has three vectors of three values
  14.  (whatever they are, but you should avoid collections)."
  15.     [board]    
  16.     (and
  17.         (vector? board)
  18.         (= 3 (count board))
  19.       (every? true? (map vector? board))
  20.       (every? #(= 3 (count %)) board)
  21.     )
  22. )
  23.  
  24.  
  25. (defn board-str
  26.     "Gets the board representation.
  27.     Board must be a collection of 3 collections of 3 values.
  28.     :cross or :circle are players values, any other value define blank."
  29.   [board] {:pre [(is-board? board)]}
  30.   (with-out-str
  31.      (doseq [line board]
  32.         (doseq [elem line]
  33.            (print ({:cross \X, :circle \O} elem \_))
  34.         )
  35.         (newline)
  36.      )
  37.   )
  38. )
  39.  
  40. (defn is-a-player-piece?
  41.     "Says whether the given value is a player piece value :
  42.     if it is either :cross or :circle."
  43.     [value]
  44.     (some #(= value %) '(:cross :circle))
  45. )
  46.  
  47. (defn valid-coords?
  48.     "Says whether a coords map is valid :
  49.      it should have both :x and :y integers values,
  50.      and both should be in [0,3[ (3 is excluded)."
  51.     [coords]
  52.     (and
  53.         (map? coords)
  54.         (:x coords) (integer? (:x coords)) (>= (:x coords) 0) (< (:x coords) 3)
  55.         (:y coords) (integer? (:y coords)) (>= (:y coords) 0) (< (:y coords) 3)
  56.     )
  57. )
  58.  
  59. (defn cell-played?
  60.     "Says whether the cell of the given coords (a map with :x and :y keys, and integers values)
  61.     in the given board has either a :cross or a :circle value."
  62.     [board coords] {:pre [(is-board? board) (valid-coords? coords)]}
  63.     (let [value ( (board (:y coords)) (:x coords) )]
  64.         (is-a-player-piece? value)
  65.     )
  66. )
  67.    
  68.  
  69. (defn play-board
  70.     "Tries to set the given value (either :cross or :circle), at the given coords
  71.   (a map with :x and :y keys, and integers values),
  72.  in the given board.
  73.  Only fails if either :circle or :cross is already set for the given cell.
  74.  Returns the modified board."
  75.     [board coords value]
  76.     {:pre [(is-board? board)
  77.                  (is-a-player-piece? value)
  78.                  (valid-coords? coords)]}
  79.     (if (cell-played? board coords)
  80.             board
  81.             (let [line-to-modify (board (:y coords))
  82.                         modified-line (assoc line-to-modify (:x coords) value)]
  83.                 (assoc board (:y coords) modified-line)
  84.             )
  85.     )
  86. )
  87.  
  88. (defn new-board
  89.     "Returns an empty board."
  90.     []
  91.     (vec (repeat 3 (vec (repeat 3 :none)) ))
  92. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement