Solve 2D mazes in Ruby

By Jorge Félix Banguera V

  • Github: felixbanguera
  • Linkedin: Jorge Felix Banguera

Jorge Félix Banguera

What's a maze

1. a complicated system of paths or passages that people try to find their way through for entertainment


2. an area in which you can get easily lost because there are so many similar streets or passages

https://dictionary.cambridge.org

Representation of a maze

A maze can be represented in different ways, one is a simple 2D image of it.

 

Another can be a set of coordinates related to other coordinates for possibilities of movements. 

 

Other Representation of a maze

As a set of unique values randomly assigned to each position.

 

As a set of unique values assigned to positions with a purpose.

 

The one we will use

 

We will assign each position an index starting from 0, from left to right in columns and top to bottom in rows.

 

It can be compared at the end as a simple Array:

 

[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]

Parameters to be used

The Maze defined as a Hash, with each key being the position, and the values being the next possible positions:

 

Maze = {

  0 => [1, 5],

  ...

}

Parameters to be used

The START and FINISH points, in this case they will be:

 

START = 0

FINISH = 11

What is Backtracking

Backtracking is a general algorithm for finding all (or some) solutions to some computational problems, notably constraint satisfaction problems, that incrementally builds candidates to the solutions, and abandons a candidate ("backtracks") as soon as it determines that the candidate cannot possibly be completed to a valid solution

Wikipedia

Backtracking Algorithm (simple)

For every possible choice, take it, if solution is found, return it, otherwise search for new possible choices. Repeat until no more possible choices found and go back. Repeat.

Backtracking Algorithm (Recursive)

Use a recursive function to be called with initial params. Then it will call itself for every possible choice until it finds the solution or until there is no more choices to make.

What's recursion

 the practice of describing numbers, expressions, etc. in terms of the numbers, expressions, etc. that come before them in a series.

https://dictionary.cambridge.org

A recursive procedure or routine is one that has the ability to call itself until it detects that some condition has been satisfied​.

http://whatis.techtarget.com/

Why Recursion works

In a recursive algorithm, the computer "remembers" every previous state of the problem. This information is "held" by the computer on the "activation stack" (i.e., inside of each functions workspace).

Every function has its own workspace PER CALL of the function.

 

https://www.cs.utah.edu

A factorial example

In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n.

 

 

Wiki

A factorial example... in code

Codewars

let f = _ => _ < 2 ? 1 : _ * f(_ - 1)
def factorial(number)
    return 1 if number < 2
    return number * factorial(number - 1)
end

...expanded:

Let's go to the code...

based on Youtube's Channel:

V. Anton Spraul

 

https://github.com/felixbanguera/ruby/blob/master/scripts_and_exercises/Backtracking/1_backtracking_with_arrays.rb

https://github.com/felixbanguera/ruby/blob/master/scripts_and_exercises/Backtracking/2_backtracking_with_list.rb

https://github.com/felixbanguera/ruby/blob/master/scripts_and_exercises/Backtracking/3_recursive_backtracking.rb

Solve 2D Puzzles in Ruby

By Felix Xilef

Solve 2D Puzzles in Ruby

  • 76