Last active
August 29, 2015 14:14
-
-
Save chendrix/95474995f6cef714d3ad to your computer and use it in GitHub Desktop.
Multiple constructors
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 Minefield | |
def initialize(map) | |
@map = map | |
end | |
def self.initialize_with_random_mines(size: , mine_count:) | |
map = Array.new(size) { Array.new(size) { Cell.new } } | |
mine_count.times do |_| | |
row = rand(size) | |
col = rand(size) | |
unless map[row][col].has_mine? | |
map[row][col].place_mine | |
end | |
end | |
self.new(map) | |
end | |
private | |
attr_reader :map | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment