Cleans up main.js to remove unused code.

This commit is contained in:
2024-07-02 12:04:19 -07:00
parent 12557bfdbe
commit 0129e43b8e

339
main.js
View File

@@ -11,14 +11,14 @@ const globals = {
}; };
const btnStart_onClick = () => { const btnStart_onClick = () => {
// globals.grid = makeGrid(4, 4, {value: 0, visible: false}); globals.grid = makeGrid(4, 4, {value: 0, visible: false});
// addRandomTile(); addRandomTile();
globals.grid = makeDevGrid([ // can't slide down or left // globals.grid = makeDevGrid([ // can't slide down or left
[1,0,0,0], // [2,1,3,4],
[0,0,0,0], // [3,5,7,9],
[0,0,0,0], // [1,2,1,2],
[0,0,0,0], // [3,4,5,0],
]); // ]);
redraw(globals.grid); redraw(globals.grid);
} }
@@ -26,91 +26,26 @@ const btnRedraw_onClick = () => {
redraw(globals.grid); redraw(globals.grid);
} }
const doTheLoop = (advance, getVector, getFromIndex, getToIndex, testForDone, swap, merge, length) => { const getSettingsForDirection = (direction) => {
let didIDoIt = false;
for (let index = 0; index < length; index++) {
const vector = getVector(index);
const swap2 = swap.bind(this, vector, index);
const fromIndex = getFromIndex(vector.length);
const toIndex = getToIndex(vector.length);
const didChangeThisPass = slide(fromIndex, toIndex, vector, advance, swap2, merge, testForDone);
didIDoIt = didIDoIt || didChangeThisPass;
}
return didIDoIt;
}
const slideVertical = (grid, direction) => {
const advanceForward = i => i - 1; const advanceForward = i => i - 1;
const advanceBackward = i => i + 1; const advanceBackward = i => i + 1;
const testForDoneForward = fromIndex => fromIndex < 0; const testForDoneForward = fromIndex => fromIndex < 0;
const testForDoneBackward = (fromIndex, length) => fromIndex >= length; const testForDoneBackward = (fromIndex, length) => fromIndex >= length;
const getFromIndexForward = (length) => length - 2; const getInitialFromIndexForward = (length) => length - 2;
const getFromIndexBackward = () => 1; const getInitialFromIndexBackward = () => 1;
const getToIndexForward = (length) => length - 1; const getInitialToIndexForward = (length) => length - 1;
const getToIndexBackward = () => 0; const getInitialToIndexBackward = () => 0;
const getColumn = (index) => grid.getColumn(index); const getColumn = (grid, index) => grid.getColumn(index);
const getRow = (index) => grid.getRow(index); const getRow = (grid, index) => grid.getRow(index);
const swapInRow = (vector, rowIndex, from, to) => { const getNumRows = (grid) => grid.numRows;
const getNumColumns = (grid) => grid.numColumns;
const swapInRow = (grid, vector, rowIndex, from, to) => {
grid.swapTiles(rowIndex, from, rowIndex, to); grid.swapTiles(rowIndex, from, rowIndex, to);
const tempTile = vector[to]; const tempTile = vector[to];
vector[to] = vector[from]; vector[to] = vector[from];
vector[from] = tempTile; vector[from] = tempTile;
}; };
const swapInColumn = (vector, columnIndex, from, to) => { const swapInColumn = (grid, vector, columnIndex, from, to) => {
grid.swapTiles(from, columnIndex, to, columnIndex);
const tempTile = vector[to];
vector[to] = vector[from];
vector[from] = tempTile;
}
const merge = (from, to) => {
to.value++;
from.value = 0;
from.visible = false;
};
switch(direction) {
case Direction.Up: {
const advance = advanceBackward;
const getVector = getColumn;
const getFromIndex = getFromIndexBackward;
const getToIndex = getToIndexBackward;
const testForDone = testForDoneBackward;
const swap = swapInColumn;
const length = grid.numColumns;
return doTheLoop(advance, getVector, getFromIndex, getToIndex, testForDone, swap, merge, length);
}
case Direction.Down: {
const advance = advanceForward;
const getVector = getColumn;
const getFromIndex = getFromIndexForward;
const getToIndex = getToIndexForward;
const testForDone = testForDoneForward;
const swap = swapInColumn;
const length = grid.numColumns;
return doTheLoop(advance, getVector, getFromIndex, getToIndex, testForDone, swap, merge, length);
}
}
return false;
}
const slideHorizontal = (grid, direction) => {
const advanceForward = i => i - 1;
const advanceBackward = i => i + 1;
const testForDoneForward = fromIndex => fromIndex < 0;
const testForDoneBackward = (fromIndex, length) => fromIndex >= length;
const getFromIndexForward = (length) => length - 2;
const getFromIndexBackward = () => 1;
const getToIndexForward = (length) => length - 1;
const getToIndexBackward = () => 0;
const getColumn = (index) => grid.getColumn(index);
const getRow = (index) => grid.getRow(index);
const swapInRow = (vector, rowIndex, from, to) => {
grid.swapTiles(rowIndex, from, rowIndex, to);
const tempTile = vector[to];
vector[to] = vector[from];
vector[from] = tempTile;
};
const swapInColumn = (vector, columnIndex, from, to) => {
grid.swapTiles(from, columnIndex, to, columnIndex); grid.swapTiles(from, columnIndex, to, columnIndex);
const tempTile = vector[to]; const tempTile = vector[to];
vector[to] = vector[from]; vector[to] = vector[from];
@@ -121,45 +56,68 @@ const slideHorizontal = (grid, direction) => {
from.value = 0; from.value = 0;
from.visible = false; from.visible = false;
}; };
let settings = { merge };
switch (direction) { if (direction === Direction.Up || direction === Direction.Left) {
case Direction.Left: { settings = {
const advance = advanceBackward; ...settings,
const getVector = getRow; advance: advanceBackward,
const getFromIndex = getFromIndexBackward; testForDone: testForDoneBackward,
const getToIndex = getToIndexBackward; getInitialFromIndex: getInitialFromIndexBackward,
const testForDone = testForDoneBackward; getInitialToIndex: getInitialToIndexBackward
const swap = swapInRow; };
const length = grid.numRows; } else if (direction === Direction.Down || direction === Direction.Right) {
return doTheLoop(advance, getVector, getFromIndex, getToIndex, testForDone, swap, merge, length); settings = {
...settings,
advance: advanceForward,
testForDone: testForDoneForward,
getInitialFromIndex: getInitialFromIndexForward,
getInitialToIndex: getInitialToIndexForward
};
}
if (direction === Direction.Up || direction === Direction.Down) {
settings = {
...settings,
getVector: getColumn,
swap: swapInColumn,
getLength: getNumColumns
} }
case Direction.Right: { } else if (direction === Direction.Left || direction === Direction.Right) {
const advance = advanceForward; settings = {
const getVector = getRow; ...settings,
const getFromIndex = getFromIndexForward; getVector: getRow,
const getToIndex = getToIndexForward; swap: swapInRow,
const testForDone = testForDoneForward; getLength: getNumRows
const swap = swapInRow;
const length = grid.numRows;
return doTheLoop(advance, getVector, getFromIndex, getToIndex, testForDone, swap, merge, length);
} }
} }
return false;
return settings;
} }
const slideInDirection = (grid, direction) => { const slideInDirection = (grid, direction) => {
switch(direction) { const settings = getSettingsForDirection(direction);
case Direction.Up: const length = settings.getLength(grid);
return slideVertical(grid, direction); let didChange = false;
case Direction.Down:
return slideVertical(grid, direction); for (let index = 0; index < length; index++) {
case Direction.Left: const vector = settings.getVector(grid, index);
return slideHorizontal(grid, direction); const swap = settings.swap.bind(this, grid, vector, index);
case Direction.Right: const initialFromIndex = settings.getInitialFromIndex(vector.length);
return slideHorizontal(grid, direction); const initialToIndex = settings.getInitialToIndex(vector.length);
default: const didChangeThisPass = slide(
return false; initialFromIndex,
initialToIndex,
vector,
settings.advance,
swap,
settings.merge,
settings.testForDone
);
didChange = didChange || didChangeThisPass;
} }
return didChange;
} }
const playMove = (grid, direction) => { const playMove = (grid, direction) => {
@@ -169,7 +127,7 @@ const playMove = (grid, direction) => {
} else { } else {
alert(`Unable to slide ${direction.name}.`); alert(`Unable to slide ${direction.name}.`);
} }
// checkForEnd(grid); checkForEnd(grid);
} }
const btnUp_onClick = () => { const btnUp_onClick = () => {
@@ -288,7 +246,7 @@ const makeGrid = (numRows, numColumns, initialValue) => {
} }
const makeTile = (value, visible, row, column) => { const makeTile = (value, visible, row, column) => {
return { const tile = {
value, value,
visible, visible,
originalLocation: { originalLocation: {
@@ -296,12 +254,16 @@ const makeTile = (value, visible, row, column) => {
column, column,
}, },
equals: (other) => { equals: (other) => {
return this.value == other.value return tile.value == other.value
&& this.visible == other.visible && tile.visible == other.visible
&& this.originalLocation.row == other.originalLocation.row && tile.originalLocation.row == other.originalLocation.row
&& this.originalLocation.column == other.originalLocation.column; && tile.originalLocation.column == other.originalLocation.column;
},
toString: (format = i => `{ value: ${i.value}, originalLocation: { row: ${i.originalLocation.row}, column: ${i.originalLocation.column} }, visible: ${i.visible ? "true" : "false"} }`) => {
return format(tile);
} }
}; };
return tile;
}; };
const addRandomTile = () => { const addRandomTile = () => {
@@ -338,7 +300,6 @@ const slide = (fromIndex, toIndex, vector, advance, swap, merge, testForDone) =>
} }
if (!toTile.visible) { if (!toTile.visible) {
if (fromTile.visible) { if (fromTile.visible) {
// need to choose row or column
swap(fromIndex, toIndex); swap(fromIndex, toIndex);
// Advance fromIndex // Advance fromIndex
fromIndex = advance(fromIndex); fromIndex = advance(fromIndex);
@@ -347,7 +308,6 @@ const slide = (fromIndex, toIndex, vector, advance, swap, merge, testForDone) =>
} else { } else {
// Advance fromIndex // Advance fromIndex
fromIndex = advance(fromIndex); fromIndex = advance(fromIndex);
hasMergedPrevious = false;
} }
} else { } else {
if (fromTile.visible) { if (fromTile.visible) {
@@ -372,141 +332,22 @@ const slide = (fromIndex, toIndex, vector, advance, swap, merge, testForDone) =>
} }
} else { } else {
fromIndex = advance(fromIndex); fromIndex = advance(fromIndex);
hasMergedPrevious = false;
} }
} }
} }
return didIDoIt; return didIDoIt;
} }
const slideUp = (grid) => { const canSlideInDirection = (grid, direction) => {
let didIDoIt = false; const clonedGrid = grid.clone();
const advance = i => i + 1; return slideInDirection(clonedGrid, direction);
for (let columnIndex = 0; columnIndex < grid.numColumns; columnIndex++) {
const swap = (from, to) => {
grid.swapTiles(from, columnIndex, to, columnIndex);
const tempTile = vector[to];
vector[to] = vector[from];
vector[from] = tempTile;
};
const merge = (from, to) => {
to.value++;
from.value = 0;
from.visible = false;
};
const vector = grid.getColumn(columnIndex);
const testForDone = fromIndex => fromIndex >= vector.length;
let fromIndex = 0;
let toIndex = -1;
const didChangeThisPass = slide(fromIndex, toIndex, vector, advance, swap, merge, testForDone);
didIDoIt = didIDoIt || didChangeThisPass;
}
return didIDoIt;
}
const slideLeft = (grid) => {
let didIDoIt = false;
const advance = i => i + 1;
for (let rowIndex = 0; rowIndex < grid.numRows; rowIndex++) {
const swap = (from, to) => {
grid.swapTiles(rowIndex, from, rowIndex, to);
const tempTile = vector[to];
vector[to] = vector[from];
vector[from] = tempTile;
};
const merge = (from, to) => {
to.value++;
from.value = 0;
from.visible = false;
};
const vector = grid.getRow(rowIndex);
const testForDone = fromIndex => fromIndex >= vector.length;
let fromIndex = 0;
let toIndex = -1;
const didChangeThisPass = slide(fromIndex, toIndex, vector, advance, swap, merge, testForDone);
didIDoIt = didIDoIt || didChangeThisPass;
}
return didIDoIt;
}
const slideDown = (grid) => {
let didIDoIt = false;
const advance = i => i - 1;
for (let columnIndex = 0; columnIndex < grid.numColumns; columnIndex++) {
const swap = (from, to) => {
grid.swapTiles(from, columnIndex, to, columnIndex);
const tempTile = vector[to];
vector[to] = vector[from];
vector[from] = tempTile;
};
const merge = (from, to) => {
to.value++;
from.value = 0;
from.visible = false;
};
const vector = grid.getColumn(columnIndex);
const testForDone = fromIndex => fromIndex < 0;
let fromIndex = vector.length - 1;
let toIndex = vector.length;
const didChangeThisPass = slide(fromIndex, toIndex, vector, advance, swap, merge, testForDone);
didIDoIt = didIDoIt || didChangeThisPass;
}
return didIDoIt;
}
const slideRight = (grid) => {
let didIDoIt = false;
const advance = i => i - 1;
for (let rowIndex = 0; rowIndex < grid.numRows; rowIndex++) {
const swap = (from, to) => {
grid.swapTiles(rowIndex, from, rowIndex, to);
const tempTile = vector[to];
vector[to] = vector[from];
vector[from] = tempTile;
};
const merge = (from, to) => {
to.value++;
from.value = 0;
from.visible = false;
};
const vector = grid.getRow(rowIndex);
const testForDone = fromIndex => fromIndex < 0;
let fromIndex = vector.length - 1;
let toIndex = vector.length;
const didChangeThisPass = slide(fromIndex, toIndex, vector, advance, swap, merge, testForDone);
didIDoIt = didIDoIt || didChangeThisPass;
}
return didIDoIt;
}
const canSlideUp = (grid) => {
const cloneGrid = grid.clone();
return slideUp(cloneGrid);
}
const canSlideDown = (grid) => {
const cloneGrid = grid.clone();
return slideDown(cloneGrid);
}
const canSlideLeft = (grid) => {
const cloneGrid = grid.clone();
return slideLeft(cloneGrid);
}
const canSlideRight = (grid) => {
const cloneGrid = grid.clone();
return slideRight(cloneGrid);
} }
const gameIsLost = (grid) => { const gameIsLost = (grid) => {
// return !canSlideUp(grid); return !canSlideInDirection(grid, Direction.Up)
// return false; && !canSlideInDirection(grid, Direction.Right)
return !canSlideUp(grid) && !canSlideInDirection(grid, Direction.Down)
&& !canSlideRight(grid) && !canSlideInDirection(grid, Direction.Left);
&& !canSlideDown(grid)
&& !canSlideLeft(grid);
} }
const checkForEnd = (grid) => { const checkForEnd = (grid) => {