Adds checks for failure.

This commit is contained in:
2024-06-30 00:33:46 -07:00
parent b96684f2ea
commit ff7a473e2e

44
main.js
View File

@@ -13,50 +13,46 @@ const btnGo_onClick = () => {
const btnUp_onClick = () => {
const grid = globals.grid;
if (canSlideUp(grid)) {
slideUp(grid);
if (slideUp(grid)) {
addRandomTile();
redraw(grid)
} else {
alert('Unable to slide Up.');
}
// alert('Up');
checkForEnd(grid);
}
const btnDown_onClick = () => {
const grid = globals.grid;
if (canSlideDown(grid)) {
slideDown(grid);
if (slideDown(grid)) {
addRandomTile();
redraw(grid)
} else {
alert('Unable to slide Down.');
}
// alert('Down');
checkForEnd(grid);
}
const btnLeft_onClick = () => {
const grid = globals.grid;
if (canSlideLeft(grid)) {
slideLeft(grid);
if (slideLeft(grid)) {
addRandomTile();
redraw(grid)
} else {
alert('Unable to slide Left.');
}
// alert('Left');
checkForEnd(grid);
}
const btnRight_onClick = () => {
const grid = globals.grid;
if (canSlideRight(grid)) {
slideRight(grid);
if (slideRight(grid)) {
addRandomTile();
redraw(grid)
} else {
alert('Unable to slide Right.');
}
// alert('Right');
checkForEnd(grid);
}
const drawGrid = (grid, element) => {
@@ -139,6 +135,7 @@ const redraw = (grid) => {
}
const slideUp = (grid) => {
let didIDoIt = false;
for (let columnIndex = 0; columnIndex < grid.numColumns; columnIndex++) {
lastIndex = -1;
for (let rowIndex = 0; rowIndex < grid.numRows; rowIndex++) {
@@ -147,13 +144,16 @@ const slideUp = (grid) => {
lastIndex++;
if (lastIndex != rowIndex) {
grid.swapTiles(rowIndex, columnIndex, lastIndex, columnIndex);
didIDoIt = true;
}
}
}
}
return didIDoIt;
}
const slideLeft = (grid) => {
let didIDoIt = false;
for (let rowIndex = 0; rowIndex < grid.numRows; rowIndex++) {
lastIndex = -1;
for (let columnIndex = 0; columnIndex < grid.numColumns; columnIndex++) {
@@ -162,13 +162,16 @@ const slideLeft = (grid) => {
lastIndex++;
if (lastIndex != columnIndex) {
grid.swapTiles(rowIndex, columnIndex, rowIndex, lastIndex);
didIDoIt = true;
}
}
}
}
return didIDoIt;
}
const slideDown = (grid) => {
let didIDoIt = false;
for (let columnIndex = 0; columnIndex < grid.numColumns; columnIndex++) {
lastIndex = grid.numRows;
for (let rowIndex = grid.numRows - 1; rowIndex >= 0; rowIndex -= 1) {
@@ -177,13 +180,16 @@ const slideDown = (grid) => {
lastIndex--;
if (lastIndex != rowIndex) {
grid.swapTiles(rowIndex, columnIndex, lastIndex, columnIndex);
didIDoIt = true;
}
}
}
}
return didIDoIt;
}
const slideRight = (grid) => {
let didIDoIt = false;
for (let rowIndex = 0; rowIndex < grid.numRows; rowIndex++) {
lastIndex = grid.numColumns;
for (let columnIndex = grid.numColumns - 1; columnIndex >= 0; columnIndex -= 1) {
@@ -192,10 +198,12 @@ const slideRight = (grid) => {
lastIndex--;
if (lastIndex != columnIndex) {
grid.swapTiles(rowIndex, columnIndex, rowIndex, lastIndex);
didIDoIt = true;
}
}
}
}
return didIDoIt;
}
const canSlideUp = (grid) => {
@@ -267,3 +275,15 @@ const canSlideRight = (grid) => {
return canIDoIt;
}
const gameIsLost = (grid) => {
return !canSlideUp(grid)
&& !canSlideRight(grid)
&& !canSlideDown(grid)
&& !canSlideLeft(grid);
}
const checkForEnd = (grid) => {
if (gameIsLost(grid)) {
alert('A loser is you!');
}
}