GithubHelp home page GithubHelp logo

ertyumpx / hashiwokakero Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 210 KB

Generator and solver algorithm repertoire for the logic puzzle Hashiwokakero.

License: GNU General Public License v3.0

Python 97.87% Makefile 2.13%
cpp puzzle puzzle-generator puzzle-solver pygame python sdl2

hashiwokakero's Introduction

Hashiwokakero Puzzle

Generator and solver algorithm repertoire for the Japanese logic puzzle Hashiwokakero - aka Hashi - created by Nikoli.

The project is still being worked on, check issues and milestones for the process.

License

This project is licensed under the GNU GPL-3.0 license.

Main purpose is to provide a free and open-source software for puzzle enthusiasts. Feel free to use the source code. Referring to the repository would be very much appreciated.

Setup

The project is written in Python 3.11.6, although should work on any Python Interpreter above 3.5.x.

Apart from built-in packages, only third party package is PyGame 2.5.2 with SDL 2.28.2.

For easy setup, create a python virtual environment and run:

make init

Mass puzzle generation scrips are currently being worked on, see ISSUE#3.

You can easily clone or fork the project, see below to find how to use source scripts.

Puzzle

Simplyfied rules from Wikipedia.

Played in rectangular grid. Encircled cells are islands numbers from 1 to 8 inclusive. The rest of the cells are empty.

The goal is to connect all of the islands by drawing a series of bridges between the islands.

The bridges must follow certain criteria:

  • All islands must be connected.
  • Bridged cannot cross.
  • Bridges are only established orthogonally, never diagonally.
  • At most two bridges connect a pair of islands.
  • An island must hold bridges that mathces it's own number.

Plot-twist is that there may not be only 1 solution. Sometimes the structure of the board allows to have 2 different ways to go and still fulfill all the rules.

General Structure

Terminology

Generator

Full Grid: A grid that satisfies given w and h dimensions and has no empty edges.

Cycle Step: Iteration count of the generator algorithm. Since generation is random, it may not reach that iteration count. Still is important if the user wants to stop generating at some point

Max Puzzle: A puzzle which cannot be extended anymore, which has no node that could potentially have another bridge in an empty direction.

Solver

Completed Grid: A grid that is completely solved.

Solving by Rules: Solving the puzzle by using the rules of the game, without any brute force method.

Solving by Brute Force: Solving the puzzle by trying all possible combinations of bridges. This method is used after solving by rules method is tried but the puzzle is still not completed. This occures because of the nature of Hashi, sometimes there could be more than 1 way to go and the rules are not enough to solve the puzzle.

Generator Algorithm

Will be written soon...

Solver Algorithm

Will be written soon...

Difficulty Precision and Mass Generation

Will be written soon...

hashiwokakero's People

Contributors

erthium avatar

Stargazers

 avatar

Watchers

 avatar

hashiwokakero's Issues

Brute Force Solver Algorithm

As it was said in the puzzle rules, there may be more than 1 solutions and puzzle could be still solvable. When the current solver with essential rules applied encounters such a situation, it remains as is.

We can create a second algorithm, that takes the output from current solver, and makes a random move and passes back to the current solver. If the output is not valid, brute force algorithm goes back and makes another move.

This gives room for the solver algorithms to find all possible outcomes as well.

To check if the output is invalid, two information is required:

  • if there is any island that has max_out < needed
  • if the islands create a non-continues connections

First one is quite easy to check. For the second one, we need to create a function which takes a grid and checks if everything is continues. This function would require a fully solved grid, meaning there is no island that has needed != 0

Mass Production

We need to make a script for creating thousands of puzzles and categorise them according to the cycle count of the solver algorithm.

This script could get input of:

  • grid width
  • grid height
  • puzzle count - t
  • difficulty

Task parallelisation might help here, collecting "Generate - Solve - Categorise" steps into one function and use multiprocessing on them t times.

Unit Tests for Solver and Generator Algorithms

While enhancing and advancing the main algorithms, it is essential to make sure that everything works on the track.

We need to create unit tests for both generator, making sure every need is fulfilled, and for solver, to be sure solved and generated solution are the same.

Impossible Bridge Build Rule

For the solver, general rules are applied and working. I'm not exactly sure if it's necessary, but there could be one more rule added to increase the speed and accuracy.

Also it's important that the solver works as much human as possible, so that the iteration count can show the similar difficulty for humans.

Here is the suggested rule:

And island that has count of 2, cannot send double bridge to another island that has count of 2.
And island that has count of 1, cannot send any bridge to another island that has count of 1.

Create an Interactive Visualiser

Create a cross-platform window, where the user can solve the puzzle, check if the solution is correct or if there is any wrong bridges.

One thing is, we cannot directly force 1 solution to be true, since there might be more than 1 solutions. We should check give the current grid to the solver and see if it can be solved from there.

[DISCUSSION] Should We Use Island Amount for Mapping

Currently we are mapping min & max step count with puzzle grid geometry, with width and height.

We might want to map the step count with island amount instead of geometry; since a grid with 50x50 geometry may at least have 4 islands and step count even be 1 in this case. Using island amount might help us more here.

Determining Difficulty While Generating

The first thing comes to mind is always the solver algorithm step count for deciding general difficulty of a puzzle.

From years of personal experience with Hashi, there are other options for determining the difficulty of the puzzle before creating it.

Which are:

  1. Having the island count (1-8) lower as possible: Higher island counts create ease for user since 8 has directly gives itself away, 7 cannot have any other bridge be on its way etc.
  2. Having the bridge lengths as short as possible: Longer bridges cut of the way of other islands and give them shorter direction options. Therefore lower bridges help the user way less and makes the puzzle more difficult.

To apply these, We need to create a scale for difficulty, add it as an input to the generator.

While creating random island counts and bridge lengths, according to the difficulty, longer bridges will have higher or lower percentage to be selected.

For island counts, as the difficulty is higher, generator algorithm will tend to create newer islands instead of extending the current ones.

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.