Class: CodeMap

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

Overview

This class is reponsible for keeping the befunge code. It will output the current operation the pointer is at, and also set and get operations based on coordinates. CodeMap#print_map will be used for animation and debuggin modes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ CodeMap

Returns a new instance of CodeMap.



7
8
9
10
11
# File 'lib/code_map.rb', line 7

def initialize(file)
  @pointer = Pointer.new
  @map = File.open(file, 'r').readlines.map { |line| line.chomp.split '' }
  format_code_map
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



6
7
8
# File 'lib/code_map.rb', line 6

def height
  @height
end

#mapObject (readonly)

Returns the value of attribute map.



6
7
8
# File 'lib/code_map.rb', line 6

def map
  @map
end

#pointerObject (readonly)

Returns the value of attribute pointer.



6
7
8
# File 'lib/code_map.rb', line 6

def pointer
  @pointer
end

#widthObject (readonly)

Returns the value of attribute width.



6
7
8
# File 'lib/code_map.rb', line 6

def width
  @width
end

Instance Method Details

#get_operationObject



13
14
15
# File 'lib/code_map.rb', line 13

def get_operation
  @map[@pointer.y][@pointer.x]
end

#get_operation_at(x, y) ⇒ Object



21
22
23
# File 'lib/code_map.rb', line 21

def get_operation_at(x, y)
  @map[y][x]
end


25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/code_map.rb', line 25

def print_map
  system 'clear'
  @map.each_with_index do |line, i|
    line.each_with_index do |char, j|
      if @pointer.x == j && @pointer.y == i
        print char.colorize(background: :red)
      else
        print char
      end
    end

    print "\n"
  end
end

#set_operation_at(x, y, op) ⇒ Object



17
18
19
# File 'lib/code_map.rb', line 17

def set_operation_at(x, y, op)
  @map[y][x] = op
end