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