Giter Site home page Giter Site logo

tetris's Introduction

tetris

俄罗斯方块大致思路

如何表达方块

俄罗斯方块可以表示为一个二维数组,0代表为没有,1代表有方块,所以 000000 代报这一行没有方块,111111代表这一行全是方块需要清除。

如何移动方块

上面表达方块的方式可以联想到二进制,于是想到了 >><<,可以利用这两个运算符轻松的解决方块左右移动的问题

const a = 0b000001;
const b = a << 1; // 2
// 而2的二进制可以表示为 000010 所以可以用来表示方块的右移动,同理
const c = a >> 1; // 0
// 0的二进制可以表达为为 000000 所以可以用来表示方块的左移动

判断方块是否可以移动

解决了左右移动问题,接下来就是判断方块是否可以移动,这里可以用到&运算符,因为方块是二进制表示,所以可以用这个运算符来判断方块是否可以移动,比如

const a = 0b000001;
const b = 0b000010;

a & 0b00001; // 1
b & 0b00001; // 0

// 结果0代表可以移动,结果不为0代表不可移动 所以判断a行不可以右移动,b行可以右移动。
// 同理判断是否可以左移动可以用 某一行的二进制 & 0b100000即可判断是否可以左移动
// 也可以简单粗暴的判断第一位或者最后一位是否为1来判断是否可以移动

tetris's People

Contributors

nierifeng avatar

Watchers

 avatar

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.