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

110
main.js
View File

@@ -13,33 +13,49 @@ const btnGo_onClick = () => {
const btnUp_onClick = () => { const btnUp_onClick = () => {
const grid = globals.grid; const grid = globals.grid;
slideUp(grid); if (canSlideUp(grid)) {
addRandomTile(); slideUp(grid);
redraw(grid) addRandomTile();
redraw(grid)
} else {
alert('Unable to slide Up.');
}
// alert('Up'); // alert('Up');
} }
const btnDown_onClick = () => { const btnDown_onClick = () => {
const grid = globals.grid; const grid = globals.grid;
slideDown(grid); if (canSlideDown(grid)) {
addRandomTile(); slideDown(grid);
redraw(grid) addRandomTile();
redraw(grid)
} else {
alert('Unable to slide Down.');
}
// alert('Down'); // alert('Down');
} }
const btnLeft_onClick = () => { const btnLeft_onClick = () => {
const grid = globals.grid; const grid = globals.grid;
slideLeft(grid); if (canSlideLeft(grid)) {
addRandomTile(); slideLeft(grid);
redraw(grid) addRandomTile();
redraw(grid)
} else {
alert('Unable to slide Left.');
}
// alert('Left'); // alert('Left');
} }
const btnRight_onClick = () => { const btnRight_onClick = () => {
const grid = globals.grid; const grid = globals.grid;
slideRight(grid); if (canSlideRight(grid)) {
addRandomTile(); slideRight(grid);
redraw(grid) addRandomTile();
redraw(grid)
} else {
alert('Unable to slide Right.');
}
// alert('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;
}