Решение на Втора задача от Йоан Динков

Обратно към всички решения

Към профила на Йоан Динков

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 20 успешни тест(а)
  • 0 неуспешни тест(а)

Код

def move(snake, direction)
new_snake = snake.dup
new_snake.push next_position(new_snake.last, direction)
new_snake.drop(1)
end
def grow(snake, direction)
new_snake = snake.dup
new_snake.push next_position(new_snake.last, direction)
end
def new_food(food, snake, dimensions)
*xs, _last_x = (0..dimensions[:width]).to_a
*ys, _last_y = (0..dimensions[:height]).to_a
occupied = food + snake
position = [xs.sample, ys.sample]
position = [xs.sample, ys.sample] while occupied.include?(position)
position
end
def obstacle_ahead?(snake, direction, dimensions)
next_x, next_y = next_position(snake.last, direction)
snake.include?([next_x, next_y]) ||
next_x == dimensions[:width] || next_y == dimensions[:height] ||
next_x < 0 || next_y < 0
end
def danger?(snake, direction, dimensions)
grown_snake = grow(snake, direction)
obstacle_ahead = obstacle_ahead? snake, direction, dimensions
obstacle_after = obstacle_ahead? grown_snake, direction, dimensions
obstacle_ahead || obstacle_after
end
def next_position(position, direction)
position_x, position_y = position
direction_x, direction_y = direction
[position_x + direction_x, position_y + direction_y]
end

Лог от изпълнението

....................

Finished in 0.01441 seconds
20 examples, 0 failures

История (4 версии и 1 коментар)

Йоан обнови решението на 16.10.2015 20:15 (преди над 9 години)

+def move(snake, direction)
+ new_snake = Array.new(snake)
+
+ new_snake.shift
+
+ new_snake << _sum_positions(new_snake.last, direction)
+end
+
+def grow(snake, direction)
+ new_snake = Array.new(snake)
+
+ new_snake << _sum_positions(new_snake.last, direction)
+end
+
+def new_food(food, snake, dimensions)
+ puts(dimensions['width'])
+ food_position = [rand(dimensions[:width]), rand(dimensions[:height])]
+
+ while food.include?(food_position) || snake.include?(food_position)
+ food_position = [rand(dimensions[:width]), rand(dimensions[:height])]
+ end
+
+ food_position
+end
+
+def obstacle_ahead?(snake, direction, dimensions)
+ next_position = _sum_positions(snake.last, direction)
+
+ if snake.include? next_position
+ return true
+ elsif next_position[0] == dimensions[:width] ||
+ next_position[1] == dimensions[:height]
+ return true
+ end
+
+ false
+end
+
+def danger?(snake, direction, dimensions)
+ grown_snake = grow(snake, direction)
+
+ first_obstacle = obstacle_ahead? snake, direction, dimensions
+ second_obstacle = obstacle_ahead? grown_snake, direction, dimensions
+
+ first_obstacle || second_obstacle
+end
+
+def _sum_positions(first_position, second_position)
+ new_position = []
+ new_position[0] = first_position[0] + second_position[0]
+ new_position[1] = first_position[1] + second_position[1]
+ new_position
+end

Йоан обнови решението на 16.10.2015 20:56 (преди над 9 години)

def move(snake, direction)
new_snake = Array.new(snake)
new_snake.shift
new_snake << _sum_positions(new_snake.last, direction)
end
def grow(snake, direction)
new_snake = Array.new(snake)
new_snake << _sum_positions(new_snake.last, direction)
end
def new_food(food, snake, dimensions)
- puts(dimensions['width'])
food_position = [rand(dimensions[:width]), rand(dimensions[:height])]
while food.include?(food_position) || snake.include?(food_position)
food_position = [rand(dimensions[:width]), rand(dimensions[:height])]
end
food_position
end
def obstacle_ahead?(snake, direction, dimensions)
next_position = _sum_positions(snake.last, direction)
- if snake.include? next_position
- return true
- elsif next_position[0] == dimensions[:width] ||
- next_position[1] == dimensions[:height]
- return true
- end
-
- false
+ snake.include? next_position ||
+ next_position[0] == dimensions[:width] ||
+ next_position[1] == dimensions[:height]
end
def danger?(snake, direction, dimensions)
grown_snake = grow(snake, direction)
first_obstacle = obstacle_ahead? snake, direction, dimensions
second_obstacle = obstacle_ahead? grown_snake, direction, dimensions
first_obstacle || second_obstacle
end
def _sum_positions(first_position, second_position)
new_position = []
new_position[0] = first_position[0] + second_position[0]
new_position[1] = first_position[1] + second_position[1]
new_position
end

Като цяло добре, няколко идеи:

  • Пропуснал си два случая в obstacle_ahead?.
  • Object#dup звучи по-добре от Array.new за правене на копия.
  • Някои методи, които биха направили решението ти по-ясно - Array#drop, Array#first/Array#last, Array#push.
  • Във форумите има малка дискусия откъм детерминизмът на new_food.

Откъм имена:

  • _sum_positions e малко странно. В текущата задача няма много смислена операция сумиране на позиции. seconds_position всъщност не е позиция, а посока. И защо подчертавката в началото. :)
  • first_obstacle/second_obstacle не са много точни. Няма две препятствия, по-скоро първият или вторият ход могат да бъдат опасни.

Йоан обнови решението на 18.10.2015 19:53 (преди над 9 години)

def move(snake, direction)
- new_snake = Array.new(snake)
+ new_snake = snake.dup
- new_snake.shift
+ new_snake.push next_position(new_snake.last, direction)
- new_snake << _sum_positions(new_snake.last, direction)
+ new_snake.drop(1)
end
def grow(snake, direction)
- new_snake = Array.new(snake)
+ new_snake = snake.dup
- new_snake << _sum_positions(new_snake.last, direction)
+ new_snake.push next_position(new_snake.last, direction)
end
def new_food(food, snake, dimensions)
- food_position = [rand(dimensions[:width]), rand(dimensions[:height])]
+ *xs, _first_x = (0..dimensions[:width]).to_a
+ *ys, _first_y = (0..dimensions[:height]).to_a
- while food.include?(food_position) || snake.include?(food_position)
- food_position = [rand(dimensions[:width]), rand(dimensions[:height])]
- end
+ occupied = food + snake
- food_position
+ position = [xs.sample, ys.sample]
+
+ position = [xs.sample, ys.sample] while occupied.include?(position)
+
+ position
end
def obstacle_ahead?(snake, direction, dimensions)
- next_position = _sum_positions(snake.last, direction)
+ next_x, next_y = next_position(snake.last, direction)
- snake.include? next_position ||
- next_position[0] == dimensions[:width] ||
- next_position[1] == dimensions[:height]
+ snake.include?([next_x, next_y]) ||
+ next_x == dimensions[:width] || next_y == dimensions[:height] ||
+ next_x < 0 || next_y < 0
end
def danger?(snake, direction, dimensions)
grown_snake = grow(snake, direction)
- first_obstacle = obstacle_ahead? snake, direction, dimensions
- second_obstacle = obstacle_ahead? grown_snake, direction, dimensions
+ obstacle_ahead = obstacle_ahead? snake, direction, dimensions
+ obstacle_after = obstacle_ahead? grown_snake, direction, dimensions
- first_obstacle || second_obstacle
+ obstacle_ahead || obstacle_after
end
-def _sum_positions(first_position, second_position)
- new_position = []
- new_position[0] = first_position[0] + second_position[0]
- new_position[1] = first_position[1] + second_position[1]
- new_position
+def next_position(position, direction)
+ position_x, position_y = position
+ direction_x, direction_y = direction
+ [position_x + direction_x, position_y + direction_y]
end

Йоан обнови решението на 18.10.2015 19:55 (преди над 9 години)

def move(snake, direction)
new_snake = snake.dup
new_snake.push next_position(new_snake.last, direction)
new_snake.drop(1)
end
def grow(snake, direction)
new_snake = snake.dup
new_snake.push next_position(new_snake.last, direction)
end
def new_food(food, snake, dimensions)
- *xs, _first_x = (0..dimensions[:width]).to_a
- *ys, _first_y = (0..dimensions[:height]).to_a
+ *xs, _last_x = (0..dimensions[:width]).to_a
+ *ys, _last_y = (0..dimensions[:height]).to_a
occupied = food + snake
position = [xs.sample, ys.sample]
position = [xs.sample, ys.sample] while occupied.include?(position)
position
end
def obstacle_ahead?(snake, direction, dimensions)
next_x, next_y = next_position(snake.last, direction)
snake.include?([next_x, next_y]) ||
next_x == dimensions[:width] || next_y == dimensions[:height] ||
next_x < 0 || next_y < 0
end
def danger?(snake, direction, dimensions)
grown_snake = grow(snake, direction)
obstacle_ahead = obstacle_ahead? snake, direction, dimensions
obstacle_after = obstacle_ahead? grown_snake, direction, dimensions
obstacle_ahead || obstacle_after
end
def next_position(position, direction)
position_x, position_y = position
direction_x, direction_y = direction
[position_x + direction_x, position_y + direction_y]
end