xxxxxxxxxx
375
class Tetris{
constructor(cols, rows){
this.cols = cols;
this.rows = rows;
this.pixel = 20;
this.grid = []; // initialize board
this.move = true;
this.rotate = true;
this.fallingPieceRot = 0;
this.rotCount = 0;
this.test = [[true, true]];
this.oPiece = [[true, true], [true, true]];
this.iPiece = [[false, true, false, false], [false, true, false, false], [false, true, false, false], [false, true, false, false]];
this.sPiece = [[false, true, false], [true, true, false], [true, false, false]];
this.zPiece = [[true, false, false], [true, true, false], [false, true, false]];
this.lPiece = [[false, true, false], [false, true, false], [true, true, false]];
this.jPiece = [[true, true, false], [false, true, false], [false, true, false]];
this.tPiece = [[false, true, false], [true, true, false], [false, true, false]];
// Pick a random tetris piece, assign color, position on board at the top
this.tetrisPieces = [this.oPiece, this.iPiece, this.sPiece, this.zPiece, this.lPiece, this.jPiece, this.tPiece];
this.tetrisPieceColors = [ "red", "yellow", "magenta", "pink", "cyan", "green", "orange" ]
let pick = Math.floor(random(0, 7));
this.fallingPiece = this.tetrisPieces[pick];
this.fallingPieceRow = 0;
this.fallingPieceCol = (width/2)/this.pixel;
this.fallingPieceRowLength = this.fallingPiece.length;
this.fallingPieceColLength = this.fallingPiece[0].length;
this.fallingPieceColor = this.tetrisPieceColors[pick];
}
make2DArray(cols, rows){
let Arr = [];
for(let i=0; i<cols; i++){
Arr[i] = [];
for(let j=0; j<rows; j++){
Arr[i][j] = [];
}
}
return Arr;
}
init(){
this.grid = this.make2DArray(this.cols, this.rows);
for (let i=0; i<this.cols; i++){
for (let j=0; j<this.rows; j++){ // top left corner
this.grid[i][j] = "blue";
// if (i == 0 && j == 0){
// this.grid[i][j] = "red";
// } else if (i == 3 && j == 0) {
// this.grid[i][j] = "red";
// } else if (i == 7 && j == 12) {
// this.grid[i][j] = "red";
// } else if (i == 0 && j == this.rows-1) { // bottom left corner
// this.grid[i][j] = "white";
// } else if (i == this.cols-1 && j == 0) { // top right corner
// this.grid[i][j] = "green";
// } else if (i != 18 && j == this.rows-2) { // bottom right corner
// this.grid[i][j] = "gray";
// } else if (i != 18 && j == this.rows-1) { // bottom right corner
// this.grid[i][j] = "gray";
// } else { // the rest
// this.grid[i][j] = "blue";
// }
}
}
}
drawBoard(){
for (let i=0; i<this.cols; i++){
for (let j=0; j<this.rows; j++){
this.drawCell(this.grid[i][j]);
rect(i*this.pixel, j*this.pixel, this.pixel, this.pixel);
}
}
}
drawCell(color){
let c = ["blue", "red", "green", "white", "gray", "yellow", "magenta", "pink", "cyan", "orange" ];
if(color == c[0]){
fill(0, 0, 255);
} else if (color == c[1]){
fill(255, 0, 0);
} else if (color == c[2]){
fill(0, 255, 0);
} else if (color == c[3]){
fill(255, 255, 255);
} else if (color == c[4]){
fill(220);
} else if (color == c[5]){
fill(255, 255, 0);
} else if (color == c[6]){
fill(255, 0, 255);
} else if (color == c[7]){
fill(255, 192, 203);
} else if (color == c[8]){
fill(0, 255, 255);
} else if (color == c[9]){
fill(255, 165, 0);
}
}
newFallingPiece(){
this.oPiece = [[true, true], [true, true]];
this.iPiece = [[false, true, false, false], [false, true, false, false], [false, true, false, false], [false, true, false, false]];
this.sPiece = [[false, true, false], [true, true, false], [true, false, false]];
this.zPiece = [[true, false, false], [true, true, false], [false, true, false]];
this.lPiece = [[false, true, false], [false, true, false], [true, true, false]];
this.jPiece = [[true, true, false], [false, true, false], [false, true, false]];
this.tPiece = [[false, true, false], [true, true, false], [false, true, false]];
this.tetrisPieces = [this.oPiece, this.iPiece, this.sPiece, this.zPiece, this.lPiece, this.jPiece, this.tPiece];
let pick = Math.floor(random(0, 7));
this.fallingPiece = this.tetrisPieces[pick];
this.fallingPieceColor = this.tetrisPieceColors[pick];
this.fallingPieceRow = 0;
this.fallingPieceCol = width/2/this.pixel;
this.fallingPieceRot = 0;
this.rotCount = 0;
}
drawFallingPiece(){
// draw when the value is True
for (let i=0; i<this.fallingPiece.length; i++){
for (let j=0; j<this.fallingPiece[0].length; j++){
if(this.fallingPiece[i][j] == true){
this.drawCell(this.fallingPieceColor);
rect(i*this.pixel+this.fallingPieceCol*this.pixel,
j*this.pixel+this.fallingPieceRow*this.pixel,
this.pixel, this.pixel);
}
}
}
}
rotateFallingPiece(){
let oldFallingPiece = this.make2DArray(this.fallingPiece.length, this.fallingPiece[0].length);
for (let i=0; i<this.fallingPiece.length; i++){
for (let j=0; j<this.fallingPiece[0].length; j++){ // Add the true/false location of the old piece into the new piece
oldFallingPiece[i][j] = this.fallingPiece[i][j];
}
}
let oldFallingPieceRot = this.fallingPieceRot;
this.rotCount += 1;
this.fallingPieceRot = this.rotCount % 4;
let newFallingPiece = this.make2DArray(this.fallingPiece.length, this.fallingPiece[0].length); // Create a new empty 2D Array list for the rotated falling piece
for (let i=0; i<this.fallingPiece.length; i++){
for (let j=0; j<this.fallingPiece[0].length; j++){ // Add the true/false location of the old piece into the new piece
newFallingPiece[this.fallingPiece.length-1-j][i] = this.fallingPiece[i][j];
}
}
for (let i=0; i<this.fallingPiece.length; i++){
for (let j=0; j<this.fallingPiece[0].length; j++){
this.fallingPiece[i][j] = newFallingPiece[i][j];
}
}
for (let i=0; i<this.fallingPiece.length; i++){
for (let j=0; j<this.fallingPiece[0].length; j++){
if (this.fallingPieceCol > 0 && this.fallingPieceCol+this.fallingPiece.length-1 < this.cols-1) {
if(this.grid[this.fallingPieceCol+i][this.fallingPieceRow+j] != "blue") {
this.rotate = false;
}
}
}
}
if(this.fallingPieceCol < 0 || this.fallingPieceCol+this.fallingPiece.length-1 > this.cols-1){
this.rotate = false;
}
if (this.rotate == false){
for (let i=0; i<this.fallingPiece.length; i++){
for (let j=0; j<this.fallingPiece[0].length; j++){
this.fallingPiece[i][j] = oldFallingPiece[i][j]; // Add new data into this.fallingPiece from the newFallingPiece variable
}
}
this.rotCount -= 1;
this.fallingPieceRot = this.rotCount % 4;
this.rotate = true;
}
//console.log("Rot = "+this.fallingPieceRot, "fallingPieceCol = "+this.fallingPieceCol, "rotCount = "+this.rotCount, "this.cols = "+this.cols);
}
moveFallingPiece(x, y){
let oldFallingPieceCol = this.fallingPieceCol;
let oldFallingPieceRow = this.fallingPieceRow;
this.fallingPieceCol += x;
this.fallingPieceRow += y;
if (this.fallingPiece.length == 2) {
if (this.fallingPieceCol < 0) {
this.fallingPieceCol = oldFallingPieceCol;
this.fallingPieceRow = oldFallingPieceRow;
} else if (this.fallingPieceCol+this.fallingPiece.length-1 > this.cols-1){
this.fallingPieceCol = oldFallingPieceCol;
this.fallingPieceRow = oldFallingPieceRow;
}
}
// Cases for all pieces but iPiece and oPiece
else if (this.fallingPiece.length == 3){
// Left Side Case
if (this.fallingPieceCol < 0 && this.fallingPieceRot != 1) {
this.fallingPieceCol = oldFallingPieceCol;
this.fallingPieceRow = oldFallingPieceRow;
} else if (this.fallingPieceCol < -1 && this.fallingPieceRot == 1){
this.fallingPieceCol = oldFallingPieceCol;
this.fallingPieceRow = oldFallingPieceRow;
// Right Side Case
} else if (this.fallingPieceCol+this.fallingPiece.length-1 > this.cols-1 && this.fallingPieceRot != 3){
this.fallingPieceCol = oldFallingPieceCol;
this.fallingPieceRow = oldFallingPieceRow;
} else if (this.fallingPieceCol+this.fallingPiece.length-1 > this.cols && this.fallingPieceRot == 3){
this.fallingPieceCol = oldFallingPieceCol;
this.fallingPieceRow = oldFallingPieceRow;
}
// Cases for iPiece
} else if (this.fallingPiece.length == 4){ // iPiece
// Left Side Case
if (this.fallingPieceCol < 0 && (this.fallingPieceRot == 0 || this.fallingPieceRot == 2)){
this.fallingPieceCol = oldFallingPieceCol;
this.fallingPieceRow = oldFallingPieceRow;
} else if (this.fallingPieceCol < -2 && this.fallingPieceRot == 1){
this.fallingPieceCol = oldFallingPieceCol;
this.fallingPieceRow = oldFallingPieceRow;
} else if (this.fallingPieceCol < -1 && this.fallingPieceRot == 3){
this.fallingPieceCol = oldFallingPieceCol;
this.fallingPieceRow = oldFallingPieceRow;
}
// Right Side Case
else if (this.fallingPieceCol+this.fallingPiece.length-1 > this.cols-1 && (this.fallingPieceRot == 0 || this.fallingPieceRot == 2)){
this.fallingPieceCol = oldFallingPieceCol;
this.fallingPieceRow = oldFallingPieceRow;
} else if (this.fallingPieceCol+this.fallingPiece.length-1 > this.cols && this.fallingPieceRot == 1){
this.fallingPieceCol = oldFallingPieceCol;
this.fallingPieceRow = oldFallingPieceRow;
} else if (this.fallingPieceCol+this.fallingPiece.length-1 > this.cols+1 && this.fallingPieceRot == 3){
this.fallingPieceCol = oldFallingPieceCol;
this.fallingPieceRow = oldFallingPieceRow;
}
}
for (let i=0; i<this.fallingPiece.length; i++) {
for (let j=0; j<this.fallingPiece[0].length; j++){
if (this.fallingPiece[i][j] == true){ //
// Case for stopping at the bottom
if (this.fallingPieceRow+this.fallingPiece[0].length-1 > this.rows-1 && this.grid[this.fallingPieceCol+i][this.fallingPieceRow+j] != "blue"){
this.fallingPieceCol = oldFallingPieceCol;
this.fallingPieceRow = oldFallingPieceRow;
// Don't move if the grid location already has something in it
} else if(this.grid[this.fallingPieceCol+i][this.fallingPieceRow+j] != "blue") {
this.fallingPieceCol = oldFallingPieceCol;
this.fallingPieceRow = oldFallingPieceRow;
}
}
}
}
}
moveDown(){
let oldFallingPieceRow = this.fallingPieceRow;
this.fallingPieceRow += 1;
for (let i=0; i<this.fallingPiece.length; i++) {
for (let j=0; j<this.fallingPiece[0].length; j++){
if (this.fallingPiece[i][j] == true){
// Case for stopping at the bottom
if (this.fallingPieceRow+this.fallingPiece[0].length-1 > this.rows-1 && this.grid[this.fallingPieceCol+i][this.fallingPieceRow+j] != "blue"){
this.fallingPieceRow = oldFallingPieceRow;
this.placeFallingPiece();
// Don't move if the grid location already has something in it
} else if(this.grid[this.fallingPieceCol+i][this.fallingPieceRow+j] != "blue") {
this.fallingPieceRow = oldFallingPieceRow;
this.placeFallingPiece();
}
}
}
}
}
isFullRow(){
let count = [];
for (let i=0; i<this.rows; i++){
count[i] = 0;
}
for(let i=0; i<this.cols; i++){
for (let j=0; j<this.rows; j++){
if(this.grid[i][j] != "blue"){
count[j] += 1;
}
}
}
let fullRow = [];
for (let i=0; i<count.length; i++){
if(count[i] == 20){
fullRow.push(i)
}
}
return fullRow;
}
removeRow(){
let fullRow = this.isFullRow();
for (let i=0; i<this.cols; i++){
if (fullRow.length > 0){
for (let k=0; k<fullRow.length; k++){
this.grid[i].splice(fullRow[k], 1);
this.grid[i].unshift("blue");
}
}
}
}
placeFallingPiece(){
for (let i=0; i<this.fallingPiece.length; i++){
for (let j=0; j<this.fallingPiece[0].length; j++){
if (this.fallingPiece[i][j] == true){
this.grid[this.fallingPieceCol+i][this.fallingPieceRow+j] = this.fallingPieceColor;
}
}
}
this.removeRow();
this.newFallingPiece();
}
reDrawAll(){
this.drawBoard();
this.drawFallingPiece();
}
}