Димитър обнови решението на 15.10.2015 10:45 (преди над 9 години)
+dimensions = {width: 10, height: 10}
+
+def get_destination(snake_head, direction)
+ snake_head.zip(direction).map{ |dimension| dimension.inject(:+) }
+end
+
+def move(snake, direction)
+ snake.drop(1).push(get_destination(snake.last, direction))
+end
+
+def grow(snake, direction)
+ snake + [get_destination(snake.last, direction)]
+end
+
+def new_food(food, snake, dimensions)
+ new_food = [rand(dimensions[:width]), rand(dimensions[:height])] while
+ snake.include?(new_food) || food.include?(new_food) || new_food.nil?
+ new_food
+end
+
+def obstacle_ahead?(snake, direction, dimensions)
+ x = get_destination(snake.last, direction)
+ !(x.first.between?(0, dimensions[:width] - 1) &&
+ x.last.between?(0, dimensions[:height] - 1))
+end
+
+def danger?(snake, direction, dimensions)
+ obstacle_ahead?(snake, direction.map{|dimension| dimension * 3}, dimensions)
+end