diff --git a/src/game/board.ts b/src/game/board.ts index 4e89bf6..0feb6d5 100644 --- a/src/game/board.ts +++ b/src/game/board.ts @@ -23,7 +23,9 @@ export default class Board { } static swipe = (board: Board, direction: Direction): void => { - board.board = cascade(board.board, direction); + let temp = cascade(board.board, direction); + board.board = temp.board; + board.score += temp.score; Board.newTile(board); } diff --git a/src/screens/main.tsx b/src/screens/main.tsx index 7c30a51..a902cdd 100644 --- a/src/screens/main.tsx +++ b/src/screens/main.tsx @@ -12,12 +12,17 @@ import { MainStyles, ScreenStyles } from './styles'; import Board from '../game/board'; import { saveGameState } from '../redux/action'; import { store } from '../redux/store'; +import { GameConfig } from '../utils/types'; interface NavProps { - navigation: StackNavigationProp + navigation: StackNavigationProp, } -class Screen extends React.Component { +interface ReduxProps { + game: GameConfig, +} + +class Screen extends React.Component { newGame = () => store.dispatch(saveGameState({ ...new Board(4) })); @@ -33,7 +38,7 @@ class Screen extends React.Component { Score - {10000} + {this.props.game.score} @@ -68,7 +73,7 @@ class Screen extends React.Component { } const mapStateToProps = state => ({ - + game: state.game, }); export default connect(mapStateToProps)(Screen); diff --git a/src/utils/array.ts b/src/utils/array.ts index 0496b18..5683801 100644 --- a/src/utils/array.ts +++ b/src/utils/array.ts @@ -1,4 +1,5 @@ import { Direction } from "./enums"; +import { GameConfig } from "./types"; const rotate = (arr: Array>, direction: Direction): Array> => { let rt: Array> = []; @@ -6,7 +7,7 @@ const rotate = (arr: Array>, direction: Direction): Array>, direction: Direction): Array>, direction: Direction): Array> => { +const cascadeHorizontal = (arr: Array>, direction: Direction): GameConfig => { let dim: number = arr.length; // filter all "empty" elements let rt: Array> = arr.map(row => row.filter(cell => cell !== -1)); // merge neighbours + let score: number = 0; for (let i = 0; i < rt.length; ++i) { let row = rt[i]; for (let j = 0; j < row.length; ++j) { @@ -30,6 +32,7 @@ const cascadeHorizontal = (arr: Array>, direction: Direction): Arr if (nextIndex >= 0 && nextIndex < row.length && row[index] === row[nextIndex]) { row[index] *= 2; row[nextIndex] = -1; + score += row[index]; } } } @@ -39,22 +42,30 @@ const cascadeHorizontal = (arr: Array>, direction: Direction): Arr // append or insert if (direction === Direction.left) - return rt.map(row => [...row, ...new Array(dim - row.length).fill(-1)]); + return { + board: rt.map(row => [...row, ...new Array(dim - row.length).fill(-1)]), + dim, + score, + }; else - return rt.map(row => [...new Array(dim - row.length).fill(-1), ...row]); + return { + board: rt.map(row => [...new Array(dim - row.length).fill(-1), ...row]), + dim, + score, + }; } -export const cascade = (arr: Array>, direction: Direction): Array> => { +export const cascade = (arr: Array>, direction: Direction): GameConfig => { if (direction === Direction.left || direction === Direction.right) return cascadeHorizontal(arr, direction); - else + else { // rotate, cascade, and rerotate - return rotate( - cascadeHorizontal( - rotate(arr, (direction === Direction.down ? 1 : 0)), - Direction.right - ), - (direction === Direction.up ? 1 : 0) - ); + let game = cascadeHorizontal( rotate(arr, (direction === Direction.down ? 1 : 0)), Direction.right); + return { + board: rotate(game.board, (direction === Direction.up ? 1 : 0)), + dim: game.score, + score: game.score, + }; + } }