Sha256: 540ed8567dcca3f19f942e6a4a80027eaed5128b7f475bcdb1e6b5bb12173b7c

Contents?: true

Size: 1.65 KB

Versions: 108

Compression:

Stored size: 1.65 KB

Contents

class Board
  VALID_BORDERS = ['+', '-', '|']
  VALID_DATA = ['*', '|', '+', '-', ' ']

  def self.transform(input)
    new(input).transform
  end

  def initialize(raw_board)
    @rows = raw_board
    validate
  end

  def transform
    rows.map.with_index do |row, i|
      decorate_row(row, i)
    end
  end

  def mine?(char)
    char == '*'
  end

  private

  attr_reader :rows

  def decorate_row(row, i)
    inner = []
    row.each_char.with_index do |space, j|
      if space != ' '
        inner << space
      else
        surroundings = surroundings(row, i, j)
        k = count_mines_nearby(surroundings)
        if k > 0
          inner << k.to_s
        else
          inner << space
        end
      end
    end
    inner.join
  end

  def surroundings(row, i, j)
    [
      row[j - 1], row[j + 1], rows[i - 1][j - 1],
      rows[i - 1][j], rows[i - 1][j + 1],
      rows[i + 1][j - 1], rows[i + 1][j], rows[i + 1][j + 1]
    ]
  end

  def count_mines_nearby(surroundings)
    surroundings.count { |datum| mine?(datum) }
  end

  def validate
    validate_size
    validate_borders
    validate_data
  end

  def validate_size
    len = rows.first.length
    if rows.any? { |row| row.length != len }
      fail ArgumentError, 'Invalid board'
    end
  end

  def validate_borders
    [rows[0], rows[-1]].each do |row|
      invalid = row.chars.any? do |char|
        !VALID_BORDERS.include?(char)
      end
      fail ArgumentError, 'Invalid board' if invalid
    end
  end

  def validate_data
    rows.each do |row|
      invalid = row.chars.any? do |char|
        !VALID_DATA.include?(char)
      end
      fail ArgumentError, 'Invalid board' if invalid
    end
  end
end

Version data entries

108 entries across 108 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/ruby/exercises/minesweeper/.meta/solutions/minesweeper.rb
trackler-2.2.1.179 tracks/ruby/exercises/minesweeper/.meta/solutions/minesweeper.rb
trackler-2.2.1.178 tracks/ruby/exercises/minesweeper/.meta/solutions/minesweeper.rb
trackler-2.2.1.177 tracks/ruby/exercises/minesweeper/.meta/solutions/minesweeper.rb
trackler-2.2.1.176 tracks/ruby/exercises/minesweeper/.meta/solutions/minesweeper.rb
trackler-2.2.1.175 tracks/ruby/exercises/minesweeper/.meta/solutions/minesweeper.rb
trackler-2.2.1.174 tracks/ruby/exercises/minesweeper/.meta/solutions/minesweeper.rb
trackler-2.2.1.173 tracks/ruby/exercises/minesweeper/.meta/solutions/minesweeper.rb
trackler-2.2.1.172 tracks/ruby/exercises/minesweeper/.meta/solutions/minesweeper.rb
trackler-2.2.1.171 tracks/ruby/exercises/minesweeper/.meta/solutions/minesweeper.rb
trackler-2.2.1.170 tracks/ruby/exercises/minesweeper/.meta/solutions/minesweeper.rb
trackler-2.2.1.169 tracks/ruby/exercises/minesweeper/.meta/solutions/minesweeper.rb
trackler-2.2.1.167 tracks/ruby/exercises/minesweeper/.meta/solutions/minesweeper.rb
trackler-2.2.1.166 tracks/ruby/exercises/minesweeper/.meta/solutions/minesweeper.rb
trackler-2.2.1.165 tracks/ruby/exercises/minesweeper/.meta/solutions/minesweeper.rb
trackler-2.2.1.164 tracks/ruby/exercises/minesweeper/.meta/solutions/minesweeper.rb
trackler-2.2.1.163 tracks/ruby/exercises/minesweeper/.meta/solutions/minesweeper.rb
trackler-2.2.1.162 tracks/ruby/exercises/minesweeper/.meta/solutions/minesweeper.rb
trackler-2.2.1.161 tracks/ruby/exercises/minesweeper/.meta/solutions/minesweeper.rb
trackler-2.2.1.160 tracks/ruby/exercises/minesweeper/.meta/solutions/minesweeper.rb