Permalink
Browse files

Merge pull request #2 from soyangel/master

Version inicial en Clojure
  • Loading branch information...
2 parents b250e4f + c29eb3e commit d7b9060489fcc0752e3948f38bd4ee4683d8938e @12meses12katas 12meses12katas committed Aug 7, 2011
Showing with 112 additions and 0 deletions.
  1. +6 −0 soyangel/project.clj
  2. +59 −0 soyangel/src/minesweeper/core.clj
  3. +47 −0 soyangel/test/minesweeper/test/core.clj
View
@@ -0,0 +1,6 @@
+(defproject minesweeper "1.0.0-SNAPSHOT"
+ :description "FIXME: write description"
+ :dependencies [
+ [org.clojure/clojure "1.2.1"]
+ [swank-clojure "1.3.2"]
+ ])
@@ -0,0 +1,59 @@
+(ns minesweeper.core
+ (:require [clojure.string :as str]))
+
+(def neighbours [[0 -1] [1 -1] [1 0] [1 1] [0 1] [-1 1] [-1 0] [-1 -1]])
+
+(defn is-size-line? [line]
+ (re-matches #"\d+ \d+" line))
+
+(defn add-to-results [col line]
+ (if (is-size-line? line)
+ (if (= "0 0" line)
+ col
+ (conj (vec col) []))
+ (let [last-elem (conj (vec (last col)) line)]
+ (conj (vec (drop-last col)) last-elem))))
+
+(defn read-fields [st]
+ (let [lines (seq (.split st "\n"))]
+ (reduce add-to-results [] lines)))
+
+(defn get-pos [field x y]
+ (nth (nth field y ".") x \.))
+
+(defn field-rows [field]
+ (count field))
+
+(defn field-columns [field]
+ (count (first field)))
+
+(defn is-mine? [field x y]
+ (= \* (get-pos field x y)))
+
+(defn adjacent-mines [field x y]
+ (let [positions (map #(map + [x y] %) neighbours)
+ neighbour-values (map #(apply get-pos field %) positions)]
+ (count (filter #(= \* %) neighbour-values))))
+
+(defn mine-count-field [field]
+ (for [y (range (field-rows field))]
+ (apply str
+ (for [x (range (field-columns field))]
+ (if (is-mine? field x y)
+ \*
+ (adjacent-mines field x y))))))
+
+(defn minefield-string [number field]
+ (let [minefield (mine-count-field field)
+ first-line (format "Field #%d:\n" number)]
+ (str first-line (str/join "\n" minefield) "\n")))
+
+(defn minefields-string [fields]
+ (str/join "\n" (map #(minefield-string %1 %2)
+ (range 1 (inc (count fields)))
+ fields)))
+
+(defn minesweeper [field-string]
+ (minefields-string (read-fields field-string)))
+
+
@@ -0,0 +1,47 @@
+(ns minesweeper.test.core
+ (:use [minesweeper.core])
+ (:use [clojure.test]))
+
+(deftest read-single-field
+ (is (= [[".*"]]
+ (read-fields "1 2\n.*\n"))))
+
+(deftest read-two-fields
+ (is (= [[".*"] ["." "*"]]
+ (read-fields "1 2\n.*\n2 1\n.\n*\n"))))
+
+(deftest get-pos-test
+ (is (= \*
+ (get-pos ["." "*"] 0 1)))
+ (is (= \.
+ (get-pos ["." "*"] 0 0))))
+
+(deftest adjacent-mines-test
+ (is (= 0
+ (adjacent-mines ["..."] 0 0)))
+ (is (= 1
+ (adjacent-mines [".*."] 0 0)))
+ (is (= 2
+ (adjacent-mines [".*." "..." "..*"] 1 1)))
+ (is (= 4
+ (adjacent-mines [".*.." ".*.*" "**.."] 2 1)))
+ (is (= 3
+ (adjacent-mines ["***" "***" "***"] 2 0)))
+ )
+
+(deftest minefield-string-test
+ (is (= "Field #1:\n111\n1*1\n"
+ (minefield-string 1 ["..." ".*."])))
+ (is (= "Field #2:\n1*21\n112*\n"
+ (minefield-string 2 [".*.." "...*"])))
+ )
+
+(deftest minefields-string-test
+ (is (= "Field #1:\n23*\n**2\n\nField #2:\n*10\n110\n"
+ (minefields-string [["..*" "**."] ["*.." "..."]])))
+ )
+
+
+(deftest minesweeper-test
+ (is (= "Field #1:\n*100\n2210\n1*10\n1110\n\nField #2:\n**100\n33200\n1*100\n"
+ (minesweeper "4 4\n*...\n....\n.*..\n....\n3 5\n**...\n.....\n.*...\n0 0"))))

0 comments on commit d7b9060

Please sign in to comment.