Last active
October 12, 2015 19:28
-
-
Save thattommyhall/4075632 to your computer and use it in GitHub Desktop.
p73
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
; Analyze a Tic-Tac-Toe Board - Hard | |
; A <a href="http://en.wikipedia.org/wiki/Tic-tac-toe">tic-tac-toe</a> | |
; board is represented by a two dimensional vector. | |
; X is represented by :x, O is represented by :o, and empty is represented by :e. | |
; A player wins by placing three Xs or three Os in a horizontal, vertical, or diagonal row. | |
; Write a function which analyzes a tic-tac-toe board and returns :x if X has won, :o if O has won, and nil if neither player has won. | |
; tags - game | |
; restricted - | |
(ns offline-4clojure.p73 | |
(:use clojure.test)) | |
(def __ | |
(fn [board] | |
(let [triples | |
(fn [board] | |
(map #(map (partial get-in board) %) | |
(concat | |
(for [x [0 1 2]] | |
[[x 0] [x 1] [x 2]]) | |
(for [y [0 1 2]] | |
[[0 y] [1 y] [2 y]]) | |
[[[0 0] [1 1] [2 2]] | |
[[0 2] [1 1] [2 0]]])))] | |
(ffirst (filter #(or (= [:o :o :o] %) | |
(= [:x :x :x] %)) | |
(triples board))))) | |
) | |
(defn -main [] | |
(are [x] x | |
(= nil (__ [[:e :e :e] | |
[:e :e :e] | |
[:e :e :e]])) | |
(= :x (__ [[:x :e :o] | |
[:x :e :e] | |
[:x :e :o]])) | |
(= :o (__ [[:e :x :e] | |
[:o :o :o] | |
[:x :e :x]])) | |
(= nil (__ [[:x :e :o] | |
[:x :x :e] | |
[:o :x :o]])) | |
(= :x (__ [[:x :e :e] | |
[:o :x :e] | |
[:o :e :x]])) | |
(= :o (__ [[:x :e :o] | |
[:x :o :e] | |
[:o :e :x]])) | |
(= nil (__ [[:x :o :x] | |
[:x :o :x] | |
[:o :x :o]])))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment