GithubHelp home page GithubHelp logo

bertoort / sugoku Goto Github PK

View Code? Open in Web Editor NEW
122.0 3.0 39.0 197 KB

Sudoku puzzle solver and generator

Home Page: https://sugoku.onrender.com/

Go 74.35% JavaScript 9.74% CSS 5.84% HTML 10.02% Procfile 0.04%
puzzle go

sugoku's Introduction

suGOku

https://sugoku.onrender.com/

Go Challenge 8 solution for the month of November 2015.

*The API is hosted on a Render free tier instance. The server gets spun down with inactivity, expect a delay of 50 secs or more for the initial request. Then it's โšก

Overview

Sudoku web app: solves, generates, grades, and validates sudoku puzzles.

The algorithm implements two solving functions:

  1. QuickFill - Called so because it quickly checks horizontally, vertically and in the nine grid box for possible options. If only one possible solution remains, it adds the value to the square.

  2. Guess - It fills an empty square with a possible, non-conflicting value. If the puzzle is solved (completely filled), it validates to see if it correct. If not, it goes back and fills the square with another possible value. It recursively fills in and backtracks until the puzzle is complete.

A very interesting finding was that implementing the QuickFill function before each guess did not improve the speed of the algorithm, in fact, it slowed it down. I was not expecting it. I was so surprised that I decided to keep the original name I had given the function without Quickfill: SlowSolve().

SlowSolve is the faster function and therefore the one used for the API.

Installation

go get github.com/bertoort/sugoku

Technologies

API

Get

Board - returns a puzzle board

https://sugoku.onrender.com/board

Arguments -

  • Difficulty:
    • easy
    • medium
    • hard
    • random

Example:

https://sugoku.onrender.com/board?difficulty=easy

Post

Solve - returns the solved puzzle, along with difficulty and status

https://sugoku.onrender.com/solve

Grade - returns the difficulty of the puzzle

https://sugoku.onrender.com/grade

Validate - returns the status of the puzzle

https://sugoku.onrender.com/validate

NOTE:

The request does not support content-type of application/json. It must be application/x-www-form-urlencoded

To help, here is a quick and dirty encoding functions for a board:

const encodeBoard = (board) => board.reduce((result, row, i) => result + `%5B${encodeURIComponent(row)}%5D${i === board.length -1 ? '' : '%2C'}`, '')

const encodeParams = (params) =>
  Object.keys(params)
  .map(key => key + '=' + `%5B${encodeBoard(params[key])}%5D`)
  .join('&');

Here is an example sending a board:

const data = {board:[[0,0,0,0,0,0,8,0,0],[0,0,4,0,0,8,0,0,9],[0,7,0,0,0,0,0,0,5],[0,1,0,0,7,5,0,0,8],[0,5,6,0,9,1,3,0,0],[7,8,0,0,0,0,0,0,0],[0,2,0,0,0,0,0,0,0],[0,0,0,9,3,0,0,1,0],[0,0,5,7,0,0,4,0,3]]}

fetch('https://sugoku.onrender.com/solve', {
  method: 'POST',
  body: encodeParams(data),
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
})
  .then(response => response.json())
  .then(response => console.log(response.solution))
  .catch(console.warn)
jQuery Example:
var data = {
  board: "[[0,0,1,0,0,0,0,0,0],
          [2,0,0,0,0,0,0,7,0],
          [0,7,0,0,0,0,0,0,0],
          [1,0,0,4,0,6,0,0,7],
          [0,0,0,0,0,0,0,0,0],
          [0,0,0,0,1,2,5,4,6],
          [3,0,2,7,6,0,9,8,0],
          [0,6,4,9,0,3,0,0,1],
          [9,8,0,5,2,1,0,6,0]]"
}
$.post('https://sugoku.onrender.com/solve', data)
  .done(function (response) {

    <% response = {
      difficulty: "hard",
      solution: Array[9],
      status: "solved"
    } &>

  });```

For more, check out the [api.js](https://github.com/bertoort/sugoku/blob/master/public/api.js) file

sugoku's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

sugoku's Issues

Not true sudokus

The generation algorithm you are using isn't making true sudokus, after solving multiple puzzles I noticed that you can solve a couple of them in different ways, meaning they don't have unique solution which is a requirement of true sudokus. I love the API and was using it to make my own sudoku implementation as a challenge to myself, but till this produces true sudokus. I'll have to look to other web apis.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.