Cleans up settings creation.
This commit is contained in:
68
main.js
68
main.js
@@ -27,69 +27,55 @@ const btnRedraw_onClick = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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;
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
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++;
|
to.value++;
|
||||||
from.value = 0;
|
from.value = 0;
|
||||||
from.visible = false;
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user