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

Commit

Permalink
fixed new tile bug
Browse files Browse the repository at this point in the history
  • Loading branch information
lochungtin committed Jun 1, 2021
1 parent 5869ba3 commit 5ebb4b9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
3 changes: 1 addition & 2 deletions src/components/Board/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ class BoardView extends React.Component<ReduxProps> {
let temp = { ...this.props.game };

// update history
if (!compare(this.props.history.board, temp.board))
store.dispatch(updateHistory({ ...temp }));
store.dispatch(updateHistory({ ...temp }));

// perform swipe
Board.swipe(temp, direction);
Expand Down
9 changes: 5 additions & 4 deletions src/game/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ export default class Board {

static swipe = (board: Board, direction: Direction): void => {
let temp = cascade(board.board, direction);
let moved: boolean = !compare(board.board, temp.board);

if (!compare(board.board, temp.board))
Board.newTile(board);

board.board = temp.board;
board.score += temp.score;

if (moved)
Board.newTile(board);
}

static validate = (board: Board): boolean => {
Expand Down Expand Up @@ -72,7 +73,7 @@ export default class Board {
private static isValidIndex = (board: Board, pair: Array<number>): boolean =>
pair[0] >= 0 && pair[1] >= 0 && pair[0] < board.dim && pair[1] < board.dim;

private static newTile = (board: Board): void => {
private static newTile = (board: Board): void => {
let empty: Array<number> = Board.getAvailable(board);
if (empty.length === 0)
return;
Expand Down

0 comments on commit 5ebb4b9

Please sign in to comment.