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