Skip to content

Instantly share code, notes, and snippets.

@filipevarjao
Created May 4, 2012 19:10
Show Gist options
  • Save filipevarjao/2597085 to your computer and use it in GitHub Desktop.
Save filipevarjao/2597085 to your computer and use it in GitHub Desktop.
Before:
positions = []
if ship_name == :battleship
positions = [ [ x_position, y_position ], [ x_position + 1 * horizontal, y_position + 1 * vertical ],
[ x_position + 2 * horizontal, y_position + 2 * vertical ], [ x_position + 3 * horizontal, y_position + 3 * vertical ] ]
elsif ship_name == :carrier
positions = [ [ x_position, y_position ], [ x_position + 1 * horizontal, y_position + 1 * vertical ],
[ x_position + 2 * horizontal, y_position + 2 * vertical ] ]
else
positions = [ [ x_position, y_position ], [ x_position + 1 * horizontal, y_position + 1 * vertical ] ]
end
ship_positions[player_name][ship_name] = positions
After:
# Esse método iterate simplifica e otimiza o código
def iterate(number, x, y, horizontal, vertical)
@positions = []
@positions.push([x, y])
(1..number).each do |p|
@positions.push([ x + (p * horizontal), y + (p * vertical) ])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment