Last active
December 16, 2015 04:39
-
-
Save GreyTeardrop/5378422 to your computer and use it in GitHub Desktop.
Tic Tac Toe Tomec
This file contains hidden or 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-tomec.core | |
| (:require [teardrop.codejam.core :as jam] | |
| [clojure.string :as str]) | |
| (:gen-class)) | |
| (def board-size 4) | |
| (defn rows [board] | |
| board) | |
| (defn columns [board] | |
| (apply mapv vector board)) | |
| (defn diagonals [board] | |
| [(map #(nth %1 %2) board (range board-size)) | |
| (map #(nth %1 %2) board (reverse (range board-size)))]) | |
| (defn all-lines [board] | |
| (concat (rows board) (columns board) (diagonals board))) | |
| (defn winning-condition [sym board] | |
| (some (partial every? #{sym \T}) (all-lines board))) | |
| (defn case-solver [board] | |
| (let [has-empty-cells (some #{\.} (apply str board)) | |
| has-x-line (winning-condition \X board) | |
| has-o-line (winning-condition \O board)] | |
| (cond | |
| (and has-x-line has-o-line) "Draw" | |
| has-x-line "X won" | |
| has-o-line "O won" | |
| has-empty-cells "Game has not completed" | |
| :else "Draw"))) | |
| (defn case-parser [line-sq] | |
| (let [[board rest-sq] (split-at board-size line-sq)] | |
| [(case-solver board) (rest rest-sq)])) | |
| (def case-runner (partial jam/run-jam case-parser)) | |
| (defn -main | |
| ([] | |
| (println (case-runner *in*))) | |
| ([input-file] | |
| (println (case-runner input-file))) | |
| ([input-file output-file] | |
| (spit output-file (case-runner input-file)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment