diff --git a/src/components/Board/index.tsx b/src/components/Board/index.tsx index 6957ab8..599349a 100644 --- a/src/components/Board/index.tsx +++ b/src/components/Board/index.tsx @@ -29,7 +29,9 @@ class BoardView extends React.Component { } swipe = (direction: Direction): void => { - + let temp = { ...this.props.game }; + Board.swipe(temp, direction); + store.dispatch(saveGameState(temp)); } render() { diff --git a/src/game/board.ts b/src/game/board.ts index a1c7792..def95c3 100644 --- a/src/game/board.ts +++ b/src/game/board.ts @@ -19,10 +19,10 @@ export default class Board { this.board.push(row); } - this.newTile(); + Board.newTile(this); } - swipe = (direction: Direction): void => { + 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]; @@ -31,25 +31,26 @@ export default class Board { // cascade to direction console.log(direction); - this.board = (cascade(this.board, direction)); + board.board = cascade(board.board, direction); + console.log(board); // add new tile - let newTile = this.newTile(); + let newTile = Board.newTile(board); } - validate = (): boolean => { - for (let i = 0; i < this.dim; ++i) { - for (let j = 0; j < this.dim; ++j) { + static validate = (board: Board): boolean => { + for (let i = 0; i < board.dim; ++i) { + for (let j = 0; j < board.dim; ++j) { // board has empty cell - if (this.board[i][j] == -1) + if (board.board[i][j] == -1) return true; // cell can be combined with neighbour - let neighbours: Array = this.getNeighbourIndices({ row: i, col: j }); + let neighbours: Array = Board.getNeighbourIndices(board, { row: i, col: j }); for (let k = 0; i < neighbours.length; ++k) { let pair: CoordinatePair = neighbours[i]; - if (this.board[pair.row][pair.col] == this.board[i][j]) + if (board.board[pair.row][pair.col] == board.board[i][j]) return true; } } @@ -57,30 +58,30 @@ export default class Board { return false; } - private getAvailable = (): Array => { + private static getAvailable = (board: Board): Array => { let rt: Array = []; - this.board.forEach((row, rIndex) => row.forEach((cell, cIndex) => { + board.board.forEach((row, rIndex) => row.forEach((cell, cIndex) => { if (cell == -1) - rt.push(rIndex * this.dim + cIndex); + rt.push(rIndex * board.dim + cIndex); })); return rt; } - private getMergableIndices = (direction: Direction): Array => { + private static getMergableIndices = (board: Board, direction: Direction): Array => { let rt: Array = []; let openList: Array = []; - for (let i = 0; i < this.dim * this.dim; ++i) + for (let i = 0; i < board.dim * board.dim; ++i) openList.push(i); - for (let i = 0; i < this.dim; ++i) { - for (let j = 0; j < this.dim; ++j) { + 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 = this.dim - i - 2; + row = board.dim - i - 2; col = j; break; case Direction.left: @@ -89,7 +90,7 @@ export default class Board { break; case Direction.right: row = i; - col = this.dim - j; + col = board.dim - j; break; default: row = i + 1; @@ -97,13 +98,13 @@ export default class Board { } let toBeMerged = [row, col]; - let tbmNumericIndex = row * this.dim + col; - let curNumericIndex = i * this.dim + j; - if (!this.isValidIndex(toBeMerged) && !openList.includes(tbmNumericIndex) && !openList.includes(curNumericIndex)) + 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 (this.board[i][j] === this.board[toBeMerged[0]][toBeMerged[j]]) { + if (board.board[i][j] === board.board[toBeMerged[0]][toBeMerged[j]]) { rt.push({ mergee: { row: i, @@ -123,24 +124,24 @@ export default class Board { return rt; } - private getNeighbourIndices = (coordinate: CoordinatePair): Array => + 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]) - .filter(this.isValidIndex) + .filter(pair => Board.isValidIndex(board, pair)) .map(pair => ({ row: pair[0], col: pair[1] })); - private isValidIndex = (pair: Array): boolean => - pair[0] >= 0 && pair[1] >= 0 && pair[0] < this.dim && pair[1] < this.dim; + private static isValidIndex = (board: Board, pair: Array): boolean => + pair[0] >= 0 && pair[1] >= 0 && pair[0] < board.dim && pair[1] < board.dim; - private newTile = (): Array => { - let empty: Array = this.getAvailable(); + private static newTile = (board: Board): Array => { + let empty: Array = Board.getAvailable(board); let selection: number = empty[Math.floor(Math.random() * empty.length)]; - let row: number = Math.floor(selection / this.dim); - let col: number = selection % this.dim; + let row: number = Math.floor(selection / board.dim); + let col: number = selection % board.dim; let value: number = (Math.random() > 0.7) ? 4 : 2; - this.board[row][col] = value; + board.board[row][col] = value; return [row, col, value]; }