Skip to content

Instantly share code, notes, and snippets.

@goldensunliu
Last active March 6, 2018 16:09
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/039c1b9a05c5396f5515695b05f66660 to your computer and use it in GitHub Desktop.
Save goldensunliu/039c1b9a05c5396f5515695b05f66660 to your computer and use it in GitHub Desktop.
import { List, fromJS } from 'immutable';
/**
* make size 9 array of 0s
* @returns {Array}
*/
function makeCountObject() {
const countObj = [];
for (let i = 0; i < 10; i += 1) countObj.push(0);
return countObj;
}
/**
* given a 2D array of numbers as the initial puzzle, generate the initial game state
* @param puzzle
* @returns {any}
*/
function makeBoard({ puzzle }) {
// create initial count object to keep track of conflicts per number value
const rows = Array.from(Array(9).keys()).map(() => makeCountObject());
const columns = Array.from(Array(9).keys()).map(() => makeCountObject());
const squares = Array.from(Array(9).keys()).map(() => makeCountObject());
const result = puzzle.map((row, i) => (
row.map((cell, j) => {
if (cell) {
rows[i][cell] += 1;
columns[j][cell] += 1;
squares[((Math.floor(i / 3)) * 3) + Math.floor(j / 3)][cell] += 1;
}
return {
value: puzzle[i][j] > 0 ? puzzle[i][j] : null,
prefilled: !!puzzle[i][j],
};
})
));
return fromJS({ puzzle: result, selected: false, choices: { rows, columns, squares } });
}
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 });
}
updateBoard = (newBoard) => {
this.setState({ board: newBoard });
};
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment