София обнови решението на 17.10.2015 04:44 (преди над 9 години)
+def field(dimensions)
+ (0..dimensions[:width]).to_a.product((0..dimensions[:height]).to_a)
+end
+
+def spot_ahead(snake, direction)
+ head_of_snake = snake.last
+ head_of_snake.zip(direction).map{|i,j| i + j }
+end
+
+def grow(snake, direction)
+ snake + [spot_ahead(snake, direction)]
+end
+
+def move(snake, direction)
+ snake[1..-1] + [spot_ahead(snake, direction)]
+end
+
+def new_food(food, snake, dimensions)
+ free_spots = field(dimensions) - (food + snake)
+ free_spots.sample
+end
+
+def obstacle_ahead?(snake, direction, dimensions)
+ next_spot = spot_ahead(snake, direction)
+ abscissa_x = next_spot[0]
+ ordinate_y = next_spot[1]
+
+ snake.include?(next_spot) or
+ abscissa_x < 0 or abscissa_x >= dimensions[:width] or
+ ordinate_y < 0 or ordinate_y >= dimensions[:height]
+end
+
+def danger?(snake, direction, dimensions)
+ obstacle_ahead?(snake, direction, dimensions) or
+ obstacle_ahead?(grow(snake, direction), direction, dimensions)
+end