Skip to content

Commit

Permalink
Merge pull request #11 from WarFox/equality
Browse files Browse the repository at this point in the history
fix: 🐛 add a function for checking equality of two boards
  • Loading branch information
WarFox committed Jun 3, 2024
2 parents 9cb09bf + 570f0d6 commit e36cf4b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/cljs_2048/board.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,13 @@
"Reverse the board"
[board]
(mapv #(vec (rseq %)) board))

(defn equal?
"Check if two boards are equal or not.
Two boards are equals if the values of all tiles are equal,
we do not care about the state of the tile."
[board1 board2]
(every? true? (map =
(remove keyword? (flatten board1))
(remove keyword? (flatten board2)))))

2 changes: 1 addition & 1 deletion src/cljs_2048/game.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@
"Returns new board after moving in the direction. Adds random tile if board has changed"
[board direction]
(let [new-board ((direction movements) board)]
(if (= new-board board)
(if (b/equal? new-board board)
new-board
(b/random-tile new-board))))
34 changes: 34 additions & 0 deletions test/cljs_2048/board_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,37 @@
[8 7 6 5]
[12 11 10 9]
[16 15 14 13]])))

(deftest equal?-test
(is (true? (sut/equal?
[[[0] [0] [0] [0]]
[[0] [0] [0] [0]]
[[0] [0] [2 :random] [0]]
[[4 :merged] [0] [4] [0]]]

[[[0] [0] [0] [0]]
[[0] [0] [0] [0]]
[[0] [0] [2] [0]]
[[4] [0] [4] [0]]])))

(is (true? (sut/equal?
[[[0] [0] [0] [0]]
[[0] [0] [0] [0]]
[[0] [0] [2 :random] [0]]
[[4 :merged] [0] [4] [0]]]

[[[0] [0] [0] [0]]
[[0] [0] [0] [0]]
[[0] [0] [2] [0]]
[[4] [0] [4] [0]]])))

(is (false? (sut/equal?
[[[0] [0] [0] [0]]
[[0] [0] [0] [0]]
[[0] [0] [2 :random] [0]]
[[4 :merged] [0] [4] [0]]]

[[[0] [0] [0] [0]]
[[0] [0] [0] [0]]
[[2] [0] [0] [0]]
[[4] [0] [4] [0]]]))))

0 comments on commit e36cf4b

Please sign in to comment.