Class: Rbfunge::Program

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProgram

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

#directionObject

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_currentObject



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

#moveObject



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