Last active
March 29, 2025 19:59
-
-
Save primaryobjects/5c21454bb8170d431f5ce077ca49c0d3 to your computer and use it in GitHub Desktop.
Two-Dimensional Array Traversal Tetris Block Fall Drop https://codesignal.com/blog/interview-prep/example-codesignal-questions https://jsfiddle.net/45th1rqo/ https://jsfiddle.net/kvboemqL/1/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Check if the figure can be placed at a specific row and column | |
const canPlaceFigure = (figure, field, startRow, startCol) => { | |
for (let i = 0; i < figure.length; i++) { | |
for (let j = 0; j < figure[0].length; j++) { | |
if (figure[i][j] === 1 && field[startRow + i][startCol + j] === 1) { | |
return false; // Conflict detected | |
} | |
} | |
} | |
return true; // Placement is possible | |
}; | |
// Merge the figure into the field at a specific position | |
const mergeFigure = (figure, field, startRow, startCol) => { | |
const newField = JSON.parse(JSON.stringify(field)); // Create a deep copy | |
for (let i = 0; i < figure.length; i++) { | |
for (let j = 0; j < figure[0].length; j++) { | |
if (figure[i][j] === 1) { | |
newField[startRow + i][startCol + j] = 1; | |
} | |
} | |
} | |
return newField; | |
}; | |
// Check if any row in the field is fully occupied | |
const hasFullRow = (field) => { | |
for (let row = 0; row < field.length; row++) { | |
if (field[row].every(cell => cell === 1)) { | |
return true; // Full row found | |
} | |
} | |
return false; // No full rows | |
}; | |
// Calculate the landing row for the figure in a given column | |
const dropCol = (figure, field, column) => { | |
const maxRow = field.length - figure.length; | |
for (let row = 0; row <= maxRow; row++) { | |
if (!canPlaceFigure(figure, field, row, column)) { | |
return row - 1; // Figure landed above the conflict | |
} | |
} | |
return field.length - figure.length; // Figure landed at the bottom | |
}; | |
// Find the best column to drop the figure | |
const drop = (figure, field) => { | |
const fieldWidth = field[0].length; | |
const figureWidth = figure[0].length; | |
const maxDropCol = fieldWidth - figureWidth + 1; | |
for (let col = 0; col < maxDropCol; col++) { | |
const landingRow = dropCol(figure, field, col); | |
if (landingRow !== -1) { | |
const newField = mergeFigure(figure, field, landingRow, col); | |
if (hasFullRow(newField)) { | |
return col; // Found a valid column | |
} | |
} | |
} | |
return -1; // No valid column found | |
}; | |
field = [[0, 0, 0], | |
[0, 0, 0], | |
[0, 0, 0], | |
[1, 0, 0], | |
[1, 1, 0]]; | |
figure = [[0, 0, 1], | |
[0, 1, 1], | |
[0, 0, 1]]; | |
console.log(drop(figure, field)); | |
field = [[0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0], | |
[1, 1, 0, 1, 0], | |
[1, 0, 1, 0, 1]]; | |
figure = [[1, 1, 1], | |
[1, 0, 1], | |
[1, 0, 1]]; | |
console.log(drop(figure, field)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment