Giter VIP home page Giter VIP logo

wechat-weapp-2048's Introduction

2048

效果图

算法

该程序主要难度在用户滑动屏幕时值相同的cell合并 将空格标为0(我代码中是用的""表示空格),假设棋盘如下:

  • 0 2 0 2
  • 0 0 0 0
  • 0 0 0 2
  • 0 0 0 0

步骤

  • 通过touch相关的事件函数确定用户滑动方向
  • 将棋盘的数字生成4*4的二维数组list
  • 根据用户滑动方向生成四个小数组,比如用户将上面的棋盘向右滑动,则四个数组为:

item[0] = [2, 0, 2, 0]; item[1] = [0, 0, 0, 0]; item[2] = [2, 0, 0, 0]; // 注意是2000而不是0002,因为是向右滑动要从右边开始 item[3] = [0, 0, 0, 0];

  • 接下来就是滑动时合并,拿item[0]举例,如果是2020,向右滑动我们应该成为0004
  • 将item[0]的所有0移到末尾变为2200,遍历item将相同的下标值相加,后面的数置为0
  • 2020 ---> 2200 ---> 4200 ----> 4000
  • 如法炮制就可以实现滑动时合并

wechat-weapp-2048's People

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.