Commit 9c53facd authored by Florent Coriat's avatar Florent Coriat
Browse files

Merge branch '5-add-different-grids' into 'master'

Resolve "Add different grids"

Closes #5

See merge request !13
parents e830160d 1adda931
This diff is collapsed.
......@@ -23,8 +23,7 @@
*/
import React from 'react';
export const validInput = new RegExp('^[1-9]$');
import { validInput } from '../constants'
/**
* Interface for the properties of the Cell.
......
......@@ -25,8 +25,9 @@
import React from 'react';
import assert from 'assert';
import Cell from './Cell';
import { validInput } from './Cell';
import { validInput, GRIDS } from '../constants'
import { client } from '@concordant/c-client';
import Submit1Input from './Submit1Input';
/**
* Interface for the properties of the Grid
......@@ -40,6 +41,7 @@ interface IGridProps {
* Interface for the state of the Grid
*/
interface IGridState {
gridNum: string,
mvmap: any,
cells: {value: string, modifiable: boolean, error: boolean}[],
isConnected: boolean,
......@@ -57,8 +59,10 @@ class Grid extends React.Component<IGridProps, IGridState> {
super(props);
let cells = new Array(81).fill(null).map(()=>({value:"", modifiable:false, error:false}));
this.modifiedCells = new Array(81).fill(null);
let mvmap = this.props.collection.open("room", "MVMap", false, function () {return});
let gridNum = "1";
let mvmap = this.props.collection.open("grid" + gridNum, "MVMap", false, function () {return});
this.state = {
gridNum: gridNum,
mvmap: mvmap,
cells: cells,
isConnected: true,
......@@ -71,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
......@@ -175,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);
......@@ -189,6 +196,19 @@ class Grid extends React.Component<IGridProps, IGridState> {
}
}
/**
* This handler is called when a new grid number is submit.
* @param gridNum Desired grid number.
*/
handleSubmit(gridNum: string) {
if (Number(gridNum) < 1 || Number(gridNum) > 100 || 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(gridNum));
}
/**
* This function return a React element corresponding to a cell.
* @param index The index of the cell to render.
......@@ -227,6 +247,10 @@ class Grid extends React.Component<IGridProps, IGridState> {
render() {
return (
<div className="sudoku">
<div>Current grid : {this.state.gridNum}</div>
<Submit1Input inputName="Grid" onSubmit={this.handleSubmit.bind(this)} />
<div>Difficulty levels: easy (1-20), medium (21-40), hard (41-60), very-hard (61-80), insane (81-100)</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">
......@@ -393,22 +417,10 @@ class Grid extends React.Component<IGridProps, IGridState> {
/**
* 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];
}
/**
......
/**
* MIT License
*
* Copyright (c) 2020, Concordant and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import React from 'react';
interface ISubmit1InputProps {
onSubmit: any,
inputName: string
}
interface ISubmit1InputState {
value: string
}
class Submit1Input extends React.Component<ISubmit1InputProps, ISubmit1InputState> {
constructor(props: any) {
super(props);
this.state = {value: "1"};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
/**
* onChange event handler
* @param event handled
*/
handleChange(event: any) {
this.setState({value: event.target.value});
}
handleSubmit(event: any) {
event.preventDefault();
if (Number(this.state.value) >= 1 && Number(this.state.value) <= 100) {
this.props.onSubmit(this.state.value);
}
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
{this.props.inputName} :
<input type="number" value={this.state.value} onChange={this.handleChange}
min="1" max="100"/>
</label>
<input type="submit" value={"Change " + this.props.inputName} />
</form>
);
}
}
export default Submit1Input
export const validInput = new RegExp('^[1-9]$');
/**
* Grids generated using sudokutoolcollection.
* https://www.npmjs.com/package/sudokutoolcollection
*/
export const GRIDS : {[key: string]: string} = {
"1": "75941832646893275113267548957418693...63.48.582.75.1..687...5.33.15672..2..8.3617",
"2": ".87..51..14.8.75...5..1.8.72715..384469378215538142679.24953761.15684.23396721458",
"3": ".816593747453129686934781529325614878749236155167.4293.2..9..363.9.4..21...2.....",
"4": ".75.2..6.19.78.23..42.96...463912587729658.4.51847362925783149693.567812681249...",
"5": "......265.52.86739..7.5.1849.35.8472275934816.....2593741695328586321947329847651",
"6": "2548..1691396254878.64915324852..9169..5468733671892455..76.39.69.31.75...3...621",
"7": ".9573241.27184965348.61592.754.281361325648799683715425.61.7.94.1749...5...2....1",
"8": "4251968377693..54181375...235.86.419194235786678941.2524.5....39876132.453.4....8",
"9": "714628..5569431..2238975..16.75.4.131.57.3..6493162..8846359127351247..9972816..4",
"10": "57921648324.537169..348..2...1.62..4452371698..6.45.12..71582468146239..625794831",
"11": ".36.5.918.9..6.35.51..3946.375.2.6.984159627362937.1.5283645791954.1.836.67983524",
"12": ".58426..13.9715.681623984..6279541835...826798.163.5424732698152.58437......71324",
"13": "84631.27932179.658597...41376.9231.513.547896.5486.73248.279561.7915.38...5438...",
"14": "4681327..251947368379568412843721...597486123612395874.3..592.118.27....92..1...7",
"15": "129..6485638594127745821936.51.6837..76.538.1.8341765.594182763.176.52...62...51.",
"16": "9145276836.5.3821923861974..462958318513.4.62.29..6.5746.951...1.784.5.65.37.2194",
"17": "49.317.6513.659.74...4281932891645..3.4.7298.57.983.2....295348.23741659945836712",
"18": "485263197631978.25972154...71...29..86.59127.529.47.812.87195..357.26819196..5742",
"19": ".5682391729315.64887.49.53213.76.28.78....46.64..8..7.924678.5.568312794317945826",
"20": "86479251321.548796..7613.42..217....7..38..2.4..259..7378961254649825371125437968",
"21": "15927..43.27.3..193.6....72.3248.7.1..53172.6.71.2943.71385.9.4568...3272.47631..",
"22": "1457362..2869513.737982..16861.73.2579428563.5236198744...6....6...9.4..9...4....",
"23": ".12..589..34....51.85....2.471962.8539..51.74258..316.52763.9188.9..75361635..742",
"24": "756..321949.5728632839165741...976.86....19.7.79...3.19...6.73.3671.948....73.196",
"25": "1976853245384..9672647.....7495..6..8569..142312864795921358476...2........1..2..",
"26": "83.176925.....4781791..5463.576.38.9.63...5.74.97.1236.4..1769291..6.374672...1.8",
"27": ".49.31725.57.92.4.6124.79387849...5..6.5.4.7.5.178.3941..345..9.3....48.496278513",
"28": "9.3786.45......96365143982.37.542681568917432412.6.7592...7..9.1..69.27..9....31.",
"29": "..58271......314757..54.82.5...7.34.6493..7183271845...5.4.8937874293651...7.5284",
"30": "23.6958..79.8.2.5.8564719..3785.9...9123.85..46521..89.4978.2.5623...7985879.6...",
"31": "819735...364129.8....846139576294813931568...482317956..3.71..8.98.5...11...8....",
"32": ".8..316....1..68.3..68.71.4..2..8.17817..236536.715.28175683.49..8174536643..9781",
"33": "45361987278253469.916782.3..3...6....4...73166..341.....5.63.47.6..78..3374295168",
"34": "83.6715....9.857342.5.498...16754283527813496..3962.57.4..9...8..14286.5.58....42",
"35": "3.748.5..46597....8213564..15286....9.41.76..6785392145867.314221.64.7..74.21....",
"36": "654.732..391526847278914.35529341786816.5.3...3.68....9.5..8...18.49756.7....5..8",
"37": ".1.....2..642..37973249.5...86.2.74547386.192125947.3.6475.29..25...9...391684257",
"38": ".63.5.792594.268317..983.4.987162...1.58.4.6924653918.6..3.857.37.24.9164.......8",
"39": ".6.47.8.1.87.5..231528397467..5.368459..24.17.467...9.8..965172...241.3821..8.469",
"40": "52.7.6.839..3....63674815.9.3956.24..469.7835158...967.731....469583...24.267.398",
"41": "...6214.....549.3....783....3...5....2.8.43.9....378.....47218571435892.285196743",
"42": "..71963....1.3.....3..7...11..82.4.3...91...5.736541.23..2815..7..3.521951.749836",
"43": ".5...8..2.8.2.......2..68...186.....3248915...96....28149582...8.57.3291273169485",
"44": "8.3..54766957432817412.8........4...23.8.714..74..6...45..81.9...9572.141.74.....",
"45": ".3.195.4..75834....1..26...361289475.4.573.8.587461.23.239.8.1..5.6........3.2...",
"46": "1.96487.5..8.5........9.1.851.2893...9...458....5.....942815673..14..2597659238..",
"47": "375814269281.....7.9427.8..527149386418..792593..82.74.5..2.6.........9..........",
"48": "3654..2918742193659.13654.7...1......1.....5.489...17.536..1..92..6..51.14.95....",
"49": ".8574931..1.....5..9...5...1.29368.535.2..69.8695.4123....6253..2..5..6.536.9.28.",
"50": "3.9.6..8.6......7.41..5..6.1.6..5.9.2......5.5...7..1.9615437287..618935835927146",
"51": "..4.6.19.....4.58.1..75.634....8427.....3.94.41..7.8639.3.2.418.4..1.359..1493726",
"52": "7.1.5.984..81.4...3..9.71.694.63.578.3.8792.1.874..693....46.....6.93.1...3.18.69",
"53": ".6....4...7....6...1...69...2.45179664783915..9.627843.3.56.2.77.6.9.3.4.8...3569",
"54": "...28..5.5.976.42.76.5.319..483.571.12.8.6.3.97..2..6.29.........76...89836957.41",
"55": "4...2538...79.3..63.916.....3.47651.17.53.9...6.891473891...2...4.21985......86.1",
"56": "4.25.71.3951.43786.378..2..7.9.8..3..6..71.24..56.2.......2..59.7...4862..6.58417",
"57": "..9..7316..7...9546......82592371..8.63...29778429613597......13..7.54.94......73",
"58": "..2.46..9.5..39..2.96.217....5672...247983516.68415..7....9......9.6....623157984",
"59": "4162859377539612...293..1561928.65..6845...1.537...6....1......9.5..8.6.2.8......",
"60": ".69472.31.21385...543691...6571.8....1..47....845.6.1.4..819...1382.4......7..148",
"61": "...593..4....217..152.47..3..725.....8.714.....593......146.....9.18.....2.3794.1",
"62": "...17469272685941394.3..785...6...3....2...6...3....24...5...7........4.1..4...56",
"63": ".7.264391419..8672...719584...6...4....89..63...4.7.5......................976815",
"64": "......3..5.3..7.....7.......89...7.33..87.....75.2..89.5.23849..3.964..59427..836",
"65": "3..7654898.7.91.2.9.6.28..128..59.......348...6...7...1...7....6..84.......516..8",
"66": ".7.6............62..6.....9...47...89...2647...71.....7619.....5.4217386823564..7",
"67": "...........1......43.629.....98.2.45.....7.8.....35.296.4281.57.523....4.73594.61",
"68": "428.6.519....1.27...1825346....4...11.4.72..3.56138.2..1.29.83...................",
"69": "...549.8..84631...93.827....5.7.4......1...7....9...5.6..2.85....5416...4.837..62",
"70": ".1.49836596.723481.3..6.972.....78..3.....2.......274...6...1.7..3.765........6..",
"71": "1...4...7.....9..6......2.19172564838.....1.2234.7.5.9.4....9.8783...6.4......7.5",
"72": "643195782..1.....995..73461..5..1...3.....9161...3.....3..1.....1.......5..32.148",
"73": "7..84..9.1.8.2.467.......8.8..697....76...8.9.2..8.67..4...1.28.1...8345.8.....16",
"74": "83..6.....2.8..63..6.13..2.6.2...3..3..5...697..6.3..2...3...46573.46..1246.....3",
"75": "874219563.6.734.18....8..4.75.3...29......736.3.....85..7.23.9.6..1........8.....",
"76": "..9...3..2.8...1.............6...971.72..35464.567.832.231.....5....621...1.27453",
"77": "32....9..9..73.1...17.9634..69..7431....6.8........6...739...6.6..32.71.1..67...3",
"78": "...5........6..1.5..71..3.2475...6.12..4.68598963152....426......985..2....7..9..",
"79": "..3.41.9.9..3..418514......63.....4.7954...6....6....9.86.349.735.8.6124.........",
"80": "...9.4.....7...4..4.52.7..9.4.87.6..5..3.27.87896..23....4289....85..1....47...82",
"81": "..2.7.4..84........1..49...4......1..2.4.3..6......24.57..........7218542..5.....",
"82": "2.9.8..4........9.....9.......92.1...9........2.7....9...2..957..2.79...9..56381.",
"83": "81..67....47.......5.....73.2....5...8........9........7..3..6.439126785.6.......",
"84": "8.2...7....6.789......3.8.6.1385...7....12...25.7...8.......37.3....7..........4.",
"85": "....51.2.2..97.......68217.8..7..64..9.2.8751........8.......1................367",
"86": "4.9......23...5.69.85....4..........694378215.2........1...4..7....134...........",
"87": ".4583......2..73..7...1....5967241..32..8.......35..6.6..273.....................",
"88": "...2.86.1..............1.......84597...6.92..8497253.....8.6...4..1........54....",
"89": "2184536794.69..32....2..41.....7...4......................4..........14...4..9.83",
"90": "..9..............4..........4.781692298...5.1.6.2..48395.83..1....1...........8..",
"91": ".1.3......................7..19.6..5.7.....9..8.......165..8...843..5...79263.854",
"92": ".78.2.1....5.........3.....9.....48..4.8........546......2..9......6....164937528",
"93": ".3..87915.7..15..25..3..4787.5.2....6.......7329....41.........................2.",
"94": "......7.2..7692.....2847...75......1.2.9.5.....6...3...7.............8.7...7.8263",
"95": "936.....55216.89.3487.3.1622.8..4...6.........7...2.......................5....3.",
"96": ".8........47........2..5....7...8...418.39.76235.179..86.92..3...3...............",
"97": "1........6.2......8.......23........265379418479...2537.61.25....................",
"98": "....7549.5.9..6.78.7...956..........2.......4417..263.....9.........8..9...5....6",
"99": ".....2.3.....3..4............5628.1...43798.5...154......4.3.....9...3.1683......",
"100": "...8..9..........1....562..8..5.172..2.7..58....24819..13.82..94................."
}
\ No newline at end of file
......@@ -18,7 +18,7 @@
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react"
"jsx": "react-jsx"
},
"include": [
"src"
......
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