Created
January 17, 2014 03:52
-
-
Save chrisfjones/8468151 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns tic-tac-toe.core | |
(:use [clojure.pprint])) | |
(def board (take 9 (repeat nil))) | |
(defn- print-board-cell [cell] | |
(cond | |
(= :o cell) "O" | |
(= :x cell) "X" | |
:else ".")) | |
#_ (print-board-col nil) | |
(defn- print-board-row [row] | |
(->> row | |
(map print-board-cell) | |
(apply str) | |
(println))) | |
(defn print-board [board] | |
(let [partitioned (partition 3 board)] | |
(doall (map print-board-row partitioned))) | |
(println "\n")) | |
(defn index-of-position [x y] | |
(+ x (* 3 y))) | |
(defn place-move [board [x y] who] | |
(seq (assoc (vec board) (index-of-position x y) who))) | |
(defn cell-value [board [x y]] | |
(let [idx (index-of-position x y)] | |
(get board idx))) | |
(defn symbol-count [board sym] | |
(count (filter #(= sym %) board))) | |
(defn current-turn-symbol [board] | |
(if (= | |
(symbol-count board :x) | |
(symbol-count board :o)) | |
:x | |
:o)) | |
(defn move [board move] | |
(when | |
(not (cell-value board move)) | |
(place-move board move (current-turn-symbol board)))) | |
(defn board-lines [board] | |
(let [horizontal (partition 3 board) | |
first-col (map first horizontal) | |
sec-col (map second horizontal) | |
thrd-col (map #(nth % 2) horizontal) | |
diag-one [(for [n (range 3)] | |
(cell-value board [n n])) | |
] | |
diag-two [(map #(cell-value board %) (map vector (range 3) (reverse (range 3))))]] | |
diag-two | |
(list horizontal first-col sec-col thrd-col))) | |
(defn win? [board] | |
) | |
(-> board | |
(move [0 0]) | |
(move [0 1]) | |
(move [1 1]) | |
(board-lines)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment