55 Life/ruby/life.rb
# Iterate from one generation to the next, returning true if the
# game of life should continue, and false if it should terminate.
Iterate from one generation to the next, returning true if the
game of life should continue, and false if it should terminate.
Iterate from one generation to the next returning true if the game of life should continue and false if it should terminate .
def iterate
    @generation = @generation + 1
    return false if @generation > @max_generations
    @counter.map! { |row| row.map! { |cell| cell >= 2 ? cell-2 : cell } }
    @population = 0
    @counter.each_index do |rownum|
      @counter[rownum].each_index do |colnum|
        cell_value = @counter[rownum][colnum]
        @invalid = true if cell_value > 0 && (
          rownum == 0 || rownum == PATTERN_HEIGHT-1 || colnum == 0 || colnum == PATTERN_WIDTH-1
        )
        neighbors = @counter[rownum-1..rownum+1].map { |row| row[colnum-1..colnum+1] }.flatten
        neighbor_count = neighbors.inject(0) do |sum, value|
          sum += (value == 1 || value == 2) ? 1 : 0
        end
        neighbor_count = neighbor_count - cell_value
        if cell_value == 0
          if neighbor_count == 3
            cell_value = 3
            @population = @population + 1
          end
        elsif neighbor_count < 2 || neighbor_count > 3
          cell_value = 2
        else
          @population = @population + 1
        end
        @counter[rownum][colnum] = cell_value
      end
    end
    @population > 0
  end
def iterate @generation = @generation + 1 return false if @generation > @max_generations @counter . map! { | row | row . map! { | cell | cell >= 2 ? cell - 2 : cell } } @population = 0 @counter . each_index do | rownum | @counter [ rownum ] . each_index do | colnum | cell_value = @counter [ rownum ] [ colnum ] @invalid = true if cell_value > 0 && ( rownum == 0 || rownum == PATTERN_HEIGHT - 1 || colnum == 0 || colnum == PATTERN_WIDTH - 1 ) neighbors = @counter [ rownum - 1 .. rownum + 1 ] . map { | row | row [ colnum - 1 .. colnum + 1 ] } . flatten neighbor_count = neighbors . inject ( 0 ) do | sum , value | sum += ( value == 1 || value == 2 ) ? 1 : 0 end neighbor_count = neighbor_count - cell_value if cell_value == 0 if neighbor_count == 3 cell_value = 3 @population = @population + 1 end elsif neighbor_count < 2 || neighbor_count > 3 cell_value = 2 else @population = @population + 1 end @counter [ rownum ] [ colnum ] = cell_value end end @population > 0 end
Iterate from one generation to the next, returning true if the
game of life should continue, and false if it should terminate.
Iterate from one generation to the next returning true if the game of life should continue and false if it should terminate .
# Update the counter array with new values. # First, change each 2 (dying) to a 0 (dead) # and each 3 (born) to a 1 (alive) # Now for each cell, count its neighbors and update it # If any cell on the border is set, our small algorithm is not # smart enough to correctly check its neighbors, so sadly our # pattern becomes invalid. We keep going though # Count the cell's neighbors (not including itself) # Update this cell based on its neighbor count, either leaving it # as a 0 or 1, or setting it to 2 (dying) or 3 (being born) # If every cell is dead, we are done. Otherwise, keep going up to the # maximum number of generations