SaverioMonaco / Flowsolver

Automatic Flow puzzle solver and in-terminal game replica. Project for Metodi informatici per la Fisica 2019-2020

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

(EN) Automatic Flow Free puzzle solver written in C and terminal-based game

Project for Metodi informatici per la Fisica a.a. 2019/2020

What is Flow Free?

Flow Free is a popular puzzle game developed for Android and iOS.

The goal of this game is to connect all pair of dots (distinguished by colors) creating pipes in the given grid, for example:

Goal of the program

Unlike the popular game, where there exists only one solution for each puzzle, this program can find all existing solutions and display them.

How the program works

To find all solutions, the algorithm must be less holistic than other solutions given online, a rule to enumerate all possible paths must be found.

First of all, all pairs (distinguished by colors) must be numerically ordered. Then the program will find all paths for the first pair, at the very moment it finds the first path, the program will try to find all path for the second pair with the presence of the first path as an obstacle and so on. If no paths are found, the program will search for the next paths for the previous pair.

In the example before the puzzle is solved by the program in this order:

Of course the program didn't find the solution on the first try, for example a valid path for the yellow pair could have been:

But the blue pair won't find a solution, so the program will find other solutions for the yellow one.

The biggest problem to address now is how can one find all possible paths that connects a pair in a grid with the presence of obstacles (paths of other pairs)

Finding all paths for one pair

Deciding from which dot of the pair to start, the maximum possible movement from there are 4: up, down, left, right (that can not be valid if on the place is occupied by a path, a dot of a pair, or if the movement leads the path out of the grid), from there, the possible movement are 3 because going back is not possible.

One can imagine to deal with a Tree, so the best approach is the technique is probably Backtracking.

Setting an arbitrary hierarchy of direction (like, up>left>down>right), the backtracking would be:

  • If avaible, always go up
  • If up is not avaible, go left
  • If neither up or left are avaible, go down
  • If up, left and down are not possible, go right
  • If no movement is possible, go back and try the next direction

This way of visualising all possible way to find a path connecting two dots in a grid shows how easy it is to deal with an incredible high number of possibilities. For example, the program calculated the number of paths connecting two dots placed on the end of one diagonal in a squared grid for different size of the grid:

Side Number of paths
2 2
3 12
4 184
5 8512
6 1262816
7 575780564

Given the amount of paths the program has to deal with, it is essential to find ways to trim some Tree branches.

Trimming the tree

Consider the following case:

Although the movement is possible, it is clear that will not lead to a solution because the other two pairs will not have enough space to connect (there must be at least 2 spaces in that column to connect those 2 pairs)

The solution is simple, yet effective:

For each column and row the computer initializes an integer that is the number of empty cells in that column/row minus the number of pairs that that column/row separated. Those structures will be upgraded every time a move is about to be made or every time the program changes pair to work on, and if one of those number is less than 0, the move will not lead to a global solution.

For the example before, without this method the program will reach a solution in 3311 moves, if this method is used, in only 1037 moves.

This was tested on various puzzles and overall the duration of calculation is significantly lower. This was also tested on levels of the official app:

Size Pairs Duration of calculation (Trimming off) Duration of calculation (Trimming on)
9x9 10 8.27 1.06
9x9 9 35.91 1.20
9x9 8 465.73 7.42
9x9 7 5198.30 650.81

In-terminal game

Using the developed algorithm, it was possible to build a terminal version of the game, where the levels are randomly generated by the computer:
  • The user has to input how big the grid he wants it to be and how many pairs he wants to connect.
  • Then the computer initializes the grid and places the dots of the pairs randomly.
  • The computer then tries to find if there is a solution, if not he will again place the pairs randomly and tries to find a solution.
  • This goes on until he finds a possible puzzle, then the user can solve it.

About

Automatic Flow puzzle solver and in-terminal game replica. Project for Metodi informatici per la Fisica 2019-2020


Languages

Language:TeX 86.7%Language:C 12.2%Language:C++ 0.6%Language:Shell 0.3%Language:Python 0.2%