This repository has been archived by the owner on Nov 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
694d1e1
commit 4fd69ca
Showing
7 changed files
with
88 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import Board from '../game/board'; | ||
import { ActionType } from '../utils/types'; | ||
import { ActionType, GameConfig } from '../utils/types'; | ||
|
||
export enum ActionName { | ||
SAVE_BOARD | ||
SAVE_GAME_STATE | ||
} | ||
|
||
export const saveBoard = (payload: Board): ActionType => ({ | ||
type: ActionName.SAVE_BOARD, | ||
export const saveGameState = (payload: GameConfig): ActionType => ({ | ||
type: ActionName.SAVE_GAME_STATE, | ||
payload, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
import { combineReducers } from 'redux'; | ||
|
||
import Board from '../game/board'; | ||
import { ActionName } from './action'; | ||
|
||
import { ActionType } from '../utils/types'; | ||
import { ActionType, GameConfig } from '../utils/types'; | ||
|
||
const saveBoard = (board: Board = null, action: ActionType) => action.type === ActionName.SAVE_BOARD ? action.payload : board; | ||
const saveGame = (board: GameConfig = null, action: ActionType) => action.type === ActionName.SAVE_GAME_STATE ? action.payload : board; | ||
|
||
export default combineReducers({ | ||
board: saveBoard | ||
game: saveGame, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Direction } from "./enums"; | ||
|
||
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) | ||
row.push(arr[(dim - j - 1) * (1 - direction) + (direction * j)][i * (1 - direction) + (dim - i - 1) * direction]); | ||
|
||
rt.push(row); | ||
} | ||
|
||
return rt; | ||
} | ||
|
||
const cascadeHorizontal = (arr: Array<Array<number>>, direction: Direction): Array<Array<number>> => { | ||
let dim: number = arr.length; | ||
// filter all "empty" elements | ||
let rt: Array<Array<number>> = arr.map(row => row.filter(cell => cell !== -1)); | ||
|
||
// append or insert | ||
if (direction === Direction.left) | ||
return rt.map(row => [...row, ...new Array(dim - row.length).fill(-1)]); | ||
else | ||
return rt.map(row => [...new Array(dim - row.length).fill(-1), ...row]); | ||
} | ||
|
||
export const cascade = (arr: Array<Array<number>>, direction: Direction): Array<Array<number>> => { | ||
if (direction === Direction.left || direction === Direction.right) | ||
return cascadeHorizontal(arr, direction); | ||
else | ||
// rotate, cascade, and rerotate | ||
return rotate( | ||
cascadeHorizontal( | ||
rotate(arr, (direction === Direction.down ? 1 : 0)), | ||
Direction.right | ||
), | ||
(direction === Direction.up ? 1 : 0) | ||
); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters