Class: VeryGood::State

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width:, height:, state: nil, cursor: Cursor.new(0, 0)) ⇒ State

Returns a new instance of State.



3
4
5
6
7
8
# File 'lib/very_good/state.rb', line 3

def initialize(width:, height:, state: nil, cursor: Cursor.new(0, 0))
  @width = width
  @height = height
  @state = state || clean_state
  @cursor = cursor
end

Instance Attribute Details

#cursorObject (readonly)

Returns the value of attribute cursor.



10
11
12
# File 'lib/very_good/state.rb', line 10

def cursor
  @cursor
end

#heightObject (readonly)

Returns the value of attribute height.



10
11
12
# File 'lib/very_good/state.rb', line 10

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



10
11
12
# File 'lib/very_good/state.rb', line 10

def width
  @width
end

Instance Method Details

#linesObject



36
37
38
# File 'lib/very_good/state.rb', line 36

def lines
  @state
end

#set(x, y, cell) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/very_good/state.rb', line 12

def set(x, y, cell)
  new_state = @state.map do |line|
    line.dup
  end

  new_state[y][x] = cell

  State.new(
    width: width,
    height: height,
    state: new_state,
    cursor: cursor
  )
end

#set_cursor(new_cursor) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/very_good/state.rb', line 27

def set_cursor(new_cursor)
  State.new(
    width: width,
    height: height,
    state: @state,
    cursor: new_cursor
  )
end