Владимир обнови решението на 18.10.2015 14:50 (преди над 9 години)
+def next_coordinates(source, destination)
+ source.zip(destination).map { |point| point.reduce(:+) }
+end
+
+def random_tile(field)
+ [rand(field[:width]), rand(field[:height])]
+end
+
+def move(snake, direction)
+ new_snake = snake.dup[1..-1]
+ grow(new_snake, direction)
+end
+
+def grow(snake, direction)
+ snake.dup.push(next_coordinates(snake.last, direction))
+end
+
+def new_food(food, snake, field)
+ food_tile = random_tile(field)
+ while (snake + food).include? food_tile
+ food_tile = random_tile(field)
+ end
+ food_tile
+end
+
+def obstacle_ahead?(snake, direction, field)
+ next_tile = next_coordinates(snake.last, direction)
+ if field[:height] <= next_tile.last || field[:width] <= next_tile.first ||
+ snake.include?(next_tile) || next_tile.first < 0 || next_tile.last < 0
+ return true
+ end
+ false
+end
+
+def danger?(snake, direction, field)
+ return true if obstacle_ahead?(snake, direction, field)
+ return true if obstacle_ahead?(move(snake, direction), direction, field)
+ false
+end