Code Monkey home page Code Monkey logo

2048-cljs's Introduction

2048-cljs

2048 game implementation in ClojureScript using re-frame, React, and tailwindcss

This project uses Shadow CLJS for development and build setup.

Core

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]]])

Movements

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.

Move Right

  • Reverse the board
  • Move left
  • Reserve it back

➡️ =️ ⤴️ ◀️ ⤵️

Move up

  • Rotate the board to left
  • Move left
  • Rotate it back

⬆️ = ⬅️ ◀️ ➡️

Move down

  • 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)))

Development

Clone the repository using git

git clone [email protected]:WarFox/2048-cljs

Commands

  1. npm run watch

    This will start shadow-cljs and tests on watch mode

  2. bb dev

    This will run npm run watch. You need to setup babashka for this to work.

References

2048-cljs's People

Contributors

warfox avatar

Stargazers

Kevin Nakamura avatar Roman avatar

Watchers

 avatar  avatar

2048-cljs's Issues

Add animation

There is no fun without animation

  • Animate tile change
  • Animate score change
  • Animate new tile

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.