Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Version inicial en Clojure #2

Merged
merged 1 commit into from

2 participants

@soyangel

No description provided.

@12meses12katas 12meses12katas merged commit d7b9060 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 4, 2011
  1. @soyangel

    Version inicial

    soyangel authored
This page is out of date. Refresh to see the latest.
View
6 soyangel/project.clj
@@ -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"]
+ ])
View
59 soyangel/src/minesweeper/core.clj
@@ -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)))
+
+
View
47 soyangel/test/minesweeper/test/core.clj
@@ -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"))))
Something went wrong with that request. Please try again.