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

Commit

Permalink
working game
Browse files Browse the repository at this point in the history
  • Loading branch information
lochungtin committed May 31, 2021
1 parent e39264c commit 0fbf7f4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/components/Board/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class BoardView extends React.Component<ReduxProps> {
swipe = (direction: Direction): void => {
let temp = { ...this.props.game };
Board.swipe(temp, direction);
if (!Board.validate(temp))
console.log('e')
store.dispatch(saveGameState(temp));
}

Expand Down
18 changes: 7 additions & 11 deletions src/game/board.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { cascade } from "../utils/array";
import { Direction } from "../utils/enums";
import { CoordinatePair, MergingPairs } from "../utils/types";
import { CoordinatePair } from "../utils/types";

export default class Board {

Expand All @@ -23,12 +23,7 @@ export default class Board {
}

static swipe = (board: Board, direction: Direction): void => {
console.log(board);

// realign to direction after merge
board.board = cascade(board.board, direction);

// add new tile
Board.newTile(board);
}

Expand All @@ -41,8 +36,8 @@ export default class Board {

// cell can be combined with neighbour
let neighbours: Array<CoordinatePair> = Board.getNeighbourIndices(board, { row: i, col: j });
for (let k = 0; i < neighbours.length; ++k) {
let pair: CoordinatePair = neighbours[i];
for (let k = 0; k < neighbours.length; ++k) {
let pair: CoordinatePair = neighbours[k];

if (board.board[pair.row][pair.col] == board.board[i][j])
return true;
Expand Down Expand Up @@ -71,16 +66,17 @@ 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): Array<number> => {
private static newTile = (board: Board): void => {
let empty: Array<number> = Board.getAvailable(board);
if (empty.length === 0)
return;

let selection: number = empty[Math.floor(Math.random() * empty.length)];

let row: number = Math.floor(selection / board.dim);
let col: number = selection % board.dim;
let value: number = (Math.random() > 0.7) ? 4 : 2;

board.board[row][col] = value;

return [row, col, value];
}
}

0 comments on commit 0fbf7f4

Please sign in to comment.