Adds check to make sure we do not add a new tile if you ca not slide.

This commit is contained in:
2024-06-30 00:24:37 -07:00
parent 3294002c80
commit b96684f2ea

86
main.js
View File

@@ -13,33 +13,49 @@ const btnGo_onClick = () => {
const btnUp_onClick = () => {
const grid = globals.grid;
if (canSlideUp(grid)) {
slideUp(grid);
addRandomTile();
redraw(grid)
} else {
alert('Unable to slide Up.');
}
// alert('Up');
}
const btnDown_onClick = () => {
const grid = globals.grid;
if (canSlideDown(grid)) {
slideDown(grid);
addRandomTile();
redraw(grid)
} else {
alert('Unable to slide Down.');
}
// alert('Down');
}
const btnLeft_onClick = () => {
const grid = globals.grid;
if (canSlideLeft(grid)) {
slideLeft(grid);
addRandomTile();
redraw(grid)
} else {
alert('Unable to slide Left.');
}
// alert('Left');
}
const btnRight_onClick = () => {
const grid = globals.grid;
if (canSlideRight(grid)) {
slideRight(grid);
addRandomTile();
redraw(grid)
} else {
alert('Unable to slide Right.');
}
// alert('Right');
}
@@ -181,3 +197,73 @@ const slideRight = (grid) => {
}
}
}
const canSlideUp = (grid) => {
canIDoIt = false;
for (let columnIndex = 0; columnIndex < grid.numColumns; columnIndex++) {
lastIndex = -1;
for (let rowIndex = 0; rowIndex < grid.numRows; rowIndex++) {
const tile = grid.getTile(rowIndex, columnIndex);
if (tile.visible) {
lastIndex++;
if (lastIndex != rowIndex) {
canIDoIt = true;
}
}
}
}
return canIDoIt;
}
const canSlideLeft = (grid) => {
let canIDoIt = false;
for (let rowIndex = 0; rowIndex < grid.numRows; rowIndex++) {
lastIndex = -1;
for (let columnIndex = 0; columnIndex < grid.numColumns; columnIndex++) {
const tile = grid.getTile(rowIndex, columnIndex);
if (tile.visible) {
lastIndex++;
if (lastIndex != columnIndex) {
canIDoIt = true;
}
}
}
}
return canIDoIt;
}
const canSlideDown = (grid) => {
let canIDoIt = false;
for (let columnIndex = 0; columnIndex < grid.numColumns; columnIndex++) {
lastIndex = grid.numRows;
for (let rowIndex = grid.numRows - 1; rowIndex >= 0; rowIndex -= 1) {
const tile = grid.getTile(rowIndex, columnIndex);
if (tile.visible) {
lastIndex--;
if (lastIndex != rowIndex) {
canIDoIt = true;
}
}
}
}
return canIDoIt;
}
const canSlideRight = (grid) => {
let canIDoIt = false;
for (let rowIndex = 0; rowIndex < grid.numRows; rowIndex++) {
lastIndex = grid.numColumns;
for (let columnIndex = grid.numColumns - 1; columnIndex >= 0; columnIndex -= 1) {
const tile = grid.getTile(rowIndex, columnIndex);
if (tile.visible) {
lastIndex--;
if (lastIndex != columnIndex) {
canIDoIt = true;
}
}
}
}
return canIDoIt;
}