<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users1923</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/1923" rel="self"/>
  <title>Alex Baranosky</title>
  <updated>Thu Feb 11 02:04:06 -0800 2010</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor440794</id>
    <published>2010-02-11T02:04:06-08:00</published>
    <title>[Ruby] On Symplify few loops</title>
    <content type="html">&lt;p&gt;Mine is using Ruby 1.9...  I refactored all the code even the stuff loading the bmp from the file.  I was not able to test any of that code however so hopefully it is bug free, the spec I show below covers generating the corrected bmp.&lt;/p&gt;

&lt;pre&gt;class BmpCorrector
  def load_bmp(name)
    bmp, original_bmp_data_array = create_bmp_from_file(name)
    bmp.generate_corrected(original_bmp_data_array)
  end

  def create_bmp_from_file(name)
    file_name = &amp;quot;Media/#{name}&amp;quot;
    fail unless FileTest.exist?(file_name)
    bmp = nil

    original_bmp_data_array = open(file_name, 'rb') do |file|
      width, height = read_width_and_height(file)
      bmp = Bmp.new(height, width)
      read_data(height, width, file)
    end

    return bmp, original_bmp_data_array
  end

  def read_width_and_height(file)
    file.seek(18, IO::SEEK_CUR)
    [file.read(4).unpack('i')[0], file.read(4).unpack('i')[0]]
  end

  def read_data(height, width, file)
    file.seek(28, IO::SEEK_CUR)
    size = width * height * Bmp::BGR_PIXEL_SLICE_WIDTH
    file.read(size).unpack('C*')
  end

end

class Bmp
  BGR_PIXEL_SLICE_WIDTH = 3

  def initialize(height, width)
    @original_height, @original_width = height, width
    @new_dimension = closest_power_of_two
  end

  def generate_corrected(raw_bgr_array)
    right_padding  = @new_dimension - @original_width
    bottom_padding = @new_dimension - @original_height

    pixels = pixels_from_raw_bgr_data(raw_bgr_array)
    pixel_rows_w_out_padding = pixel_2d_array(pixels)
    add_padding(pixel_rows_w_out_padding, bottom_padding, right_padding)
  end

  def pixels_from_raw_bgr_data(raw_bgr_array)
    raw_bgr_array.each_slice(BGR_PIXEL_SLICE_WIDTH).
            collect { |slice| Pixel.from_bgr(*slice) }.
            reverse
  end

  def pixel_2d_array(pixels)
    pixels.each_slice(@original_width).inject([]) { |result, row| result &amp;lt;&amp;lt; row }
  end

  def add_padding(pixel_rows, bottom_padding, right_padding)
    corrected_array = pixel_rows.collect {|row| row + Pixel.blanks(right_padding) }
    corrected_array += Pixel.blanks(@new_dimension * bottom_padding)
    corrected_array.flatten.collect(&amp;amp;:to_a).flatten
  end

  def closest_power_of_two
    result  = 2
    result *= 2 while (result &amp;lt; @original_height || result &amp;lt; @original_width)
    result
  end
end

class Pixel

  def initialize(red = 0, green = 0, blue = 0, alpha = 0)
    @red, @green, @blue, @alpha = red, green, blue, alpha
  end

  def self.from_bgr(blue, green, red)
    Pixel.new(red, green, blue, 255)
  end

  def self.blanks(amount = 1)
    Array.new(amount) { Pixel.new }
  end

  def to_a
    [@red, @green, @blue, @alpha]
  end

end

BmpCorrector.new.load_bmp(&amp;quot;filename.....&amp;quot;)

## Spec File

require &amp;quot;spec&amp;quot;
require 'corrector'

describe Bmp do
  width = 2
  height = 3

  data = [
          255, 0, 0, # 1st pixel, blue (third row)
          255, 0, 0, # 2st pixel, blue

          0, 255, 0, # 3nd pixel, green (second row)
          0, 255, 0, # 4nd pixel, green

          0, 0, 255, # 5rd pixel, red (first row)
          0, 0, 255, # 6rd pixel, red
  ]

  expected_corrected = [
          255, 0, 0, 255, # 1st pixel, red (first row)
          255, 0, 0, 255, # 2st pixel, red
          0, 0, 0, 0, # Empty
          0, 0, 0, 0, # Empty

          0, 255, 0, 255, # 3nd pixel, green (second row)
          0, 255, 0, 255, # 4nd pixel, green
          0, 0, 0, 0, # Empty
          0, 0, 0, 0, # Empty

          0, 0, 255, 255, # 5rd pixel, blue (third row)
          0, 0, 255, 255, # 6rd pixel, blue
          0, 0, 0, 0, # Empty
          0, 0, 0, 0, # Empty

          0, 0, 0, 0, # Empty (last row)
          0, 0, 0, 0, # Empty
          0, 0, 0, 0, # Empty
          0, 0, 0, 0  # Empty
  ]

  it &amp;quot;should correct the bmp properly&amp;quot; do
    actual_corrected = Bmp.new(height, width).generate_corrected(data)
    actual_corrected.should == expected_corrected
  end
