Open plh97 opened 8 months ago
Tetris
2D Array like this
[ 0b0000000000, 0b0000000000, 0b0000000000, 0b0000000000, 0b0000000000, 0b0000000000, 0b0000000000, 0b0000000000, 0b0000000000, 0b0000000000, 0b0000000000, 0b0000000000, 0b0000000000, 0b0000000000, 0b0000000000, 0b0000000000, 0b0000000000, 0b0000000000, 0b0000000000, 0b0000000000, ]
0b0000000000 >> 1
0b0000000000 << 1
export function moveDown(blocks: number[]) { const newArr = [...blocks]; newArr.unshift(0b0000000000); newArr.pop(); return newArr; }
blocks = EMPTY_BLOCK
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(""))); }
function addBlock(blocks: number[]) { const newArr = [...EMPTY_BLOCK]; newArr[0] = 0b0000110000; newArr[1] = 0b0000110000; return newArr; }
俄罗斯方块实现思路【渡一教育】
Online Tetris link
Tetris
how to describe?
2D Array like this
Move left
0b0000000000 >> 1
Move right
0b0000000000 << 1
Move down
Remove
blocks = EMPTY_BLOCK
Rotate
Add block
Reference
俄罗斯方块实现思路【渡一教育】