Fixed bugs with tile addressing in the grid. Extracted sliding to shared methods.
This commit is contained in:
165
main.js
165
main.js
@@ -11,14 +11,14 @@ const globals = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const btnStart_onClick = () => {
|
const btnStart_onClick = () => {
|
||||||
globals.grid = makeGrid(4, 4, {value: 0, visible: false});
|
// globals.grid = makeGrid(4, 4, {value: 0, visible: false});
|
||||||
addRandomTile();
|
// addRandomTile();
|
||||||
// globals.grid = makeDevGrid([ // can't slide down or left
|
globals.grid = makeDevGrid([ // can't slide down or left
|
||||||
// [0,0,1,0],
|
[1,0,0,0],
|
||||||
// [0,0,0,0],
|
[0,0,0,0],
|
||||||
// [0,0,0,0],
|
[0,0,0,0],
|
||||||
// [3,2,3,0],
|
[0,0,0,0],
|
||||||
// ]);
|
]);
|
||||||
redraw(globals.grid);
|
redraw(globals.grid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,16 +26,137 @@ const btnRedraw_onClick = () => {
|
|||||||
redraw(globals.grid);
|
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) => {
|
const slideInDirection = (grid, direction) => {
|
||||||
switch(direction) {
|
switch(direction) {
|
||||||
case Direction.Up:
|
case Direction.Up:
|
||||||
return slideUp(grid);
|
return slideVertical(grid, direction);
|
||||||
case Direction.Down:
|
case Direction.Down:
|
||||||
return slideDown(grid);
|
return slideVertical(grid, direction);
|
||||||
case Direction.Left:
|
case Direction.Left:
|
||||||
return slideLeft(grid);
|
return slideHorizontal(grid, direction);
|
||||||
case Direction.Right:
|
case Direction.Right:
|
||||||
return slideRight(grid);
|
return slideHorizontal(grid, direction);
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -48,7 +169,7 @@ const playMove = (grid, direction) => {
|
|||||||
} else {
|
} else {
|
||||||
alert(`Unable to slide ${direction.name}.`);
|
alert(`Unable to slide ${direction.name}.`);
|
||||||
}
|
}
|
||||||
checkForEnd(grid);
|
// checkForEnd(grid);
|
||||||
}
|
}
|
||||||
|
|
||||||
const btnUp_onClick = () => {
|
const btnUp_onClick = () => {
|
||||||
@@ -71,9 +192,10 @@ const drawGrid = (grid, element) => {
|
|||||||
// grid is our grid
|
// grid is our grid
|
||||||
// element is our div
|
// element is our div
|
||||||
let text = "<table cellspacing=\"0\">\n";
|
let text = "<table cellspacing=\"0\">\n";
|
||||||
for (const row of grid.rows) {
|
for (let rowIndex = 0; rowIndex < grid.numRows; rowIndex++) {
|
||||||
text += " <tr>\n";
|
text += " <tr>\n";
|
||||||
for (const tile of row) {
|
for (let columnIndex = 0; columnIndex < grid.numColumns; columnIndex++) {
|
||||||
|
const tile = grid.getTile(rowIndex, columnIndex);
|
||||||
text += " <td title=\"(" + tile.originalLocation.row + ", " + tile.originalLocation.column + ")\"class=\"" + (tile.visible ? "visible " : "hidden ") + "\">" + tile.value + "</td>\n";
|
text += " <td title=\"(" + tile.originalLocation.row + ", " + tile.originalLocation.column + ")\"class=\"" + (tile.visible ? "visible " : "hidden ") + "\">" + tile.value + "</td>\n";
|
||||||
}
|
}
|
||||||
text += " </tr>\n";
|
text += " </tr>\n";
|
||||||
@@ -133,13 +255,13 @@ const makeGrid = (numRows, numColumns, initialValue) => {
|
|||||||
},
|
},
|
||||||
getTile: (row, column) => {
|
getTile: (row, column) => {
|
||||||
if (row >= 0 && row < numRows && column >= 0 && column < numColumns) {
|
if (row >= 0 && row < numRows && column >= 0 && column < numColumns) {
|
||||||
return rows[row][column];
|
return rows[column][row];
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
setTile: (row, column, tile) => {
|
setTile: (row, column, tile) => {
|
||||||
if (row >= 0 && row < numRows && column >= 0 && column < numColumns) {
|
if (row >= 0 && row < numRows && column >= 0 && column < numColumns) {
|
||||||
rows[row][column] = tile;
|
rows[column][row] = tile;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
swapTiles: (fromRow, fromColumn, toRow, toColumn) => {
|
swapTiles: (fromRow, fromColumn, toRow, toColumn) => {
|
||||||
@@ -197,16 +319,9 @@ const redraw = (grid) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const slide = (fromIndex, toIndex, vector, advance, swap, merge, testForDone) => {
|
const slide = (fromIndex, toIndex, vector, advance, swap, merge, testForDone) => {
|
||||||
// const oldSwap = swap;
|
|
||||||
// swap = (fromIndex, toIndex) => {
|
|
||||||
// const temp = vector[toIndex];
|
|
||||||
// vector[toIndex] = vector[fromIndex];
|
|
||||||
// vector[fromIndex] = temp;
|
|
||||||
// oldSwap(fromIndex, toIndex);
|
|
||||||
// };
|
|
||||||
let didIDoIt = false;
|
let didIDoIt = false;
|
||||||
let hasMergedPrevious = false;
|
let hasMergedPrevious = false;
|
||||||
while(!testForDone(fromIndex)) {
|
while(!testForDone(fromIndex, vector.length)) {
|
||||||
const fromTile = vector[fromIndex];
|
const fromTile = vector[fromIndex];
|
||||||
const toTile = toIndex < vector.length && toIndex >= 0 ? vector[toIndex] : null;
|
const toTile = toIndex < vector.length && toIndex >= 0 ? vector[toIndex] : null;
|
||||||
if (toTile == null) {
|
if (toTile == null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user