Hey guys, how are you?
I’m working on a project and I’m getting the weirdest error I’ve ever seen in my life. I’m quite new to clojure, so I really don’t know what I’m doing wrong. Following is the code I’m using to test my code:
(testing "should attack dinosaurs next to border"
(do (add-robot 0 0 "L"))
(do (add-dino 0 1))
(do (issue-instruction 0 0 "A"))
(is (nil? (get-in @board [0 1])))))
First, @board is a 50x50 atom (a vector of vectors). Then, It adds a robot (which is represented by a string “RL” since its facing left), then it adds a dinosaur, which doesn’t face any direction, and then it issues an attack command which makes the robot attack dinosaurs directly above, below, to the left or to the right of it. Attacking a dinosaur means the program will change the position in which that dinosaur is in from “D” to nil in the board (which itself is an atom).
The code for add-dino is as follows:
(defn add-dino [x y]
(if (or (out-of-bounds? x y)(not (is-spot-available? x y))) @board
(swap! board update-in [(Integer. x) (Integer. y)] str "D")))
The code for add-robot is as follows:
(defn add-robot [x y direction]
(if (or (out-of-bounds? x y)(not (is-spot-available? x y))(nil? direction)) @board
(swap! board update-in [(Integer. x) (Integer. y)] str "R" direction)))
I’ve tested both of them in another testcases, so I’m fairly confident the problem is not with them. The code to attack is the following:
(defn perform-attack [[x y]]
(swap! board update-in [x y] (fn [arg] nil)))
(defn get-attack-coordinates [x y]
(into [] (filter #(and (not(out-of-bounds?(:x %)(:y %)))
(is-dino?(:x %)(:y %)))(coordinates x y))))
(defn attack [x y] (map (fn [{:keys [x y]}] (perform-attack [x y])) (get-attack-coordinates x y)))
I’ve also tested get-attack-coordinates, which als seems to work. So, when I naively test my file (board_controller.clj) by placing the following commands to the end of it:
(add-robot 0 0 "L")
(add-dino 0 1)
(println (get-board))
(println (issue-instruction 0 0 "A"))
I’m able to see that board[0 1] is correctly updated to nil. However, in my test file (which I mentioned previously), the test only passes if I add a println
command to the issue-instruction method. So, this test doesn’t pass:
(testing "should attack dinosaurs next to border"
(do (add-robot 0 0 "L"))
(do (add-dino 0 1))
(do (issue-instruction 0 0 "A"))
(is (nil? (get-in @board [0 1])))))
but this one does:
(testing "should attack dinosaurs next to border"
(do (add-robot 0 0 "L"))
(do (add-dino 0 1))
(do (println (issue-instruction 0 0 "A")))
(is (nil? (get-in @board [0 1])))))
Does anyone have any idea on why that could be happening?