2048 game implementation in ClojureScript using re-frame, React, and tailwindcss
This project uses Shadow CLJS for development and build setup.
The core of this project is in board.cljs
. The board is represented as a vector of vectors that represents rows and colums. Each cell is a vector that has a value and a state.
(def board
[[[2 :random] [0] [0] [0]]
[[0] [0] [0] [0]]
[[4] [0] [0] [0]]
[[8 :merged] [0] [0] [0]]])
There are 4 movements possible in the game, move up ⬆️, move down ⬇️, move left ⬅️, and move right ➡️.
Instead of implementing separate logic for each movement, all movements are based on the logic to move left, and move left is simply merges each row in the board towards left.
- Reverse the board
- Move left
- Reserve it back
➡️ =️
- Rotate the board to left
- Move left
- Rotate it back
⬆️ = ⬅️
- Rotate the board to right
- Move left
- Rotate it back
⬇️ = ➡️
(merg-left [[4 :random] [4 :merged] [0] [0]]) ;; [[8] [0] [0] [0]]
(defn move-left
"Move tiles to left and combine equal tiles"
[board]
(mapv b/merge-left board))
(defn move-right
"Move tiles to right and combine equal tiles"
[board]
(-> board
(b/reverse-board)
(move-left)
(b/reverse-board)))
(defn move-up
"Move tiles to up and combine equal tiles"
[board]
(-> board
(b/rotate-left)
(move-left)
(b/rotate-right)))
(defn move-down
"Move tiles to down and combine equal tiles"
[board]
(-> board
(b/rotate-right)
(move-left)
(b/rotate-left)))
Clone the repository using git
git clone git@github.com:WarFox/2048-cljs
npm run watch
This will start
shadow-cljs
and tests on watch modebb dev
This will run
npm run watch
. You need to setup babashka for this to work.