From 18e6686e593e65751f68b3fea03fcf50d6c4b76e Mon Sep 17 00:00:00 2001 From: Tom Hicks Date: Tue, 2 Jul 2024 12:10:34 -0700 Subject: [PATCH] Cleans up settings creation. --- main.js | 76 +++++++++++++++++++++++---------------------------------- 1 file changed, 31 insertions(+), 45 deletions(-) diff --git a/main.js b/main.js index 73fcf0d..13edd3f 100644 --- a/main.js +++ b/main.js @@ -26,70 +26,56 @@ const btnRedraw_onClick = () => { redraw(globals.grid); } -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; +const getSettingsForDirection = (direction) => { + 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 } }