Class: MazeSolver::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/maze_solver/element.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, position, type) ⇒ Element

Returns a new instance of Element.



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/maze_solver/element.rb', line 22

def initialize(value, position, type)
  @value = value
  @position = position
  @type = type

  ## Default Astar values
  @g = 0
  @h = 0
  @f = 0
  @parent = nil
end

Instance Attribute Details

#fObject

Represent an element in the Maze, defined by his position and value There are also some Astar factors like g, h, f functions Also the precedent element, (parent) visited is a flag that aknowledge that the element had been visited



8
9
10
# File 'lib/maze_solver/element.rb', line 8

def f
  @f
end

#gObject

Represent an element in the Maze, defined by his position and value There are also some Astar factors like g, h, f functions Also the precedent element, (parent) visited is a flag that aknowledge that the element had been visited



8
9
10
# File 'lib/maze_solver/element.rb', line 8

def g
  @g
end

#hObject

Represent an element in the Maze, defined by his position and value There are also some Astar factors like g, h, f functions Also the precedent element, (parent) visited is a flag that aknowledge that the element had been visited



8
9
10
# File 'lib/maze_solver/element.rb', line 8

def h
  @h
end

#parentObject

Represent an element in the Maze, defined by his position and value There are also some Astar factors like g, h, f functions Also the precedent element, (parent) visited is a flag that aknowledge that the element had been visited



8
9
10
# File 'lib/maze_solver/element.rb', line 8

def parent
  @parent
end

#positionObject (readonly)

Returns the value of attribute position.



9
10
11
# File 'lib/maze_solver/element.rb', line 9

def position
  @position
end

#typeObject (readonly)

Returns the value of attribute type.



9
10
11
# File 'lib/maze_solver/element.rb', line 9

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



9
10
11
# File 'lib/maze_solver/element.rb', line 9

def value
  @value
end

Instance Method Details

#==(element) ⇒ Object



16
17
18
19
20
# File 'lib/maze_solver/element.rb', line 16

def ==(element)
  ## Compare two elements by its position
  return true if ( @position == element.position && @value == element.value && @type == element.type )
  return false
end

#is_goal?Boolean

Returns:

  • (Boolean)


34
35
36
37
38
# File 'lib/maze_solver/element.rb', line 34

def is_goal?
  ## is the end point?
  return true if  @type == 'GOAL' 
  false
end

#is_path?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
# File 'lib/maze_solver/element.rb', line 40

def is_path?
  ## Can be traversed?
  return true if ( @type == 'PATH' ||  @type == 'START' || @type == 'GOAL' )
  false
end

#to_sObject



11
12
13
14
# File 'lib/maze_solver/element.rb', line 11

def to_s
  ## Represent the class
  "(#{@position}), #{@value}"
end