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

Commit

Permalink
scoring mechanic
Browse files Browse the repository at this point in the history
  • Loading branch information
lochungtin committed May 31, 2021
1 parent 1e11ac6 commit 6bf7b55
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
4 changes: 3 additions & 1 deletion src/game/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
13 changes: 9 additions & 4 deletions src/screens/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<any, any>
navigation: StackNavigationProp<any, any>,
}

class Screen extends React.Component<NavProps> {
interface ReduxProps {
game: GameConfig,
}

class Screen extends React.Component<NavProps & ReduxProps> {

newGame = () => store.dispatch(saveGameState({ ...new Board(4) }));

Expand All @@ -33,7 +38,7 @@ class Screen extends React.Component<NavProps> {
Score
</Text>
<Text style={{ ...MainStyles.scoreText, color: darktheme.textColor }}>
{10000}
{this.props.game.score}
</Text>
</View>
<View style={{ ...MainStyles.scoreContainer, backgroundColor: darktheme.textboxColor }}>
Expand Down Expand Up @@ -68,7 +73,7 @@ class Screen extends React.Component<NavProps> {
}

const mapStateToProps = state => ({

game: state.game,
});

export default connect(mapStateToProps)(Screen);
37 changes: 24 additions & 13 deletions src/utils/array.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Direction } from "./enums";
import { GameConfig } from "./types";

const rotate = (arr: Array<Array<number>>, direction: Direction): Array<Array<number>> => {
let rt: Array<Array<number>> = [];
let dim: number = arr.length;

for (let i = 0; i < dim; ++i) {
let row = [];
for (let j = 0; j < dim; ++j)
for (let j = 0; j < dim; ++j)
row.push(arr[(dim - j - 1) * (1 - direction) + (direction * j)][i * (1 - direction) + (dim - i - 1) * direction]);

rt.push(row);
Expand All @@ -15,12 +16,13 @@ const rotate = (arr: Array<Array<number>>, direction: Direction): Array<Array<nu
return rt;
}

const cascadeHorizontal = (arr: Array<Array<number>>, direction: Direction): Array<Array<number>> => {
const cascadeHorizontal = (arr: Array<Array<number>>, direction: Direction): GameConfig => {
let dim: number = arr.length;
// filter all "empty" elements
let rt: Array<Array<number>> = 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) {
Expand All @@ -30,6 +32,7 @@ const cascadeHorizontal = (arr: Array<Array<number>>, direction: Direction): Arr
if (nextIndex >= 0 && nextIndex < row.length && row[index] === row[nextIndex]) {
row[index] *= 2;
row[nextIndex] = -1;
score += row[index];
}
}
}
Expand All @@ -39,22 +42,30 @@ const cascadeHorizontal = (arr: Array<Array<number>>, 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<Array<number>>, direction: Direction): Array<Array<number>> => {
export const cascade = (arr: Array<Array<number>>, 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,
};
}
}

0 comments on commit 6bf7b55

Please sign in to comment.