Commit 2b1b87a7 authored by Yannick Li's avatar Yannick Li
Browse files

Add multiple static grids

parent 59e7ea51
......@@ -75,7 +75,7 @@ class Grid extends React.Component<IGridProps, IGridState> {
* It set a timer to refresh cells values.
*/
componentDidMount() {
this.initFrom(generateStaticGrid());
this.initFrom(generateStaticGrid(this.state.gridNum));
this.timerID = setInterval(
() => this.updateGrid(),
1000
......@@ -179,7 +179,10 @@ class Grid extends React.Component<IGridProps, IGridState> {
handleChange(index: number, value: string) {
assert.ok(value === "" || (Number(value) >= 1 && Number(value) <= 9))
assert.ok(index >= 0 && index < 81)
if (!this.state.cells[index].modifiable) {
console.error("Trying to change an non modifiable cell. Should not happend");
}
let cells = this.state.cells;
cells[index].value = value;
this.updateState(cells);
......@@ -198,9 +201,12 @@ class Grid extends React.Component<IGridProps, IGridState> {
* @param gridNum Desired grid number.
*/
handleSubmit(gridNum: string) {
if (Number(gridNum) < 1 || Number(gridNum) > 46 || gridNum === this.state.gridNum) {
return;
}
let mvmap = this.props.collection.open("grid" + gridNum, "MVMap", false, function () {return});
this.setState({gridNum: gridNum, mvmap: mvmap});
this.initFrom(generateStaticGrid());
this.initFrom(generateStaticGrid(gridNum));
}
/**
......@@ -242,7 +248,9 @@ class Grid extends React.Component<IGridProps, IGridState> {
return (
<div className="sudoku">
<div>Current grid : {this.state.gridNum}</div>
<Submit1Input inputName="Grid" onSubmit={this.handleSubmit.bind(this)} /><br />
<Submit1Input inputName="Grid" onSubmit={this.handleSubmit.bind(this)} />
<div>Grid difficulty on scale of 1-46 (1 being the easier and 46 being the harder)</div>
<br />
<div><button onClick={this.reset.bind(this)}>Reset</button></div><br />
<div><button onClick={() => this.switchConnection()}>{this.state.isConnected ? "Disconnect" : "Connect"}</button></div><br />
<table className="grid">
......@@ -407,24 +415,65 @@ class Grid extends React.Component<IGridProps, IGridState> {
}
}
/**
* Grids generated using sudokutoolcollection.
* https://www.npmjs.com/package/sudokutoolcollection
*/
const GRIDS : {[key: string]: string} = {
"1": "8354.721.4293.17..1675298437826.4.51514.32.76.9617...8..8..6132271..3684643218597",
"2": "..6.2.74..42.7..86175486293538694172..185743.4973128657192683548.4.3..27..374..18",
"3": "31.29.864...31.79292..481.58649.157315....928792..3641539...216678132459241..9387",
"4": ".962.8.7.314657928.8.9.45164...25.6976.4932819281...5.8723.16951495.283765378....",
"5": "42.8.713585.31.247.312548969176483523.8....1.2.51.3...592731...67348592.1.49..573",
"6": "18.43.79.56.792..4.9.581263871.54.36.5..7...132..19..7235148679948367..2.1.925348",
"7": ".5.9416731..786925...53214842.6.378.3..8.7462...2.453....1653945..4298179..378256",
"8": "2.9756.48548.9..76..7.84529873529614124637.859..4182377...4.86.4...6375.....7.49.",
"9": ".862.9.3437..462.94..35167.853762491...5138.7...984365.67.98..3938125746...63....",
"10": "1..9.3765...5..19339.7168246398542..7526314894812795365.31.794....4..3.....3..6..",
"11": "..617.29519263547857428.163258..3.1.613...58494751832.4.5..1.3.7.9.4..51..1......",
"12": ".3.7.14.9..136.....24895.6..97.43.5..45186..78639..124.125..6936.94.251835...9742",
"13": "578316924..3542817421789.5...5..7468286134579...865231.....8.4...2....8.8......92",
"14": "795362.8.164978...283145..98412.7..6..68.4...9.2651..8.17.83.....9.26.17.28719...",
"15": "691..5...2351.9....8..2.59116.2.8.5.523...1..4785136298.2...9..9468327153.7...28.",
"16": ".4.8.9....8.514......3.7.4.....46.2.834295.......38.94519673482376482951428951...",
"17": "2..9134..4.9.56...1...48....4..9.....1..3..4.9..184526351.69.72628375194794821...",
"18": "4265318977..942...1958764.3..7195...612784.35954263178.4.....89..................",
"19": ".31487259.8.3..41..4....8738935..647.7.94..2.124.7.395.......32.69...581.1.....64",
"20": "254986..7...1459.2169732....9.461....1...7.4342.358.91.71.2.............9428.35.6",
"21": ".8.3.....57....81..2.87....295438671164957382738.62594942.8.1...5........1.......",
"22": ".4..8.....18.3..9..6........5.86732.327195...68.3427..89...36.2.769285..23.6.49..",
"23": "923..8.6..68..4.7...76.928383...2.5.295..184.74...5.2.61...75..38...67.257.......",
"24": "........77.9.63.8....7.89..53.87.649....3.1288.....375...6.92..916327854......796",
"25": "2..496.5336957124.....3...9......521.1..254.6...61.38.....42..5..48....2...1.7.34",
"26": "1.279.5....6.........8.....671924385358167...429385....9....4...6.....5..146.9..3",
"27": ".....9.6..7.3..48........3.......84.469.38.7281...4.9......1.2.146..3.5.298675314",
"28": "4.3895.21...6123.4.1.374.85...7.8246.2..6.517....21.39............2......3......2",
"29": ".819462.5.9.7384164.621........9256.6..45.....5..67.....8.....1.2.6.1.9..........",
"30": ".2398..5....453...549.7....3..7958..854.1.......348..5.758.41...3.5...........5..",
"31": "284..7...7..8.2......4.18.......5........4........97..675..8...8927.31561.35269..",
"32": "....9..........8..14...2..7.95..7.....2.......81....65916875324874........3..9678",
"33": "5...1..8..7....9..............165823162...579385792......6...5..3.52..9..5.......",
"34": "....4.7.17.....5....2...38....4..21..2.....4...1....3.1.6..4..92....81.3839..547.",
"35": "698..4...1..5...4.574..3.2.4.5...7..96.....8.2873...1.3...96...7........8........",
"36": "...389..7..5761..47..542.......5.........34...........3.6.........8.5.464871.6...",
"37": ".5....3.6.7....491.......5..4...7..9.8....6..21.8.5734...3...6....6........9...43",
"38": "2936.8415...4.37......2.6.........3................8......673..748.15.........5.1",
"39": ".23.....9.6....2...8.......65........7..6...214.............9..........6296458731",
"40": "..845.6.26....75.4.5...29............16..572.............5........3..2......29.5.",
"41": "..8.............4........5............43.6.87...............5.1...5.48.928579.634",
"42": "...7.4.81............5....2..3..51.8.1...........1.....7......52.6.....9.....7316",
"43": "......9.....9....2.93.2.54..1.....65.........7.....2.....8..........38..987.1....",
"44": "......4.1......2........9.8......6.3.5.9.71.2.2...4....71....2....8......9.......",
"45": "....6.9..56....7..........6...348.7...4...8..........4....97....15..4............",
"46": "9.....7....5...6.8..8...2..2....6...6.....4.....4..3........5.2........3..2......"
}
/**
* Return a predefined Sudoku grid as a string.
* @param gridNum Desired grid number
*/
function generateStaticGrid() {
/**
* 32.17.654
* 6152947..
* .783.6291
* .574.2816
* 18.7659.2
* 236.1.54.
* 742.813.9
* 8.36.7125
* 56.9234.8
*/
const values = "32.17.6546152947...783.6291.574.281618.7659.2236.1.54.742.813.98.36.712556.9234.8"
assert.ok(values.split('').every(x => (x === "." || (Number(x) >= 1 && Number(x) <= 9))));
return values;
function generateStaticGrid(gridNum: string) {
return GRIDS[gridNum];
}
/**
......
......@@ -36,7 +36,7 @@ interface ISubmit1InputState {
class Submit1Input extends React.Component<ISubmit1InputProps, ISubmit1InputState> {
constructor(props: any) {
super(props);
this.state = {value: ""};
this.state = {value: "1"};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
......@@ -52,7 +52,9 @@ class Submit1Input extends React.Component<ISubmit1InputProps, ISubmit1InputStat
handleSubmit(event: any) {
event.preventDefault();
this.props.onSubmit(this.state.value);
if (Number(this.state.value) >= 1 && Number(this.state.value) <= 46) {
this.props.onSubmit(this.state.value);
}
}
render() {
......@@ -60,7 +62,8 @@ class Submit1Input extends React.Component<ISubmit1InputProps, ISubmit1InputStat
<form onSubmit={this.handleSubmit}>
<label>
{this.props.inputName} :
<input value={this.state.value} onChange={this.handleChange} />
<input type="number" value={this.state.value} onChange={this.handleChange}
min="1" max="46"/>
</label>
<input type="submit" value={"Change " + this.props.inputName} />
</form>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment