diff --git a/main.js b/main.js index 0cd0e63..e6a9dac 100644 --- a/main.js +++ b/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 - // [0,0,1,0], - // [0,0,0,0], - // [0,0,0,0], - // [3,2,3,0], - // ]); + // 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], + ]); redraw(globals.grid); } @@ -26,16 +26,137 @@ 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 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) => { + 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) => { + 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.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); + } + 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); + } + } + return false; +} + const slideInDirection = (grid, direction) => { switch(direction) { case Direction.Up: - return slideUp(grid); + return slideVertical(grid, direction); case Direction.Down: - return slideDown(grid); + return slideVertical(grid, direction); case Direction.Left: - return slideLeft(grid); + return slideHorizontal(grid, direction); case Direction.Right: - return slideRight(grid); + return slideHorizontal(grid, direction); default: return false; } @@ -48,7 +169,7 @@ const playMove = (grid, direction) => { } else { alert(`Unable to slide ${direction.name}.`); } - checkForEnd(grid); + // checkForEnd(grid); } const btnUp_onClick = () => { @@ -71,9 +192,10 @@ const drawGrid = (grid, element) => { // grid is our grid // element is our div let text = "
| " + tile.value + " | \n"; } text += "