Георги обнови решението на 18.10.2015 15:00 (преди над 9 години)
+dimensions = {width: 10, height: 10}
+snake = [[4, 5], [4, 6], [5, 6], [5, 7]]
+direction = [0, 1]
+food = [[3, 2], [1, 1], [0, 5]]
+
+def move(snake, direction)
+ new_head = [0, 0]
+ new_head[0] = snake[snake.length - 1][0] + direction[0]
+ new_head[1] = snake[snake.length - 1][1] + direction[1]
+ snake.shift
+ snake + [new_head]
+end
+
+def grow(snake, direction)
+ new_head = [0, 0]
+ new_head[0] = snake[snake.length - 1][0] + direction[0]
+ new_head[1] = snake[snake.length - 1][1] + direction[1]
+ snake + [new_head]
+end
+
+def new_food(food, snake, dimensions)
+ xs = rand(dimensions[:width] - 1)
+ ys = rand(dimensions[:height] - 1)
+ if snake.include?([xs, ys]) or food.include?([xs, ys])
+ new_food(food, snake, dimensions)
+ else return [xs, ys]
+ end
+end
+
+def obstacle_ahead?(snake, direction, dimensions)
+ xs = snake[snake.length - 1][0] + direction[0]
+ ys = snake[snake.length - 1][1] + direction[1]
+ if xs >= dimensions[:width] or ys >= dimensions[:height]
+ return true
+ elsif snake.include?([xs, ys])
+ return true
+ else return false
+ end
+end
+
+def danger?(snake, direction, dimensions)
+ xs = snake[snake.length - 1][0] + direction[0]
+ ys = snake[snake.length - 1][1] + direction[1]
+ if (xs >= dimensions[:width] - 1) or (ys >= dimensions[:height] - 1)
+ return true
+ elsif snake.include?([xs + direction[0], ys + direction[1]])
+ return true
+ else return false
+ end
+end