Created
May 29, 2019 15:39
-
-
Save Whatapalaver/a9ea8fc1e6a749dbb5e1fa09d872c219 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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