Class: Rbfunge::Program
- Inherits:
-
Object
- Object
- Rbfunge::Program
- Defined in:
- lib/rbfunge/program.rb
Instance Attribute Summary collapse
-
#direction ⇒ Object
Returns the value of attribute direction.
Instance Method Summary collapse
- #get(x, y) ⇒ Object
- #get_current ⇒ Object
-
#initialize ⇒ Program
constructor
A new instance of Program.
- #load_code(code) ⇒ Object
- #move ⇒ Object
- #pad_line(line) ⇒ Object
- #put(x, y, value) ⇒ Object
Constructor Details
#initialize ⇒ Program
Returns a new instance of Program.
6 7 8 9 10 11 |
# File 'lib/rbfunge/program.rb', line 6 def initialize @program = [] @current_x = 0 @current_y = 0 @direction = :right end |
Instance Attribute Details
#direction ⇒ Object
Returns the value of attribute direction.
4 5 6 |
# File 'lib/rbfunge/program.rb', line 4 def direction @direction end |
Instance Method Details
#get(x, y) ⇒ Object
45 46 47 |
# File 'lib/rbfunge/program.rb', line 45 def get(x, y) @program[y][x] end |
#get_current ⇒ Object
49 50 51 |
# File 'lib/rbfunge/program.rb', line 49 def get_current get(@current_x, @current_y) end |
#load_code(code) ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/rbfunge/program.rb', line 13 def load_code(code) line_array = code.split("\n") 25.times do line = line_array.shift line = pad_line(line) byte_array = [] line.each_byte { |b| byte_array << b} @program << byte_array end end |
#move ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rbfunge/program.rb', line 32 def move case @direction when :right @current_x = (@current_x + 1) % 80 when :left @current_x = (@current_x - 1) % 80 when :up @current_y = (@current_y - 1) % 25 when :down @current_y = (@current_y + 1) % 25 end end |
#pad_line(line) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/rbfunge/program.rb', line 24 def pad_line(line) if line.nil? padded_line = "".ljust(80) else padded_line = line[0..79].ljust(80) end end |
#put(x, y, value) ⇒ Object
53 54 55 |
# File 'lib/rbfunge/program.rb', line 53 def put(x, y, value) @program[x][y] = value end |