Станимира обнови решението на 16.10.2015 16:15 (преди над 9 години)
+def get_next_position(snake, direction)
+ head = snake.last
+ next_position = [head[0] + direction[0], head[1] + direction[1]]
+ return next_position
+end
+
+def out_of_bounds?(position, dimensions)
+ width = dimensions[:width]
+ height = dimensions[:height]
+ x = position[0]
+ y = position[1]
+
+ return ((x < 0 || x >= width) || (y < 0 || y >= height))
+end
+
+def grow(snake, direction)
+ next_position = get_next_position(snake, direction)
+ snake.push next_position
+
+ return snake
+end
+
+def move(snake, direction)
+ grow(snake, direction)
+ snake.delete_at(0)
+
+ return snake
+end
+
+def new_food(food, snake, dimensions)
+ x = rand(dimensions[:width])
+ y = rand(dimensions[:height])
+ new_food = [x, y]
+
+ if(snake.include?(new_food) || food.include?(new_food))
+ new_food(food, snake, dimensions)
+ else
+ return new_food
+ end
+end
+
+def obstacle_ahead?(snake, direction, dimensions)
+ next_position = get_next_position(snake, direction)
+ out_of_bounds = out_of_bounds?(next_position, dimensions)
+
+ return (out_of_bounds || snake.include?(next_position))
+end
+
+def danger?(snake, direction, dimensions)
+ obstacle_one_turn = obstacle_ahead?(snake, direction, dimensions)
+
+ next_position = move(snake, direction)
+ obstacle_two_turns = obstacle_ahead?(next_position, direction, dimensions)
+
+ return (obstacle_one_turn || obstacle_two_turns)
+end
returnovich returnovich returnovich!
Сериозно, няма нужда да return последната стойност. Може би си свикнала от python
или другаде, но в руби го ползваш само ако връщаш при някакво условие преди края на функцията
В out_of_bounds? няма нужда да слагаш условието на if в скоби
В new_food също, освен това не наименувай променливи с името на функция. Възможно е, но е много mindfucking за четене
Отново скоби след return в последните. Руби не изисква такива там. Като цяло скобите се избягват в повечето случаи, в които е възможно, така че извади if-овете от синята зона
Добро решение иначе, успех