Class: ConnectFour::Board

Inherits:
Object
  • Object
show all
Includes:
Display
Defined in:
lib/connect_four/board.rb

Overview

Board for ConnectFour game

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Display

#board_string, #display_board, #redraw

Constructor Details

#initializeBoard

Returns a new instance of Board.



14
15
16
# File 'lib/connect_four/board.rb', line 14

def initialize
  @data = (1..7).to_h { |i| [i, Column.new] }
end

Instance Attribute Details

#dataInteger=> Column (readonly)

Note:

There are 7 columns with max height of 6 in a Board.

Returns column_number => Column , whole board data.

Returns:

  • (Integer=> Column)

    column_number => Column , whole board data



12
13
14
# File 'lib/connect_four/board.rb', line 12

def data
  @data
end

Instance Method Details

#four_connected?Boolean

returns true if four connected peices vertically,horizontally or diagonally found else false

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
# File 'lib/connect_four/board.rb', line 37

def four_connected?
  # if a line is found in one direction this won't check others.
  lines = (vertical_lines + horizontal_lines + left_diagonal_lines + right_diagonal_lines)

  lines.each do |line|
    return true if four_in_line?(line)
  end
  false
end

#four_in_line?(line) ⇒ Boolean

Returns Boolean returns true if four consecutive non nil values found in a line else false.

Parameters:

  • Array

    a line

Returns:

  • (Boolean)

    Boolean returns true if four consecutive non nil values found in a line else false



49
50
51
52
53
54
55
56
57
58
# File 'lib/connect_four/board.rb', line 49

def four_in_line?(line)
  result = false
  line.each_cons(4) do |elements|
    next if elements.include?(nil)

    first_element = elements.first
    result = true if elements.all?(first_element)
  end
  result
end

#horizontal_linesObject

rubocop:disable Metrics/MethodLength



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/connect_four/board.rb', line 69

def horizontal_lines # rubocop:disable Metrics/MethodLength
  arrays = []
  5.downto(0) do |index|
    array = []
    @data.each_value do |elem|
      node = elem.at(index)
      data = node.data unless node.nil?
      array << data # appens nil if there's no node for storing space between
    end
    arrays << array unless array.length < 4
  end
  arrays
end

#insert_at(column_number, peice) ⇒ void

This method returns an undefined value.

Parameters:

  • (Integer, String)


22
23
24
25
# File 'lib/connect_four/board.rb', line 22

def insert_at(column_number, peice)
  list = @data[column_number]
  list.append(peice) unless list.full?
end

#left_diagonal_linesObject

rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/connect_four/board.rb', line 83

def left_diagonal_lines # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  # for readability disable some style
  arrays = []
  (3..5).each do |index|
    array = []
    @data.each_value do |list|
      break if index.negative?

      node = list.at(index)
      data = node.data unless node.nil?
      array << data
      index -= 1
    end
    arrays << array
  end
  (0..2).each do |index|
    array = []
    @data.each_value.reverse_each do |list|
      break if index > 5

      node = list.at(index)
      data = node.data unless node.nil?
      array << data
      index += 1
    end
    arrays << array
  end
  arrays
end

#right_diagonal_linesObject

rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/connect_four/board.rb', line 113

def right_diagonal_lines # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  # for readability disable some style
  arrays = []
  (0..2).each do |index|
    array = []
    @data.each_value do |list|
      break if index > 5

      node = list.at(index)
      data = node.data unless node.nil?
      array << data
      index += 1
    end
    arrays << array
  end
  (3..5).each do |index|
    array = []
    @data.each_value.reverse_each do |list|
      break if index.negative?

      node = list.at(index)
      data = node.data unless node.nil?
      array << data
      index -= 1
    end
    arrays << array
  end
  arrays
end

#space_left?(column_number) ⇒ Boolean

Returns Boolean returns true if column is not Column#full? else false.

Parameters:

  • Integer

    column_number for the column on the Board #data

Returns:

  • (Boolean)

    Boolean returns true if column is not Column#full? else false



29
30
31
32
# File 'lib/connect_four/board.rb', line 29

def space_left?(column_number)
  column = @data[column_number]
  !column&.full?
end

#vertical_linesObject



60
61
62
63
64
65
66
67
# File 'lib/connect_four/board.rb', line 60

def vertical_lines
  # due to LinkedList nature there will be no nil as space in between the values
  # in vertical_lines because i am only appending non nil values.
  lists = @data.values.filter { |e| e.size > 3 }
  lists.map do |list|
    list.map { |node| node.data }
  end
end