Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Harold Gao committed Dec 21, 2020
1 parent 63ba71b commit 0800500
Show file tree
Hide file tree
Showing 22 changed files with 11,689 additions and 2 deletions.
20 changes: 20 additions & 0 deletions .codesandbox/workspace.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"responsive-preview": {
"Mobile": [
320,
675
],
"Tablet": [
1024,
765
],
"Desktop": [
1400,
800
],
"Desktop HD": [
1920,
1080
]
}
}
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"semi": true,
"singleQuote": true
}
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
# sudoku-solver
Created with CodeSandbox
# Sudoku Solver

Tags: `React` `JavaScript`

We provide a solver to give you the answer of the sudoku game.

1. Valid: Check if the input puzzle is valid. If not, hightlight the wrong row, column or block.
2. Solve: After enter part of valid puzzle, give the solution to the sudoku puzzle.
3. History: Auto save recent 10 puzzle and its solution.

## Acknowledgement

Solver Algorithm is inspired by LeetCode problems:[36 Valid Sudoku](https://leetcode.com/problems/valid-sudoku), [37 Sudoku Solver](https://leetcode.com/problems/sudoku-solver).

Our sudoku game board interface is inspired by [Web Sudoku](https://www.websudoku.com/?register).
55 changes: 55 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"name": "sudoku",
"version": "0.1.0",
"private": true,
"dependencies": {
"@reduxjs/toolkit": "1.5.0",
"@types/classnames": "^2.2.9",
"@types/jest": "24.0.18",
"@types/node": "12.7.2",
"@types/parse-link-header": "^1.0.0",
"@types/react": "16.9.2",
"@types/react-dom": "16.9.0",
"@types/react-paginate": "^6.2.1",
"@types/react-redux": "7.1.11",
"@types/webpack-env": "^1.14.0",
"axios": "^0.19.0",
"classnames": "^2.2.6",
"parse-link-header": "^1.0.1",
"react": "^16.9.0",
"react-dom": "^16.9.0",
"react-markdown": "^4.1.0",
"react-paginate": "^6.3.0",
"react-redux": "7.2.2",
"react-scripts": "^3.2.0",
"redux-thunk": "2.3.0",
"typescript": "3.5.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"format": "prettier --write \"src/**/*\""
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"prettier": "^1.18.2"
},
"keywords": [],
"description": "RTK Advanced Tutorial: Initial plain React app"
}
1 change: 1 addition & 0 deletions public/favicon.ico
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��i���k����$r����n�;?�� ��nn+,��ũ����m�ݽ�姶�yխ�wN5�ݵ����7wM������j���x�
43 changes: 43 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
2 changes: 2 additions & 0 deletions public/logo192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions public/logo512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
2 changes: 2 additions & 0 deletions public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
64 changes: 64 additions & 0 deletions src/app/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
.App {
max-width: 768px;
margin: 0 auto;
padding: 1rem;
}

.board-row:after {
clear: both;
content: '';
display: table;
}

.square {
font-family: 'lucida handwriting', 'comic sans ms', sans-serif;
color: #7777dd;
font-size: 22px;
background: #fff;
border: #ddddee solid 1px;
float: left;
line-height: 34px;
height: 34px;
margin-right: -1px;
margin-top: -1px;
padding: 0;
text-align: center;
width: 34px;
}

.square:focus {
outline: none;
}

.square.left-line {
border-left: #666699 solid 2px;
}
.square.top-line {
border-top: #666699 solid 2px;
}
.square.bottom-line {
border-bottom: #666699 solid 2px;
}
.square.right-line {
border-right: #666699 solid 2px;
}

.square.invalid-one {
background-color: #ffbfbf;
}
.square.invalid-two {
background-color: #ff7f7f;
}
.square.invalid-three {
background-color: #ff3f3f;
}

.invalid-hint {
color: #ff0000;
}

input:read-only {
font-family: times, 'times new roman', serif;
color: #000000;
font-size: 25px;
}
109 changes: 109 additions & 0 deletions src/app/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import React, { useState } from 'react';
import './App.css';
import Board from 'features/board/Board';
import { isValidSudoku, solveSudoku, InvalidInfo } from 'utils/Sudoku';

const emptyBoard = function (): string[][] {
return Array(9)
.fill(null)
.map((ele) => Array(9).fill(''));
};
const emptySolvedMark = function (): boolean[][] {
return Array(9)
.fill(null)
.map((ele) => Array(9).fill(false));
};
const testBoard: string[][] = [
['1', '5', '4', '7', '2', '6', '8', '9', '3'],
['7', '9', '8', '1', '5', '3', '6', '2', '4'],
['6', '2', '3', '4', '9', '8', '1', '7', '5'],
['5', '6', '1', '3', '4', '7', '2', '8', '9'],
['4', '7', '2', '8', '6', '9', '3', '5', '1'],
['3', '8', '9', '5', '1', '2', '7', '4', '6'],
['8', '4', '5', '2', '3', '1', '9', '6', '7'],
['2', '3', '6', '9', '7', '4', '5', '1', '8'],
['9', '1', '7', '6', '8', '5', '4', '3', '2']
];

function isNumberValid(val: string): boolean {
if (val.length === 1) {
return !Number.isNaN(Number(val));
}
if (val.length === 0) {
return true;
}
return false;
}

const App: React.FC = () => {
let [input, setInput] = useState(emptyBoard);
let [board, setBoard] = useState(testBoard);
let [checkResult, setCheckResult] = useState('');
let [invalidInfo, setInvalid] = useState<InvalidInfo | null>(null);
let [solvedMarks, setSolvedMark] = useState(emptySolvedMark);
function onTextChange(row: number, col: number, newVal: string) {
if (isNumberValid(newVal)) {
let newInput = input.map((arr) => arr.slice());
newInput[row][col] = newVal;
setInput(newInput);
setBoard(newInput);
}
}

function checkIsValid(): boolean {
const VALID_HINT = 'Valid input:)';
const INVALID_HINT = 'You have made some mistakes, highlighted in red!';
const invalidInfo = isValidSudoku(board);
setCheckResult(invalidInfo ? INVALID_HINT : VALID_HINT);
setInvalid(invalidInfo);
return invalidInfo === null;
}

function reset() {
setCheckResult('');
setInvalid(null);
setInput(emptyBoard);
setBoard(emptyBoard);
setSolvedMark(emptySolvedMark);
}

function solve() {
if (checkIsValid()) {
let newSolvedMark = solvedMarks.map((arr) => arr.slice());
for (let i = 0; i < 9; i++) {
for (let j = 0; j < 9; j++) {
newSolvedMark[i][j] = input[i][j] === '';
}
}
let newOutput = solveSudoku(board);
if (newOutput === null) {
setCheckResult('Sorry! I have no solution!');
return;
}
setBoard(newOutput);
setSolvedMark(newSolvedMark);
}
}

let hintClass = 'hint-txt';
if (invalidInfo) {
hintClass += ' invalid-hint';
}
let content = (
<>
<Board
squares={board}
solvedMarks={solvedMarks}
onTextChange={onTextChange}
invalidInfo={invalidInfo}
/>
<button onClick={() => checkIsValid()}>Check is valid</button>
<p className={hintClass}>{checkResult}</p>
<button onClick={() => reset()}>Reset</button>
<button onClick={() => solve()}>Solve</button>
</>
);
return <div className="App">{content}</div>;
};

export default App;
1 change: 1 addition & 0 deletions src/app/store.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { createStore } from '@reduxjs/toolkit'
Loading

0 comments on commit 0800500

Please sign in to comment.