Class: GameOfLife::Outputters::SimpleStringOutputter

Inherits:
Object
  • Object
show all
Defined in:
lib/game_of_life/outputters/simple_string_outputter.rb

Overview

Renders a board as a simple string, that can be used to inspect it from, say a test

Instance Method Summary collapse

Instance Method Details

#render(board) ⇒ String

Renders the given board as a simple string. Cells are delimited by spaces and rows by newlnes A live cell is marked ‘X’ and a dead cell with a ‘-’

Parameters:

Returns:

  • (String)

    the board rendered as a string



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/game_of_life/outputters/simple_string_outputter.rb', line 13

def render(board)
  output = ""
  board.each_row do |row|
    row.each do |cell|
      output << simplified_state(cell.state)
      output << " "
    end
    output.strip!
    output << "\n"
  end
  output.chomp
end

#simplified_state(state) ⇒ Object (private)



27
28
29
30
31
32
33
34
# File 'lib/game_of_life/outputters/simple_string_outputter.rb', line 27

def simplified_state(state)
  case state
  when :live
    'X'
  when :dead
    '-'
  end
end