diff --git a/src/cljs_2048/board.cljs b/src/cljs_2048/board.cljs index 388146a..73b958e 100644 --- a/src/cljs_2048/board.cljs +++ b/src/cljs_2048/board.cljs @@ -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))))) + diff --git a/src/cljs_2048/game.cljs b/src/cljs_2048/game.cljs index f271baf..026fb55 100644 --- a/src/cljs_2048/game.cljs +++ b/src/cljs_2048/game.cljs @@ -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)))) diff --git a/test/cljs_2048/board_test.cljs b/test/cljs_2048/board_test.cljs index e0c2778..524e15a 100644 --- a/test/cljs_2048/board_test.cljs +++ b/test/cljs_2048/board_test.cljs @@ -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]]]))))