Skip to content
This repository has been archived by the owner on Nov 11, 2021. It is now read-only.

Commit

Permalink
combined merge algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
lochungtin committed May 30, 2021
1 parent f260fa8 commit e39264c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 66 deletions.
70 changes: 4 additions & 66 deletions src/game/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,13 @@ export default class Board {
}

static swipe = (board: Board, direction: Direction): void => {
// merge all
// this.getMergableIndices(direction).forEach(pair => {
// this.board[pair.mergee.row][pair.mergee.col] += this.board[pair.merger.row][pair.merger.col];
// this.board[pair.merger.row][pair.merger.col] = -1;
// });

// cascade to direction
console.log(direction);
board.board = cascade(board.board, direction);
console.log(board);

// realign to direction after merge
board.board = cascade(board.board, direction);

// add new tile
let newTile = Board.newTile(board);
Board.newTile(board);
}

static validate = (board: Board): boolean => {
Expand Down Expand Up @@ -68,62 +62,6 @@ export default class Board {
return rt;
}

private static getMergableIndices = (board: Board, direction: Direction): Array<MergingPairs> => {
let rt: Array<MergingPairs> = [];

let openList: Array<number> = [];
for (let i = 0; i < board.dim * board.dim; ++i)
openList.push(i);

for (let i = 0; i < board.dim; ++i) {
for (let j = 0; j < board.dim; ++j) {
// directional ordering
let row, col;
switch (direction) {
case Direction.down:
row = board.dim - i - 2;
col = j;
break;
case Direction.left:
row = i;
col = j - 1;
break;
case Direction.right:
row = i;
col = board.dim - j;
break;
default:
row = i + 1;
col = j;
}

let toBeMerged = [row, col];
let tbmNumericIndex = row * board.dim + col;
let curNumericIndex = i * board.dim + j;
if (!Board.isValidIndex(board, toBeMerged) && !openList.includes(tbmNumericIndex) && !openList.includes(curNumericIndex))
continue;

// can be merged
if (board.board[i][j] === board.board[toBeMerged[0]][toBeMerged[j]]) {
rt.push({
mergee: {
row: i,
col: j,
},
merger: {
row: toBeMerged[0],
col: toBeMerged[1],
}
});
openList.splice(openList.indexOf(tbmNumericIndex), 1);
openList.splice(openList.indexOf(curNumericIndex), 1);
}
}
}

return rt;
}

private static getNeighbourIndices = (board: Board, coordinate: CoordinatePair): Array<CoordinatePair> =>
[[0, 1], [0, -1], [-1, 0], [1, 0]]
.map(pair => [pair[0] + coordinate.row, pair[1] + coordinate.col])
Expand Down
17 changes: 17 additions & 0 deletions src/utils/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ const cascadeHorizontal = (arr: Array<Array<number>>, direction: Direction): Arr
// filter all "empty" elements
let rt: Array<Array<number>> = arr.map(row => row.filter(cell => cell !== -1));

// merge neighbours
for (let i = 0; i < rt.length; ++i) {
let row = rt[i];
for (let j = 0; j < row.length; ++j) {
let index = (direction === Direction.left ? (2 * j) : row.length) - j;
let nextIndex = index + (direction === Direction.left ? 1 : -1);

if (nextIndex >= 0 && nextIndex < row.length && row[index] === row[nextIndex]) {
row[index] *= 2;
row[nextIndex] = -1;
}
}
}

// refilter after merge
rt = rt.map(row => row.filter(cell => cell !== -1));

// append or insert
if (direction === Direction.left)
return rt.map(row => [...row, ...new Array(dim - row.length).fill(-1)]);
Expand Down

0 comments on commit e39264c

Please sign in to comment.