Created
February 29, 2012 14:43
-
-
Save semmons99/1941274 to your computer and use it in GitHub Desktop.
minecraft crafting data modeling exercise
This file contains 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 Recipe | |
attr_reader :recipe | |
def initialize(recipe) | |
@recipe = recipe | |
standardize | |
end | |
private | |
# There's no error checking for a recipe composed of nothing :empty cells | |
def standardize | |
rotate_cols while first_col_empty? | |
rotate_rows while first_row_empty? | |
end | |
def rotate_cols | |
@recipe.each &:rotate! | |
end | |
def rotate_rows | |
@recipe[0], @recipe[1], @recipe[2] = @recipe[1], @recipe[2], @recipe[0] | |
end | |
def first_col_empty? | |
@recipe.all?{|r| r[0] == :empty} | |
end | |
def first_row_empty? | |
@recipe[0].all?{|c| c == :empty} | |
end | |
end | |
standardized_input = [ | |
[:coal, :empty, :empty, ], | |
[:stick, :empty, :empty, ], | |
[:empty, :empty, :empty, ], | |
] | |
inputs = [ | |
[ | |
[:coal, :empty, :empty, ], | |
[:stick, :empty, :empty, ], | |
[:empty, :empty, :empty, ], | |
], | |
[ | |
[:empty, :empty, :empty, ], | |
[:coal, :empty, :empty, ], | |
[:stick, :empty, :empty, ], | |
], | |
[ | |
[:empty, :coal, :empty, ], | |
[:empty, :stick, :empty, ], | |
[:empty, :empty, :empty, ], | |
], | |
[ | |
[:empty, :empty, :empty, ], | |
[:empty, :coal, :empty, ], | |
[:empty, :stick, :empty, ], | |
], | |
[ | |
[:empty, :empty, :coal, ], | |
[:empty, :empty, :stick, ], | |
[:empty, :empty, :empty, ], | |
], | |
[ | |
[:empty, :empty, :empty, ], | |
[:empty, :empty, :coal, ], | |
[:empty, :empty, :stick, ], | |
], | |
] | |
inputs.each do |input| | |
recipe = Recipe.new(input) | |
puts recipe.recipe == standardized_input | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment