Class: BingoBoardGenerator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(word_list, grid_size = 4) ⇒ BingoBoardGenerator

Returns a new instance of BingoBoardGenerator.



7
8
9
10
11
# File 'lib/bingo_board_generator.rb', line 7

def initialize(word_list, grid_size = 4)
  @grid_size = grid_size
  @word_list = word_list # REQ: word_list.count >= grid_size**2
  @box_length = @word_list.max_by(&:length).length + 2
end

Instance Attribute Details

#box_lengthObject (readonly)

Returns the value of attribute box_length.



3
4
5
# File 'lib/bingo_board_generator.rb', line 3

def box_length
  @box_length
end

#grid_sizeObject (readonly)

Returns the value of attribute grid_size.



3
4
5
# File 'lib/bingo_board_generator.rb', line 3

def grid_size
  @grid_size
end

#word_listObject (readonly)

Returns the value of attribute word_list.



3
4
5
# File 'lib/bingo_board_generator.rb', line 3

def word_list
  @word_list
end

Instance Method Details

#randomize_board_wordsObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/bingo_board_generator.rb', line 13

def randomize_board_words
  shuffled = @word_list.shuffle # Randomize word_list
  sliced = shuffled.slice(0, @grid_size.to_i * @grid_size.to_i - 1) # Slice a range from 0 to # squares to fill minus 1 (space for free square).

  if @grid_size.odd?
    sliced.insert((sliced.length / 2), 'free') # Odd size grid has a center spot
  else
    sliced << 'free'
    sliced.shuffle # Even size grid has no center, so place 'free' randomly
  end
end