Class: SlippyTilesScorer::MaxSquare

Inherits:
Object
  • Object
show all
Defined in:
lib/slippy_tiles_scorer/max_square.rb

Overview

Finds the maximum fully-filled square in a collection of x/y tiles.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tiles_x_y: Set.new) ⇒ MaxSquare

Returns a new instance of MaxSquare.



10
11
12
# File 'lib/slippy_tiles_scorer/max_square.rb', line 10

def initialize(tiles_x_y: Set.new)
  self.tiles_x_y = tiles_x_y
end

Instance Attribute Details

#tiles_x_yObject

Returns the value of attribute tiles_x_y.



8
9
10
# File 'lib/slippy_tiles_scorer/max_square.rb', line 8

def tiles_x_y
  @tiles_x_y
end

Instance Method Details

#max_square(x:, y:) ⇒ Integer

Returns the size of the largest fully-filled square whose top-left tile is x/y.

Parameters:

  • x (Integer)

    X coordinate of the top-left tile.

  • y (Integer)

    Y coordinate of the top-left tile.

Returns:

  • (Integer)

    Square size, or 0 if the tile does not exist.



64
65
66
67
68
69
70
# File 'lib/slippy_tiles_scorer/max_square.rb', line 64

def max_square(x:, y:)
  row = square_scores[y]

  return 0 unless row

  row[x] || 0
end

#max_square_result(min_size: 3) ⇒ Hash

Returns Maximum size and its top-left tile coordinates.

Parameters:

  • min_size (Integer) (defaults to: 3)

    Minimum square size to report.

Returns:

  • (Hash)

    Maximum size and its top-left tile coordinates.

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/slippy_tiles_scorer/max_square.rb', line 32

def max_square_result(min_size: 3) # rubocop:disable Metrics/MethodLength
  raise ArgumentError, "min_size must be 2 or greater" if min_size < 2

  max_size = 0
  top_left_tiles = Set.new

  square_scores.each do |y, row|
    row.each do |x, size|
      next if size < min_size

      if size > max_size
        max_size = size
        top_left_tiles.clear
        top_left_tiles << [x, y]
      elsif size == max_size
        top_left_tiles << [x, y]
      end
    end
  end

  {
    size: max_size,
    top_left_tile_x_y: top_left_tiles
  }
end

#max_squares(min_size: 3) ⇒ Hash

Finds all largest fully-filled squares.

Parameters:

  • min_size (Integer) (defaults to: 3)

    Minimum square size to report.

Returns:

  • (Hash)

    Maximum size and its top-left tile coordinates.



23
24
25
26
27
28
# File 'lib/slippy_tiles_scorer/max_square.rb', line 23

def max_squares(min_size: 3)
  # Rebuild for every scoring run because tiles_x_y may be a mutable Set.
  @square_scores = nil

  max_square_result(min_size: min_size)
end

#steps_fulfilled?(x:, y:, steps:) ⇒ Boolean

Reports whether the square starting at x/y can grow beyond steps.

For example:

steps_fulfilled?(x: 0, y: 0, steps: 2)

is true when a fully-filled square of at least 3x3 exists there.

Parameters:

  • x (Integer)

    X coordinate of the top-left tile.

  • y (Integer)

    Y coordinate of the top-left tile.

  • steps (Integer)

    Current square size.

Returns:

  • (Boolean)


83
84
85
# File 'lib/slippy_tiles_scorer/max_square.rb', line 83

def steps_fulfilled?(x:, y:, steps:)
  max_square(x: x, y: y) > steps
end