Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to write a Tetris game #207

Open
plh97 opened this issue Feb 22, 2024 · 0 comments
Open

How to write a Tetris game #207

plh97 opened this issue Feb 22, 2024 · 0 comments
Assignees
Labels
javaScript 关于js的一些事 react react项目 学习 如果不学习,那今天和昨天又有什么区别

Comments

@plh97
Copy link
Owner

plh97 commented Feb 22, 2024

Online Tetris link

Tetris

how to describe?

2D Array like this

[
    0b0000000000, 
    0b0000000000, 
    0b0000000000, 
    0b0000000000, 
    0b0000000000,
    0b0000000000, 
    0b0000000000, 
    0b0000000000, 
    0b0000000000, 
    0b0000000000,
    0b0000000000, 
    0b0000000000, 
    0b0000000000, 
    0b0000000000, 
    0b0000000000,
    0b0000000000, 
    0b0000000000, 
    0b0000000000, 
    0b0000000000, 
    0b0000000000,
]

Move left

0b0000000000 >> 1

Move right

0b0000000000 << 1

Move down

export function moveDown(blocks: number[]) {
    const newArr = [...blocks];
    newArr.unshift(0b0000000000);
    newArr.pop();
    return newArr;
}

Remove

blocks = EMPTY_BLOCK

Rotate

export function rotateMatrix(matrix: number[][]) {
  const len = matrix.length;
  const temp: number[][] = [];
  for (var i = 0; i < len; i++) {
      for (var j = 0; j < len; j++) {
          var k = len - 1 - j;
          if (!temp[k]) {
              temp[k] = [];
          }
          temp[k][i] = matrix[i][j];
      }
  }
  return temp;
}

export function rotateBlock(blocks: number[]) {
  // 1. from '0000110000' to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  const matrix = blocks.map((block) => binaryFmt(block).map(Number));
  let temp: number[][] = [];
  // 2. get minimal square matrix
  const { top, left, bottom, right } = getBoundary(matrix);
  // 3. rotate this matrix
  const len = Math.max(bottom - top, right - left) + 1;
  const newMatrix = matrix
      .filter((_, i) => {
          return i >= top && i < len + top;
      })
      .map((row) => {
          return row.filter((_, i) => {
              return i >= left && i < len + left;
          });
      });
  temp = rotateMatrix(newMatrix);
  const x = top;
  const y = left;
  for (let i = 0; i < len; i++) {
      for (let j = 0; j < len; j++) {
          matrix[i + x][j + y] = temp[i][j];
      }
  }
  // 4. assign back
  return matrix.map((row) => string2Binary(row.join("")));
}

Add block

function addBlock(blocks: number[]) {
  const newArr = [...EMPTY_BLOCK];
  newArr[0] = 0b0000110000;
  newArr[1] = 0b0000110000;
  return newArr;
}

Reference

俄罗斯方块实现思路【渡一教育】

@plh97 plh97 self-assigned this Feb 22, 2024
@plh97 plh97 added the 学习 如果不学习,那今天和昨天又有什么区别 label Feb 22, 2024
@plh97 plh97 changed the title How to dev a Tetris How to write a Tetris game Feb 23, 2024
@plh97 plh97 added javaScript 关于js的一些事 react react项目 labels Feb 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
javaScript 关于js的一些事 react react项目 学习 如果不学习,那今天和昨天又有什么区别
Projects
None yet
Development

When branches are created from issues, their pull requests are automatically linked.

1 participant