Skip to content

Instantly share code, notes, and snippets.

@goldensunliu
Last active March 5, 2018 23:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goldensunliu/d352e61606dd887c28d3c86b9731efe6 to your computer and use it in GitHub Desktop.
Save goldensunliu/d352e61606dd887c28d3c86b9731efe6 to your computer and use it in GitHub Desktop.
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