React Tetris
By: Jack Overby
Tags: React, JavaScript, Object Oriented, Tetris, Components

Last week, I did a very simple implementation of Tetris that caused Xs and Os to appear on the screen, with basic logic to handle rotations, checking the validity of moves (e.g. whether a piece can go down or be moved left or right) completion of rows, updating score and determining when a game is over. Now, we’re going to give this program a makeover. The underlying logic will be the same, but the presentation will be in… React!

Creating a React App

There are several ways to launch a React App, but the easiest path is to run the following lines of code:

npx create-react-app my-app

cd my-app

npm start

Per React’s documentation, you need to have both Node Package Manager (npm) version ≥5.6 and Node (node) version ≥ 8.10. React contains a large number of files, so the download can take 10 or more minutes. Be patient… it’s worth it! And you can delete the extraneous files later.

Recall that our program last week contained two classes (Piece and Tetris), as well as a dictionary containing the seven different pieces that can appear (and be rotated) during a game of Tetris. We’re going to upgrade the simple vanilla JavaScript classes into React class components, and we’re going to flesh out the pieceObj object to contain some visual elements, such as color.

pieceObj

Each piece will have two basic visual attributes:

Color (ranging from red to purple)

Shape (represented as an array of arrays- e.g. 2x2 square, 4x1 rod)

const pieceObj = {

1: {'color': 'orange',

'shape': [[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,1,1,1]]

},

2: {'color': 'dark-blue',

'shape': [[0,0,0],[1,1,1],[1,0,0]]

},

3: {'color': 'purple',

'shape': [[0,0,0],[1,1,1],[0,0,1]]

},

4: {'color': 'yellow',

'shape': [[0,0,0],[1,1,1],[0,1,0]]

},

5: {'color': 'light-blue',

'shape': [[0,0,0],[1,1,0],[0,1,1]]

},

6: {'color': 'green',

'shape': [[0,0,0],[0,1,1],[1,1,0]]

},

7: {'color': 'red',

'shape': [[1,1],[1,1]]

}

};

Piece

Next, we move to our Piece class, which will be initialized with a randomly selected piece object from pieceObj and then given several additional functions for rotating, calculating borders and rendering:

import pieceDict from './PieceDict';

import React from 'react'; class Piece extends React.Component {

constructor(n=Math.ceil(7 * Math.random())) {

this.piece = pieceDict[n];

} rotate() {

const copy = JSON.parse(JSON.stringify(this.piece));

const n = this.piece.length;

for (let i=0;i<n;i++) {

for (let j=0;j<n;j++) {

copy[i][n-1-j] = this.piece[j][i];

}

}

this.piece = copy;

} renderPiece() {

console.log(this.piece.map(row=>row.map(col=>col?"X":" ").join('')).join('

'))

} leftmost() {

const n = this.piece.length;

return [...Array(n).keys()].map(i=> {

for (let j=0;j<n;j++) {

if (this.piece[i][j]) {

return j;

}

}

return null;

})

} rightmost() {

const n = this.piece.length;

return [...Array(n).keys()].map(i=> {

for (let j=n-1;j>=0;j--) {

if (this.piece[i][j]) {

return j;

}

}

return null;

})

} bottommost() {

const n = this.piece.length;

return [...Array(n).keys()].map(i=> {

for (let j=n-1;j>=0;j--) {

if (this.piece[j][i]) {

return j;

}

}

return null;

})

} } export default Piece;

Tetris