Skip to content

Instantly share code, notes, and snippets.

@Whatapalaver
Created May 29, 2019 15:39
Show Gist options
  • Save Whatapalaver/a9ea8fc1e6a749dbb5e1fa09d872c219 to your computer and use it in GitHub Desktop.
Save Whatapalaver/a9ea8fc1e6a749dbb5e1fa09d872c219 to your computer and use it in GitHub Desktop.
class Grains
LOCATIONS = (1..64)
MULTIPLIER = 2
def self.square(location)
new(location).square
end
def self.total
new(LOCATIONS.min).total
end
private
def initialize(location)
unless valid?(location)
raise ArgumentError, "BoardLocationError: Must be between #{LOCATIONS.min} and #{LOCATIONS.max}"
end
@location = location
end
def valid?(location)
LOCATIONS.include?(location)
end
def position_total(index)
MULTIPLIER.pow(index)
end
public
def square
position_total(@location - 1)
end
def total
position_total(LOCATIONS.max) - 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment