Class: RubySnake::Snake

Inherits:
Object
  • Object
show all
Includes:
Config
Defined in:
lib/ruby_snake/snake.rb

Constant Summary

Constants included from Config

Config::CONNECT_TO_HOST, Config::CREATE_HOST, Config::DOWN, Config::LEFT, Config::NO, Config::OPERATIONS, Config::PAUSE, Config::QUIT, Config::RIGHT, Config::SINGLE_PLAYER, Config::TWO_PLAYERS, Config::UP, Config::YES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height) ⇒ Snake

Returns a new instance of Snake.



7
8
9
# File 'lib/ruby_snake/snake.rb', line 7

def initialize(width, height)
  @width, @height = width - 3, height - 3
end

Instance Attribute Details

#directionObject

Returns the value of attribute direction.



5
6
7
# File 'lib/ruby_snake/snake.rb', line 5

def direction
  @direction
end

#foodObject

Returns the value of attribute food.



5
6
7
# File 'lib/ruby_snake/snake.rb', line 5

def food
  @food
end

#headObject

Returns the value of attribute head.



5
6
7
# File 'lib/ruby_snake/snake.rb', line 5

def head
  @head
end

#heightObject

Returns the value of attribute height.



5
6
7
# File 'lib/ruby_snake/snake.rb', line 5

def height
  @height
end

#tailObject

Returns the value of attribute tail.



5
6
7
# File 'lib/ruby_snake/snake.rb', line 5

def tail
  @tail
end

#widthObject

Returns the value of attribute width.



5
6
7
# File 'lib/ruby_snake/snake.rb', line 5

def width
  @width
end

Instance Method Details

#bodyObject



16
17
18
19
20
21
# File 'lib/ruby_snake/snake.rb', line 16

def body
  @body ||= Proc.new do 
    body = []
    body << [1, 0] << [0, 0]
  end.call
end

#can_eat_food?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/ruby_snake/snake.rb', line 43

def can_eat_food?
  self.head == food
end

#eat_foodObject



39
40
41
# File 'lib/ruby_snake/snake.rb', line 39

def eat_food
  self.food = nil
end

#initObject



11
12
13
14
# File 'lib/ruby_snake/snake.rb', line 11

def init
  body.clear
  body << [1, 0] << [0, 0]
end

#move(o = nil) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/ruby_snake/snake.rb', line 89

def move(o=nil)
  self.direction = o unless o.nil?
  return false unless move_head(direction)
  if can_eat_food?
    eat_food
  else
    move_tail
  end
  true
end

#move_head(direction) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ruby_snake/snake.rb', line 65

def move_head(direction)
  x = head.first
  y = head.last
  case direction
  when DOWN
    return false if y + 1 > height
    body.unshift [x, y + 1]
  when UP
    return false if y - 1 <  0
    body.unshift [x, y - 1]
  when LEFT
    return false if x - 2 < 0
    body.unshift [x - 2, y]
  when RIGHT
    return false if x + 2 > width
    body.unshift [x + 2, y]
  end
  body.uniq == body
end

#move_tailObject



85
86
87
# File 'lib/ruby_snake/snake.rb', line 85

def move_tail
  body.pop
end

#near_border?Boolean

Returns:

  • (Boolean)


55
56
57
58
59
# File 'lib/ruby_snake/snake.rb', line 55

def near_border?
  return true if head.first == 1 or head.last == 0
  return true if head.first == width or head.first == width - 1
  return true if head.last == height 
end

#odd_columnsObject



23
24
25
# File 'lib/ruby_snake/snake.rb', line 23

def odd_columns
  @odd_columns ||= (0..width).select { |c| c % 2 != 0 }
end

#rand_foodObject



31
32
33
34
35
36
37
# File 'lib/ruby_snake/snake.rb', line 31

def rand_food
  while true
    food_coordinate = [odd_columns.sample, rand(height)]
    break unless body.include? food_coordinate
  end
  food_coordinate
end