Cleans up settings creation.

This commit is contained in:
2024-07-02 12:10:34 -07:00
parent 0129e43b8e
commit 18e6686e59

76
main.js
View File

@@ -26,70 +26,56 @@ const btnRedraw_onClick = () => {
redraw(globals.grid); redraw(globals.grid);
} }
const getSettingsForDirection = (direction) => { const getSettingsForDirection = (direction) => {
const advanceForward = i => i - 1; let settings = {
const advanceBackward = i => i + 1; merge: (from, to) => {
const testForDoneForward = fromIndex => fromIndex < 0; to.value++;
const testForDoneBackward = (fromIndex, length) => fromIndex >= length; from.value = 0;
const getInitialFromIndexForward = (length) => length - 2; from.visible = false;
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 = (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;
};
let settings = { merge };
if (direction === Direction.Up || direction === Direction.Left) { if (direction === Direction.Up || direction === Direction.Left) {
settings = { settings = {
...settings, ...settings,
advance: advanceBackward, advance: i => i + 1,
testForDone: testForDoneBackward, testForDone: (fromIndex, length) => fromIndex >= length,
getInitialFromIndex: getInitialFromIndexBackward, getInitialFromIndex: () => 1,
getInitialToIndex: getInitialToIndexBackward getInitialToIndex: () => 0
}; };
} else if (direction === Direction.Down || direction === Direction.Right) { } else if (direction === Direction.Down || direction === Direction.Right) {
settings = { settings = {
...settings, ...settings,
advance: advanceForward, advance: i => i - 1,
testForDone: testForDoneForward, testForDone: fromIndex => fromIndex < 0,
getInitialFromIndex: getInitialFromIndexForward, getInitialFromIndex: (length) => length - 2,
getInitialToIndex: getInitialToIndexForward getInitialToIndex: (length) => length - 1
}; };
} }
if (direction === Direction.Up || direction === Direction.Down) { if (direction === Direction.Up || direction === Direction.Down) {
settings = { settings = {
...settings, ...settings,
getVector: getColumn, getVector: (grid, index) => grid.getColumn(index),
swap: swapInColumn, swap: (grid, vector, columnIndex, from, to) => {
getLength: getNumColumns grid.swapTiles(from, columnIndex, to, columnIndex);
const tempTile = vector[to];
vector[to] = vector[from];
vector[from] = tempTile;
},
getLength: (grid) => grid.numColumns
} }
} else if (direction === Direction.Left || direction === Direction.Right) { } else if (direction === Direction.Left || direction === Direction.Right) {
settings = { settings = {
...settings, ...settings,
getVector: getRow, getVector: (grid, index) => grid.getRow(index),
swap: swapInRow, swap: (grid, vector, rowIndex, from, to) => {
getLength: getNumRows grid.swapTiles(rowIndex, from, rowIndex, to);
const tempTile = vector[to];
vector[to] = vector[from];
vector[from] = tempTile;
},
getLength: (grid) => grid.numRows
} }
} }