Решение на Втора задача от Мариян Борисов

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

Към профила на Мариян Борисов

Резултати

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

Код

def grow(snake, direction)
grown_snake = snake.dup
grown_snake << snake.last.map.with_index { |e, i| e + direction[i] }
end
def move(snake, direction)
moved_snake = grow(snake, direction)
moved_snake.delete_at 0
moved_snake
end
def new_food(food, snake, dimensions)
x_axis_indexes = (0..dimensions[:width] - 1).to_a
y_axis_indexes = (0..dimensions[:height] - 1).to_a
free_blocks = x_axis_indexes.product(y_axis_indexes) - food - snake
free_blocks.sample
end
def obstacle_ahead?(snake, direction, dimensions)
moved_snake = move(snake, direction)
head_position = moved_snake.last
head_position.first < 0 or head_position.first >= dimensions[:width] \
or head_position.last < 0 or head_position.last >= dimensions[:height] \
or moved_snake.index(head_position) != moved_snake.size - 1
end
def danger?(snake, direction, dimensions)
if obstacle_ahead?(snake, direction, dimensions)
true
else
moved_snake = move(snake, direction)
obstacle_ahead?(moved_snake, direction, dimensions)
end
end

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

............F.......

Failures:

  1) #obstacle_ahead? returns true if snake body ahead
     Failure/Error: expect(
       
       expected: true
            got: false
       
       (compared using ==)
     # /tmp/d20151026-15631-1kzantg/spec.rb:83:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.01493 seconds
20 examples, 1 failure

Failed examples:

rspec /tmp/d20151026-15631-1kzantg/spec.rb:82 # #obstacle_ahead? returns true if snake body ahead

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

Мариян обнови решението на 19.10.2015 04:10 (преди над 9 години)

+def grow(snake, direction)
+ grown_snake = snake.dup
+ grown_snake << snake.last.map.with_index { |e, i| e + direction[i] }
+end
+
+def move(snake, direction)
+ moved_snake = grow(snake, direction)
+ moved_snake.delete_at 0
+ moved_snake
+end
+
+def new_food(food, snake, dimensions)
+ x_axis_indexes = (0..dimensions[:width] - 1).to_a
+ y_axis_indexes = (0..dimensions[:height] - 1).to_a
+ free_blocks = x_axis_indexes.product(y_axis_indexes) - food - snake
+ free_blocks.sample
+end
+
+def obstacle_ahead?(snake, direction, dimensions)
+ moved_snake = move(snake, direction)
+ head_position = moved_snake.last
+ head_position.first < 0 or head_position.first >= dimensions[:width] \
+ or head_position.last < 0 or head_position.last >= dimensions[:height] \
+ or moved_snake.index(head_position) != moved_snake.size - 1
+end
+
+def danger?(snake, direction, dimensions)
+ if obstacle_ahead?(snake, direction, dimensions)
+ true
+ else
+ moved_snake = move(snake, direction)
+ obstacle_ahead?(moved_snake, direction, dimensions)
+ end
+end