class RubyTerminalGames::Snake::Snake

Attributes

height[R]
state[R]
width[R]

Public Class Methods

new(width:, height:) click to toggle source
# File lib/ruby_terminal_games/snake/snake.rb, line 5
def initialize(width:, height:)
  @width = width
  @height = height
  @state = [
    [0, [2, 2]],
    [1, [2, 3]],
    [2, [2, 4]],
    [3, [2, 5]],
    [4, [2, 6]],
    [5, [2, 7]],
    [6, [2, 8]],
    [7, [2, 9]]
  ]
end

Public Instance Methods

died?() click to toggle source
# File lib/ruby_terminal_games/snake/snake.rb, line 32
def died?
  _, position = state.last
  row, col = position
  across_rows_limit?(row) || across_cols_limit?(col) || collision_with_self?
end
eat?(apple) click to toggle source
# File lib/ruby_terminal_games/snake/snake.rb, line 38
def eat?(apple)
  eat = used_positions.include?(apple.position)
  apple.move(self) if eat
  eat
end
grow!(direction) click to toggle source
# File lib/ruby_terminal_games/snake/snake.rb, line 44
def grow!(direction)
  last_state = @state.last
  index, pos = last_state
  moved = update_position(pos, direction)
  @state.concat([[index + 1, moved]])
end
move!(direction) click to toggle source
# File lib/ruby_terminal_games/snake/snake.rb, line 20
def move!(direction)
  *tail, head = state
  (0..state.length-2).each do |i|
    i, _ = tail[i]
    tail[i] = [i, state[i + 1].last]
  end

  index, position = head
  moved = update_position(position, direction)
  @state = tail.concat([[index, moved]])
end
used?(position) click to toggle source
# File lib/ruby_terminal_games/snake/snake.rb, line 51
def used?(position)
  used_positions.include?(position)
end

Private Instance Methods

across_cols_limit?(col) click to toggle source
# File lib/ruby_terminal_games/snake/snake.rb, line 71
def across_cols_limit?(col)
  (col < 2 || col > width - 1)
end
across_rows_limit?(row) click to toggle source
# File lib/ruby_terminal_games/snake/snake.rb, line 67
def across_rows_limit?(row)
  (row < 2 || row > height - 1)
end
collision_with_self?() click to toggle source
# File lib/ruby_terminal_games/snake/snake.rb, line 79
def collision_with_self?
  used_positions.uniq.size != used_positions.size
end
update_position(position, direction) click to toggle source
# File lib/ruby_terminal_games/snake/snake.rb, line 57
def update_position(position, direction)
  row, col = position
  case direction
  when UP then [row - 1, col]
  when RIGHT then [row, col + 1]
  when DOWN then [row + 1, col]
  when LEFT then [row, col - 1]
  end
end
used_positions() click to toggle source
# File lib/ruby_terminal_games/snake/snake.rb, line 75
def used_positions
  state.map { |s| s.last }
end