end
&lt;/pre&gt;</content>
    <author>
      <name>Alex Baranosky</name>
      <email>abaranosky@hotmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1163-symplify-few-loops/refactors/440794" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor438350</id>
    <published>2010-02-08T07:37:40-08:00</published>
    <title>[Ruby] On Looking for other/better solutions to this Greed Dice game</title>
    <content type="html">&lt;p&gt;3rd and final refactoring:&lt;/p&gt;

&lt;pre&gt;class GreedGame
  THREE_OF_A_KIND_POINT_MULTIPLIER = 100
  POINTS_FOR_ONE = 100
  POINTS_FOR_FIVE = 50
  THREE_ONES_BONUS_POINTS = 900

  def score_the_dice(dice = [])
    raise Exception('can only enter 5 dice at most') if dice.length &amp;gt; 5

    dice_tally = tally_dice(dice)

    @score = 0
    score_three_of_a_kind(dice_tally)
    score_ones_special_case(dice_tally[1])
    score_fives_special_case(dice_tally[5])
    @score
  end

  private

  def tally_dice(dice)
    dice_tally = { 1 =&amp;gt; 0, 2 =&amp;gt; 0, 3 =&amp;gt; 0, 4 =&amp;gt; 0, 5 =&amp;gt; 0, 6 =&amp;gt; 0 }
    dice.each_with_object(dice_tally) do |die_value, tally|
      tally[die_value] += 1
    end
  end

  def score_three_of_a_kind(dice_tally)
    (1..6).each do |die_value|
      @score += die_value * THREE_OF_A_KIND_POINT_MULTIPLIER if dice_tally[die_value] &amp;gt;= 3
    end
  end

  def score_ones_special_case(ones_rolled)
    @score += ones_rolled &amp;gt;= 3 ? THREE_ONES_BONUS_POINTS : 0
    score_special_case(ones_rolled, POINTS_FOR_ONE)
  end

  def score_fives_special_case(fives_rolled)
    score_special_case(fives_rolled, POINTS_FOR_FIVE)
  end

  def score_special_case(num_rolled, points_for_die)
    @score +=
            if num_rolled &amp;lt; 3
              num_rolled * points_for_die
            elsif num_rolled &amp;gt; 3
              extra_rolls(num_rolled) * points_for_die
            else
              0
            end
  end

  def extra_rolls(times_rolled)
    times_rolled - 3
  end

end&lt;/pre&gt;</content>
    <author>
      <name>Alex Baranosky</name>
      <email>abaranosky@hotmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1165-looking-for-other-better-solutions-to-this-greed-dice-game/refactors/438350" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor438346</id>
    <published>2010-02-08T06:57:04-08:00</published>
    <title>[Ruby] On Looking for other/better solutions to this Greed Dice game</title>
    <content type="html">&lt;p&gt;Here's my second iteration of refactoring :)  &lt;/p&gt;

&lt;pre&gt;class GreedGame
  SET_OF_THREE_POINT_MULTIPLIER = 100
  POINTS_FOR_ONE = 100
  POINTS_FOR_FIVE = 50
  THREE_ONES_BONUS_POINTS = 900

  def score_the_dice(dice = [])
    raise Exception('can only enter 5 dice at most') if dice.length &amp;gt; 5
    return 0 if dice == []

    score = 0
    dice_tally = tally_dice(dice)
    score += score_ones_through_sixes(dice_tally)
    score += score_ones_special_case(dice_tally[1])
    score += score_fives_special_case(dice_tally[5])
    score
  end

  private

  def tally_dice(dice)
    dice_tally = { 1 =&amp;gt; 0, 2 =&amp;gt; 0, 3 =&amp;gt; 0, 4 =&amp;gt; 0, 5 =&amp;gt; 0, 6 =&amp;gt; 0 }
    dice.each_with_object(dice_tally) do |die_value, tally|
      tally[die_value] += 1
    end
  end

  def score_ones_through_sixes(dice_tally)
    (1..6).each { |num| return num * SET_OF_THREE_POINT_MULTIPLIER if dice_tally[num] &amp;gt;= 3 }
    0
  end

  def score_ones_special_case(ones_rolled)
    score = ones_rolled &amp;gt;= 3 ? THREE_ONES_BONUS_POINTS : 0
    score += score_special_case(ones_rolled, POINTS_FOR_ONE)
  end

  def score_fives_special_case(fives_rolled)
    score_special_case(fives_rolled, POINTS_FOR_FIVE)
  end

  def score_special_case(num_rolled, points_for_die)
    if num_rolled &amp;lt; 3
      num_rolled * points_for_die
    elsif num_rolled &amp;gt; 3
      extra_rolls(num_rolled) * points_for_die
    else
      0
    end
  end

  def extra_rolls(times_rolled)
    times_rolled - 3
  end

end


&lt;/pre&gt;</content>
    <author>
      <name>Alex Baranosky</name>
      <email>abaranosky@hotmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1165-looking-for-other-better-solutions-to-this-greed-dice-game/refactors/438346" rel="alternate"/>
  </entry>
</feed>

