Class: TTT::Squares

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Squares

collection_of_squares is a multi-dimensional array



7
8
9
# File 'lib/tictactoe/squares.rb', line 7

def initialize(args)
  @collection_of_squares = args[:collection_of_squares]
end

Instance Attribute Details

#collection_of_squaresObject (readonly)

Returns the value of attribute collection_of_squares.



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

def collection_of_squares
  @collection_of_squares
end

Instance Method Details

#any_combination_won?Boolean

Returns:

  • (Boolean)


48
49
50
51
52
# File 'lib/tictactoe/squares.rb', line 48

def any_combination_won?
  possible_winning_combinations.any? do |possible_winning_combination|
    won?(possible_winning_combination)
  end
end

#available_choicesObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tictactoe/squares.rb', line 29

def available_choices
  available_choices = Array.new
  collection_of_squares.each do |row|
    row.each do |square|
      if square.empty?
        available_choices.push(square.display_value)
      end
    end
  end
  available_choices
end

#display_valuesObject



21
22
23
24
25
26
27
# File 'lib/tictactoe/squares.rb', line 21

def display_values
  collection_of_squares.map do |row|
    row.map do |square|
      square.display_value
    end
  end
end

#full?Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
# File 'lib/tictactoe/squares.rb', line 41

def full?
  collection_of_squares.each do |row|
    return false if row.all? { |square| square.full? } == false
  end
  return true
end

#number_of_rows_colsObject



60
61
62
# File 'lib/tictactoe/squares.rb', line 60

def number_of_rows_cols
  collection_of_squares.length
end

#retrieve_square(display_value) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/tictactoe/squares.rb', line 11

def retrieve_square(display_value)
  row = (display_value - 1) / number_of_rows_cols
  col = (display_value - 1) % number_of_rows_cols
  if row >= number_of_rows_cols || col >= number_of_rows_cols
    return nil
  else
    return collection_of_squares[row][col]
  end
end

#won?(array) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
# File 'lib/tictactoe/squares.rb', line 54

def won?(array)
  #https://stackoverflow.com/questions/3233278/how-do-i-test-if-all-items-in-an-array-are-identical
  return false if array[0].value == nil
  array.all? {|square| square.value == array[0].value}
end