Борис обнови решението на 17.10.2015 18:12 (преди над 9 години)
+def sum_bin_arr(last, direction)
+ group = -> a,b { [a, b] }
+ group.(last[0] + direction[0], last[1] + direction[1])
+end
+
+def move(snake, direction)
+ result = sum_bin_arr(snake.last, direction)
+ result if snake.length == 1
+ else
+ snake_copy = snake.clone
+ snake_copy.delete_at(0)
+ snake_copy << result
+end
+
+def grow(snake, direction)
+ result = sum_bin_arr(snake.last, direction)
+ snake_copy = snake.clone
+ snake_copy << result
+end
+
+def new_food(food, snake, dimensions)
+ width_dimension = rand(dimensions[:width])
+ height_dimension = rand(dimensions[:height])
+ new_food_location = [width_dimension, height_dimension]
+ while snake.include?(new_food_location) or food.include?(new_food_location)
+ width_dimension = rand(dimensions[:width])
+ height_dimension = rand(dimensions[:height])
+ new_food_location = [width_dimension, height_dimension]
+ end
+ new_food_location
+end
+
+def obstacle_ahead?(snake, direction, dimensions)
+ result = sum_bin_arr(snake.last, direction)
+ return true if (snake.include?(result) or result.include?(0) or
+ result[0] >= (dimensions[:width]) or
+ result[1] >= (dimensions[:height]))
+ false
+end
+
+def danger?(snake, direction, dimensions)
+ one_move = move(snake, direction)
+ two_moves = move(one_move, direction)
+ return true if obstacle_ahead?(one_move, direction, dimensions) or
+ obstacle_ahead?(two_moves, direction, dimensions)
+ false
+end