Last active
September 16, 2024 05:36
-
-
Save baz44/2888fdb0d29c16bfc7117ce8360a4793 to your computer and use it in GitHub Desktop.
tic-tac-toe
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.core) | |
| (def board-size 3) | |
| (defn winner [board] | |
| "It takes a board and checks for a winner, returns nil if no winner is found" | |
| (let [rows board | |
| columns (apply mapv vector board) | |
| diagonal (mapv #(get-in board [% %]) (range board-size)) | |
| anti-diagonal (mapv #(get-in board [% (- (dec board-size) %)]) (range board-size))] | |
| (let [check-winner-fn (fn [coll] | |
| (when (= (-> coll distinct count) 1) | |
| (first coll)))] | |
| (or (some check-winner-fn rows) | |
| (some check-winner-fn columns) | |
| (check-winner-fn diagonal) | |
| (check-winner-fn anti-diagonal))))) |
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.core-test | |
| (:require [tic-tac-toe.core :as core] | |
| [clojure.test :refer :all])) | |
| (deftest winner-test | |
| (testing "it should return nil if empty board is given" | |
| (is (nil? (core/winner [[nil nil nil] | |
| [nil nil nil] | |
| [nil nil nil]])))) | |
| (testing "it should return nil if game is finished an no winner can be found" | |
| (is (nil? (core/winner [[\x \o \o] | |
| [\o \x \x] | |
| [\x \o \o]])))) | |
| (testing "it should return nil if game is not finished an no winner can be found" | |
| (is (nil? (core/winner [[\x \o \o] | |
| [\o \x \x] | |
| [\x \o nil]])))) | |
| (testing "it should return x if player x won the game by matching a row" | |
| (is (= \x (core/winner [[\x \x \x] | |
| [\o \x \x] | |
| [\x \o \o]])))) | |
| (testing "it should return o if player o won the game by matching a row" | |
| (is (= \o (core/winner [[\x \o \x] | |
| [\o \o \o] | |
| [\x \x \o]])))) | |
| (testing "it should return o if player o won the game by matching a column" | |
| (is (= \o (core/winner [[\x \o \x] | |
| [\o \o \x] | |
| [\x \o \o]])))) | |
| (testing "it should return o if player o won the game by matching a diagonal" | |
| (is (= \o (core/winner [[\o \x \x] | |
| [\o \o \x] | |
| [\x \o \o]])))) | |
| (testing "it should return o if player o won the game by matching a counter diagonal" | |
| (is (= \o (core/winner [[\x \x \o] | |
| [\o \o \x] | |
| [\o \o \x]])))) | |
| (testing "it should return o if player o won the game by matching a counter diagonal and board is not full" | |
| (is (= \o (core/winner [[\x \x \o] | |
| [\o \o \x] | |
| [\o nil nil]]))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment