class GameGrid::Grid
Attributes
default[R]
grid[R]
height[R]
width[R]
Public Class Methods
new(args = {})
click to toggle source
# File lib/game_grid/grid.rb, line 14 def initialize(args = {}) @default = args[:default] @height = args[:height] || 3 @width = args[:width] || 3 @grid = build(width, height) populate if default end
Public Instance Methods
cell(x, y)
click to toggle source
# File lib/game_grid/grid.rb, line 22 def cell(x, y) select { |cell| cell.coords == [x, y] }[0] end
cells()
click to toggle source
# File lib/game_grid/grid.rb, line 26 def cells columns.flatten end
clear()
click to toggle source
# File lib/game_grid/grid.rb, line 30 def clear populate(nil) end
columns()
click to toggle source
# File lib/game_grid/grid.rb, line 34 def columns grid end
diagonals()
click to toggle source
# File lib/game_grid/grid.rb, line 38 def diagonals positive_diagonals + negative_diagonals end
negative_diagonals()
click to toggle source
# File lib/game_grid/grid.rb, line 52 def negative_diagonals diagonals = [] negative_diagonal_starting_points.each do |coordinate| diagonals << get_negative_diagonal(coordinate) end diagonals end
neighbors(cell: nil, coords: nil)
click to toggle source
# File lib/game_grid/grid.rb, line 62 def neighbors(cell: nil, coords: nil) raise ArgumentError unless cell || coords coordinates = cell ? cell.coords : coords neighbors_coordinates = neighbors_coordinates(coordinates) cells.select do |neighbor| neighbors_coordinates.include?(neighbor.coords) end end
populate(new_value = default)
click to toggle source
# File lib/game_grid/grid.rb, line 73 def populate(new_value = default) each do |cell| begin value = new_value.dup rescue TypeError value = new_value ensure cell.value = value end end end
positive_diagonals()
click to toggle source
# File lib/game_grid/grid.rb, line 42 def positive_diagonals diagonals = [] positive_diagonal_starting_points.each do |coordinate| diagonals << get_positive_diagonal(coordinate) end diagonals end
rows()
click to toggle source
# File lib/game_grid/grid.rb, line 85 def rows grid.transpose end
view()
click to toggle source
# File lib/game_grid/grid.rb, line 89 def view rows.map do |row| stringify_row(row) end.join("\n") end
Private Instance Methods
build(width, height)
click to toggle source
# File lib/game_grid/grid.rb, line 162 def build(width, height) grid = Array.new(width) { Array.new } cell_coordinates.each do |coords| grid[coords.x] << GameGrid::Cell.new(coords: coords) end grid end
cell_coordinates()
click to toggle source
# File lib/game_grid/grid.rb, line 170 def cell_coordinates cell_coordinates = [] x_range.each do |x_position| y_range.each do |y_position| cell_coordinates << Coords.new(x: x_position, y: y_position) end end cell_coordinates end
get_negative_diagonal(starting_coordinate)
click to toggle source
# File lib/game_grid/grid.rb, line 132 def get_negative_diagonal(starting_coordinate) diagonal = [] x_position = starting_coordinate.x y_position = starting_coordinate.y - 1 while y_position >= lower_limit break if x_position > right_hand_limit diagonal << cell(x_position, y_position) x_position += 1 y_position -= 1 end diagonal end
get_positive_diagonal(starting_coordinate)
click to toggle source
# File lib/game_grid/grid.rb, line 102 def get_positive_diagonal(starting_coordinate) diagonal = [] x_position = starting_coordinate.x y_position = starting_coordinate.y while y_position <= upper_limit break if x_position > right_hand_limit diagonal << cell(x_position, y_position) x_position += 1 y_position += 1 end diagonal end
left_hand_limit()
click to toggle source
# File lib/game_grid/grid.rb, line 187 def left_hand_limit 0 end
lower_limit()
click to toggle source
# File lib/game_grid/grid.rb, line 199 def lower_limit 0 end
negative_diagonal_starting_points()
click to toggle source
# File lib/game_grid/grid.rb, line 148 def negative_diagonal_starting_points diagonal_starting_points = [] ((lower_limit + 1)..height).each do |y| diagonal_starting_points << Coords.new(x: left_hand_limit, y: y) end ((left_hand_limit + 1)..right_hand_limit).each do |x| diagonal_starting_points << Coords.new(x: x, y: height) end diagonal_starting_points end
neighbors_coordinates(coordinates)
click to toggle source
# File lib/game_grid/grid.rb, line 180 def neighbors_coordinates(coordinates) x, y = coordinates [[(x - 1), (y - 1)], [(x - 1), y], [(x - 1), (y + 1)], [x, (y + 1)], [(x + 1), (y + 1)], [(x + 1), y], [(x + y), (y - 1)], [x, (y - 1)]] end
positive_diagonal_starting_points()
click to toggle source
# File lib/game_grid/grid.rb, line 118 def positive_diagonal_starting_points diagonal_starting_points = [] upper_limit.downto(lower_limit).each do |y| diagonal_starting_points << Coords.new(x: left_hand_limit, y: y) end ((left_hand_limit + 1)..right_hand_limit).each do |x| diagonal_starting_points << Coords.new(x: x, y: lower_limit) end diagonal_starting_points end
right_hand_limit()
click to toggle source
# File lib/game_grid/grid.rb, line 191 def right_hand_limit (width - 1) end
stringify_row(row)
click to toggle source
# File lib/game_grid/grid.rb, line 98 def stringify_row(row) (row.map { |cell| "|#{ cell.value || '-' }" } + ["|"]).join end
upper_limit()
click to toggle source
# File lib/game_grid/grid.rb, line 195 def upper_limit (height - 1) end
x_range()
click to toggle source
# File lib/game_grid/grid.rb, line 203 def x_range (left_hand_limit..right_hand_limit) end
y_range()
click to toggle source
# File lib/game_grid/grid.rb, line 207 def y_range (lower_limit..upper_limit) end