diff --git a/src/game/board.ts b/src/game/board.ts index def95c3..6e06fbc 100644 --- a/src/game/board.ts +++ b/src/game/board.ts @@ -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 => { @@ -68,62 +62,6 @@ export default class Board { return rt; } - private static getMergableIndices = (board: Board, direction: Direction): Array => { - let rt: Array = []; - - let openList: Array = []; - 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 => [[0, 1], [0, -1], [-1, 0], [1, 0]] .map(pair => [pair[0] + coordinate.row, pair[1] + coordinate.col]) diff --git a/src/utils/array.ts b/src/utils/array.ts index aba80fb..0496b18 100644 --- a/src/utils/array.ts +++ b/src/utils/array.ts @@ -20,6 +20,23 @@ const cascadeHorizontal = (arr: Array>, direction: Direction): Arr // filter all "empty" elements let rt: Array> = 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)]);