Skip to content

Instantly share code, notes, and snippets.

@Diegow3b
Created November 9, 2020 02:23
Show Gist options
  • Save Diegow3b/fdf1ad965711e3badbba664c19deeab1 to your computer and use it in GitHub Desktop.
Save Diegow3b/fdf1ad965711e3badbba664c19deeab1 to your computer and use it in GitHub Desktop.
/*
Tetris Move
Have the function TetrisMove(strArr) take strArr parameter being passed
which will be an array containing one letter followed by 12 numbers
representing a Tetris piece followed by the fill levels for the 12 columns
of the board.
Calculate the greatest number of horizontal lines that can be completed
when the piece arrives at the bottom assuming it is dropped immediately
after being rotated and moved horizontally from the top.
Tricky combinations of vertical and horizontal movements are excluded.
The piece types are represented by capital letters.
Pieces: I, J, L, O, S, T, Z
For example,
with an input of ["L","3","4","4","5","6","2","0","6","5","3","6","6"],
the board will look something like this:
Your result should be 3 because the L piece can be rotated and dropped
in column 6-7 which will complete 3 full rows of blocks.
Hard challenges are worth 15 points and you are not timed for them.
Examples
Input: ["I", "2", "4", "3", "4", "5", "2", "0", "2", "2", "3", "3", "3"]
Output: 2
Input: ["O", "4", "3", "2", "3", "5", "1", "0", "1", "2", "4", "3", "4"]
Output: 0
*/
console.clear();
const pieceTypeL = (columns, base, occurrencesBase, basePosition) => {
let nodesMinHeight = Math.min(...columns.filter((n) => n !== base));
if (occurrencesBase === 1 && columns[basePosition - 1] === 2) {
return "TODO";
} else if (occurrencesBase === 1) {
return "TODO";
} else {
return 0;
}
};
const pieceTypeJ = (columns, base, occurrencesBase, basePosition) => {
if (
occurrencesBase === 2 &&
(columns[basePosition + 1] === base || columns[basePosition - 1] === base)
) {
return 1;
} else if (occurrencesBase === 1) {
let borderMinHeight =
columns[basePosition - 1] < columns[basePosition + 1]
? columns[basePosition - 1]
: columns[basePosition + 1];
let nodesMinHeight = Math.min(...columns.filter((n) => n !== base));
if (nodesMinHeight >= borderMinHeight) {
return 1;
} else {
return 0;
}
} else {
return 0;
}
};
const pieceTypeI = (columns, base, occurrencesBase) => {
if (occurrencesBase === 1) {
return Math.min(...columns.filter((n) => n !== base));
} else {
return 0;
}
};
const processPiece = (piece, columns, base, occurrencesBase, basePosition) => {
switch (piece) {
case "I":
return pieceTypeI(columns, base, occurrencesBase);
case "J":
return pieceTypeJ(columns, base, occurrencesBase, basePosition);
default:
return 0;
}
};
const TetrisMove = (strArr) => {
let { piece, columns } = ((arr) => {
return { piece: arr.shift(), columns: arr.map(Number) };
})(strArr);
let base = Math.min(...strArr);
let occurrencesBase = columns.filter((n) => n === base).length;
let basePosition = columns.indexOf(base);
let baseSize =
occurrencesBase >= 2 &&
(columns[basePosition - 1] === base || columns[basePosition + 1] === base);
console.log("piece", piece);
console.log("occurrencesBase", occurrencesBase);
console.log("baseSize", baseSize);
console.log("columns[basePosition-1]", columns[basePosition - 1]);
console.log("columns[basePosition+1]", columns[basePosition + 1]);
console.log("base", base);
if (occurrencesBase >= 1 && occurrencesBase < 3) {
return processPiece(piece, columns, base, occurrencesBase, basePosition);
} else {
return 0;
}
};
let inputL = ["L", "3", "4", "4", "5", "6", "2", "0", "6", "5", "3", "6", "6"];
let inputI = ["I", "2", "4", "3", "4", "5", "2", "0", "2", "2", "3", "3", "3"];
let inputO = ["O", "4", "3", "2", "3", "5", "1", "0", "1", "2", "4", "3", "4"];
let inputJ = ["J", "5", "6", "7", "8", "6", "7", "0", "5", "5", "8", "7", "9"];
console.log("Answer: ", TetrisMove(inputJ));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment