Last active
March 5, 2018 23:52
-
-
Save goldensunliu/d352e61606dd887c28d3c86b9731efe6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { List } from 'immutable'; | |
class Game extends Compoent { | |
constructor(props) { | |
super(props); | |
this.generateGame(); | |
} | |
generateGame = (finalCount = 20) => { | |
// get a filled puzzle generated | |
const solution = makePuzzle(); | |
// pluck values from cells to create the game | |
const { puzzle } = pluck(solution, finalCount); | |
// initialize the board with choice counts | |
const board = makeBoard({ puzzle }); | |
this.setState({ | |
board, history: List.of(board), historyOffSet: 0, solution, | |
}); | |
} | |
updateBoard = (newBoard) => { | |
let { history } = this.state; | |
const { historyOffSet } = this.state; | |
// anything before current step is still in history | |
history = history.slice(0, historyOffSet + 1); | |
// add itself onto the history | |
history = history.push(newBoard); | |
// update the game | |
this.setState({ board: newBoard, history, historyOffSet: history.size - 1 }); | |
}; | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment