Class: Tile
- Inherits:
-
Object
show all
- Defined in:
- lib/tile.rb
Defined Under Namespace
Classes: Large, Medium, Small
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(rows, cols) ⇒ Tile
Returns a new instance of Tile.
47
48
49
|
# File 'lib/tile.rb', line 47
def initialize(rows, cols)
@rows, @cols = rows, cols
end
|
Instance Attribute Details
#col ⇒ Object
Returns the value of attribute col.
3
4
5
|
# File 'lib/tile.rb', line 3
def col
@col
end
|
#cols ⇒ Object
Returns the value of attribute cols.
4
5
6
|
# File 'lib/tile.rb', line 4
def cols
@cols
end
|
#mosaic ⇒ Object
Returns the value of attribute mosaic.
2
3
4
|
# File 'lib/tile.rb', line 2
def mosaic
@mosaic
end
|
#row ⇒ Object
Returns the value of attribute row.
3
4
5
|
# File 'lib/tile.rb', line 3
def row
@row
end
|
#rows ⇒ Object
Returns the value of attribute rows.
4
5
6
|
# File 'lib/tile.rb', line 4
def rows
@rows
end
|
Class Method Details
.area(tiles) ⇒ Object
43
44
45
|
# File 'lib/tile.rb', line 43
def self.area(tiles)
tiles.inject(0) {|sum, tile| sum + tile.area}
end
|
.rand(max_area = nil) ⇒ Object
28
29
30
31
32
33
|
# File 'lib/tile.rb', line 28
def self.rand(max_area=nil)
tiles = [Large, Medium, Small].map(&:new).select do |tile|
max_area.nil? || tile.area <= max_area
end
tiles[Kernel.rand(tiles.size)]
end
|
.rand_covering(area) ⇒ Object
35
36
37
38
39
40
41
|
# File 'lib/tile.rb', line 35
def self.rand_covering(area)
tiles = []
while self.area(tiles) < area
tiles << Tile.rand(area - self.area(tiles))
end
tiles
end
|
Instance Method Details
#==(other) ⇒ Object
51
52
53
54
|
# File 'lib/tile.rb', line 51
def ==(other)
return false unless other.is_a?(Tile)
self.rows == other.rows && self.cols == other.cols
end
|
#area ⇒ Object
56
57
58
|
# File 'lib/tile.rb', line 56
def area
@rows * @cols
end
|
#place!(mosaic, row, col) ⇒ Object
60
61
62
|
# File 'lib/tile.rb', line 60
def place!(mosaic, row, col)
@mosaic, @row, @col = mosaic, row, col
end
|
#shatter ⇒ Object
64
65
66
|
# File 'lib/tile.rb', line 64
def shatter
Tile.rand_covering(area)
end
|