Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns morpion.core
- (:gen-class))
- (defn -main
- []
- (let [frame ('morpion.graphic/make-frame)]
- (.setVisible frame true)
- )
- )
- (defn is-board?
- "Says whether the board is well defined :
- it must be a vector which has three vectors of three values
- (whatever they are, but you should avoid collections)."
- [board]
- (and
- (vector? board)
- (= 3 (count board))
- (every? true? (map vector? board))
- (every? #(= 3 (count %)) board)
- )
- )
- (defn board-str
- "Gets the board representation.
- Board must be a collection of 3 collections of 3 values.
- :cross or :circle are players values, any other value define blank."
- [board] {:pre [(is-board? board)]}
- (with-out-str
- (doseq [line board]
- (doseq [elem line]
- (print ({:cross \X, :circle \O} elem \_))
- )
- (newline)
- )
- )
- )
- (defn is-a-player-piece?
- "Says whether the given value is a player piece value :
- if it is either :cross or :circle."
- [value]
- (some #(= value %) '(:cross :circle))
- )
- (defn valid-coords?
- "Says whether a coords map is valid :
- it should have both :x and :y integers values,
- and both should be in [0,3[ (3 is excluded)."
- [coords]
- (and
- (map? coords)
- (:x coords) (integer? (:x coords)) (>= (:x coords) 0) (< (:x coords) 3)
- (:y coords) (integer? (:y coords)) (>= (:y coords) 0) (< (:y coords) 3)
- )
- )
- (defn cell-played?
- "Says whether the cell of the given coords (a map with :x and :y keys, and integers values)
- in the given board has either a :cross or a :circle value."
- [board coords] {:pre [(is-board? board) (valid-coords? coords)]}
- (let [value ( (board (:y coords)) (:x coords) )]
- (is-a-player-piece? value)
- )
- )
- (defn play-board
- "Tries to set the given value (either :cross or :circle), at the given coords
- (a map with :x and :y keys, and integers values),
- in the given board.
- Only fails if either :circle or :cross is already set for the given cell.
- Returns the modified board."
- [board coords value]
- {:pre [(is-board? board)
- (is-a-player-piece? value)
- (valid-coords? coords)]}
- (if (cell-played? board coords)
- board
- (let [line-to-modify (board (:y coords))
- modified-line (assoc line-to-modify (:x coords) value)]
- (assoc board (:y coords) modified-line)
- )
- )
- )
- (defn new-board
- "Returns an empty board."
- []
- (vec (repeat 3 (vec (repeat 3 :none)) ))
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement