From: Morton Goldberg Date: 2007-09-13T06:59:44+09:00 Subject: Re: Filling individual cells in a grid On Sep 12, 2007, at 5:36 PM, Morton Goldberg wrote: > > n = 3 > n.times do > grid.each_with_remaining do |cell, other| > cn, on = neighbors(cell), neighbors(other) > before_score = score(cell.cell_type, cn) + > score(other.cell_type, on) > after_score = score(other.cell_type, cn) + > score(cell.cell_type, on) > if after_score <= before_score > cell.cell_type, other.cell_type = other.cell_type, > cell.cell_type > end > end > grid.print_field_values > puts > end > I realized I was doing something silly in the above code -- I was scoring two cells even if they were of the same type. It runs noticeably faster if I don't do that. n = 3 n.times do grid.each_with_remaining do |cell, other| next if cell.cell_type == other.cell_type # <-- added this line cn, on = neighbors(cell), neighbors(other) before_score = score(cell.cell_type, cn) + score (other.cell_type, on) after_score = score(other.cell_type, cn) + score (cell.cell_type, on) if after_score <= before_score cell.cell_type, other.cell_type = other.cell_type, cell.cell_type end end grid.print_field_values puts end Regards, Morton