chore: minor changes
diff --git a/src/battleship/core.clj b/src/battleship/core.clj
index aa344c9..b955fc9 100644
--- a/src/battleship/core.clj
+++ b/src/battleship/core.clj
@@ -4,6 +4,7 @@
(def matrix-row 5)
+(def true-period 5)
(defn compute-index
"Computes a cell index based on a row and a column. "
@@ -11,28 +12,29 @@
(+ col (* matrix-row row)))
(defn get-cell
- "Retrieves a cell from the battlefield: [:a :b :c]"
+ "Retrieves a cell from a battlefield."
[row col battlefield]
(battlefield (compute-index row col)))
(defn random-bool
"Generate a boolean based on the following frequency: 1/5 for a true and 4/5 for a false."
- []
- (= (rand-int (+ matrix-row 1)) matrix-row))
+ [period]
+ (= (rand-int (+ period 1)) 0))
(defn init-cell
- "Initialize a cell"
+ "Initialize a cell.
+ Ex: {:has-enemy? true|false :shot-by :none}"
[]
- {:has-enemy? (random-bool) :shot-by :none})
+ {:has-enemy? (random-bool true-period) :shot-by :none})
(defn generate-game
- "Generates a 5x5 matrix game.
- Each cell of this matrix looks like this: {:has-enemy? true/false :shot-by playerX}"
+ "Generates 5x5 matrix game.
+ Each cell of this matrix looks like this: {:has-enemy? true|false :shot-by :none}"
[]
(into [] (repeatedly (* matrix-row matrix-row) init-cell)))
-(defn found-active-enemy?
- "Predicate that tells if there is an active enemy at a given location."
+(defn found-enemy?
+ "Predicate that tells if there is an enemy at a given location."
[row col battlefield]
(let [cell (get-cell row col battlefield)]
(and (:has-enemy? cell) (= :none (:shot-by cell)))))
@@ -43,9 +45,9 @@
(frequencies (for [cell battlefield :when (not= (:shot-by cell) :none)] (:shot-by cell))))
(defn is-game-over?
- "Checks whether of not not there is an enemy left."
+ "Checks whether of not there is an enemy left."
[battlefield]
- (not (true? (some #(= {:has-enemy? true :shot-by :none} %) battlefield))))
+ (not (some #(= {:has-enemy? true :shot-by :none} %) battlefield)))
(defn battlefield-string
"Draws the battlefield with shot enemies."
diff --git a/src/battleship/logic.clj b/src/battleship/logic.clj
index bc1bbe0..2c24a1d 100644
--- a/src/battleship/logic.clj
+++ b/src/battleship/logic.clj
@@ -24,7 +24,7 @@
"A player attempt to shoot an enemy.
If an enemy is shot the fun returns :success otherwise :failure"
[row col player game]
- (if (found-active-enemy? row col @game)
+ (if (found-enemy? row col @game)
(do (shoot-enemy row col player game) :success)
:failure))
diff --git a/test/battleship/test/core.clj b/test/battleship/test/core.clj
index 8c3ac2a..2db6e96 100644
--- a/test/battleship/test/core.clj
+++ b/test/battleship/test/core.clj
@@ -50,18 +50,18 @@
(let [actual (get-cell 2 1 battlefield)]
(is (= actual :l))))))
-(deftest test-found-active-enemy?
+(deftest test-found-enemy?
(let [battlefield [{:has-enemy? true :shot-by "playerX"} :b {:has-enemy? false :shot-by :none} :d :e :f :g :h :i :j :k {:has-enemy? true :shot-by :none} :m :n :o :p :q :r :s :t :u :v :w :x :y]]
(testing "5x5 matrice: Active enemy found at location 2 1"
- (let [actual (found-active-enemy? 2 1 battlefield)]
+ (let [actual (found-enemy? 2 1 battlefield)]
(is (= actual true))))
(testing "5x5 matrice: Active enemy not found at location 0 2"
- (let [actual (found-active-enemy? 0 2 battlefield)]
+ (let [actual (found-enemy? 0 2 battlefield)]
(is (= actual false))))
(testing "5x5 matrice: Active Enemy not found at location 0 0"
- (let [actual (found-active-enemy? 0 0 battlefield)]
+ (let [actual (found-enemy? 0 0 battlefield)]
(is (= actual false))))))
(deftest test-score