Cleans up settings creation.

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

74
main.js
View File

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