Module: GameOfGithubLife

Defined in:
lib/game_of_github_life.rb,
lib/game_of_github_life/game.rb,
lib/game_of_github_life/version.rb,
lib/game_of_github_life/git_executor.rb,
lib/game_of_github_life/date_calculator.rb

Defined Under Namespace

Classes: DateCalculator, Game, GitExecutor

Constant Summary collapse

NUMBER_OF_COLUMNS =

first and last columns could include another year, so skip it

DateCalculator::NUMBER_OF_COLUMNS - 2
NUMBER_OF_ROWS =
DateCalculator::DAYS_IN_WEEK
START_FIELD =

.….….….….….….….….….….….…… .….….….….….….….….….….….…… .….….….….….….….….….….….…… 000000000000000000000000000000000000000000000000000 .….….….….….….….….….….….…… .….….….….….….….….….….….…… .….….….….….….….….….….….……

[
    ([0] * NUMBER_OF_COLUMNS).freeze,
    ([0] * NUMBER_OF_COLUMNS).freeze,
    ([0] * NUMBER_OF_COLUMNS).freeze,
    ([1] * NUMBER_OF_COLUMNS).freeze,
    ([0] * NUMBER_OF_COLUMNS).freeze,
    ([0] * NUMBER_OF_COLUMNS).freeze,
    ([0] * NUMBER_OF_COLUMNS).freeze,
].freeze
START_YEAR =

1969 days are invalid dates for git

1970
VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.commit_field(executor, field, year) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/game_of_github_life.rb', line 53

def commit_field(executor, field, year)
  puts(year)
  ppp(field)
  iterate_alive_cells(field) do |x, y|
    date = DateCalculator.date_by_cell(x, y + 1, year: year)
    executor.commit(date, "#{year}:#{x}:#{y}")
  end
end

.iterate_alive_cells(field) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/game_of_github_life.rb', line 62

def iterate_alive_cells(field)
  field.each_with_index.map do |row, x|
    row.each_with_index.map do |value, y|
      yield x, y if value == 1
    end
  end
end

.play(path, end_year) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/game_of_github_life.rb', line 35

def play(path, end_year)
  end_year ||= Time.now.year
  game = Game.new(START_FIELD)
  executor = GitExecutor.new(path)
  year = START_YEAR

  commit_field(executor, START_FIELD, START_YEAR)

  while year < end_year && !game.ended?
    field = game.next
    year += 1
    commit_field(executor, field, year)
  end

  executor.push
  puts('done')
end

.ppp(field) ⇒ Object



70
71
72
73
74
# File 'lib/game_of_github_life.rb', line 70

def ppp(field)
  puts('---')
  puts(field.map { |row| row.map { |cell| cell == 1 ? '#' : ' ' }.join('') })
  puts('---')
end